sdl4r 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ #--
1
2
  # Simple Declarative Language (SDL) for Ruby
2
3
  # Copyright 2005 Ikayzo, inc.
3
4
  #
@@ -12,6 +13,7 @@
12
13
  # You should have received a copy of the GNU Lesser General Public License
13
14
  # along with this program; if not, contact the Free Software Foundation, Inc.,
14
15
  # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
+ #++
15
17
 
16
18
 
17
19
  module SDL4R
@@ -1,3 +1,4 @@
1
+ #--
1
2
  # Simple Declarative Language (SDL) for Ruby
2
3
  # Copyright 2005 Ikayzo, inc.
3
4
  #
@@ -12,6 +13,7 @@
12
13
  # You should have received a copy of the GNU Lesser General Public License
13
14
  # along with this program; if not, contact the Free Software Foundation, Inc.,
14
15
  # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
+ #++
15
17
 
16
18
 
17
19
  if RUBY_VERSION < '1.9.0'
@@ -38,7 +40,7 @@ module SDL4R
38
40
  #
39
41
  # @author Daniel Leuck
40
42
  #
41
- class Test < Test::Unit::TestCase
43
+ class SDL4RTest < Test::Unit::TestCase
42
44
 
43
45
  # Tag datastructure tests
44
46
  TAG = "Tag"
@@ -115,9 +117,9 @@ module SDL4R
115
117
 
116
118
  # Checking attributes namespaces
117
119
  t2.set_attribute("name", "bill")
118
- t2.set_attribute("smoker", true, "private")
119
- t2.set_attribute("hobby", "hiking", "public")
120
- t2.set_attribute("nickname", "tubby", "private")
120
+ t2.set_attribute("private", "smoker", true)
121
+ t2.set_attribute("public", "hobby", "hiking")
122
+ t2.set_attribute("private", "nickname", "tubby")
121
123
 
