test/sdl4r/parser_test.rb in sdl4r-0.9.1 vs test/sdl4r/parser_test.rb in sdl4r-0.9.2
- old
+ new
@@ -28,23 +28,10 @@
require File.dirname(__FILE__) + '/../../lib/sdl4r/tag'
require File.dirname(__FILE__) + '/../../lib/sdl4r/sdl_binary'
class ParserTest < Test::Unit::TestCase
- # Called before every test method runs. Can be used
- # to set up fixture information.
- def setup
- # Do nothing
- end
-
- # Called after every test method runs. Can be used to tear
- # down fixture information.
-
- def teardown
- # Do nothing
- end
-
public
def test_empty
root = Tag.new("root")
root.read("")
@@ -215,15 +202,17 @@
assert_equal(123.45, values[4])
assert_equal(67.8, values[5])
assert_equal(910.11, values[6])
assert_equal(12.13, values[7])
assert_equal(1415.16, values[8])
- assert_equal(171.8, values[9])
- assert_equal(1.920, values[10])
if defined? Flt::DecNum
+ assert_equal(Flt::DecNum("171.8"), values[9])
+ assert_equal(Flt::DecNum("1.920"), values[10])
assert_equal(Flt::DecNum("12345678901234567890"), values[11])
else
+ assert_equal(171.8, values[9])
+ assert_equal(1.920, values[10])
assert_equal(12345678901234567890, values[11])
end
end
def test_booleans
@@ -238,10 +227,11 @@
tag1 = parse_one_tag1("tag1 null attr1=null")
values = tag1.values
assert_equal(1, values.size)
assert_equal(nil, values[0])
assert_equal(nil, tag1.attribute("attr1"))
+ assert(tag1.has_attribute?("attr1"))
end
def test_comments
root = Tag.new("root")
root.read(
@@ -270,26 +260,107 @@
assert_nil(root.child("tag8"))
assert_nil(root.child("tag9"))
assert_equal(890, root.child("tag10").value)
end
+ def test_double_quote_strings
+ root = SDL4R::read(
+<<EOF
+tag1 "cheese and cherry jam"
+tag2 "cheese and \\
+ cherry jam"
+tag3 "cheese \\
+ and \\
+ cherry jam"
+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
+ )
+
+ 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")
+ assert_equal(
+ "cheese and cherry jam", root.child("tag3").value, "continued double-quote string")
+ assert_equal(
+ 'Did you say this soup was "good"?', root.child("tag4").value, "escaped quotes")
+ assert_equal(
+ "Even my dog wouldn't\thave\tit!", root.child("tag5").value, "escaped tabs")
+ 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_backquote_strings
+ root = SDL4R::read <<EOF
+winfile `c:\\directory\\myfile.xls`
+talk `I said "something"`
+xml `
+<product>
+ <shoes color="blue"/>
+</product>
+`
+regex `\\w+\\.suite\\(\\)`
+EOF
+
+ 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
+wax {
+}
+steack {
+ bees {
+ monopoly {
+ }
+ }
+ goat_cheese
+ truck {
+ cathedral
+ }
+}
+peanut.butter
+EOF
+
+ expected = Tag.new("root") do
+ new_child("wax")
+ new_child("steack") do
+ new_child("bees") do
+ new_child("monopoly")
+ end
+ new_child("goat_cheese")
+ new_child("truck") do
+ new_child("cathedral")
+ end
+ end
+ new_child("peanut.butter")
+ end
+
+ assert_equal expected, root
+ end
+
private
# 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 = Rational(Time.now.utc_offset, 24 * 60 * 60) if zone_offset.nil?
return DateTime.civil(year, month, day, hour, min, sec, zone_offset)
end
def parse_one_tag1(text)
- root = Tag.new("root")
- root.read(text)
+ root = SDL4R::read(text)
tag1 = root.child("tag1")
assert_not_nil(tag1, "tag1")
-
assert_equal 1, root.child_count, "only 1 tag expected"
-
return tag1
end
end
end
\ No newline at end of file