sdl4r 0.9.9 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/CHANGELOG +70 -1
  2. data/LICENSE +499 -497
  3. data/Rakefile +38 -28
  4. data/TODO +194 -45
  5. data/doc/classes/SDL4R/AbbreviationTimezoneProxy.html +151 -0
  6. data/doc/classes/SDL4R/ConstantTimezone.html +129 -0
  7. data/doc/classes/SDL4R/Element.html +148 -0
  8. data/doc/classes/SDL4R/Reader.html +683 -0
  9. data/doc/classes/SDL4R/RelativeTimezone.html +187 -0
  10. data/doc/classes/SDL4R/SdlBinary.html +188 -0
  11. data/doc/classes/SDL4R/SdlParseError.html +110 -0
  12. data/doc/classes/SDL4R/SdlTimeSpan.html +651 -0
  13. data/doc/classes/SDL4R/Serializer.html +557 -0
  14. data/doc/classes/SDL4R/Serializer/Ref.html +138 -0
  15. data/doc/classes/SDL4R/TZAbbreviationDB/Record.html +117 -0
  16. data/doc/classes/SDL4R/Tag.html +1274 -0
  17. data/doc/classes/SDL4R/Token.html +131 -0
  18. data/doc/created.rid +1 -0
  19. data/doc/files/CHANGELOG.html +290 -0
  20. data/doc/files/LICENSE.html +53 -0
  21. data/doc/files/README.html +405 -0
  22. data/doc/files/lib/sdl4r/abbreviation_timezone_proxy_rb.html +63 -0
  23. data/doc/files/lib/sdl4r/constant_timezone_rb.html +54 -0
  24. data/doc/files/lib/sdl4r/element_rb.html +54 -0
  25. data/doc/files/lib/sdl4r/reader_rb.html +68 -0
  26. data/doc/files/lib/sdl4r/relative_timezone_rb.html +62 -0
  27. data/doc/files/lib/sdl4r/sdl4r_rb.html +66 -0
  28. data/doc/files/lib/sdl4r/sdl4r_tzinfo_rb.html +64 -0
  29. data/doc/files/lib/sdl4r/sdl4r_version_rb.html +54 -0
  30. data/doc/files/lib/sdl4r/sdl_binary_rb.html +54 -0
  31. data/doc/files/lib/sdl4r/sdl_parse_error_rb.html +54 -0
  32. data/doc/files/lib/sdl4r/sdl_time_span_rb.html +54 -0
  33. data/doc/files/lib/sdl4r/serializer_rb.html +62 -0
  34. data/doc/files/lib/sdl4r/tag_rb.html +66 -0
  35. data/doc/files/lib/sdl4r/token_rb.html +54 -0
  36. data/doc/files/lib/sdl4r/tokenizer_rb.html +64 -0
  37. data/doc/files/lib/sdl4r/tz_abbreviation_db_rb.html +67 -0
  38. data/doc/files/lib/sdl4r_rb.html +54 -0
  39. data/doc/files/lib/sdl4r_tzinfo_rb.html +54 -0
  40. data/doc/fr_class_index.html +23 -0
  41. data/doc/fr_file_index.html +40 -0
  42. data/doc/fr_method_index.html +4711 -0
  43. data/doc/index.html +15 -0
  44. data/doc/rdoc-style.css +328 -0
  45. data/lib/sdl4r.rb +3 -1
  46. data/lib/sdl4r/abbreviation_timezone_proxy.rb +38 -0
  47. data/lib/sdl4r/constant_timezone.rb +58 -0
  48. data/{test/sdl4r/sdl_test.rb → lib/sdl4r/element.rb} +19 -14
  49. data/lib/sdl4r/reader.rb +772 -0
  50. data/lib/sdl4r/relative_timezone.rb +156 -0
  51. data/lib/sdl4r/sdl4r.rb +187 -45
  52. data/lib/sdl4r/sdl4r_tzinfo.rb +75 -0
  53. data/lib/sdl4r/sdl4r_version.rb +24 -0
  54. data/lib/sdl4r/sdl_parse_error.rb +1 -1
  55. data/lib/sdl4r/sdl_time_span.rb +5 -1
  56. data/lib/sdl4r/serializer.rb +473 -60
  57. data/lib/sdl4r/tag.rb +126 -51
  58. data/lib/sdl4r/token.rb +49 -0
  59. data/lib/sdl4r/tokenizer.rb +431 -0
  60. data/lib/sdl4r/tz_abbreviation_db.rb +266 -0
  61. data/read_jprof.html +16934 -0
  62. data/read_jprof_pull.html +14451 -0
  63. data/read_prof.html +4983 -0
  64. data/read_prof_pull.html +4896 -0
  65. data/test/sdl4r/parser_test.rb +577 -503
  66. data/test/sdl4r/read_jprof.rb +58 -0
  67. data/test/sdl4r/read_prof.rb +70 -0
  68. data/test/sdl4r/reader_test.rb +173 -0
  69. data/test/sdl4r/relative_timezone_test.rb +102 -0
  70. data/test/sdl4r/sdl4r_test.rb +611 -528
  71. data/test/sdl4r/sdl4r_tzinfo_test.rb +108 -0
  72. data/test/sdl4r/sdl_test_case.rb +60 -0
  73. data/test/sdl4r/serializer_test.rb +448 -11
  74. data/test/sdl4r/tag_test.rb +84 -5
  75. data/test/sdl4r/tokenizer_test.rb +128 -0
  76. metadata +69 -11
  77. data/lib/sdl4r/parser.rb +0 -648
  78. data/lib/sdl4r/parser/reader.rb +0 -184
  79. data/lib/sdl4r/parser/time_span_with_zone.rb +0 -57
  80. data/lib/sdl4r/parser/token.rb +0 -138
  81. data/lib/sdl4r/parser/tokenizer.rb +0 -507
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby -w
2
+ # encoding: UTF-8
3
+
4
+ #--
5
+ #
6
+ # Simple Declarative Language (SDL) for Ruby
7
+ # Copyright 2005 Ikayzo, inc.
8
+ #
9
+ # This program is free software. You can distribute or modify it under the
10
+ # terms of the GNU Lesser General Public License version 2.1 as published by
11
+ # the Free Software Foundation.
12
+ #
13
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
14
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15
+ # See the GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
19
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
+ #++
21
+
22
+ # Work-around a bug in NetBeans (http://netbeans.org/bugzilla/show_bug.cgi?id=188653)
23
+ if ENV["NB_EXEC_EXTEXECUTION_PROCESS_UUID"]
24
+ $:[0] = File.join(File.dirname(__FILE__),'../../lib')
25
+ $:.unshift(File.join(File.dirname(__FILE__),'../../test'))
26
+ end
27
+
28
+ if RUBY_VERSION < '1.9.0'
29
+ $KCODE = 'u'
30
+ require 'jcode'
31
+ end
32
+
33
+ module SDL4R
34
+
35
+ require 'fileutils'
36
+ require 'pathname'
37
+ require 'date'
38
+ require 'test/unit'
39
+
40
+ # We don't do
41
+ #
42
+ # require 'sdl4r_tzinfo'
43
+ #
44
+ # as we only want to enable TZInfo on a per test basis.
45
+ require 'sdl4r'
46
+ require 'sdl4r/sdl4r_tzinfo'
47
+
48
+ require 'sdl4r/sdl_test_case'
49
+
50
+ class Sdl4rTZInfoTest < Test::Unit::TestCase
51
+ include SdlTestCase
52
+
53
+ def setup
54
+ super
55
+ SDL4R::enable_tzinfo
56
+ end
57
+
58
+ def teardown
59
+ begin
60
+ SDL4R::disable_tzinfo
61
+ rescue
62
+ super
63
+ end
64
+ end
65
+
66
+ def test_datetime
67
+ # no timezone
68
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768")
69
+ assert_kind_of DateTime, root.child.value
70
+ assert_equal_date_time local_civil_date(1998, 05, 12, 12, 34, 56.768), root.child.value
71
+
72
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-UTC")
73
+ assert_equal_date_time local_civil_date(1998, 05, 12, 12, 34, 56.768, "UTC"), root.child.value
74
+
75
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-JST")
76
+ assert_equal_date_time local_civil_date(1998, 05, 12, 12, 34, 56.768, "JST"), root.child.value
77
+
78
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-GMT-01:00")
79
+ assert_equal_date_time local_civil_date(1998, 05, 12, 12, 34, 56.768, "GMT-01:00"), root.child.value
80
+ end
81
+
82
+ # Date parsing tests using Time rather than DateTime
83
+ def test_time
84
+ SDL4R::use_datetime = false
85
+ begin
86
+ # no timezone
87
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768")
88
+ assert_kind_of Time, root.child.value
89
+ assert_equal_date_time Time.local(1998, 05, 12, 12, 34, 56, 768000), root.child.value
90
+
91
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-UTC")
92
+ assert_equal_date_time Time.utc(1998, 05, 12, 12, 34, 56, 768000), root.child.value
93
+
94
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-JST")
95
+ assert_equal_date_time Time.xmlschema("1998-05-12T12:34:56.768+09:00"), root.child.value
96
+
97
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-GMT-01:00")
98
+ assert_equal_date_time Time.xmlschema("1998-05-12T12:34:56.768-01:00"), root.child.value
99
+
100
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-JST-01:30")
101
+ assert_equal_date_time Time.xmlschema("1998-05-12T12:34:56.768+07:30"), root.child.value
102
+
103
+ ensure
104
+ SDL4R::use_datetime = true
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby -w
2
+ # encoding: UTF-8
3
+
4
+ #--
5
+ # Simple Declarative Language (SDL) for Ruby
6
+ # Copyright 2005 Ikayzo, inc.
7
+ #
8
+ # This program is free software. You can distribute or modify it under the
9
+ # terms of the GNU Lesser General Public License version 2.1 as published by
10
+ # the Free Software Foundation.
11
+ #
12
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
13
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
14
+ # See the GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
18
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
+ #++
20
+
21
+ module SDL4R
22
+
23
+ require 'test/unit'
24
+
25
+ module SdlTestCase
26
+
27
+ def assert_equal_date_time(expected, actual, message = nil)
28
+ begin
29
+ assert_equal expected, actual
30
+ rescue Test::Unit::AssertionFailedError
31
+ first_error = $!
32
+ begin
33
+ if expected.class == actual.class
34
+ if expected.is_a? DateTime
35
+ assert_equal expected.strftime("%F %T.%L %z"), actual.strftime("%F %T.%L %z"), message
36
+ elsif expected.is_a? Time
37
+ assert_equal expected.xmlschema(3), actual.xmlschema(3), message
38
+ else
39
+ assert_equal expected, actual, message
40
+ end
41
+ else
42
+ assert_equal expected, actual, message
43
+ end
44
+ raise first_error
45
+ rescue
46
+ raise $!
47
+ end
48
+ end
49
+ end
50
+
51
+ # Creates and returns a DateTime where an unspecified +zone_offset+ means 'the local zone
52
+ # offset' (contrarily to DateTime#civil())
53
+ def local_civil_date(year, month, day, hour = 0, min = 0, sec = 0, zone_offset = nil)
54
+ sec = Rational((sec * 1000).to_i, 1000) if sec.is_a? Float
55
+ @@current_zone_offset ||= Rational(Time.now.utc_offset, 24 * 60 * 60)
56
+ zone_offset ||= @@current_zone_offset
57
+ return DateTime.civil(year, month, day, hour, min, sec, zone_offset)
58
+ end
59
+ end
60
+ end
@@ -19,16 +19,23 @@
19
19
  #++