122
124
  assert_equal(
123
125
  t2.attributes("private"),
@@ -500,7 +502,7 @@ module SDL4R
500
502
  grandparent2 = root.child("grandparent2")
501
503
  assert_equal(grandparent2.children(true, "child").size, 5, CHILDREN)
502
504
  assert_equal(
503
- grandparent2.child("daughter", true).attribute("birthday"),
505
+ grandparent2.child(true, "daughter").attribute("birthday"),
504
506
  Date.civil(1976,04,18),
505
507
  CHILDREN)
506
508
 
@@ -518,13 +520,13 @@ module SDL4R
518
520
 
519
521
  def test_namespaces
520
522
  root = @@root_structures
521
- assert_equal(8, root.children(true, nil, "person").size, NAMESPACES);
523
+ assert_equal(8, root.children(true, "person", nil).size, NAMESPACES);
522
524
 
523
525
  grandparent2 = root.child("grandparent3");
524
526
 
525
527
  # get only the attributes for Akiko in the public namespace
526
528
  assert_equal(
527
- grandparent2.child("daughter", true).attributes("public"),
529
+ grandparent2.child(true, "daughter").attributes("public"),
528
530
  {"name"=>"Akiko", "birthday"=>Date.civil(1976,04,18)},
529
531
  NAMESPACES);
530
532
  end
@@ -0,0 +1,487 @@
1
+ #--
2
+ # Simple Declarative Language (SDL) for Ruby
3
+ # Copyright 2005 Ikayzo, inc.
4
+ #
5
+ # This program is free software. You can distribute or modify it under the
6
+ # terms of the GNU Lesser General Public License version 2.1 as published by
7
+ # the Free Software Foundation.
8
+ #
9
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
10
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11
+ # See the GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public License
14
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
15
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
+ #++
17
+
18
+ module SDL4R
19
+
20
+ require 'test/unit'
21
+ require "rexml/document"
22
+
23
+ require File.dirname(__FILE__) + '/../../lib/sdl4r/tag'
24
+
25
+
26
+ class TagTest < Test::Unit::TestCase
27
+
28
+ public
29
+
30
+ def test_initialize
31
+ tag = Tag.new("tag1")
32
+ assert_equal "tag1", tag.name, "name"
33
+ assert_equal "", tag.namespace, "namespace"
34
+ assert_equal 0, tag.child_count
35
+ assert_equal false, tag.has_children?
36
+ assert_equal [], tag.children
37
+ assert_equal [], tag.values
38
+ assert_equal Hash.new, tag.attributes
39
+
40
+ tag = Tag.new("ns1", "tag1")
41
+ assert_equal "tag1", tag.name, "name"
42
+ assert_equal "ns1", tag.namespace, "namespace"
43
+
44
+ # Check we can't pass garbage to the constructor
45
+ assert_raise ArgumentError do
46
+ Tag.new(1)
47
+ end
48
+ assert_raise ArgumentError do
49
+ Tag.new("%@!")
50
+ end
51
+ assert_raise ArgumentError do
52
+ Tag.new(1, "tag1")
53
+ end
54
+ assert_raise ArgumentError do
55
+ Tag.new("%@!", "tag1")
56
+ end
57
+
58
+ # check the block idiom
59
+ tag = Tag.new("ns1", "tag1") do
60
+ self.name = "tag2"
61
+ self.namespace = "ns2"
62
+ end
63
+ assert_equal "tag2", tag.name
64
+ assert_equal "ns2", tag.namespace
65
+ end
66
+
67
+ def test_children
68
+ tag1 = nil
69
+ tag2 = nil
70
+ tag3 = nil
71
+ tag4 = nil
72
+ tag5_1 = nil
73
+ tag5_2 = nil
74
+
75
+ tag1 = Tag.new("tag1") do
76
+ tag2 = new_child "ns1", "tag2" do
77
+ tag3 = new_child "tag3"
78
+ end
79
+ tag4 = new_child "tag4" do
80
+ tag5_1 = new_child "ns1", "tag5"
81
+ tag5_2 = new_child "ns2", "tag5"
82
+ end
83
+ end
84
+
85
+ assert_equal 2, tag1.child_count
86
+ assert_equal tag2, tag1.child
87
+ assert_equal tag2, tag1.child("tag2")
88
+ assert tag1.has_child?("tag2")
89
+ assert !tag1.has_child?("tag10")
90
+ assert_equal tag2, tag1.children[0]
91
+ assert_equal tag3, tag1.child(true, "tag3")
92
+ assert_equal tag3, tag2.children[0]
93
+ assert_equal 1, tag2.child_count
94
+ assert tag2.has_children?
95
+ assert_equal tag4, tag1.child("tag4")
96
+ assert_equal tag4, tag1.children[1]
97
+ assert_equal [tag5_1, tag5_2], tag4.children
98
+
99
+ assert_equal [tag2, tag4], tag1.children(false)
100
+ array = []
101
+ tag1.children(false) { |child| array << child }
102
+ assert_equal [tag2, tag4], array
103
+
104
+ assert_equal [tag2, tag3, tag4, tag5_1, tag5_2], tag1.children(true)
105
+ array = []
106
+ tag1.children(true) { |child| array << child }
107
+ assert_equal [tag2, tag3, tag4, tag5_1, tag5_2], array
108
+
109
+ assert_equal [tag5_1, tag5_2], tag1.children(true, "tag5")
110
+ array = []
111
+ tag1.children(true, "tag5") { |child| array << child }
112
+ assert_equal [tag5_1, tag5_2], array
113
+
114
+ assert_equal [tag2, tag5_1], tag1.children(true, "ns1", nil)
115
+ array = []
116
+ tag1.children(true, "ns1", nil) { |child| array << child }
117
+ assert_equal [tag2, tag5_1], array
118
+
119
+ assert_equal [tag2], tag1.children(false, "ns1", nil)
120
+ array = []
121
+ tag1.children(false, "ns1", nil) { |child| array << child }
122
+ assert_equal [tag2], array
123
+
124
+ removed_tag = tag4.remove_child(tag5_1)
125
+ assert_equal [tag5_2], tag4.children
126
+ assert removed_tag
127
+
128
+ removed_tag = tag1.remove_child(tag5_1)
129
+ assert_equal [tag2, tag4], tag1.children
130
+ assert !removed_tag
131
+
132
+ tag1.clear_children
133
+ assert_equal nil, tag1.child
134
+ assert_equal [], tag1.children
135
+ end
136
+
137
+ def test_values
138
+ tag1 = Tag.new "tag1"
139
+
140
+ assert_equal nil, tag1.value
141
+ assert_equal [], tag1.values
142
+
143
+ tag1.values { fail "there should be no value" }
144
+
145
+ tag1.values = [1]
146
+ assert_equal 1, tag1.value
147
+ assert_equal [1], tag1.values
148
+ assert tag1.has_value?(1)
149
+ assert !tag1.has_value?(2)
150
+
151
+ tag1.values = [1, 2]
152
+ assert_equal 1, tag1.value
153
+ assert_equal [1, 2], tag1.values
154
+ assert tag1.has_value?(1)
155
+ assert tag1.has_value?(2)
156
+ assert !tag1.has_value?(3)
157
+
158
+ tag1.add_value(3)
159
+ assert_equal 1, tag1.value
160
+ assert_equal [1, 2, 3], tag1.values
161
+ assert !tag1.has_value?(nil)
162
+
163
+ tag1.value = nil
164
+ assert_equal nil, tag1.value
165
+ assert_equal [nil, 2, 3], tag1.values
166
+ assert !tag1.has_value?(1)
167
+ assert tag1.has_value?(nil)
168
+
169
+ assert tag1.remove_value(2)
170
+ assert !tag1.remove_value(2)
171
+ assert_equal nil, tag1.value
172
+ assert_equal [nil, 3], tag1.values
173
+
174
+ tag1.add_value(nil)
175
+ assert_equal nil, tag1.value
176
+ assert_equal [nil, 3, nil], tag1.values
177
+ assert !tag1.has_value?(2)
178
+ assert tag1.has_value?(nil)
179
+
180
+ tag1.remove_value(nil)
181
+ assert_equal 3, tag1.value
182
+ assert_equal [3, nil], tag1.values
183
+ assert !tag1.has_value?(2)
184
+ assert tag1.has_value?(3)
185
+ assert tag1.has_value?(nil)
186
+ end
187
+
188
+ def test_stream_operator
189
+ tag1 = Tag.new "tag1"
190
+
191
+ tag1 << Tag.new("child")
192
+ assert_equal [Tag.new("child")], tag1.children
193
+
194
+ tag1.clear_children
195
+ tag1 << Tag.new("tag1") << Tag.new("tag2") # test that all tags are added to tag1
196
+ assert_equal [Tag.new("tag1"), Tag.new("tag2")], tag1.children
197
+
198
+ tag1 << 123
199
+ assert_equal [123], tag1.values
200
+ tag1 << nil << "abc"
201
+ assert_equal [123, nil, "abc"], tag1.values
202
+
203
+ tag1.clear_values
204
+ tag1 << ["def", 678, nil]
205
+ assert_equal ["def", 678, nil], tag1.values
206
+
207
+ tag1 << { "length" => 13 }
208
+ assert_equal 13, tag1.attribute("length")
209
+ tag1 << { "side:length" => 54, "top:length" => 67}
210
+ assert_equal 13, tag1.attribute("length")
211
+ assert_equal 54, tag1.attribute("side", "length")
212
+ assert_equal 67, tag1.attribute("top", "length")
213
+
214
+ # Test matrix construction
215
+ tag1.clear_children
216
+ tag1 << [[1, 2, 3], [4, 5, 6]]
217
+ assert_equal "content", tag1.children[0].name
218
+ assert_equal "content", tag1.children[1].name
219
+ assert_equal [1, 2, 3], tag1.children[0].values
220
+ assert_equal [4, 5, 6], tag1.children[1].values
221
+ assert_equal 2, tag1.child_count
222
+ end
223
+
224
+ def test_attributes
225
+ tag = Tag.new("tag1")
226
+
227
+ assert_equal 0, tag.attributes.size
228
+ assert !tag.has_attribute?("a1")
229
+
230
+ tag.set_attribute("a1", 1)
231
+ assert_equal 1, tag.attribute("a1")
232
+ assert_equal({"a1" => 1}, tag.attributes)
233
+ assert tag.has_attribute?("a1")
234
+ assert !tag.has_attribute?("a2")
235
+
236
+ tag.set_attribute("a2", 2)
237
+ assert_equal 1, tag.attribute("a1")
238
+ assert_equal 1, tag.attribute("", "a1")
239
+ assert_equal 2, tag.attribute("a2")
240
+ assert_equal 2, tag.attribute("", "a2")
241
+ assert_equal({"a1" => 1, "a2" => 2}, tag.attributes)
242
+
243
+ tag.remove_attribute("a3") # nothing should change
244
+ assert_equal({"a1" => 1, "a2" => 2}, tag.attributes)
245
+
246
+ tag.remove_attribute("a1", "ns1") # nothing should change
247
+ assert_equal({"a1" => 1, "a2" => 2}, tag.attributes)
248
+
249
+ tag.set_attribute("ns1", "a1", 456)
250
+ assert tag.has_attribute?("ns1", "a1")
251
+ assert_equal 456, tag.attribute("ns1", "a1")
252
+
253
+ tag.remove_attribute("a1")
254
+ assert_nil tag.attribute("a1")
255
+ assert tag.has_attribute?("ns1", "a1")
256
+ assert_equal({"a2" => 2, "ns1:a1" => 456}, tag.attributes)
257
+
258
+ tag.clear_attributes
259
+ assert_nil tag.attribute("a2")
260
+ assert_equal({}, tag.attributes)
261
+ end
262
+
263
+ def test_attributes_with_namespace
264
+ tag = Tag.new "tag1"
265
+
266
+ tag.set_attribute("", "a1", 1)
267
+ tag.set_attribute("ns1", "a1", 2)
268
+ assert_equal 1, tag.attribute("a1")
269
+ assert_equal 2, tag.attribute("ns1", "a1")
270
+ assert_equal nil, tag.attribute("ns2", "a1")
271
+
272
+ tag.set_attribute("a1", 3)
273
+ assert_equal 3, tag.attribute("a1")
274
+ assert_equal 3, tag.attribute("", "a1")
275
+ assert_equal 2, tag.attribute("ns1", "a1")
276
+ assert_equal nil, tag.attribute("ns2", "a1")
277
+
278
+ tag.set_attribute("ns2", "a1", 4)
279
+ assert_equal 3, tag.attribute("a1")
280
+ assert_equal 2, tag.attribute("ns1", "a1")
281
+ assert_equal 4, tag.attribute("ns2", "a1")
282
+
283
+ tag.set_attribute("ns1", "b1", 5)
284
+ assert_equal({"a1" => 2, "b1" => 5}, tag.attributes("ns1"))
285
+ assert_equal({"a1" => 4}, tag.attributes("ns2"))
286
+ assert_equal({"a1" => 3}, tag.attributes(""))
287
+ assert_equal({"a1" => 3, "ns1:a1" => 2, "ns2:a1" => 4, "ns1:b1" => 5}, tag.attributes)
288
+
289
+ tag.remove_attribute("ns1", "a1")
290
+ assert_equal nil, tag.attribute("ns1", "a1")
291
+
292
+ tag.clear_attributes("")
293
+ assert_equal({"ns2:a1" => 4, "ns1:b1" => 5}, tag.attributes)
294
+
295
+ # test bad arguments
296
+ assert_raise(ArgumentError) { tag.set_attribute "1", 123 }
297
+ assert_raise(ArgumentError) { tag.set_attribute "&o^", 123 }
298
+ assert_raise(ArgumentError) { tag.set_attribute "1", "a1", 123 }
299
+ assert_raise(ArgumentError) { tag.set_attribute "&o^", "a1", 123 }
300
+ end
301
+
302
+ def test_set_attributes
303
+ tag = Tag.new "tag1"
304
+
305
+ tag.set_attributes({"a1" => 1})
306
+ assert tag.has_attribute?("a1")
307
+ assert_equal 1, tag.attribute("a1")
308
+ assert_equal({"a1" => 1}, tag.attributes)
309
+
310
+ tag.set_attributes({"a2" => 2, "a3" => 3})
311
+ assert !tag.has_attribute?("a1")
312
+ assert tag.has_attribute?("a2")
313
+ assert tag.has_attribute?("a3")
314
+ assert_equal nil, tag.attribute("a1")
315
+ assert_equal 2, tag.attribute("a2")
316
+ assert_equal 3, tag.attribute("a3")
317
+ assert_equal({"a2" => 2, "a3" => 3}, tag.attributes)
318
+
319
+ tag.set_attributes("ns1", {"a2" => 12, "a3" => 13})
320
+ assert_equal 2, tag.attribute("a2")
321
+ assert_equal 3, tag.attribute("a3")
322
+ assert_equal 12, tag.attribute("ns1", "a2")
323
+ assert_equal 13, tag.attribute("ns1", "a3")
324
+
325
+ tag.set_attributes("", {}) # removes all attributes in the default namespace
326
+ assert_equal({"ns1:a2" => 12, "ns1:a3" => 13}, tag.attributes)
327
+ end
328
+
329
+ def test_to_child_hash
330
+ root = Tag.new("root") do
331
+ (new_child "child1") << "abc"
332
+ new_child "child2" do
333
+ self << 123
334
+ new_child "child2_1"
335
+ end
336
+ (new_child "child3") << nil << 456
337
+ end
338
+
339
+ assert_equal({"child1" => "abc", "child2" => 123, "child3" => nil}, root.to_child_hash)
340
+ end
341
+
342
+ def test_to_child_string_hash
343
+ root = Tag.new("root") do
344
+ (new_child "child1") << "abc"
345
+ new_child "child2" do
346
+ self << 123
347
+ new_child "child2_1"
348
+ end
349
+ (new_child "child3") << nil << 456
350
+ end
351
+
352
+ assert_equal(
353
+ {"child1" => "abc", "child2" => "123", "child3" => ""}, root.to_child_string_hash)
354
+ end
355
+
356
+ def test_eql?
357
+ tag1 = Tag.new "node1"
358
+
359
+ assert tag1.equal?(tag1)
360
+ assert tag1.eql?(tag1)
361
+ assert tag1 == tag1
362
+ assert_equal tag1.hash, tag1.hash
363
+
364
+ tag1_bis = Tag.new "node1"
365
+
366
+ assert tag1.eql?(tag1_bis)
367
+ assert tag1 == tag1_bis
368
+ assert !(tag1.equal?(tag1_bis))
369
+ assert_equal tag1.hash, tag1_bis.hash
370
+
371
+ tag2 = Tag.new "node2"
372
+ assert !(tag1.eql?(tag2))
373
+ assert !(tag1 == tag2)
374
+ assert !(tag1.equal?(tag2))
375
+ assert_not_equal tag1.hash, tag2.hash
376
+
377
+ tag1 = Tag.new "node1" do
378
+ self << 123 << "abc" << nil
379
+ self << {"length"=>45, "ns:length"=>100}
380
+ new_child "child1"
381
+ new_child "child2" do
382
+ self << [[1,2], [3,4]]
383
+ end
384
+ new_child "child3"
385
+ end
386
+
387
+ assert tag1.eql?(tag1)
388
+ assert tag1 == tag1
389
+ assert_equal tag1.hash, tag1.hash
390
+ assert tag1.equal?(tag1)
391
+
392
+ tag1_bis = Tag.new "node1" do
393
+ self << 123 << "abc" << nil
394
+ self << {"length"=>45, "ns:length"=>100}
395
+ new_child "child1"
396
+ new_child "child2" do
397
+ self << [[1,2], [3,4]]
398
+ end
399
+ new_child "child3"
400
+ end
401
+
402
+ assert tag1.eql?(tag1_bis)
403
+ assert tag1 == tag1_bis
404
+ assert !(tag1.equal?(tag1_bis))
405
+ assert_equal tag1.hash, tag1_bis.hash
406
+
407
+ tag2 = Tag.new "node1" do
408
+ self << 123 << "abc" << nil
409
+ self << {"length"=>45, "ns:length"=>101} # the difference is here
410
+ new_child "child1"
411
+ new_child "child2" do
412
+ self << [[1,2], [3,4]]
413
+ end
414
+ new_child "child3"
415
+ end
416
+
417
+ tag2 = Tag.new "node2"
418
+ assert !(tag1.eql?(tag2))
419
+ assert !(tag1 == tag2)
420
+ assert !(tag1.equal?(tag2))
421
+ assert_not_equal tag1.hash, tag2.hash
422
+ end
423
+
424
+ def test_children_values
425
+ root = Tag.new "root"
426
+
427
+ assert_equal [], root.children_values
428
+ assert_equal [], root.children_values("child1")
429
+
430
+ root = SDL4R::read(<<EOF
431
+ child1 123 length=45.6
432
+ child2 2010/01/25 "abc"
433
+ child3
434
+ child4 null
435
+ child5 null null
436
+ EOF
437
+ )
438
+
439
+ assert_equal [], root.children_values("child0")
440
+ assert_equal [123], root.children_values("child1")
441
+ assert_equal [[Date.civil(2010, 01, 25), "abc"]], root.children_values("child2")
442
+ assert_equal [nil], root.children_values("child3")
443
+ assert_equal [nil], root.children_values("child4")
444
+ assert_equal [[nil, nil]], root.children_values("child5")
445
+ assert_equal [
446
+ 123,
447
+ [Date.civil(2010, 01, 25), "abc"],
448
+ nil,
449
+ nil,
450
+ [nil, nil]],
451
+ root.children_values
452
+ end
453
+
454
+ def test_to_xml_string
455
+ tag = Tag.new "tag1"
456
+ xml_doc = REXML::Document.new(tag.to_xml_string)
457
+ assert_equal "tag1", xml_doc[0].name
458
+ assert_equal "", xml_doc[0].namespace
459
+
460
+ tag = Tag.new "ns1", "tag1"
461
+ xml_doc = REXML::Document.new(tag.to_xml_string("", {"ns1" => "ns1"}))
462
+ assert_equal "tag1", xml_doc[0].name
463
+ assert_equal "ns1", xml_doc[0].namespace
464
+
465
+ tag = Tag.new "tag1" do
466
+ self << 123
467
+ end
468
+ xml_doc = REXML::Document.new(tag.to_xml_string)
469
+ assert_equal "tag1", xml_doc[0].name
470
+ assert_equal "123", xml_doc[0].attribute("_val0").value
471
+
472
+ tag = Tag.new "tag1" do
473
+ new_child "tag2" do
474
+ self << {"a1" => 123}
475
+ end
476
+ new_child "tag3"
477
+ end
478
+ xml_doc = REXML::Document.new(tag.to_xml_string)
479
+ assert_equal "tag1", xml_doc.elements[1].name
480
+ assert_equal "tag2", xml_doc.elements[1].elements[1].name
481
+ assert_equal "tag3", xml_doc.elements[1].elements[2].name
482
+ assert_equal "123", xml_doc.elements[1].elements[1].attribute("a1").value
483
+ end
484
+
485
+ end
486
+
487
+ end