sdl4r 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,295 @@
1
+ # Simple Declarative Language (SDL) for Ruby
2
+ # Copyright 2005 Ikayzo, inc.
3
+ #
4
+ # This program is free software. You can distribute or modify it under the
5
+ # terms of the GNU Lesser General Public License version 2.1 as published by
6
+ # the Free Software Foundation.
7
+ #
8
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
9
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
10
+ # See the GNU Lesser General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU Lesser General Public License
13
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
14
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15
+
16
+
17
+ module SDL4R
18
+
19
+ require 'test/unit'
20
+
21
+ begin
22
+ # Try to use the Flt library, which defines DecNum
23
+ require "flt"
24
+ rescue LoadError
25
+ # Well, shouganai.
26
+ end
27
+
28
+ require File.dirname(__FILE__) + '/../../lib/sdl4r/tag'
29
+ require File.dirname(__FILE__) + '/../../lib/sdl4r/sdl_binary'
30
+
31
+ class ParserTest < Test::Unit::TestCase
32
+
33
+ # Called before every test method runs. Can be used
34
+ # to set up fixture information.
35
+ def setup
36
+ # Do nothing
37
+ end
38
+
39
+ # Called after every test method runs. Can be used to tear
40
+ # down fixture information.
41
+
42
+ def teardown
43
+ # Do nothing
44
+ end
45
+
46
+ public
47
+
48
+ def test_empty
49
+ root = Tag.new("root")
50
+ root.read("")
51
+ root.children(false) { fail("no child expected") }
52
+ end
53
+
54
+ def test_one_tag_alone
55
+ # Tag without namespace
56
+ tag1 = parse_one_tag1("tag1")
57
+ assert_equal("tag1", tag1.name, "name" )
58
+ assert_equal("", tag1.namespace, "namespace" )
59
+
60
+ assert_equal(0, tag1.values.size, "values")
61
+ assert_equal(0, tag1.attributes.size, "attributes")
62
+
63
+ # Tag with namespace
64
+ tag1 = parse_one_tag1("ns1:tag1")
65
+ assert_equal("tag1", tag1.name, "name" )
66
+ assert_equal("ns1", tag1.namespace, "namespace" )
67
+ end
68
+
69
+ def test_tag_with_one_value
70
+ tag1 = parse_one_tag1("tag1 1")
71
+ assert_not_nil(tag1, "tag1")
72
+ assert_equal(1, tag1.value, "value")
73
+ end
74
+
75
+ def test_tag_with_two_values
76
+ tag1 = parse_one_tag1("tag1 1 \"abc\"")
77
+ assert_not_nil(tag1, "tag1")
78
+ values = tag1.values
79
+ assert_equal(1, values[0], "1st value")
80
+ assert_equal("abc", values[1], "2nd value")
81
+ end
82
+
83
+ def test_tag_with_double_quote_string_values
84
+ tag1 = parse_one_tag1("tag1 \"abc\" \"d\\\ne\\\nf\" \"g\\\n \t hi\"")
85
+ assert_not_nil(tag1, "tag1")
86
+ values = tag1.values
87
+ assert_equal("abc", values[0], "values[0]")
88
+ assert_equal("def", values[1], "values[1]")
89
+ assert_equal("ghi", values[2], "values[2]")
90
+ end
91
+
92
+ def test_tag_with_back_quote_string_values
93
+ tag1 = parse_one_tag1(
94
+ "tag1 `abc` \"d`e`f\" `g\"h\"i` `j\\k+l` `m\\\nn\\\r\n \t o\r`")
95
+ assert_not_nil(tag1, "tag1")
96
+ values = tag1.values
97
+ assert_equal("abc", values[0], "values[0]")
98
+ assert_equal("d`e`f", values[1], "values[1]")
99
+ assert_equal("g\"h\"i", values[2], "values[2]")
100
+ assert_equal("j\\k+l", values[3], "values[3]")
101
+ assert_equal("m\\\nn\\\n \t o\n", values[4], "values[4]")
102
+ end
103
+
104
+ def test_tag_with_base64_values
105
+ tag1 = parse_one_tag1(
106
+ <<EOF
107
+ tag1 [V2VsY29tZSB0byB0aGUgY3J1ZWwgd29ybGQu] [
108
+ SG9wZSB5
109
+ b3UnbGwg
110
+ ZmluZCB5
111
+ b3VyIHdh
112
+ eS4=
113
+ ]
114
+ EOF
115
+ )
116
+ assert_not_nil(tag1, "tag1")
117
+ values = tag1.values
118
+ assert_equal(SDL4R::SdlBinary("Welcome to the cruel world."), values[0], "values[0]")
119
+ assert_equal(SDL4R::SdlBinary("Hope you'll find your way."), values[1], "values[1]")
120
+ end
121
+
122
+ def test_tag_with_one_attribute
123
+ tag1 = parse_one_tag1("tag1 attr1=99")
124
+ assert_not_nil(tag1, "tag1")
125
+
126
+ values = tag1.values
127
+ assert(values.empty?, "value count")
128
+
129
+ attributes = tag1.attributes
130
+ assert_equal(1, attributes.size, "attribute count")
131
+ assert_equal(99, attributes["attr1"], "attr1")
132
+ end
133
+
134
+ def test_tag_with_attributes
135
+ tag1 = parse_one_tag1(
136
+ "tag1 attr1=\"99\" ns:attr2=[QmVhdXR5IGlzIG5vdCBlbm91Z2gu]")
137
+ assert_not_nil(tag1, "tag1")
138
+
139
+ attributes = tag1.attributes
140
+ assert_equal(2, attributes.size, "attribute count")
141
+ assert_equal("99", attributes["attr1"], "attr1")
142
+ assert_equal(SDL4R::SdlBinary("Beauty is not enough."), attributes["ns:attr2"], "attr2")
143
+ end
144
+
145
+ def test_date
146
+ tag1 = parse_one_tag1("tag1 2005/12/05")
147
+ date = tag1.value
148
+ assert_equal(Date.civil(2005, 12, 5), date, "date value")
149
+ end
150
+
151
+ def test_time
152
+ tag1 = parse_one_tag1(
153
+ "tag1 time=12:23:56 short_time=00:12:32.423" +
154
+ " long_time=30d:15:23:04.023 before=-00:02:30")
155
+ assert_equal(
156
+ SdlTimeSpan.new(0, 12, 23, 56),
157
+ tag1.attribute("time"),
158
+ "time");
159
+ assert_equal(
160
+ SdlTimeSpan.new(0, 0, 12, 32, 423),
161
+ tag1.attribute("short_time"),
162
+ "short_time");
163
+ assert_equal(
164
+ SdlTimeSpan.new(30, 15, 23, 4, 23),
165
+ tag1.attribute("long_time"),
166
+ "long_time");
167
+ assert_equal(
168
+ SdlTimeSpan.new(0, 0, -2, -30),
169
+ tag1.attribute("before"),
170
+ "before");
171
+ end
172
+
173
+ def test_date_time
174
+ tag1 = parse_one_tag1(
175
+ "tag1 date1=2008/06/01 12:34" +
176
+ " date2=1999/12/31 23:59:58" +
177
+ " date3=2000/05/01 12:01:35.997" +
178
+ " date4=2015/12/05 14:12:23.345-JST" +
179
+ " date5=1414/05/12 03:00:01.107-UTC-04" +
180
+ " date6=1807/11/11 22:28:13.888-GMT-08:30")
181
+ assert_equal(
182
+ local_civil_date(2008, 6, 1, 12, 34),
183
+ tag1.attribute("date1"),
184
+ "date1");
185
+ assert_equal(
186
+ local_civil_date(1999, 12, 31, 23, 59, 58),
187
+ tag1.attribute("date2"),
188
+ "date2");
189
+ assert_equal(
190
+ local_civil_date(2000, 5, 1, 12, 1, Rational(35997, 1000)),
191
+ tag1.attribute("date3"),
192
+ "date3");
193
+ assert_equal(
194
+ local_civil_date(2015, 12, 5, 14, 12, Rational(23345, 1000), Rational(9, 24)),
195
+ tag1.attribute("date4"),
196
+ "date4");
197
+ assert_equal(
198
+ local_civil_date(1414, 5, 12, 3, 0, Rational(1107, 1000), Rational(-4, 24)),
199
+ tag1.attribute("date5"),
200
+ "date5");
201
+ assert_equal(
202
+ local_civil_date(1807, 11, 11, 22, 28, Rational(13888, 1000), Rational(-85, 240)),
203
+ tag1.attribute("date6"),
204
+ "date6");
205
+ end
206
+
207
+ def test_numbers
208
+ tag1 = parse_one_tag1(
209
+ "tag1 123 3000000000 456l 789L 123.45f 67.8F 910.11 12.13d 1415.16D 171.8BD 1.920bd 12345678901234567890BD")
210
+ values = tag1.values
211
+ assert_equal(123, values[0])
212
+ assert_equal(3000000000, values[1])
213
+ assert_equal(456, values[2])
214
+ assert_equal(789, values[3])
215
+ assert_equal(123.45, values[4])
216
+ assert_equal(67.8, values[5])
217
+ assert_equal(910.11, values[6])
218
+ assert_equal(12.13, values[7])
219
+ assert_equal(1415.16, values[8])
220
+ assert_equal(171.8, values[9])
221
+ assert_equal(1.920, values[10])
222
+ if defined? Flt::DecNum
223
+ assert_equal(Flt::DecNum("12345678901234567890"), values[11])
224
+ else
225
+ assert_equal(12345678901234567890, values[11])
226
+ end
227
+ end
228
+
229
+ def test_booleans
230
+ tag1 = parse_one_tag1("tag1 b1=true b2=false b3=on b4=off")
231
+ assert_equal(true, tag1.attribute("b1"))
232
+ assert_equal(false, tag1.attribute("b2"))
233
+ assert_equal(true, tag1.attribute("b3"))
234
+ assert_equal(false, tag1.attribute("b4"))
235
+ end
236
+
237
+ def test_null
238
+ tag1 = parse_one_tag1("tag1 null attr1=null")
239
+ values = tag1.values
240
+ assert_equal(1, values.size)
241
+ assert_equal(nil, values[0])
242
+ assert_equal(nil, tag1.attribute("attr1"))
243
+ end
244
+
245
+ def test_comments
246
+ root = Tag.new("root")
247
+ root.read(
248
+ <<EOF
249
+ tag1 123
250
+ #tag2 456
251
+ tag3 789
252
+ --tag4 012
253
+ tag5 345
254
+ //tag6 678
255
+ tag7 901
256
+ /*tag8 234
257
+ tag9 567*/
258
+ tag10 890
259
+ EOF
260
+ )
261
+ children = root.children
262
+ assert_equal(5, children.size, "children count")
263
+ assert_equal(123, root.child("tag1").value)
264
+ assert_nil(root.child("tag2"))
265
+ assert_equal(789, root.child("tag3").value)
266
+ assert_nil(root.child("tag4"))
267
+ assert_equal(345, root.child("tag5").value)
268
+ assert_nil(root.child("tag6"))
269
+ assert_equal(901, root.child("tag7").value)
270
+ assert_nil(root.child("tag8"))
271
+ assert_nil(root.child("tag9"))
272
+ assert_equal(890, root.child("tag10").value)
273
+ end
274
+
275
+ private
276
+
277
+ # Creates and returns a DateTime where an unspecified +zone_offset+ means 'the local zone
278
+ # offset' (contrarily to DateTime#civil())
279
+ def local_civil_date(year, month, day, hour = 0, min = 0, sec = 0, zone_offset = nil)
280
+ zone_offset = Rational(Time.now.utc_offset, 24 * 60 * 60) if zone_offset.nil?
281
+ return DateTime.civil(year, month, day, hour, min, sec, zone_offset)
282
+ end
283
+
284
+ def parse_one_tag1(text)
285
+ root = Tag.new("root")
286
+ root.read(text)
287
+ tag1 = root.child("tag1")
288
+ assert_not_nil(tag1, "tag1")
289
+
290
+ assert_equal 1, root.child_count, "only 1 tag expected"
291
+
292
+ return tag1
293
+ end
294
+ end
295
+ end
@@ -0,0 +1,541 @@
1
+ # Simple Declarative Language (SDL) for Ruby
2
+ # Copyright 2005 Ikayzo, inc.
3
+ #
4
+ # This program is free software. You can distribute or modify it under the
5
+ # terms of the GNU Lesser General Public License version 2.1 as published by
6
+ # the Free Software Foundation.
7
+ #
8
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
9
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
10
+ # See the GNU Lesser General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU Lesser General Public License
13
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
14
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15
+
16
+
17
+ if RUBY_VERSION < '1.9.0'
18
+ $KCODE = 'u'
19
+ require 'jcode'
20
+ end
21
+
22
+ module SDL4R
23
+
24
+ require 'fileutils'
25
+ require 'date'
26
+
27
+ require 'test/unit'
28
+ require File.dirname(__FILE__) + '/../../lib/sdl4r/tag'
29
+
30
+ # SDL unit tests.
31
+ #
32
+ # @author Daniel Leuck
33
+ #
34
+ class Test < Test::Unit::TestCase
35
+
36
+ # Tag datastructure tests
37
+ TAG = "Tag"
38
+ TAG_WRITE_PARSE = "Tag Write Parse"
39
+
40
+ # Basic Types Tests
41
+ STRING_DECLARATIONS = "String Declarations"
42
+ CHARACTER_DECLARATIONS = "Character Declarations"
43
+ NUMBER_DECLARATIONS = "Number Declarations"
44
+ BOOLEAN_DECLARATIONS = "Boolean Declarations"
45
+ NULL_DECLARATION = "Null Declaration"
46
+ DATE_DECLARATIONS = "Date Declarations"
47
+ TIME_SPAN_DECLARATIONS = "Time Span Declarations"
48
+ DATE_TIME_DECLARATIONS = "Date Time Declarations"
49
+ BINARY_DECLARATIONS = "Binary Declarations";
50
+
51
+ # Structure Tests
52
+ EMPTY_TAG = "Empty Tag"
53
+ VALUES = "Values"
54
+ ATTRIBUTES = "Attributes"
55
+ VALUES_AND_ATTRIBUTES = "Values and Attributes"
56
+ CHILDREN = "Children"
57
+ NAMESPACES = "Namespaces"
58
+
59
+
60
+ def initialize(test_method_name)
61
+ super(test_method_name)
62
+ @failures = 0
63
+ @assert_count = 0
64
+ end
65
+
66
+ def assert_tags_equal(expected, actual, message)
67
+ if expected != actual
68
+ assert_equal(expected.to_s, actual.to_s, message)
69
+ end
70
+ end
71
+
72
+ ######################################
73
+ # Tag Tests
74
+ ######################################
75
+ def test_tag
76
+ # Test to make sure Tag ignores the order in which attributes are
77
+ # added.
78
+ t1 = Tag.new("test")
79
+ t1.set_attribute("foo", "bar")
80
+ t1.set_attribute("john", "doe")
81
+
82
+ t2 = Tag.new("test")
83
+ t2.set_attribute("john", "doe")
84
+ t2.set_attribute("foo", "bar")
85
+
86
+ assert_tags_equal(t1, t2, TAG)
87
+
88
+ # Making sure tags with different structures return false from .equals
89
+ t2.value = "item"
90
+ assert_not_equal(t1, t2, TAG)
91
+
92
+ t2.remove_value("item")
93
+ t2.set_attribute("another", "attribute")
94
+ assert_not_equal(t1, t2, TAG)
95
+
96
+ # Checking attributes namespaces
97
+ t2.set_attribute("name", "bill")
98
+ t2.set_attribute("smoker", true, "private")
99
+ t2.set_attribute("hobby", "hiking", "public")
100
+ t2.set_attribute("nickname", "tubby", "private")
101
+
102
+ assert_equal(
103
+ t2.attributes("private"),
104
+ { "smoker" => true, "nickname" => "tubby" },
105
+ "attributes()")
106
+ end
107
+
108
+ def get_file_as_string(filename)
109
+ data = ''
110
+ File.open(filename, "r") { |f|
111
+ f.each_line do |line|
112
+ data += line
113
+ end
114
+ }
115
+ return data
116
+ end
117
+
118
+ #
119
+ # Does a to_s/parse test on the specified Tag (+root+)
120
+ #
121
+ def test_tag_write_parse(root)
122
+ # puts '========================================'
123
+ # puts root.to_s
124
+ # puts '========================================'
125
+ #
126
+ # File.open("d:\\dev\\tmp\\root.sdl", "w") { |io| io.write(root.children_to_string) }
127
+ # File.open("d:\\dev\\tmp\\root_reparsed.sdl", "w") { |io| io.write(Tag.new("test").read(root.to_s).child("root").children_to_string) }
128
+
129
+ assert_tags_equal(
130
+ root,
131
+ Tag.new("test").read(root.to_s).child("root"),
132
+ "write/parse")
133
+ end
134
+
135
+ ######################################
136
+ # Basic Types Tests
137
+ ######################################
138
+
139
+ #
140
+ # +root+: a Tag
141
+ #
142
+ def test_strings(root)
143
+ # Doing String tests...
144
+ # Doing basic tests including new line handling...
145
+ assert_equal(root.child("string1").value, "hello", STRING_DECLARATIONS)
146
+ assert_equal(root.child("string2").value, "hi", STRING_DECLARATIONS)
147
+ assert_equal(root.child("string3").value, "aloha", STRING_DECLARATIONS)
148
+ assert_equal(root.child("string4").value, "hi there", STRING_DECLARATIONS)
149
+ assert_equal(root.child("string5").value, "hi there joe", STRING_DECLARATIONS)
150
+ assert_equal(root.child("string6").value, "line1\nline2", STRING_DECLARATIONS)
151
+ assert_equal(root.child("string7").value, "line1\nline2", STRING_DECLARATIONS)
152
+ assert_equal(root.child("string8").value, "line1\nline2\nline3", STRING_DECLARATIONS)
153
+ assert_equal(
154
+ root.child("string9").value,
155
+ "Anything should go in this line without escapes \\ \\\\ \\n " +
156
+ "\\t \" \"\" ' ''", STRING_DECLARATIONS)
157
+ assert_equal(root.child("string10").value, "escapes \"\\\n\t", STRING_DECLARATIONS)
158
+
159
+ # Checking unicode strings...
160
+ assert_equal(root.child("japanese").value, "日本語", STRING_DECLARATIONS)
161
+ assert_equal(root.child("korean").value, "여보세요", STRING_DECLARATIONS)
162
+ assert_equal(root.child("russian").value, "здравствулте", STRING_DECLARATIONS)
163
+
164
+ # More new line tests...
165
+ assert(root.child("xml").value.index("<text>Hi there!</text>") >= 0, STRING_DECLARATIONS)
166
+ assert_equal(root.child("line_test").value, "\nnew line above and below\n", STRING_DECLARATIONS)
167
+ end
168
+
169
+ def test_characters(root)
170
+ # Doing character tests...
171
+ assert_equal(root.child("char1").value, 'a', CHARACTER_DECLARATIONS)
172
+ assert_equal(root.child("char2").value, 'A', CHARACTER_DECLARATIONS)
173
+ assert_equal(root.child("char3").value, '\\', CHARACTER_DECLARATIONS)
174
+ assert_equal(root.child("char4").value, "\n", CHARACTER_DECLARATIONS)
175
+ assert_equal(root.child("char5").value, "\t", CHARACTER_DECLARATIONS)
176
+ assert_equal(root.child("char6").value, '\'', CHARACTER_DECLARATIONS)
177
+ assert_equal(root.child("char7").value, '"', CHARACTER_DECLARATIONS)
178
+
179
+ # Doing unicode character tests...
180
+ if RUBY_VERSION >= '1.9.0'
181
+ assert_equal(root.child("char8").value, "\u65e5", CHARACTER_DECLARATIONS)
182
+ assert_equal(root.child("char9").value, "\uc5ec", CHARACTER_DECLARATIONS)
183
+ assert_equal(root.child("char10").value, "\u0437", CHARACTER_DECLARATIONS)
184
+ end
185
+ end
186
+
187
+ def test_numbers(root)
188
+ # Testing ints...
189
+ assert_equal(root.child("int1").value, 0, NUMBER_DECLARATIONS)
190
+ assert_equal(root.child("int2").value, 5, NUMBER_DECLARATIONS)
191
+ assert_equal(root.child("int3").value, -100, NUMBER_DECLARATIONS)
192
+ assert_equal(root.child("int4").value, 234253532, NUMBER_DECLARATIONS)
193
+
194
+ # Testing longs...
195
+ assert_equal(root.child("long1").value, 0, NUMBER_DECLARATIONS)
196
+ assert_equal(root.child("long2").value, 5, NUMBER_DECLARATIONS)
197
+ assert_equal(root.child("long3").value, 5, NUMBER_DECLARATIONS)
198
+ assert_equal(root.child("long4").value, 3904857398753453453, NUMBER_DECLARATIONS)
199
+
200
+ # Testing floats...
201
+ assert_equal(root.child("float1").value, 1, NUMBER_DECLARATIONS)
202
+ assert_equal(root.child("float2").value, 0.23, NUMBER_DECLARATIONS)
203
+ assert_equal(root.child("float3").value, -0.34, NUMBER_DECLARATIONS)
204
+
205
+ # Testing doubles..."
206
+ assert_equal(root.child("double1").value, 2, NUMBER_DECLARATIONS)
207
+ assert_equal(root.child("double2").value, -0.234, NUMBER_DECLARATIONS)
208
+ assert_equal(root.child("double3").value, 2.34, NUMBER_DECLARATIONS)
209
+
210
+ # Testing decimals (BigDouble in Java)...
211
+ assert_equal(
212
+ root.child("decimal1").value, 0, NUMBER_DECLARATIONS);
213
+ assert_equal(
214
+ root.child("decimal2").value, 11.111111, NUMBER_DECLARATIONS);
215
+ assert_equal(
216
+ root.child("decimal3").value, 234535.3453453453454345345341242343, NUMBER_DECLARATIONS);
217
+ end
218
+
219
+ def test_booleans(root)
220
+ assert_equal(root.child("light-on").value, true, BOOLEAN_DECLARATIONS)
221
+ assert_equal(root.child("light-off").value, false, BOOLEAN_DECLARATIONS)
222
+ assert_equal(root.child("light1").value, true, BOOLEAN_DECLARATIONS)
223
+ assert_equal(root.child("light2").value, false, BOOLEAN_DECLARATIONS)
224
+ end
225
+
226
+ def test_null(root)
227
+ assert_equal(root.child("nothing").value, nil, NULL_DECLARATION);
228
+ end
229
+
230
+ def test_dates(root)
231
+ assert_equal(root.child("date1").value, Date.civil(2005, 12, 31), DATE_DECLARATIONS)
232
+ assert_equal(root.child("date2").value, Date.civil(1882, 5, 2), DATE_DECLARATIONS)
233
+ assert_equal(root.child("date3").value, Date.civil(1882, 5, 2), DATE_DECLARATIONS)
234
+ assert_equal(root.child("_way_back").value, Date.civil(582, 9, 16), DATE_DECLARATIONS)
235
+ end
236
+
237
+ def test_time_spans(root)
238
+ assert_equal(
239
+ root.child("time1").value, SdlTimeSpan.new(0,12,30,0,0), TIME_SPAN_DECLARATIONS)
240
+ assert_equal(
241
+ root.child("time2").value, SdlTimeSpan.new(0,24,0,0,0), TIME_SPAN_DECLARATIONS)
242
+ assert_equal(
243
+ root.child("time3").value, SdlTimeSpan.new(0,1,0,0,0), TIME_SPAN_DECLARATIONS)
244
+ assert_equal(
245
+ root.child("time4").value, SdlTimeSpan.new(0,1,0,0,0), TIME_SPAN_DECLARATIONS)
246
+ assert_equal(
247
+ root.child("time5").value, SdlTimeSpan.new(0,12,30,2,0), TIME_SPAN_DECLARATIONS)
248
+ assert_equal(
249
+ root.child("time6").value, SdlTimeSpan.new(0,12,30,23,0), TIME_SPAN_DECLARATIONS)
250
+ assert_equal(
251
+ root.child("time7").value, SdlTimeSpan.new(0,12,30,23,100), TIME_SPAN_DECLARATIONS)
252
+ assert_equal(
253
+ root.child("time8").value, SdlTimeSpan.new(0,12,30,23,120), TIME_SPAN_DECLARATIONS)
254
+ assert_equal(
255
+ root.child("time9").value, SdlTimeSpan.new(0,12,30,23,123), TIME_SPAN_DECLARATIONS)
256
+
257
+ # Checking time spans with days...
258
+ assert_equal(
259
+ root.child("time10").value, SdlTimeSpan.new(34,12,30,23,100), TIME_SPAN_DECLARATIONS)
260
+ assert_equal(
261
+ root.child("time11").value, SdlTimeSpan.new(1,12,30,0,0), TIME_SPAN_DECLARATIONS)
262
+ assert_equal(
263
+ root.child("time12").value, SdlTimeSpan.new(5,12,30,23,123), TIME_SPAN_DECLARATIONS)
264
+
265
+ # Checking negative time spans...
266
+ assert_equal(
267
+ root.child("time13").value, SdlTimeSpan.new(0,-12,-30,-23,-123), TIME_SPAN_DECLARATIONS)
268
+ assert_equal(
269
+ root.child("time14").value, SdlTimeSpan.new(-5,-12,-30,-23,-123), TIME_SPAN_DECLARATIONS)
270
+ end
271
+
272
+ def test_date_times(root)
273
+ local_offset = DateTime.now.offset
274
+
275
+ assert_equal(root.child("date_time1").value,
276
+ DateTime.civil(2005,12,31,12,30,0, local_offset), DATE_TIME_DECLARATIONS)
277
+ assert_equal(root.child("date_time2").value,
278
+ DateTime.civil(1882,5,2,12,30,0, local_offset), DATE_TIME_DECLARATIONS)
279
+ assert_equal(root.child("date_time3").value,
280
+ DateTime.civil(2005,12,31,1,0,0, local_offset), DATE_TIME_DECLARATIONS)
281
+ assert_equal(root.child("date_time4").value,
282
+ DateTime.civil(1882,5,2,1,0,0, local_offset), DATE_TIME_DECLARATIONS)
283
+ assert_equal(root.child("date_time5").value,
284
+ DateTime.civil(2005,12,31,12,30,2312.to_r/100, local_offset), DATE_TIME_DECLARATIONS)
285
+ assert_equal(root.child("date_time6").value,
286
+ DateTime.civil(1882,5,2,12,30,23123.to_r/1000, local_offset), DATE_TIME_DECLARATIONS)
287
+
288
+ # Checking timezones...
289
+ assert_equal(root.child("date_time7").value,
290
+ DateTime.civil(1882,5,2,12,30,23123.to_r/1000,"JST"), DATE_TIME_DECLARATIONS)
291
+ assert_equal(root.child("date_time8").value,
292
+ DateTime.civil(985,04,11,12,30,23123.to_r/1000,"PST"), DATE_TIME_DECLARATIONS)
293
+ end
294
+
295
+ def test_binaries(root)
296
+ assert_equal(SDL4R::SdlBinary("hi"), root.child("hi").value, BINARY_DECLARATIONS)
297
+ assert_equal(
298
+ SDL4R::SdlBinary("hi"), root.child("hi").value, BINARY_DECLARATIONS)
299
+ assert_equal(
300
+ SdlBinary.decode64(
301
+ "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAKnRFWHRDcmVhdGlvbiBUaW1l" +
302
+ "AERpIDQgTXJ6IDIwMDMgMDA6MjQ6MDQgKzAxMDDdSQ6OAAAAB3RJTUUH0wMEAAcllPlrJgAA" +
303
+ "AAlwSFlzAAAK8AAACvABQqw0mAAAAARnQU1BAACxjwv8YQUAAADQSURBVHjaY2CgEDCCyZn/" +
304
+ "3YHkDhL1ejCkM+5kgXJ2zDQmXueShwwMh9+ALWSEGcCQfhZIvHlDnAk8PAwMHBxgJtyAa7bX" +
305
+ "UdT8/cvA8Ps3hP7zB4FBYn/+vGbweqyJaoCmpiaKASDFv35BNMBoZMzwGKKOidJYoNgAuBdm" +
306
+ "naXQgHRKDfgagxD89w8S+iAaFICwGIHFAgjrHUczAByySAaAMEgDLBphhv7/D8EYLgDZhAxA" +
307
+ "mkAKYYbAMMwwDAOQXYDuDXRXgDC6AR7SW8jITNQAACjZgdj4VjlqAAAAAElFTkSuQmCC"
308
+ ),
309
+ root.child("png").value,
310
+ BINARY_DECLARATIONS)
311
+ end
312
+
313
+ ######################################
314
+ # Structure Tests (values, attributes, children)
315
+ ######################################
316
+
317
+ def test_empty_tag(root)
318
+ assert_equal(root.child("empty_tag"), Tag.new("empty_tag"), EMPTY_TAG)
319
+ end
320
+
321
+ def test_values(root)
322
+ local_offset = DateTime.now.offset
323
+
324
+ assert_equal(root.child("values1").values, ["hi"], VALUES)
325
+ assert_equal(root.child("values2").values, ["hi","ho"], VALUES)
326
+ assert_equal(root.child("values3").values, [1, "ho"], VALUES)
327
+ assert_equal(root.child("values4").values, ["hi",5], VALUES)
328
+ assert_equal(root.child("values5").values, [1,2], VALUES)
329
+ assert_equal(root.child("values6").values, [1,2,3], VALUES)
330
+ assert_equal(
331
+ root.child("values7").values,
332
+ [nil,"foo",false,Date.civil(1980,12,5)],
333
+ VALUES)
334
+ assert_equal(
335
+ root.child("values8").values,
336
+ [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
337
+ "there", SdlTimeSpan.new(0,15,23,12,234)],
338
+ VALUES)
339
+ assert_equal(
340
+ root.child("values9").values,
341
+ [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
342
+ "there", DateTime.civil(1989,8,12,15,23,12234.to_r/1000,"JST")],
343
+ VALUES)
344
+ assert_equal(
345
+ root.child("values10").values,
346
+ [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
347
+ "there", SdlTimeSpan.new(0,15,23,12,234), "more stuff"],
348
+ VALUES)
349
+ assert_equal(
350
+ root.child("values11").values,
351
+ [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
352
+ "there", SdlTimeSpan.new(123,15,23,12,234), "more stuff here"],
353
+ VALUES)
354
+ assert_equal(root.child("values12").values, [1,3], VALUES)
355
+ assert_equal(root.child("values13").values, [1,3], VALUES)
356
+ assert_equal(root.child("values14").values, [1,3], VALUES)
357
+ assert_equal(root.child("values15").values, [1,2,4,5,6], VALUES)
358
+ assert_equal(root.child("values16").values, [1,2,5], VALUES)
359
+ assert_equal(root.child("values17").values, [1,2,5], VALUES)
360
+ assert_equal(root.child("values18").values, [1,2,7], VALUES)
361
+ assert_equal(root.child("values19").values, [1,3,5,7], VALUES)
362
+ assert_equal(root.child("values20").values, [1,3,5], VALUES)
363
+ assert_equal(root.child("values21").values, [1,3,5], VALUES)
364
+ assert_equal(root.child("values22").values, ["hi","ho","ho",5,"hi"], VALUES)
365
+ end
366
+
367
+ def test_attributes(root)
368
+ assert_equal(
369
+ root.child("atts1").attributes, {"name" => "joe"}, ATTRIBUTES);
370
+ assert_equal(root.child("atts2").attributes, {"size" => 5}, ATTRIBUTES);
371
+ assert_equal(
372
+ root.child("atts3").attributes, {"name" => "joe","size" => 5}, ATTRIBUTES);
373
+ assert_equal(
374
+ root.child("atts4").attributes,
375
+ {"name"=>"joe","size"=>5,"smoker"=>false},
376
+ ATTRIBUTES);
377
+ assert_equal(
378
+ root.child("atts5").attributes, {"name"=>"joe","smoker"=>false}, ATTRIBUTES);
379
+ assert_equal(
380
+ root.child("atts6").attributes, {"name"=>"joe","smoker"=>false}, ATTRIBUTES);
381
+ assert_equal(root.child("atts7").attributes, {"name"=>"joe"}, ATTRIBUTES);
382
+ assert_equal(
383
+ root.child("atts8").attributes,
384
+ {"name"=>"joe","size"=>5,"smoker"=>false,
385
+ "text"=>"hi","birthday"=>Date.civil(1972,5,23)},
386
+ ATTRIBUTES);
387
+ assert_equal(
388
+ root.child("atts9").attribute("key"), SDL4R::SdlBinary("mykey"), ATTRIBUTES)
389
+ end
390
+
391
+ def test_values_and_attributes(root)
392
+ assert_equal(root.child("valatts1").values, ["joe"], VALUES_AND_ATTRIBUTES)
393
+ assert_equal(
394
+ root.child("valatts1").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
395
+
396
+ assert_equal(root.child("valatts2").values, ["joe"], VALUES_AND_ATTRIBUTES)
397
+ assert_equal(root.child("valatts2").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
398
+
399
+ assert_equal(root.child("valatts3").values, ["joe"], VALUES_AND_ATTRIBUTES)
400
+ assert_equal(root.child("valatts3").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
401
+
402
+ assert_equal(root.child("valatts4").values, ["joe"], VALUES_AND_ATTRIBUTES)
403
+ assert_equal(
404
+ root.child("valatts4").attributes,
405
+ {"size"=>5, "weight"=>160, "hat"=>"big"},
406
+ VALUES_AND_ATTRIBUTES)
407
+
408
+ assert_equal(
409
+ root.child("valatts5").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
410
+ assert_equal(
411
+ root.child("valatts5").attributes,
412
+ {"size"=>5, "smoker"=>false},
413
+ VALUES_AND_ATTRIBUTES);
414
+
415
+ assert_equal(
416
+ root.child("valatts6").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
417
+ assert_equal(
418
+ root.child("valatts6").attributes,
419
+ {"size"=>5, "house"=>"big and\n blue"},
420
+ VALUES_AND_ATTRIBUTES)
421
+
422
+ #####
423
+
424
+ assert_equal(
425
+ root.child("valatts7").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
426
+ assert_equal(
427
+ root.child("valatts7").attributes,
428
+ {"size"=>5, "smoker"=>false},
429
+ VALUES_AND_ATTRIBUTES)
430
+
431
+ assert_equal(
432
+ root.child("valatts8").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
433
+ assert_equal(
434
+ root.child("valatts8").attributes,
435
+ {"size"=>5, "smoker"=>false},
436
+ VALUES_AND_ATTRIBUTES);
437
+
438
+ assert_equal(
439
+ root.child("valatts9").values,["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
440
+ assert_equal(
441
+ root.child("valatts9").attributes,
442
+ {"size"=>5, "smoker"=>false},
443
+ VALUES_AND_ATTRIBUTES)
444
+ end
445
+
446
+ def test_children(root)
447
+ parent = root.child("parent")
448
+
449
+ assert_equal(parent.children.size, 2, CHILDREN)
450
+ assert_equal(parent.children[1].name, "daughter", CHILDREN)
451
+
452
+
453
+ grandparent = root.child("grandparent")
454
+
455
+ assert_equal(grandparent.children.size, 2, CHILDREN)
456
+ # recursive fetch of children
457
+ assert_equal(grandparent.children(true).size, 6, CHILDREN)
458
+ assert_equal(grandparent.children(true, "son").size, 2, CHILDREN)
459
+
460
+ grandparent2 = root.child("grandparent2")
461
+ assert_equal(grandparent2.children(true, "child").size, 5, CHILDREN)
462
+ assert_equal(
463
+ grandparent2.child("daughter", true).attribute("birthday"),
464
+ Date.civil(1976,04,18),
465
+ CHILDREN)
466
+
467
+ files = root.child("files")
468
+
469
+ assert_equal(
470
+ ["c:/file1.txt", "c:/file2.txt", "c:/folder"],
471
+ files.children_values("content"),
472
+ CHILDREN)
473
+
474
+ matrix = root.child("matrix")
475
+
476
+ assert_equal([[1,2,3],[4,5,6]], matrix.children_values("content"), CHILDREN);
477
+ end
478
+
479
+ def test_namespaces(root)
480
+ assert_equal(8, root.children(true, nil, "person").size, NAMESPACES);
481
+
482
+ grandparent2 = root.child("grandparent3");
483
+
484
+ # get only the attributes for Akiko in the public namespace
485
+ assert_equal(
486
+ grandparent2.child("daughter", true).attributes("public"),
487
+ {"name"=>"Akiko", "birthday"=>Date.civil(1976,04,18)},
488
+ NAMESPACES);
489
+ end
490
+
491
+ def get_test_sdl_file_path(filename)
492
+ dir = File.dirname(__FILE__)
493
+ return dir + '/' + filename
494
+ end
495
+
496
+ def test_basic_types
497
+ root = nil
498
+
499
+ # Reading test_basic_types.sdl
500
+ File.open(get_test_sdl_file_path("test_basic_types.sdl")) do |io|
501
+ root = Tag.new("root")
502
+ root.read(io)
503
+ end
504
+
505
+ test_tag_write_parse(root)
506
+ test_strings(root)
507
+ test_characters(root)
508
+ test_numbers(root)
509
+ test_booleans(root)
510
+ test_null(root)
511
+ test_dates(root)
512
+ test_time_spans(root)
513
+ test_date_times(root)
514
+ test_binaries(root)
515
+ end
516
+
517
+ def test_structures
518
+ root = nil
519
+
520
+ # Reading test_structures.sdl
521
+ File.open(get_test_sdl_file_path("test_structures.sdl")) do |io|
522
+ root = Tag.new("root")
523
+ root.read(io)
524
+ end
525
+
526
+ test_tag_write_parse(root)
527
+ test_empty_tag(root)
528
+ test_values(root)
529
+ test_attributes(root)
530
+ test_values_and_attributes(root)
531
+ test_children(root)
532
+ test_namespaces(root)
533
+ end
534
+
535
+ def report_error(test_name, error)
536
+ @failures += 1
537
+ puts "!! Failure: #{test_name} - #{error}"
538
+ end
539
+
540
+ end
541
+ end