sdl4r 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,19 +30,6 @@ module SDL4R
30
30
 
31
31
  class ParserTest < Test::Unit::TestCase
32
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
33
  public
47
34
 
48
35
  def test_empty
@@ -217,11 +204,13 @@ EOF
217
204
  assert_equal(910.11, values[6])
218
205
  assert_equal(12.13, values[7])
219
206
  assert_equal(1415.16, values[8])
220
- assert_equal(171.8, values[9])
221
- assert_equal(1.920, values[10])
222
207
  if defined? Flt::DecNum
208
+ assert_equal(Flt::DecNum("171.8"), values[9])
209
+ assert_equal(Flt::DecNum("1.920"), values[10])
223
210
  assert_equal(Flt::DecNum("12345678901234567890"), values[11])
224
211
  else
212
+ assert_equal(171.8, values[9])
213
+ assert_equal(1.920, values[10])
225
214
  assert_equal(12345678901234567890, values[11])
226
215
  end
227
216
  end
@@ -240,6 +229,7 @@ EOF
240
229
  assert_equal(1, values.size)
241
230
  assert_equal(nil, values[0])
242
231
  assert_equal(nil, tag1.attribute("attr1"))
232
+ assert(tag1.has_attribute?("attr1"))
243
233
  end
244
234
 
245
235
  def test_comments
@@ -272,6 +262,90 @@ EOF
272
262
  assert_equal(890, root.child("tag10").value)
273
263
  end
274
264
 
265
+ def test_double_quote_strings
266
+ root = SDL4R::read(
267
+ <<EOF
268
+ tag1 "cheese and cherry jam"
269
+ tag2 "cheese and \\
270
+ cherry jam"
271
+ tag3 "cheese \\
272
+ and \\
273
+ cherry jam"
274
+ tag4 "Did you say this soup was \\"good\\"?"
275
+ tag5 "Even my dog wouldn't\\thave\\tit!"
276
+ tag6 "\\"\\t\\r\\n\\\\"
277
+ tag7 equation="is not x=y*z" color="blue \\
278
+ and yellow"
279
+ EOF
280
+ )
281
+
282
+ assert_equal "cheese and cherry jam", root.child("tag1").value, "double-quote string"
283
+ assert_equal(
284
+ "cheese and cherry jam", root.child("tag2").value, "continued double-quote string")
285
+ assert_equal(
286
+ "cheese and cherry jam", root.child("tag3").value, "continued double-quote string")
287
+ assert_equal(
288
+ 'Did you say this soup was "good"?', root.child("tag4").value, "escaped quotes")
289
+ assert_equal(
290
+ "Even my dog wouldn't\thave\tit!", root.child("tag5").value, "escaped tabs")
291
+ assert_equal "\"\t\r\n\\", root.child("tag6").value, "escaped white spaces"
292
+ assert_equal "is not x=y*z", root.child("tag7").attribute("equation")
293
+ assert_equal "blue and yellow", root.child("tag7").attribute("color")
294
+ end
295
+
296
+ def test_backquote_strings
297
+ root = SDL4R::read <<EOF
298
+ winfile `c:\\directory\\myfile.xls`
299
+ talk `I said "something"`
300
+ xml `
301
+ <product>
302
+ <shoes color="blue"/>
303
+ </product>
304
+ `
305
+ regex `\\w+\\.suite\\(\\)`
306
+ EOF
307
+
308
+ assert_equal "c:\\directory\\myfile.xls", root.child("winfile").value
309
+ assert_equal 'I said "something"', root.child("talk").value
310
+ assert_equal(
311
+ "\n<product>\n <shoes color=\"blue\"/>\n</product>\n", root.child("xml").value)
312
+ assert_equal "\\w+\\.suite\\(\\)", root.child("regex").value
313
+ end
314
+
315
+ def test_sub_tags
316
+ root = SDL4R::read <<EOF
317
+ wax {
318
+ }
319
+ steack {
320
+ bees {
321
+ monopoly {
322
+ }
323
+ }
324
+ goat_cheese
325
+ truck {
326
+ cathedral
327
+ }
328
+ }
329
+ peanut.butter
330
+ EOF
331
+
332
+ expected = Tag.new("root") do
333
+ new_child("wax")
334
+ new_child("steack") do
335
+ new_child("bees") do
336
+ new_child("monopoly")
337
+ end
338
+ new_child("goat_cheese")
339
+ new_child("truck") do
340
+ new_child("cathedral")
341
+ end
342
+ end
343
+ new_child("peanut.butter")
344
+ end
345
+
346
+ assert_equal expected, root
347
+ end
348
+
275
349
  private