20
20
 
21
21
  # Work-around a bug in NetBeans (http://netbeans.org/bugzilla/show_bug.cgi?id=188653)
22
- $:[0] = File.join(File.dirname(__FILE__),'../../lib') if ENV["NB_EXEC_EXTEXECUTION_PROCESS_UUID"]
22
+ if ENV["NB_EXEC_EXTEXECUTION_PROCESS_UUID"]
23
+ $:[0] = File.join(File.dirname(__FILE__),'../../lib')
24
+ $:.unshift(File.join(File.dirname(__FILE__),'../../test'))
25
+ end
23
26
 
24
27
  module SDL4R
25
28
 
26
29
  require 'ostruct'
27
30
  require 'date'
31
+ require 'test/unit'
28
32
 
29
- require 'sdl4r/serializer'
33
+ require 'sdl4r'
34
+
35
+ require "sdl4r/sdl_test_case"
30
36
 
31
37
  class SerializerTest < Test::Unit::TestCase
38
+ include SdlTestCase
32
39
 
33
40
  def test_basic_open_struct
34
41
  serializer = Serializer.new
@@ -46,11 +53,11 @@ module SDL4R
46
53
  tag = serializer.serialize(car)
47
54
 
48
55
  assert_equal SDL4R::ROOT_TAG_NAME, tag.name
