test/sdl4r/parser_test.rb in sdl4r-0.9.6 vs test/sdl4r/parser_test.rb in sdl4r-0.9.7
- old
+ new
@@ -84,24 +84,24 @@
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\n", values[4], "values[4]")
+ assert_equal("m\\\nn\\\n \t o\r", values[4], "values[4]")
end
def test_tag_with_base64_values
tag1 = parse_one_tag1(
- <<EOF
+ <<EOS
tag1 [V2VsY29tZSB0byB0aGUgY3J1ZWwgd29ybGQu] [
SG9wZSB5
b3UnbGwg
ZmluZCB5
b3VyIHdh
eS4=
]
-EOF
+EOS
)
assert_not_nil(tag1, "tag1")
values = tag1.values
assert_equal(SDL4R::SdlBinary("Welcome to the cruel world."), values[0], "values[0]")
assert_equal(SDL4R::SdlBinary("Hope you'll find your way."), values[1], "values[1]")
@@ -115,10 +115,15 @@
assert(values.empty?, "value count")
attributes = tag1.attributes
assert_equal(1, attributes.size, "attribute count")
assert_equal(99, attributes["attr1"], "attr1")
+
+ # check the parsing with line continuations
+ tag1 = parse_one_tag1("tag1\\\nattr1=\\\n99")
+ assert_not_nil(tag1, "tag1")
+ assert_equal(99, tag1.attribute("attr1"), "attr1")
end
def test_tag_with_attributes
tag1 = parse_one_tag1(
"tag1 attr1=\"99\" ns:attr2=[QmVhdXR5IGlzIG5vdCBlbm91Z2gu]")
@@ -206,10 +211,20 @@
assert_equal(12.13, values[7])
assert_equal(1415.16, values[8])
assert_equal(BigDecimal("171.8"), values[9])
assert_equal(BigDecimal("1.920"), values[10])
assert_equal(BigDecimal("12345678901234567890"), values[11])
+
+ assert_raise SdlParseError do
+ parse_one_tag1("tag1 123.2.2")
+ end
+ assert_raise SdlParseError do
+ parse_one_tag1("tag1 123.2e")
+ end
+ assert_raise SdlParseError do
+ parse_one_tag1("tag1 +-123.2")
+ end
end
def test_booleans
tag1 = parse_one_tag1("tag1 b1=true b2=false b3=on b4=off")
assert_equal(true, tag1.attribute("b1"))
@@ -228,22 +243,22 @@
end
def test_comments
root = Tag.new("root")
root.read(
- <<EOF
+ <<EOS
tag1 123
#tag2 456
tag3 789
--tag4 012
tag5 345
//tag6 678
tag7 901
/*tag8 234
tag9 567*/
tag10 890
-EOF
+EOS
)
children = root.children
assert_equal(5, children.size, "children count")
assert_equal(123, root.child("tag1").value)
assert_nil(root.child("tag2"))
@@ -257,11 +272,11 @@
assert_equal(890, root.child("tag10").value)
end
def test_double_quote_strings
root = SDL4R::read(
-<<EOF
+<<EOS
tag1 "cheese and cherry jam"
tag2 "cheese and \\
cherry jam"
tag3 "cheese \\
and \\
@@ -269,11 +284,11 @@
tag4 "Did you say this soup was \\"good\\"?"
tag5 "Even my dog wouldn't\\thave\\tit!"
tag6 "\\"\\t\\r\\n\\\\"
tag7 equation="is not x=y*z" color="blue \\
and yellow"
-EOF
+EOS
)
assert_equal "cheese and cherry jam", root.child("tag1").value, "double-quote string"
assert_equal(
"cheese and cherry jam", root.child("tag2").value, "continued double-quote string")
@@ -286,31 +301,55 @@
assert_equal "\"\t\r\n\\", root.child("tag6").value, "escaped white spaces"
assert_equal "is not x=y*z", root.child("tag7").attribute("equation")
assert_equal "blue and yellow", root.child("tag7").attribute("color")
end
+ def test_characters
+ root = SDL4R::read "tag1 ' ' 'a' '\\\\' '\\t' '\\n' '\\r'"
+ assert_equal [" ", "a", "\\", "\t", "\n", "\r"], root.child("tag1").values
+
+ assert_raise SdlParseError do
+ SDL4R::read "tag1 '"
+ end
+ assert_raise SdlParseError do
+ SDL4R::read "tag1 'a"
+ end
+ assert_raise SdlParseError do
+ SDL4R::read "tag1 'abc'"
+ end
+ assert_raise SdlParseError do
+ SDL4R::read "tag1 ''"
+ end
+ assert_raise SdlParseError do
+ SDL4R::read "tag1 '\\'"
+ end
+ assert_raise SdlParseError do
+ SDL4R::read "tag1 '\\"
+ end
+ end
+
def test_backquote_strings
- root = SDL4R::read <<EOF
+ root = SDL4R::read <<EOS
winfile `c:\\directory\\myfile.xls`
talk `I said "something"`
xml `
<product>
<shoes color="blue"/>
</product>
`
regex `\\w+\\.suite\\(\\)`
-EOF
+EOS
assert_equal "c:\\directory\\myfile.xls", root.child("winfile").value
assert_equal 'I said "something"', root.child("talk").value
assert_equal(
"\n<product>\n <shoes color=\"blue\"/>\n</product>\n", root.child("xml").value)
assert_equal "\\w+\\.suite\\(\\)", root.child("regex").value
end
def test_sub_tags
- root = SDL4R::read <<EOF
+ root = SDL4R::read <<EOS
wax {
}
steack {
bees {
monopoly {
@@ -320,11 +359,11 @@
truck {
cathedral
}
}
peanut.butter
-EOF
+EOS
expected = Tag.new("root") do
new_child("wax")
new_child("steack") do
new_child("bees") do
@@ -339,10 +378,69 @@
end
assert_equal expected, root
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 = <<EOS
+-- my comment
+=
+EOS
+ assert_error_xy source, 2, 1
+
+ source = <<EOS
+murder_plot 123 \\
+ weight=456 \\
+ * \\
+ length=789
+EOS
+ assert_error_xy source, 3, 6
+
+ assert_error_xy 'tag1 "text\\"', 1, 13
+
+ source = <<EOS
+murder_plot "abcd\\
+ efghij\\
+ kl\\
+ mnopq
+EOS
+ assert_error_xy source, 4, 13
+ end
+
private
+
+ def assert_error_xy source, expected_line, expected_pos
+ error = get_parse_error_or_fail source
+ begin
+ assert_equal expected_line, error.line, "line"
+ assert_equal expected_pos, error.position, "position"
+ rescue
+ puts "Expected error: #{error}"
+ puts error.backtrace
+ raise $!
+ end
+ end
+
+ def get_parse_error_or_fail source
+ begin
+ SDL4R::read source
+
+ rescue
+ return $! if $!.is_a? SdlParseError
+ raise AssertionFailedError, "was expecting a SdlParseError"
+ end
+ end
# Creates and returns a DateTime where an unspecified +zone_offset+ means 'the local zone
# offset' (contrarily to DateTime#civil())
def local_civil_date(year, month, day, hour = 0, min = 0, sec = 0, zone_offset = nil)
zone_offset ||= @@zone_offset
\ No newline at end of file