276
350
 
277
351
  # Creates and returns a DateTime where an unspecified +zone_offset+ means 'the local zone
@@ -282,13 +356,10 @@ EOF
282
356
  end
283
357
 
284
358
  def parse_one_tag1(text)
285
- root = Tag.new("root")
286
- root.read(text)
359
+ root = SDL4R::read(text)
287
360
  tag1 = root.child("tag1")
288
361
  assert_not_nil(tag1, "tag1")
289
-
290
362
  assert_equal 1, root.child_count, "only 1 tag expected"
291
-
292
363
  return tag1
293
364
  end
294
365
  end
data/test/sdl4r/test.rb CHANGED
@@ -22,7 +22,14 @@ end
22
22
  module SDL4R
23
23
 
24
24
  require 'fileutils'
25
+ require 'pathname'
25
26
  require 'date'
27
+ begin
28
+ # Try to use the Flt library, which defines DecNum
29
+ require "flt"
30
+ rescue LoadError
31
+ # Well, shouganai.
32
+ end
26
33
 
27
34
  require 'test/unit'
28
35
  require File.dirname(__FILE__) + '/../../lib/sdl4r/tag'
@@ -57,18 +64,31 @@ module SDL4R
57
64
  NAMESPACES = "Namespaces"
58
65
 
59
66
 
60
- def initialize(test_method_name)
61
- super(test_method_name)
62
- @failures = 0
63
- @assert_count = 0
64
- end
65
-
66
67
  def assert_tags_equal(expected, actual, message)
67
68
  if expected != actual
68
69
  assert_equal(expected.to_s, actual.to_s, message)
69
70
  end
70
71
  end
71
72
 
73
+ # Returns a Pathname giving the location of the specified relative +filename+.
74
+ #
75
+ # +filename+:: path to a file relatively to this source file
76
+ #
77
+ def get_test_sdl_file_path(filename)
78
+ dir = File.dirname(__FILE__)
79
+ return Pathname.new(dir + '/' + filename)
80
+ end
81
+
82
+ @@root_basic_types = nil
83
+ @@root_structures = nil
84
+
85
+ def setup
86
+ super
87
+
88
+ @@root_basic_types ||= SDL4R::read(get_test_sdl_file_path("test_basic_types.sdl"))
89
+ @@root_structures ||= SDL4R::read(get_test_sdl_file_path("test_structures.sdl"))
90
+ end
91
+
72
92
  ######################################
73
93
  # Tag Tests
74
94
  ######################################
@@ -105,14 +125,16 @@ module SDL4R
105
125
  "attributes()")
106
126
  end
107
127
 
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
128
+ def test_tag_write_parse_basic_types
129
+ if not defined? Flt::DecNum
130
+ fail("this test fails without the Flt library")
131
+ end
132
+
133
+ test_tag_write_parse @@root_basic_types
134
+ end
135
+
136
+ def test_tag_write_parse_structures
137
+ test_tag_write_parse @@root_structures
116
138
  end
117
139
 
118
140
  #
@@ -122,24 +144,22 @@ module SDL4R
122
144
  # puts '========================================'
123
145
  # puts root.to_s
124
146
  # 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
147
 
129
- assert_tags_equal(
130
- root,
131
- Tag.new("test").read(root.to_s).child("root"),
132
- "write/parse")
148
+ write_parse_root = Tag.new("test").read(root.to_s).child("root");
149
+
150
+ # File.open("d:\\dev\\tmp\\root.sdl", "w") { |io| io.write(root.to_string) }
151
+ # File.open("d:\\dev\\tmp\\root_reparsed.sdl", "w") { |io| io.write(write_parse_root.to_string) }
152
+
153
+ assert_tags_equal(root, write_parse_root, "write/parse")
133
154
  end
134
155
 
135
156
  ######################################
136
157
  # Basic Types Tests
137
158
  ######################################
138
159
 
139
- #
140
- # +root+: a Tag
141
- #
142
- def test_strings(root)
160
+ def test_strings
161
+ root = @@root_basic_types
162
+
143
163
  # Doing String tests...
144
164
  # Doing basic tests including new line handling...
145
165
  assert_equal(root.child("string1").value, "hello", STRING_DECLARATIONS)
@@ -157,17 +177,18 @@ module SDL4R
157
177
  assert_equal(root.child("string10").value, "escapes \"\\\n\t", STRING_DECLARATIONS)