49
- assert_equal(
50
- { "brand" => car.brand, "wheels" => car.wheels, "max_speed" => car.max_speed },
51
- tag.attributes)
52
- assert_equal 1, tag.children.length
53
- assert_equal 'radio', tag.children[0].name
56
+ assert_equal({}, tag.attributes, "the root tag cannot have attributes")
57
+ assert_equal 4, tag.children.length
58
+ assert_equal car.brand, tag.child('brand').value
59
+ assert_equal car.wheels, tag.child('wheels').value
60
+ assert_equal car.max_speed, tag.child('max_speed').value
54
61
  assert_equal(
55
62
  { 'brand' => car.radio.brand, 'signal_noise_ratio' => car.radio.signal_noise_ratio,
56
63
  'construction_date' => car.radio.construction_date },
@@ -67,12 +74,442 @@ module SDL4R
67
74
  assert_equal car.radio.construction_date, car2.radio.construction_date
68
75
  end
69
76
 
70
- def test_struct
71
- dir = File.dirname(__FILE__)
72
- o = Serializer.new.deserialize(SDL4R::read(Pathname.new(dir + '/' + "test_structures.sdl")))
73
- p o
77
+ # Class used for serialization tests
78
+ # a is a private instance variable
79
+ # b has a read accessor
80
+ # c has read/write accessors
81
+ # d has read/write accessors and the read accessor can take an optional parameter
82
+ # e has read/write accessors and the read accessor takes a parameter (not a real accessor, then)
83
+ class SerializeA
84
+
85
+ def initialize(a, b, c, d, e)
86
+ @a = a
87
+ @b = b
88
+ @c = c
89
+ @d = d
90
+ @e = e
91
+ end
92
+
93
+ attr_reader :b
94
+
95
+ def c
96
+ @c + 1
97
+ end
98
+
99
+ attr_writer :c
100
+
101
+ def d(x = 0)
102
+ @d + 1
103
+ end
104
+
105
+ attr_writer :d
106
+
107
+ def e(x)
108
+ @e + 1
109
+ end
110
+
111
+ attr_writer :e
112
+
113
+ end
114
+
115
+ def test_serialize_object_and_property_access
116
+ o = SerializeA.new(10, 20, 30, 40, 50)
117
+
118
+ serializer = Serializer.new
119
+ tag = serializer.serialize(o, Tag.new("SerializeA"))
120
+
121
+ assert_equal 10, tag.attribute('a')
122
+ assert_equal 20, tag.attribute('b')
123
+ # the read accessor should be have been used (it adds 1 to the serialized value)
124
+ assert_equal 31, tag.attribute('c')
125
+ # the read accessor should be have been used (it adds 1 to the serialized value)
126
+ assert_equal 41, tag.attribute('d')
127
+ # the read accessor has a wrong arity and therefore, 'e' should be considered a private variable
128
+ assert_equal 50, tag.attribute('e')
129
+ end
130
+
131
+ def test_serialize_array_property
132
+ o = OpenStruct.new
133
+ o.array1 = [ 407, 5, 2.3 ]
134
+ o.array2 = []
135
+ o.array3 = [ OpenStruct.new(:values => [1, nil]), OpenStruct.new(:flavor => 'strawberry') ]
136
+
137
+ serializer = Serializer.new
138
+ top_tag = serializer.serialize({:o => o})
139
+ o_tag = top_tag.child('o')
140
+
141
+ assert_equal [ 407, 5, 2.3 ], o_tag.child('array1').values
142
+ assert_equal [], o_tag.child('array2').values
143
+
144
+ array3_tags = o_tag.children(false, 'array3')
145
+ assert_equal [1, nil], array3_tags[0].child('values').values
146
+ assert_equal 'strawberry', array3_tags[1].attribute('flavor')
74
147
  end
75
148
 
149
+ def test_serialize_hash
150
+ top = OpenStruct.new
151
+
152
+ serializer = Serializer.new
153
+ top.h = {}
154
+ tag = serializer.serialize(top)
155
+ assert_equal top.h, tag.child('h').attributes
156
+
157
+ serializer = Serializer.new
158
+ top.h = { "a" => 10, "b" => "xyz", "c" => nil }
159
+ tag = serializer.serialize(top)
160
+ assert_equal top.h, tag.child('h').attributes
161
+ end
162
+
163
+ class Top
164
+ attr_reader :books
165
+ def initialize
166
+ @books = []
167
+ end
168
+ end
169
+
170
+ class Book
171
+ attr_accessor :title, :introduction, :conclusion, :keywords, :chapters
172
+ end
173
+
174
+ class Chapter
175
+ attr_accessor :page, :title
176
+ end
177
+
178
+ def test_deserialize_object
179
+ tag = SDL4R::read(<<EOS
180
+ books {
181
+ book title="My Life, My Potatoes" {
182
+ keywords "life" "potatoes"
183
+ introduction page=12
184
+ conclusion page=255
185
+ }
186
+ book {
187
+ keywords "self-development"
188
+ keywords "learning"
189
+ keywords "paradoxal"
190
+ title "Learn to Read in a Week"
191
+ introduction page=3
192
+ chapter page=5 title="A, B, C"
193
+ chapter page=6 title="Read on my Lips"
194
+ }
195
+ }
196
+ EOS
197
+ )
198
+
199
+ serializer = Serializer.new
200
+
201
+ def serializer.new_plain_object(tag, o = nil, parent_object = nil)
202
+ if parent_object.nil?
203
+ Top.new
204
+ elsif tag.name == "book"
205
+ Book.new
206
+ elsif parent_object.is_a? Book and ["introduction", "conclusion"].include?(tag.name)
207
+ Chapter.new
208
+ else
209
+ super(tag, o, parent_object)
210
+ end
211
+ end
212
+
213
+ def serializer.serialize_array_as_child_tags(name, array, parent_tag)
214
+ if parent_tag.name == SDL4R::ROOT_TAG_NAME and name == "books"
215
+ super("book", array, new_tag("books", array, parent_tag))
216
+ else
217
+ return super(name, array, parent_tag)
218
+ end
219
+ end
220
+
221
+ def serializer.get_deserialized_property_name(tag, parent_tag, parent_object)
222
+ if parent_tag.name == "book" and tag.name == "chapter"
223
+ return "chapters"
224
+ else
225
+ super(tag, parent_tag, parent_object)
226
+ end
227
+ end
228
+
229
+ top = serializer.deserialize(tag)
230
+ check_book_hierarchy(top)
231
+
232
+ top2 = serializer.deserialize(serializer.serialize(top))
233
+ check_book_hierarchy(top2)
234
+ end
235
+
236
+ def check_book_hierarchy(top)
237
+ assert_instance_of Top, top
238
+ assert_instance_of Array, top.books
239
+ assert_equal 2, top.books.length
240
+
241
+ assert_instance_of Book, top.books[0]
242
+ assert_equal "My Life, My Potatoes", top.books[0].title
243
+ assert_equal ["life", "potatoes"], top.books[0].keywords
244
+ assert_instance_of Chapter, top.books[0].introduction
245
+ assert_equal 12, top.books[0].introduction.page
246
+ assert_instance_of Chapter, top.books[0].conclusion
247
+ assert_equal 255, top.books[0].conclusion.page
248
+
249
+ assert_instance_of Book, top.books[1]
250
+ assert_equal "Learn to Read in a Week", top.books[1].title
251
+ assert_equal ["self-development", "learning", "paradoxal"], top.books[1].keywords
252
+ assert_instance_of Chapter, top.books[1].introduction
253
+ assert_equal 3, top.books[1].introduction.page
254
+ assert_equal 2, top.books[1].chapters.length
255
+ assert_equal 5, top.books[1].chapters[0].page
256
+ assert_equal "A, B, C", top.books[1].chapters[0].title
257
+ assert_equal 6, top.books[1].chapters[1].page
258
+ assert_equal "Read on my Lips", top.books[1].chapters[1].title
259
+ assert_nil top.books[1].conclusion
260
+ end
261
+
262
+ def test_option_omit_nil_properties
263
+ o = OpenStruct.new(:a => 1, :b => nil)
264
+
265
+ serializer = Serializer.new(:omit_nil_properties => false)
266
+ tag = serializer.serialize(o, Tag.new("o"))
267
+ assert_equal({ "a" => 1, "b" => nil }, tag.attributes)
268
+
269
+ serializer = Serializer.new(:omit_nil_properties => true)
270
+ tag = serializer.serialize(o, Tag.new("o"))
271
+ assert_equal({ "a" => 1 }, tag.attributes)
272
+ end
273
+
274
+ def test_deserialize_anonymous
275
+ tag = SDL4R::read(<<EOS
276
+ colors {
277
+ "red"
278
+ "blue"
279
+ "yellow"
280
+ alpha 0.5
281
+ }
282
+ files {
283
+ "readme.txt"
284
+ "LICENSE.rtf"
285
+ }
286
+ matrix {
287
+ 1 2 3
288
+ 4 5 6
289
+ }
290
+ alone {
291
+ "in the dark"
292
+ }
293
+ EOS
294
+ )
295
+ serializer = Serializer.new
296
+ top = serializer.deserialize(tag)
297
+ assert_equal ["red", "blue", "yellow"], top.colors.content
298
+ assert_equal 0.5, top.colors.alpha
299
+ assert_equal ["readme.txt", "LICENSE.rtf"], top.files
300
+ assert_equal [[1, 2, 3], [4, 5, 6]], top.matrix
301
+ assert_equal "in the dark", top.alone
302
+ end
303
+
304
+ def test_deserialize_root_as_hash
305
+ tag = SDL4R::read(<<EOS
306
+ fingers 7
307
+ matrix {
308
+ 1 2 3
309
+ 4 5 6
310
+ }
311
+ EOS
312
+ )
313
+ serializer = Serializer.new
314
+ top = serializer.deserialize(tag, Hash.new)
315
+ assert_kind_of Hash, top
316
+ assert_equal 7, top["fingers"]
317
+ assert_equal [[1, 2, 3], [4, 5, 6]], top["matrix"]
318
+ end
319
+
320
+ def test_deserialized_properties_priorities
321
+ top = SDL4R::load(<<EOS
322
+ fruit name="BANANA" {
323
+ name "banana"
324
+ }
325
+ o1 "123" value="xyz"
326
+ o2 "123" value="xyz" {
327
+ value 456
328
+ }
329
+ o3 content="xyz" {
330
+ "123"
331
+ }
332
+ o4 content="xyz" {
333
+ "123"
334
+ content 456
335
+ }
336
+ o5 {
337
+ "123"
338
+ }
339
+ EOS
340
+ )
341
+
342
+ assert_equal "banana", top.fruit.name
343
+ assert_equal "xyz", top.o1.value
344
+ assert_equal nil, top.o1.values # otherwise the previous assertion is not interesting
345
+ assert_equal 456, top.o2.value
346
+ assert_equal nil, top.o2.values # otherwise the previous assertion is not interesting
347
+ assert_equal "xyz", top.o3.content
348
+ # Any child tag named "content" is considered anonymous EVEN IF the name was explicit in the
349
+ # SDL.
350
+ assert_equal "xyz", top.o4.content
351
+ assert_equal "123", top.o5 # otherwise the two previous assertions are not interesting
352
+ end
353
+
354
+ # This test will loop forever or make a stack error if the cycles are not handled property.
355
+ def test_serialize_object_references
356
+ top = OpenStruct.new
357
+ o1 = OpenStruct.new(:name => '1')
358
+ o2 = OpenStruct.new(:name => '2')
359
+ o3 = OpenStruct.new(:name => '3')
360
+ o4 = OpenStruct.new(:name => '4')
361
+
362
+ top.worker = [o1, o2, o4]
363
+
364
+ # cycles
365
+ o1.slave = o2
366
+ o1.myself = o1
367
+ o2.master = o1
368
+ o2.slave = o3
369
+ o3.slave = o1
370
+
371
+ serializer = Serializer.new
372
+ root = serializer.serialize(top)
373
+
374
+ assert_equal 3, root.child_count
375
+
376
+ o1_tag = root.children[0]
377
+ o2_tag = root.children[1]
378
+ o4_tag = root.children[2]
379
+
380
+ assert_equal '1', o1_tag.attribute('name')
381
+ assert_not_nil o1_tag.attribute('oid')
382
+ assert_equal o1_tag.attribute('oid'), o1_tag.child('myself').attribute('oref')
383
+
384
+ assert_equal o2_tag.attribute('oref'), o1_tag.child('slave').attribute('oid')
385
+ assert_equal '2', o1_tag.child('slave').attribute('name')
386
+ assert_equal o1_tag.attribute('oid'), o1_tag.child('slave').child('master').attribute('oref')
387
+
388
+ o3_tag = o1_tag.child('slave').child('slave')
389
+ assert_not_nil o3_tag
390
+ assert_equal '3', o3_tag.attribute('name')
391
+ assert_equal o1_tag.attribute('oid'), o3_tag.child('slave').attribute('oref')
392
+
393
+ assert_nil o2_tag.attribute('name')
394
+ assert_not_nil o2_tag.attribute('oref')
395
+
396
+ assert_nil o4_tag.attribute('oid')
397
+ assert_equal '4', o4_tag.attribute('name')
398
+ end
399
+
400
+ def test_deserialize_object2
401
+ tag = SDL4R::read(<<EOS
402
+ robot 'A' oid=14
403
+ robot 'B' oid=57 {
404
+ me oref=57
405
+ wife oref=14
406
+ infant 'C' oid=110 {
407
+ parent oref=14
408
+ parent oref=57
409
+ }
410
+ }
411
+ robot oref=110
412
+ EOS
413
+ )
414
+
415
+ serializer = Serializer.new
416
+ top = serializer.deserialize(tag)
417
+
418
+ assert_equal 3, top.robot.length
419
+ assert_same top.robot[1], top.robot[1].me
420
+ assert_same top.robot[0], top.robot[1].wife
421
+ assert_same top.robot[2], top.robot[1].infant
422
+ assert_same top.robot[0], top.robot[1].infant.parent[0]
423
+ assert_same top.robot[1], top.robot[1].infant.parent[1]
424
+ end
425
+
426
+ class FromSdlA
427
+ attr_accessor :firstname
428
+ def from_sdl(tag, serializer = Serializer.new)
429
+ self.firstname = tag.attribute("name")
430
+ self
431
+ end
432
+ end
433
+
434
+ def test_from_sdl
435
+ serializer = Serializer.new
436
+ def serializer.new_plain_object(tag, o = nil, parent_object = nil)
437
+ if tag.name == 'player'
438
+ FromSdlA.new
439
+ else
440
+ super(tag, o, parent_object)
441
+ end
442
+ end
443
+
444
+ tag = SDL4R::read(<<EOS
445
+ player name="Roberto"
446
+ EOS
447
+ )
448
+ top = serializer.deserialize(tag)
449
+ assert_kind_of FromSdlA, top.player
450
+ assert_equal "Roberto", top.player.firstname
451
+
452
+ tag = SDL4R::read(<<EOS
453
+ player name="Roberto"
454
+ player name="Georgio"
455
+ EOS
456
+ )
457
+ top = serializer.deserialize(tag)
458
+ assert_kind_of FromSdlA, top.player[0]
459
+ assert_equal "Roberto", top.player[0].firstname
460
+ assert_kind_of FromSdlA, top.player[1]
461
+ assert_equal "Georgio", top.player[1].firstname
462
+ end
463
+
464
+ class ToSdlA
465
+ def initialize(firstname, lastname)
466
+ @firstname = firstname
467
+ @lastname = lastname
468
+ end
469
+
470
+ def to_sdl(serializer = Serializer.new, tag = serializer.new_tag(nil, self, nil))
471
+ tag.set_attribute("fullname", @firstname + ' ' + @lastname)
472
+ tag
473
+ end
474
+ end
475
+
476
+ def test_to_sdl
477
+ s = SDL4R::dump(:knight => ToSdlA.new('Lancelot', 'du Lac'))
478
+ assert_equal("knight fullname=\"Lancelot du Lac\"\n", s)
479
+
480
+ s = SDL4R::dump(:knight => [ToSdlA.new('Lancelot', 'du Lac'), ToSdlA.new('Perceval', 'Halla')])
481
+ assert_equal(
482
+ "knight fullname=\"Lancelot du Lac\"\n" +
483
+ "knight fullname=\"Perceval Halla\"\n",
484
+ s)
485
+ end
486
+
487
+ def test_default_namespace
488
+ serializer = Serializer.new(:default_namespace => "ns")
489
+
490
+ origin = OpenStruct.new(:name => "Bourgogne")
491
+ wine1 = OpenStruct.new(:color => "red", :origin => origin)
492
+ wine2 = OpenStruct.new(:color => "white", :origin => origin)
493
+
494
+
495
+ assert_equal(
496
+ Tag.new(SDL4R::ROOT_TAG_NAME) do
497
+ new_child("ns", "wine") do
498
+ set_attribute("color", "red") # no namespace on attributes
499
+ new_child("ns", "origin") do
500
+ set_attribute("name", "Bourgogne")
501
+ set_attribute("oid", 2)
502
+ end
503
+ end
504
+ new_child("ns", "wine") do
505
+ set_attribute("color", "white") # no namespace on attributes
506
+ new_child("ns", "origin") do
507
+ set_attribute(Serializer::OBJECT_REF_ATTR_NAME, 2)
508
+ end
509
+ end
510
+ end,
511
+ serializer.serialize("wine" => [wine1, wine2]))
512
+ end
76
513
  end
77
514
 
78
515
  end