jvyaml 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,516 @@
1
+
2
+ require 'test/unit'
3
+ require 'jvyaml'
4
+
5
+ class JvYAMLUnitTests < Test::Unit::TestCase
6
+ def test_basic_strings
7
+ assert_equal("str", JvYAML.load("!str str"))
8
+ assert_equal("str", JvYAML.load("--- str"))
9
+ assert_equal("str", JvYAML.load("---\nstr"))
10
+ assert_equal("str", JvYAML.load("--- \nstr"))
11
+ assert_equal("str", JvYAML.load("--- \n str"))
12
+ assert_equal("str", JvYAML.load("str"))
13
+ assert_equal("str", JvYAML.load(" str"))
14
+ assert_equal("str", JvYAML.load("\nstr"))
15
+ assert_equal("str", JvYAML.load("\n str"))
16
+ assert_equal("str", JvYAML.load('"str"'))
17
+ assert_equal("str", JvYAML.load("'str'"))
18
+ assert_equal("str", JvYAML.load(" --- 'str'"))
19
+ assert_equal("1.0", JvYAML.load("!str 1.0"))
20
+ end
21
+
22
+
23
+ def test_other_basic_types
24
+ assert_equal(:str, JvYAML.load(":str"))
25
+
26
+ assert_equal(47, JvYAML.load("47"))
27
+ assert_equal(0, JvYAML.load("0"))
28
+ assert_equal(-1, JvYAML.load("-1"))
29
+
30
+ assert_equal({'a' => 'b', 'c' => 'd' }, JvYAML.load("a: b\nc: d"))
31
+ assert_equal({'a' => 'b', 'c' => 'd' }, JvYAML.load("c: d\na: b\n"))
32
+
33
+ assert_equal({'a' => 'b', 'c' => 'd' }, JvYAML.load("{a: b, c: d}"))
34
+ assert_equal({'a' => 'b', 'c' => 'd' }, JvYAML.load("{c: d,\na: b}"))
35
+
36
+ assert_equal(%w(a b c), JvYAML.load("--- \n- a\n- b\n- c\n"))
37
+ assert_equal(%w(a b c), JvYAML.load("--- [a, b, c]"))
38
+ assert_equal(%w(a b c), JvYAML.load("[a, b, c]"))
39
+
40
+ assert_equal("--- str\n", "str".to_jvyaml)
41
+ assert_equal("--- \na: b\n", {'a'=>'b'}.to_jvyaml)
42
+ assert_equal("--- \n- a\n- b\n- c\n", %w(a b c).to_jvyaml)
43
+
44
+ assert_equal("--- \"1.0\"\n", "1.0".to_jvyaml)
45
+ end
46
+
47
+ class TestBean
48
+ attr_accessor :value, :key
49
+ def initialize(v,k)
50
+ @value=v
51
+ @key=k
52
+ end
53
+
54
+ def ==(other)
55
+ self.class == other.class && self.value == other.value && self.key == other.key
56
+ end
57
+ end
58
+
59
+ TestStruct = Struct.new(:foo,:bar)
60
+ def test_custom_serialization
61
+ assert(["--- !ruby/object:JvYAMLUnitTests::TestBean \nvalue: 13\nkey: 42\n",
62
+ "--- !ruby/object:JvYAMLUnitTests::TestBean \nkey: 42\nvalue: 13\n"].include?(TestBean.new(13,42).to_jvyaml))
63
+ assert_equal(TestBean.new(13,42),JvYAML.load("--- !ruby/object:JvYAMLUnitTests::TestBean \nvalue: 13\nkey: 42\n"))
64
+
65
+ assert(["--- !ruby/struct:JvYAMLUnitTests::TestStruct \nfoo: 13\nbar: 42\n","--- !ruby/struct:JvYAMLUnitTests::TestStruct \nbar: 42\nfoo: 13\n"].include?(TestStruct.new(13,42).to_jvyaml))
66
+ assert_equal("--- !ruby/exception:StandardError \nmessage: foobar\n", StandardError.new("foobar").to_jvyaml)
67
+ end
68
+
69
+ def test_symbols
70
+ assert_equal("--- :foo\n", :foo.to_jvyaml)
71
+ end
72
+
73
+ def test_range
74
+ assert_equal(["--- !ruby/range ", "begin: 1", "end: 3", "excl: false"], (1..3).to_jvyaml.split("\n").sort)
75
+ assert_equal(["--- !ruby/range ", "begin: 1", "end: 3", "excl: true"], (1...3).to_jvyaml.split("\n").sort)
76
+ end
77
+
78
+ def test_regexps
79
+ assert_equal("--- !ruby/regexp /^abc/\n", /^abc/.to_jvyaml)
80
+ end
81
+
82
+ def test_times_and_dates
83
+ assert_equal("--- 1982-05-03 15:32:44 Z\n",Time.utc(1982,05,03,15,32,44).to_jvyaml)
84
+ assert_equal("--- 2005-05-03\n",Date.new(2005,5,3).to_jvyaml)
85
+ end
86
+
87
+ def test_weird_numbers
88
+ assert_equal("--- .NaN\n",(0.0/0.0).to_jvyaml)
89
+ assert_equal("--- .Inf\n",(1.0/0.0).to_jvyaml)
90
+ assert_equal("--- -.Inf\n",(-1.0/0.0).to_jvyaml)
91
+ assert_equal("--- 0.0\n", (0.0).to_jvyaml)
92
+ assert_equal("--- 0\n", 0.to_jvyaml)
93
+ end
94
+
95
+ def test_boolean
96
+ assert_equal("--- true\n", true.to_jvyaml)
97
+ assert_equal("--- false\n", false.to_jvyaml)
98
+ end
99
+
100
+ def test_nil
101
+ assert_equal("--- \n", nil.to_jvyaml)
102
+ end
103
+
104
+ def test_JRUBY_718
105
+ assert_equal("--- \"\"\n", ''.to_jvyaml)
106
+ assert_equal('', JvYAML.load("---\n!str"))
107
+ end
108
+
109
+ def test_JRUBY_719
110
+ assert_equal('---', JvYAML.load("--- ---\n"))
111
+ assert_equal('---', JvYAML.load("---"))
112
+ end
113
+
114
+ def test_shared_strings
115
+ astr = "abcde"
116
+ shared = astr[2..-1]
117
+ assert_equal('cde', JvYAML.load(shared))
118
+ assert_equal("--- cde\n", shared.to_jvyaml)
119
+ end
120
+
121
+ def test_JRUBY_1026
122
+ a = "one0.1"
123
+ b = a[3..-1]
124
+ assert_equal("--- \"0.1\"\n", JvYAML.dump(b))
125
+ end
126
+
127
+ class HashWithIndifferentAccess < Hash
128
+ end
129
+
130
+ def test_JRUBY_1169
131
+ hash = HashWithIndifferentAccess.new
132
+ hash['kind'] = 'human'
133
+ need_to_be_serialized = {:first => 'something', :second_params => hash}
134
+ a = {:x => need_to_be_serialized.to_jvyaml}
135
+ assert_equal need_to_be_serialized, JvYAML.load(JvYAML.load(a.to_jvyaml)[:x])
136
+ end
137
+
138
+ def test_JRUBY_1220
139
+ bad_text = " A\nR"
140
+ dump = JvYAML.dump({'text' => bad_text})
141
+ loaded = JvYAML.load(dump)
142
+ assert_equal bad_text, loaded['text']
143
+
144
+ bad_text = %{
145
+ ActiveRecord::StatementInvalid in ProjectsController#confirm_delete
146
+ RuntimeError: ERROR C23503 Mupdate or delete on "projects" violates foreign
147
+ }
148
+
149
+ dump = JvYAML.dump({'text' => bad_text})
150
+ loaded = JvYAML.load(dump)
151
+ assert_equal bad_text, loaded['text']
152
+
153
+ string = <<-YAML
154
+ outer
155
+ property1: value1
156
+ additional:
157
+ - property2: value2
158
+ color: green
159
+ data: SELECT 'xxxxxxxxxxxxxxxxxxx', COUNT(*) WHERE xyzabc = 'unk'
160
+ combine: overlay-bottom
161
+ YAML
162
+ assert_equal string, JvYAML.load(JvYAML.dump(string))
163
+ end
164
+
165
+ def test_whitespace_variations
166
+ text = " "*80 + "\n" + " "*30
167
+ assert_equal text, JvYAML.load(JvYAML.dump(text))
168
+
169
+ text = <<-YAML
170
+ - label: New
171
+ color: green
172
+ data: SELECT 'Iteration Scheduled', COUNT(*) WHERE Status = 'New'
173
+ combine: overlay-bottom
174
+ - label: Open
175
+ color: pink
176
+ data: SELECT 'Iteration Scheduled', COUNT(*) WHERE Status = 'Open'
177
+ combine: overlay-bottom
178
+ - label: Ready for Development
179
+ color: yellow
180
+ data: SELECT 'Iteration Scheduled', COUNT(*) WHERE Status = 'Ready for Development'
181
+ combine: overlay-bottom
182
+ color: blue
183
+ data: SELECT 'Iteration Scheduled', COUNT(*) WHERE Status = 'Complete'
184
+ combine: overlay-bottom
185
+ - label: Other statuses
186
+ color: red
187
+ data: SELECT 'Iteration Scheduled', COUNT(*)
188
+ combine: total
189
+ YAML
190
+
191
+ assert_equal text, JvYAML.load(JvYAML.dump(text))
192
+
193
+ text = <<-YAML
194
+ stack-bar-chart
195
+ conditions: 'Release' in (R1) and not 'Iteration Scheduled' = null
196
+ labels: SELECT DISTINCT 'Iteration Scheduled' ORDER BY 'Iteration Scheduled'
197
+ cumulative: true
198
+ series:
199
+ - label: New
200
+ color: green
201
+ data: SELECT 'Iteration Scheduled', COUNT(*) WHERE Status = 'New'
202
+ combine: overlay-bottom
203
+ - label: Open
204
+ color: pink
205
+ data: SELECT 'Iteration Scheduled', COUNT(*) WHERE Status = 'Open'
206
+ combine: overlay-bottom
207
+ - label: Ready for Development
208
+ color: yellow
209
+ data: SELECT 'Iteration Scheduled', COUNT(*) WHERE Status = 'Ready for Development'
210
+ combine: overlay-bottom
211
+ - label: Complete
212
+ color: blue
213
+ data: SELECT 'Iteration Scheduled', COUNT(*) WHERE Status = 'Complete'
214
+ combine: overlay-bottom
215
+ - label: Other statuses
216
+ color: red
217
+ data: SELECT 'Iteration Scheduled', COUNT(*)
218
+ combine: total
219
+ YAML
220
+
221
+ assert_equal text, JvYAML.load(JvYAML.dump(text))
222
+ end
223
+
224
+ def test_nested_stuff
225
+ text = <<YAML
226
+ valid_key:
227
+ key1: value
228
+ invalid_key
229
+ akey: blah
230
+ YAML
231
+
232
+ assert_raises(ArgumentError) do
233
+ JvYAML.load(text)
234
+ end
235
+ end
236
+
237
+ def roundtrip(text)
238
+ assert_equal text, JvYAML.load(JvYAML.dump(text))
239
+ end
240
+
241
+ def test_weird_stuff
242
+ roundtrip("C VW\205\v\321XU\346")
243
+ roundtrip("\n8 xwKmjHG")
244
+ roundtrip("1jq[\205qIB\ns")
245
+ roundtrip("\rj\230fso\304\nEE")
246
+ roundtrip("ks]qkYM\2073Un\317\nL\346Yp\204 CKMfFcRDFZ\vMNk\302fQDR<R\v \314QUa\234P\237s aLJnAu \345\262Wqm_W\241\277J\256ILKpPNsMPuok")
247
+ roundtrip :"1"
248
+ end
249
+
250
+ def fuzz_roundtrip(str)
251
+ out = JvYAML.load(JvYAML.dump(str))
252
+ assert_equal str, out
253
+ end
254
+
255
+ values = (1..255).to_a
256
+ more = ('a'..'z').to_a + ('A'..'Z').to_a
257
+ blanks = [' ', "\t", "\n"]
258
+
259
+ types = [more*10 + blanks*2, values + more*10 + blanks*2, values + more*10 + blanks*20]
260
+ sizes = [10, 81, 214]
261
+
262
+ errors = []
263
+ num = 0
264
+ types.each do |t|
265
+ sizes.each do |s|
266
+ 1000.times do |vv|
267
+ val = ""
268
+ s.times do
269
+ val << t[rand(t.length)]
270
+ end
271
+ define_method :"test_fuzz_#{num+=1}" do
272
+ fuzz_roundtrip(val)
273
+ end
274
+ end
275
+ end
276
+ end
277
+
278
+ class YamlTest
279
+ def initialize
280
+ @test = Hash.new
281
+ @test["hello"] = "foo"
282
+ end
283
+ end
284
+
285
+ def test_JRUBY_1471
286
+ list = [YamlTest.new, YamlTest.new, YamlTest.new]
287
+ assert_equal 3, list.map{ |ll| ll.object_id }.uniq.length
288
+ list2 = JvYAML.load(JvYAML.dump(list))
289
+ assert_equal 3, list2.map{ |ll| ll.object_id }.uniq.length
290
+ end
291
+
292
+ def test_JRUBY_1659
293
+ JvYAML.load("{a: 2007-01-01 01:12:34}")
294
+ end
295
+
296
+ def test_JRUBY_1765
297
+ assert_equal Date.new(-1,1,1), JvYAML.load(Date.new(-1,1,1).to_jvyaml)
298
+ end
299
+
300
+ def test_JRBY_1766
301
+ assert JvYAML.load(Time.now.to_jvyaml).instance_of?(Time)
302
+ assert JvYAML.load("2007-01-01 01:12:34").instance_of?(String)
303
+ assert JvYAML.load("2007-01-01 01:12:34.0").instance_of?(String)
304
+ assert JvYAML.load("2007-01-01 01:12:34 +00:00").instance_of?(Time)
305
+ assert JvYAML.load("2007-01-01 01:12:34.0 +00:00").instance_of?(Time)
306
+ assert JvYAML.load("{a: 2007-01-01 01:12:34}")["a"].instance_of?(String)
307
+ end
308
+
309
+ def test_JRUBY_1898
310
+ val = JvYAML.load(<<YAML)
311
+ ---
312
+ - foo
313
+ - foo
314
+ - [foo]
315
+ - [foo]
316
+ - {foo: foo}
317
+ - {foo: foo}
318
+ YAML
319
+
320
+ assert val[0].object_id != val[1].object_id
321
+ assert val[2].object_id != val[3].object_id
322
+ assert val[4].object_id != val[5].object_id
323
+ end
324
+
325
+ def test_JRUBY_1911
326
+ val = JvYAML.load(<<YAML)
327
+ ---
328
+ foo: { bar }
329
+ YAML
330
+
331
+ assert_equal({"foo" => {"bar" => nil}}, val)
332
+ end
333
+
334
+ def test_JRUBY_1756
335
+ # This is almost certainly invalid JvYAML. but MRI handles it...
336
+ val = JvYAML.load(<<YAML)
337
+ ---
338
+ default: –
339
+ - a
340
+ YAML
341
+ assert_equal({"default" => ['a']}, val)
342
+ end
343
+
344
+ def test_JRUBY_1978
345
+ # JRUBY-1978, scalars can start with , if it's not ambigous
346
+ assert_equal(",a", JvYAML.load("--- \n,a"))
347
+ end
348
+
349
+ # Make sure that overriding to_jvyaml always throws an exception unless it returns the correct thing
350
+
351
+ class TestYamlFoo
352
+ def to_jvyaml(*args)
353
+ "foo"
354
+ end
355
+ end
356
+
357
+ def test_returning_type_of_to_jvyaml
358
+ assert_raises(TypeError) do
359
+ { :foo => TestYamlFoo.new }.to_jvyaml
360
+ end
361
+ end
362
+
363
+ def test_JRUBY_2019
364
+ assert_equal({
365
+ "tag:yaml.org,2002:omap"=>JvYAML::Omap,
366
+ "tag:yaml.org,2002:pairs"=>JvYAML::Pairs,
367
+ "tag:yaml.org,2002:set"=>JvYAML::Set,
368
+ "tag:yaml.org,2002:timestamp#ymd"=>Date,
369
+ "tag:yaml.org,2002:bool#yes"=>TrueClass,
370
+ "tag:yaml.org,2002:int"=>Integer,
371
+ "tag:yaml.org,2002:timestamp"=>Time,
372
+ "tag:yaml.org,2002:binary"=>String,
373
+ "tag:yaml.org,2002:str"=>String,
374
+ "tag:yaml.org,2002:map"=>Hash,
375
+ "tag:yaml.org,2002:null"=>NilClass,
376
+ "tag:yaml.org,2002:bool#no"=>FalseClass,
377
+ "tag:yaml.org,2002:seq"=>Array,
378
+ "tag:yaml.org,2002:float"=>Float,
379
+ "tag:ruby.yaml.org,2002:sym"=>Symbol,
380
+ "tag:ruby.yaml.org,2002:object"=>Object,
381
+ "tag:ruby.yaml.org,2002:hash"=>Hash,
382
+ "tag:ruby.yaml.org,2002:time"=>Time,
383
+ "tag:ruby.yaml.org,2002:symbol"=>Symbol,
384
+ "tag:ruby.yaml.org,2002:string"=>String,
385
+ "tag:ruby.yaml.org,2002:regexp"=>Regexp,
386
+ "tag:ruby.yaml.org,2002:range"=>Range,
387
+ "tag:ruby.yaml.org,2002:array"=>Array,
388
+ "tag:ruby.yaml.org,2002:exception"=>Exception,
389
+ "tag:ruby.yaml.org,2002:struct"=>Struct,
390
+ "tag:data.allman.ms,2008:Person"=>JvYAMLUnitTests::PersonTestOne
391
+ },
392
+ JvYAML::tagged_classes)
393
+ end
394
+
395
+ def test_JRUBY_2083
396
+ assert_equal({'foobar' => '>= 123'}, JvYAML.load("foobar: >= 123"))
397
+ end
398
+
399
+ def test_JRUBY_2135
400
+ assert_equal({'foo' => 'bar'}, JvYAML.load("---\nfoo: \tbar"))
401
+ end
402
+
403
+ def test_JRUBY_1911
404
+ assert_equal({'foo' => {'bar' => nil, 'qux' => nil}}, JvYAML.load("---\nfoo: {bar, qux}"))
405
+ end
406
+
407
+ class YAMLTestException < Exception;end
408
+ class YAMLTestString < String; end
409
+
410
+ def test_JRUBY_2323
411
+ assert_equal('--- !str:JvYAMLUnitTests::YAMLTestString', YAMLTestString.new.to_jvyaml.strip)
412
+ assert_equal(YAMLTestString.new, JvYAML::load('--- !str:JvYAMLUnitTests::YAMLTestString'))
413
+ assert_equal("--- !ruby/exception:JvYAMLUnitTests::YAMLTestException \nmessage: JvYAMLUnitTests::YAMLTestException\n", YAMLTestException.new.to_jvyaml)
414
+ assert_equal(YAMLTestException.new.inspect, JvYAML::load(YAMLTestException.new.to_jvyaml).inspect)
415
+ end
416
+
417
+ def test_JRUBY_2409
418
+ assert_equal("*.rb", JvYAML::load("---\n*.rb"))
419
+ assert_equal("&.rb", JvYAML::load("---\n&.rb"))
420
+ end
421
+
422
+ def test_JRUBY_2443
423
+ a_str = "foo"
424
+ a_str.instance_variable_set :@bar, "baz"
425
+
426
+ assert(["--- !str \nstr: foo\n'@bar': baz\n", "--- !str \n'@bar': baz\nstr: foo\n"].include?(a_str.to_jvyaml))
427
+ assert_equal "baz", JvYAML.load(a_str.to_jvyaml).instance_variable_get(:@bar)
428
+
429
+ assert_equal :"abc\"flo", JvYAML.load("---\n:\"abc\\\"flo\"")
430
+ end
431
+
432
+ def test_JRUBY_2579
433
+ assert_equal [:year], JvYAML.load("---\n[:year]")
434
+
435
+ assert_equal({
436
+ 'date_select' => { 'order' => [:year, :month, :day] },
437
+ 'some' => {
438
+ 'id' => 1,
439
+ 'name' => 'some',
440
+ 'age' => 16}}, JvYAML.load(<<YAML))
441
+ date_select:
442
+ order: [:year, :month, :day]
443
+ some:
444
+ id: 1
445
+ name: some
446
+ age: 16
447
+ YAML
448
+ end
449
+
450
+ def test_JRUBY_2754
451
+ obj = Object.new
452
+ objects1 = [obj, obj]
453
+ assert objects1[0].object_id == objects1[1].object_id
454
+
455
+ objects2 = JvYAML::load objects1.to_jvyaml
456
+ assert objects2[0].object_id == objects2[1].object_id
457
+ end
458
+
459
+ class FooYSmith < Array; end
460
+ class FooXSmith < Hash; end
461
+
462
+ def test_JRUBY_2192
463
+ obj = JvYAML.load(<<YAMLSTR)
464
+ --- !ruby/array:JvYAMLUnitTests::FooYSmith
465
+ - val
466
+ - val2
467
+ YAMLSTR
468
+
469
+ assert_equal FooYSmith, obj.class
470
+
471
+ obj = JvYAML.load(<<YAMLSTR)
472
+ --- !ruby/hash:JvYAMLUnitTests::FooXSmith
473
+ key: value
474
+ otherkey: othervalue
475
+ YAMLSTR
476
+
477
+ assert_equal FooXSmith, obj.class
478
+ end
479
+
480
+ class PersonTestOne
481
+ jvyaml_as 'tag:data.allman.ms,2008:Person'
482
+ end
483
+
484
+ def test_JRUBY_2976
485
+ assert_equal "--- !data.allman.ms,2008/Person {}\n\n", PersonTestOne.new.to_jvyaml
486
+ assert_equal PersonTestOne, JvYAML.load(PersonTestOne.new.to_jvyaml).class
487
+
488
+ Hash.class_eval do
489
+ def to_jvyaml( opts = {} )
490
+ JvYAML::quick_emit( self, opts ) do |out|
491
+ out.map( jv_taguri, to_jvyaml_style ) do |map|
492
+ each do |k, v|
493
+ map.add( k, v )
494
+ end
495
+ end
496
+ end
497
+ end
498
+ end
499
+
500
+ roundtrip({ "element" => "value", "array" => [ { "nested_element" => "nested_value" } ] })
501
+
502
+ jruby3639 = <<Y
503
+ --- !ruby/object:MySoap::InterfaceOne::DiscountServiceRequestType
504
+ orderRequest: !ruby/object:MySoap::InterfaceOne::OrderType
505
+ brand: !str
506
+ str: ""
507
+ Y
508
+
509
+ assert_nothing_raised { JvYAML.load(jruby3639) }
510
+ end
511
+
512
+ def test_JRUBY_3412
513
+ y = "--- 2009-02-16 22::40:26.574754 -05:00\n"
514
+ assert_equal JvYAML.load(y).to_jvyaml, y
515
+ end
516
+ end