158
178
 
159
179
  # 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)
180
+ assert_equal(root.child("japanese.hello").value, "日本語", STRING_DECLARATIONS)
181
+ assert_equal(root.child("korean.hello").value, "여보세요", STRING_DECLARATIONS)
182
+ assert_equal(root.child("russian.hello").value, "здравствулте", STRING_DECLARATIONS)
163
183
 
164
184
  # More new line tests...
165
185
  assert(root.child("xml").value.index("<text>Hi there!</text>") >= 0, STRING_DECLARATIONS)
166
186
  assert_equal(root.child("line_test").value, "\nnew line above and below\n", STRING_DECLARATIONS)
167
187
  end
168
188
 
169
- def test_characters(root)
170
- # Doing character tests...
189
+ def test_characters
190
+ root = @@root_basic_types
191
+
171
192
  assert_equal(root.child("char1").value, 'a', CHARACTER_DECLARATIONS)
172
193
  assert_equal(root.child("char2").value, 'A', CHARACTER_DECLARATIONS)
173
194
  assert_equal(root.child("char3").value, '\\', CHARACTER_DECLARATIONS)
@@ -175,16 +196,19 @@ module SDL4R
175
196
  assert_equal(root.child("char5").value, "\t", CHARACTER_DECLARATIONS)
176
197
  assert_equal(root.child("char6").value, '\'', CHARACTER_DECLARATIONS)
177
198
  assert_equal(root.child("char7").value, '"', CHARACTER_DECLARATIONS)
199
+ end
178
200
 
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
201
+ def test_characters_unicode
202
+ root = @@root_basic_types
203
+
204
+ assert_equal(root.child("char8").value, "", CHARACTER_DECLARATIONS) # \u65e5
205
+ assert_equal(root.child("char9").value, "", CHARACTER_DECLARATIONS) # \uc5ec
206
+ assert_equal(root.child("char10").value, "з", CHARACTER_DECLARATIONS) # \u0437
185
207
  end
186
208
 
187
- def test_numbers(root)
209
+ def test_numbers
210
+ root = @@root_basic_types
211
+
188
212
  # Testing ints...
189
213
  assert_equal(root.child("int1").value, 0, NUMBER_DECLARATIONS)
190
214
  assert_equal(root.child("int2").value, 5, NUMBER_DECLARATIONS)
@@ -216,25 +240,33 @@ module SDL4R
216
240
  root.child("decimal3").value, 234535.3453453453454345345341242343, NUMBER_DECLARATIONS);
217
241
  end
218
242
 
219
- def test_booleans(root)
243
+ def test_booleans
244
+ root = @@root_basic_types
245
+
220
246
  assert_equal(root.child("light-on").value, true, BOOLEAN_DECLARATIONS)
221
247
  assert_equal(root.child("light-off").value, false, BOOLEAN_DECLARATIONS)
222
248
  assert_equal(root.child("light1").value, true, BOOLEAN_DECLARATIONS)
223
249
  assert_equal(root.child("light2").value, false, BOOLEAN_DECLARATIONS)
224
250
  end
225
251
 
226
- def test_null(root)
252
+ def test_null
253
+ root = @@root_basic_types
254
+
227
255
  assert_equal(root.child("nothing").value, nil, NULL_DECLARATION);
228
256
  end
229
257
 
230
- def test_dates(root)
258
+ def test_dates
259
+ root = @@root_basic_types
260
+
231
261
  assert_equal(root.child("date1").value, Date.civil(2005, 12, 31), DATE_DECLARATIONS)
232
262
  assert_equal(root.child("date2").value, Date.civil(1882, 5, 2), DATE_DECLARATIONS)
233
263
  assert_equal(root.child("date3").value, Date.civil(1882, 5, 2), DATE_DECLARATIONS)
234
264
  assert_equal(root.child("_way_back").value, Date.civil(582, 9, 16), DATE_DECLARATIONS)
235
265
  end
236
266
 
237
- def test_time_spans(root)
267
+ def test_time_spans
268
+ root = @@root_basic_types
269
+
238
270
  assert_equal(
239
271
  root.child("time1").value, SdlTimeSpan.new(0,12,30,0,0), TIME_SPAN_DECLARATIONS)
240
272
  assert_equal(
@@ -269,7 +301,8 @@ module SDL4R
269
301
  root.child("time14").value, SdlTimeSpan.new(-5,-12,-30,-23,-123), TIME_SPAN_DECLARATIONS)
