#!/usr/bin/env ruby -w # encoding: UTF-8 #-- # Simple Declarative Language (SDL) for Ruby # Copyright 2005 Ikayzo, inc. # # This program is free software. You can distribute or modify it under the # terms of the GNU Lesser General Public License version 2.1 as published by # the Free Software Foundation. # # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND, # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. # See the GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, contact the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #++ # Work-around a bug in NetBeans (http://netbeans.org/bugzilla/show_bug.cgi?id=188653) # $:[0] = File.join(File.dirname(__FILE__),'../../lib') if ENV["NB_EXEC_EXTEXECUTION_PROCESS_UUID"] module SDL4R require 'bigdecimal' require 'test/unit' require "sdl4r/tag" class ParserTest < Test::Unit::TestCase @@zone_offset = Rational(Time.now.utc_offset, 24 * 60 * 60) public def test_empty root = Tag.new("root") root.read("") root.children(false) { fail("no child expected") } end def test_one_tag_alone # Tag without namespace tag1 = parse_one_tag1("tag1") assert_equal("tag1", tag1.name, "name" ) assert_equal("", tag1.namespace, "namespace" ) assert_equal(0, tag1.values.size, "values") assert_equal(0, tag1.attributes.size, "attributes") # Tag with namespace tag1 = parse_one_tag1("ns1:tag1") assert_equal("tag1", tag1.name, "name" ) assert_equal("ns1", tag1.namespace, "namespace" ) end def test_tag_with_one_value tag1 = parse_one_tag1("tag1 1") assert_not_nil(tag1, "tag1") assert_equal(1, tag1.value, "value") end def test_tag_with_two_values tag1 = parse_one_tag1("tag1 1 \"abc\"") assert_not_nil(tag1, "tag1") values = tag1.values assert_equal(1, values[0], "1st value") assert_equal("abc", values[1], "2nd value") end def test_tag_with_double_quote_string_values tag1 = parse_one_tag1("tag1 \"abc\" \"d\\\ne\\\nf\" \"g\\\n \t hi\"") assert_not_nil(tag1, "tag1") values = tag1.values assert_equal("abc", values[0], "values[0]") assert_equal("def", values[1], "values[1]") assert_equal("ghi", values[2], "values[2]") end def test_tag_with_back_quote_string_values tag1 = parse_one_tag1( "tag1 `abc` \"d`e`f\" `g\"h\"i` `j\\k+l` `m\\\nn\\\r\n \t o\r`") assert_not_nil(tag1, "tag1") values = tag1.values assert_equal("abc", values[0], "values[0]") assert_equal("d`e`f", values[1], "values[1]") assert_equal("g\"h\"i", values[2], "values[2]") assert_equal("j\\k+l", values[3], "values[3]") assert_equal("m\\\nn\\\n \t o\r", values[4], "values[4]") end def test_tag_with_base64_values tag1 = parse_one_tag1( < ` regex `\\w+\\.suite\\(\\)` EOS assert_equal "c:\\directory\\myfile.xls", root.child("winfile").value assert_equal 'I said "something"', root.child("talk").value assert_equal( "\n\n \n\n", root.child("xml").value) assert_equal "\\w+\\.suite\\(\\)", root.child("regex").value end def test_sub_tags root = SDL4R::read < -13.8 }, root.child.attributes) root = SDL4R::read("_my_ns:_my_tag _my_ns2:_my_attr=-13.8") assert_equal "_my_ns", root.child.namespace assert_equal "_my_tag", root.child.name assert_equal({ "_my_ns2:_my_attr" => -13.8 }, root.child.attributes) root = SDL4R::read("my.ns:my.tag my.ns2:my.attr=-13.8") assert_equal "my.ns", root.child.namespace assert_equal "my.tag", root.child.name assert_equal({ "my.ns2:my.attr" => -13.8 }, root.child.attributes) root = SDL4R::read("my$ns:my$tag my$ns2:my$attr=-13.8") assert_equal "my$ns", root.child.namespace assert_equal "my$tag", root.child.name assert_equal({ "my$ns2:my$attr" => -13.8 }, root.child.attributes) end def test_empty_block tag1 = parse_one_tag1("tag1 {\n}") assert_equal [], tag1.values assert_equal({}, tag1.attributes) assert_equal [], tag1.children # tag1 = parse_one_tag1("tag1 {}") # assert_equal [], tag1.values # assert_equal({}, tag1.attributes) # assert_equal [], tag1.children end def test_parse_error # WARNING: the line and col of an error is not accurate science. The goal here is to point to # coordinates that are useful to the user. # Exampe for a string litteral that spans over several line, some errors could be point to # the start or to the end without too much ambiguity. # Consequently, feel free to change the coordinates, if a change in the implementation # modifies the x/y of the error and they still make sense. assert_error_xy "=", 1, 1 assert_error_xy "tag1 xyz", 1, 6 assert_error_xy "tag1 \\\nxyz", 2, 1 assert_error_xy "tag1 \\\n xyz", 2, 4 source = <