270
302
  end
271
303
 
272
- def test_date_times(root)
304
+ def test_date_times
305
+ root = @@root_basic_types
273
306
  local_offset = DateTime.now.offset
274
307
 
275
308
  assert_equal(root.child("date_time1").value,
@@ -292,7 +325,9 @@ module SDL4R
292
325
  DateTime.civil(985,04,11,12,30,23123.to_r/1000,"PST"), DATE_TIME_DECLARATIONS)
293
326
  end
294
327
 
295
- def test_binaries(root)
328
+ def test_binaries
329
+ root = @@root_basic_types
330
+
296
331
  assert_equal(SDL4R::SdlBinary("hi"), root.child("hi").value, BINARY_DECLARATIONS)
297
332
  assert_equal(
298
333
  SDL4R::SdlBinary("hi"), root.child("hi").value, BINARY_DECLARATIONS)
@@ -314,11 +349,13 @@ module SDL4R
314
349
  # Structure Tests (values, attributes, children)
315
350
  ######################################
316
351
 
317
- def test_empty_tag(root)
352
+ def test_empty_tag
353
+ root = @@root_structures
318
354
  assert_equal(root.child("empty_tag"), Tag.new("empty_tag"), EMPTY_TAG)
319
355
  end
320
356
 
321
- def test_values(root)
357
+ def test_values
358
+ root = @@root_structures
322
359
  local_offset = DateTime.now.offset
323
360
 
324
361
  assert_equal(root.child("values1").values, ["hi"], VALUES)
@@ -364,15 +401,16 @@ module SDL4R
364
401
  assert_equal(root.child("values22").values, ["hi","ho","ho",5,"hi"], VALUES)
365
402
  end
366
403
 
367
- def test_attributes(root)
404
+ def test_attributes
405
+ root = @@root_structures
406
+
368
407
  assert_equal(
369
408
  root.child("atts1").attributes, {"name" => "joe"}, ATTRIBUTES);
370
409
  assert_equal(root.child("atts2").attributes, {"size" => 5}, ATTRIBUTES);
371
410
  assert_equal(
372
411
  root.child("atts3").attributes, {"name" => "joe","size" => 5}, ATTRIBUTES);
373
412
  assert_equal(
374
- root.child("atts4").attributes,
375
- {"name"=>"joe","size"=>5,"smoker"=>false},
413
+ root.child("atts4").attributes, {"name"=>"joe","size"=>5,"smoker"=>false},
376
414
  ATTRIBUTES);
377
415
  assert_equal(
378
416
  root.child("atts5").attributes, {"name"=>"joe","smoker"=>false}, ATTRIBUTES);
@@ -388,7 +426,9 @@ module SDL4R
388
426
  root.child("atts9").attribute("key"), SDL4R::SdlBinary("mykey"), ATTRIBUTES)
389
427
  end
390
428
 
391
- def test_values_and_attributes(root)
429
+ def test_values_and_attributes
430
+ root = @@root_structures
431
+
392
432
  assert_equal(root.child("valatts1").values, ["joe"], VALUES_AND_ATTRIBUTES)
393
433
  assert_equal(
394
434
  root.child("valatts1").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
@@ -443,13 +483,13 @@ module SDL4R
443
483
  VALUES_AND_ATTRIBUTES)
444
484
  end
445
485
 
446
- def test_children(root)
486
+ def test_children
487
+ root = @@root_structures
447
488
  parent = root.child("parent")
448
489
 
449
490
  assert_equal(parent.children.size, 2, CHILDREN)
450
491
  assert_equal(parent.children[1].name, "daughter", CHILDREN)
451
492
 
452
-
453
493
  grandparent = root.child("grandparent")
454
494
 
455
495
  assert_equal(grandparent.children.size, 2, CHILDREN)
@@ -476,7 +516,8 @@ module SDL4R
476
516
  assert_equal([[1,2,3],[4,5,6]], matrix.children_values("content"), CHILDREN);
477
517
  end
478
518
 
479
- def test_namespaces(root)
519
+ def test_namespaces
520
+ root = @@root_structures
480
521
  assert_equal(8, root.children(true, nil, "person").size, NAMESPACES);
481
522
 
482
523
  grandparent2 = root.child("grandparent3");
@@ -488,54 +529,5 @@ module SDL4R
488
529
  NAMESPACES);
489
530
  end
490
531
 
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
532
  end
541
533
  end