psych 2.1.0-java → 2.1.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +14 -0
  3. data/Gemfile +3 -0
  4. data/Mavenfile +7 -0
  5. data/bin/console +7 -0
  6. data/bin/setup +6 -0
  7. data/ext/java/PsychEmitter.java +345 -0
  8. data/ext/java/PsychLibrary.java +93 -0
  9. data/ext/java/PsychParser.java +399 -0
  10. data/ext/java/PsychToRuby.java +79 -0
  11. data/ext/java/PsychYamlTree.java +55 -0
  12. data/ext/psych/.gitignore +11 -0
  13. data/lib/psych.rb +2 -2
  14. data/lib/psych/visitors/to_ruby.rb +2 -2
  15. data/psych.gemspec +39 -0
  16. metadata +19 -53
  17. data/test/psych/handlers/test_recorder.rb +0 -26
  18. data/test/psych/helper.rb +0 -122
  19. data/test/psych/json/test_stream.rb +0 -110
  20. data/test/psych/nodes/test_enumerable.rb +0 -44
  21. data/test/psych/test_alias_and_anchor.rb +0 -97
  22. data/test/psych/test_array.rb +0 -58
  23. data/test/psych/test_boolean.rb +0 -37
  24. data/test/psych/test_class.rb +0 -37
  25. data/test/psych/test_coder.rb +0 -207
  26. data/test/psych/test_date_time.rb +0 -39
  27. data/test/psych/test_deprecated.rb +0 -215
  28. data/test/psych/test_document.rb +0 -47
  29. data/test/psych/test_emitter.rb +0 -112
  30. data/test/psych/test_encoding.rb +0 -269
  31. data/test/psych/test_exception.rb +0 -158
  32. data/test/psych/test_hash.rb +0 -95
  33. data/test/psych/test_json_tree.rb +0 -66
  34. data/test/psych/test_merge_keys.rb +0 -181
  35. data/test/psych/test_nil.rb +0 -19
  36. data/test/psych/test_null.rb +0 -20
  37. data/test/psych/test_numeric.rb +0 -46
  38. data/test/psych/test_object.rb +0 -45
  39. data/test/psych/test_object_references.rb +0 -72
  40. data/test/psych/test_omap.rb +0 -76
  41. data/test/psych/test_parser.rb +0 -340
  42. data/test/psych/test_psych.rb +0 -184
  43. data/test/psych/test_safe_load.rb +0 -98
  44. data/test/psych/test_scalar.rb +0 -12
  45. data/test/psych/test_scalar_scanner.rb +0 -111
  46. data/test/psych/test_serialize_subclasses.rb +0 -39
  47. data/test/psych/test_set.rb +0 -50
  48. data/test/psych/test_stream.rb +0 -94
  49. data/test/psych/test_string.rb +0 -231
  50. data/test/psych/test_struct.rb +0 -50
  51. data/test/psych/test_symbol.rb +0 -26
  52. data/test/psych/test_tainted.rb +0 -131
  53. data/test/psych/test_to_yaml_properties.rb +0 -64
  54. data/test/psych/test_tree_builder.rb +0 -80
  55. data/test/psych/test_yaml.rb +0 -1293
  56. data/test/psych/test_yamldbm.rb +0 -193
  57. data/test/psych/test_yamlstore.rb +0 -86
  58. data/test/psych/visitors/test_depth_first.rb +0 -50
  59. data/test/psych/visitors/test_emitter.rb +0 -145
  60. data/test/psych/visitors/test_to_ruby.rb +0 -332
  61. data/test/psych/visitors/test_yaml_tree.rb +0 -180
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: false
2
- require_relative 'helper'
3
-
4
- module Psych
5
- class TestToYamlProperties < Psych::TestCase
6
- class Foo
7
- attr_accessor :a, :b, :c
8
- def initialize
9
- @a = 1
10
- @b = 2
11
- @c = 3
12
- end
13
-
14
- def to_yaml_properties
15
- [:@a, :@b]
16
- end
17
- end
18
-
19
- def test_object_dump_yaml_properties
20
- foo = Psych.load(Psych.dump(Foo.new))
21
- assert_equal 1, foo.a
22
- assert_equal 2, foo.b
23
- assert_nil foo.c
24
- end
25
-
26
- class Bar < Struct.new(:foo, :bar)
27
- attr_reader :baz
28
- def initialize *args
29
- super
30
- @baz = 'hello'
31
- end
32
-
33
- def to_yaml_properties
34
- []
35
- end
36
- end
37
-
38
- def test_struct_dump_yaml_properties
39
- bar = Psych.load(Psych.dump(Bar.new('a', 'b')))
40
- assert_equal 'a', bar.foo
41
- assert_equal 'b', bar.bar
42
- assert_nil bar.baz
43
- end
44
-
45
- def test_string_dump
46
- string = "okonomiyaki"
47
- class << string
48
- def to_yaml_properties
49
- [:@tastes]
50
- end
51
- end
52
-
53
- string.instance_variable_set(:@tastes, 'delicious')
54
- v = Psych.load Psych.dump string
55
- assert_equal 'delicious', v.instance_variable_get(:@tastes)
56
- end
57
-
58
- def test_string_load_syck
59
- str = Psych.load("--- !str \nstr: okonomiyaki\n:@tastes: delicious\n")
60
- assert_equal 'okonomiyaki', str
61
- assert_equal 'delicious', str.instance_variable_get(:@tastes)
62
- end
63
- end
64
- end
@@ -1,80 +0,0 @@
1
- # frozen_string_literal: false
2
- require_relative 'helper'
3
-
4
- module Psych
5
- class TestTreeBuilder < TestCase
6
- def setup
7
- super
8
- @parser = Psych::Parser.new TreeBuilder.new
9
- @parser.parse(<<-eoyml)
10
- %YAML 1.1
11
- ---
12
- - foo
13
- - {
14
- bar : &A !!str baz,
15
- boo : *A
16
- }
17
- - *A
18
- eoyml
19
- @tree = @parser.handler.root
20
- end
21
-
22
- def test_stream
23
- assert_instance_of Nodes::Stream, @tree
24
- end
25
-
26
- def test_documents
27
- assert_equal 1, @tree.children.length
28
- assert_instance_of Nodes::Document, @tree.children.first
29
- doc = @tree.children.first
30
-
31
- assert_equal [1,1], doc.version
32
- assert_equal [], doc.tag_directives
33
- assert_equal false, doc.implicit
34
- end
35
-
36
- def test_sequence
37
- doc = @tree.children.first
38
- assert_equal 1, doc.children.length
39
-
40
- seq = doc.children.first
41
- assert_instance_of Nodes::Sequence, seq
42
- assert_nil seq.anchor
43
- assert_nil seq.tag
44
- assert_equal true, seq.implicit
45
- assert_equal Nodes::Sequence::BLOCK, seq.style
46
- end
47
-
48
- def test_scalar
49
- doc = @tree.children.first
50
- seq = doc.children.first
51
-
52
- assert_equal 3, seq.children.length
53
- scalar = seq.children.first
54
- assert_instance_of Nodes::Scalar, scalar
55
- assert_equal 'foo', scalar.value
56
- assert_nil scalar.anchor
57
- assert_nil scalar.tag
58
- assert_equal true, scalar.plain
59
- assert_equal false, scalar.quoted
60
- assert_equal Nodes::Scalar::PLAIN, scalar.style
61
- end
62
-
63
- def test_mapping
64
- doc = @tree.children.first
65
- seq = doc.children.first
66
- map = seq.children[1]
67
-
68
- assert_instance_of Nodes::Mapping, map
69
- end
70
-
71
- def test_alias
72
- doc = @tree.children.first
73
- seq = doc.children.first
74
- assert_equal 3, seq.children.length
75
- al = seq.children[2]
76
- assert_instance_of Nodes::Alias, al
77
- assert_equal 'A', al.anchor
78
- end
79
- end
80
- end
@@ -1,1293 +0,0 @@
1
- # -*- coding: us-ascii; mode: ruby; ruby-indent-level: 4; tab-width: 4 -*-
2
- # frozen_string_literal: false
3
- # vim:sw=4:ts=4
4
- # $Id$
5
- #
6
- require_relative 'helper'
7
- require 'ostruct'
8
-
9
- # [ruby-core:01946]
10
- module Psych_Tests
11
- StructTest = Struct::new( :c )
12
- end
13
-
14
- class Psych_Unit_Tests < Psych::TestCase
15
- def teardown
16
- Psych.domain_types.clear
17
- end
18
-
19
- def test_y_method
20
- assert_raises(NoMethodError) do
21
- OpenStruct.new.y 1
22
- end
23
- end
24
-
25
- def test_syck_compat
26
- time = Time.utc(2010, 10, 10)
27
- yaml = Psych.dump time
28
- assert_match "2010-10-10 00:00:00.000000000 Z", yaml
29
- end
30
-
31
- def test_multiline_regexp
32
- assert_cycle(Regexp.new("foo\nbar"))
33
- end
34
-
35
- # [ruby-core:34969]
36
- def test_regexp_with_n
37
- assert_cycle(Regexp.new('',0,'n'))
38
- end
39
- #
40
- # Tests modified from 00basic.t in Psych.pm
41
- #
42
- def test_basic_map
43
- # Simple map
44
- assert_parse_only(
45
- { 'one' => 'foo', 'three' => 'baz', 'two' => 'bar' }, <<EOY
46
- one: foo
47
- two: bar
48
- three: baz
49
- EOY
50
- )
51
- end
52
-
53
- def test_basic_strings
54
- # Common string types
55
- assert_cycle("x")
56
- assert_cycle(":x")
57
- assert_cycle(":")
58
- assert_parse_only(
59
- { 1 => 'simple string', 2 => 42, 3 => '1 Single Quoted String',
60
- 4 => 'Psych\'s Double "Quoted" String', 5 => "A block\n with several\n lines.\n",
61
- 6 => "A \"chomped\" block", 7 => "A folded\n string\n", 8 => ": started string" },
62
- <<EOY
63
- 1: simple string
64
- 2: 42
65
- 3: '1 Single Quoted String'
66
- 4: "Psych's Double \\\"Quoted\\\" String"
67
- 5: |
68
- A block
69
- with several
70
- lines.
71
- 6: |-
72
- A "chomped" block
73
- 7: >
74
- A
75
- folded
76
- string
77
- 8: ": started string"
78
- EOY
79
- )
80
- end
81
-
82
- #
83
- # Test the specification examples
84
- # - Many examples have been changes because of whitespace problems that
85
- # caused the two to be inequivalent, or keys to be sorted wrong
86
- #
87
-
88
- def test_spec_simple_implicit_sequence
89
- # Simple implicit sequence
90
- assert_to_yaml(
91
- [ 'Mark McGwire', 'Sammy Sosa', 'Ken Griffey' ], <<EOY
92
- - Mark McGwire
93
- - Sammy Sosa
94
- - Ken Griffey
95
- EOY
96
- )
97
- end
98
-
99
- def test_spec_simple_implicit_map
100
- # Simple implicit map
101
- assert_to_yaml(
102
- { 'hr' => 65, 'avg' => 0.278, 'rbi' => 147 }, <<EOY
103
- avg: 0.278
104
- hr: 65
105
- rbi: 147
106
- EOY
107
- )
108
- end
109
-
110
- def test_spec_simple_map_with_nested_sequences
111
- # Simple mapping with nested sequences
112
- assert_to_yaml(
113
- { 'american' =>
114
- [ 'Boston Red Sox', 'Detroit Tigers', 'New York Yankees' ],
115
- 'national' =>
116
- [ 'New York Mets', 'Chicago Cubs', 'Atlanta Braves' ] }, <<EOY
117
- american:
118
- - Boston Red Sox
119
- - Detroit Tigers
120
- - New York Yankees
121
- national:
122
- - New York Mets
123
- - Chicago Cubs
124
- - Atlanta Braves
125
- EOY
126
- )
127
- end
128
-
129
- def test_spec_simple_sequence_with_nested_map
130
- # Simple sequence with nested map
131
- assert_to_yaml(
132
- [
133
- {'name' => 'Mark McGwire', 'hr' => 65, 'avg' => 0.278},
134
- {'name' => 'Sammy Sosa', 'hr' => 63, 'avg' => 0.288}
135
- ], <<EOY
136
- -
137
- avg: 0.278
138
- hr: 65
139
- name: Mark McGwire
140
- -
141
- avg: 0.288
142
- hr: 63
143
- name: Sammy Sosa
144
- EOY
145
- )
146
- end
147
-
148
- def test_spec_sequence_of_sequences
149
- # Simple sequence with inline sequences
150
- assert_parse_only(
151
- [
152
- [ 'name', 'hr', 'avg' ],
153
- [ 'Mark McGwire', 65, 0.278 ],
154
- [ 'Sammy Sosa', 63, 0.288 ]
155
- ], <<EOY
156
- - [ name , hr , avg ]
157
- - [ Mark McGwire , 65 , 0.278 ]
158
- - [ Sammy Sosa , 63 , 0.288 ]
159
- EOY
160
- )
161
- end
162
-
163
- def test_spec_mapping_of_mappings
164
- # Simple map with inline maps
165
- assert_parse_only(
166
- { 'Mark McGwire' =>
167
- { 'hr' => 65, 'avg' => 0.278 },
168
- 'Sammy Sosa' =>
169
- { 'hr' => 63, 'avg' => 0.288 }
170
- }, <<EOY
171
- Mark McGwire: {hr: 65, avg: 0.278}
172
- Sammy Sosa: {hr: 63,
173
- avg: 0.288}
174
- EOY
175
- )
176
- end
177
-
178
- def test_ambiguous_comments
179
- # [ruby-talk:88012]
180
- assert_to_yaml( "Call the method #dave", <<EOY )
181
- --- "Call the method #dave"
182
- EOY
183
- end
184
-
185
- def test_spec_nested_comments
186
- # Map and sequences with comments
187
- assert_parse_only(
188
- { 'hr' => [ 'Mark McGwire', 'Sammy Sosa' ],
189
- 'rbi' => [ 'Sammy Sosa', 'Ken Griffey' ] }, <<EOY
190
- hr: # 1998 hr ranking
191
- - Mark McGwire
192
- - Sammy Sosa
193
- rbi:
194
- # 1998 rbi ranking
195
- - Sammy Sosa
196
- - Ken Griffey
197
- EOY
198
- )
199
- end
200
-
201
- def test_spec_anchors_and_aliases
202
- # Anchors and aliases
203
- assert_parse_only(
204
- { 'hr' =>
205
- [ 'Mark McGwire', 'Sammy Sosa' ],
206
- 'rbi' =>
207
- [ 'Sammy Sosa', 'Ken Griffey' ] }, <<EOY
208
- hr:
209
- - Mark McGwire
210
- # Name "Sammy Sosa" scalar SS
211
- - &SS Sammy Sosa
212
- rbi:
213
- # So it can be referenced later.
214
- - *SS
215
- - Ken Griffey
216
- EOY
217
- )
218
-
219
- assert_to_yaml(
220
- [{"arrival"=>"EDI", "departure"=>"LAX", "fareref"=>"DOGMA", "currency"=>"GBP"}, {"arrival"=>"MEL", "departure"=>"SYD", "fareref"=>"MADF", "currency"=>"AUD"}, {"arrival"=>"MCO", "departure"=>"JFK", "fareref"=>"DFSF", "currency"=>"USD"}], <<EOY
221
- -
222
- &F fareref: DOGMA
223
- &C currency: GBP
224
- &D departure: LAX
225
- &A arrival: EDI
226
- - { *F: MADF, *C: AUD, *D: SYD, *A: MEL }
227
- - { *F: DFSF, *C: USD, *D: JFK, *A: MCO }
228
- EOY
229
- )
230
-
231
- assert_to_yaml(
232
- {"ALIASES"=>["fareref", "currency", "departure", "arrival"], "FARES"=>[{"arrival"=>"EDI", "departure"=>"LAX", "fareref"=>"DOGMA", "currency"=>"GBP"}, {"arrival"=>"MEL", "departure"=>"SYD", "fareref"=>"MADF", "currency"=>"AUD"}, {"arrival"=>"MCO", "departure"=>"JFK", "fareref"=>"DFSF", "currency"=>"USD"}]}, <<EOY
233
- ---
234
- ALIASES: [&f fareref, &c currency, &d departure, &a arrival]
235
- FARES:
236
- - *f: DOGMA
237
- *c: GBP
238
- *d: LAX
239
- *a: EDI
240
-
241
- - *f: MADF
242
- *c: AUD
243
- *d: SYD
244
- *a: MEL
245
-
246
- - *f: DFSF
247
- *c: USD
248
- *d: JFK
249
- *a: MCO
250
-
251
- EOY
252
- )
253
-
254
- end
255
-
256
- def test_spec_mapping_between_sequences
257
- # Complex key #1
258
- assert_parse_only(
259
- { [ 'Detroit Tigers', 'Chicago Cubs' ] => [ Date.new( 2001, 7, 23 ) ],
260
- [ 'New York Yankees', 'Atlanta Braves' ] => [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ), Date.new( 2001, 8, 14 ) ] }, <<EOY
261
- ? # PLAY SCHEDULE
262
- - Detroit Tigers
263
- - Chicago Cubs
264
- :
265
- - 2001-07-23
266
-
267
- ? [ New York Yankees,
268
- Atlanta Braves ]
269
- : [ 2001-07-02, 2001-08-12,
270
- 2001-08-14 ]
271
- EOY
272
- )
273
-
274
- # Complex key #2
275
- assert_parse_only(
276
- { [ 'New York Yankees', 'Atlanta Braves' ] =>
277
- [ Date.new( 2001, 7, 2 ), Date.new( 2001, 8, 12 ),
278
- Date.new( 2001, 8, 14 ) ],
279
- [ 'Detroit Tigers', 'Chicago Cubs' ] =>
280
- [ Date.new( 2001, 7, 23 ) ]
281
- }, <<EOY
282
- ?
283
- - New York Yankees
284
- - Atlanta Braves
285
- :
286
- - 2001-07-02
287
- - 2001-08-12
288
- - 2001-08-14
289
- ?
290
- - Detroit Tigers
291
- - Chicago Cubs
292
- :
293
- - 2001-07-23
294
- EOY
295
- )
296
- end
297
-
298
- def test_spec_sequence_key_shortcut
299
- # Shortcut sequence map
300
- assert_parse_only(
301
- { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ),
302
- 'bill-to' => 'Chris Dumars', 'product' =>
303
- [ { 'item' => 'Super Hoop', 'quantity' => 1 },
304
- { 'item' => 'Basketball', 'quantity' => 4 },
305
- { 'item' => 'Big Shoes', 'quantity' => 1 } ] }, <<EOY
306
- invoice: 34843
307
- date : 2001-01-23
308
- bill-to: Chris Dumars
309
- product:
310
- - item : Super Hoop
311
- quantity: 1
312
- - item : Basketball
313
- quantity: 4
314
- - item : Big Shoes
315
- quantity: 1
316
- EOY
317
- )
318
- end
319
-
320
- def test_spec_sequence_in_sequence_shortcut
321
- # Seq-in-seq
322
- assert_parse_only( [ [ [ 'one', 'two', 'three' ] ] ], <<EOY )
323
- - - - one
324
- - two
325
- - three
326
- EOY
327
- end
328
-
329
- def test_spec_sequence_shortcuts
330
- # Sequence shortcuts combined
331
- assert_parse_only(
332
- [
333
- [
334
- [ [ 'one' ] ],
335
- [ 'two', 'three' ],
336
- { 'four' => nil },
337
- [ { 'five' => [ 'six' ] } ],
338
- [ 'seven' ]
339
- ],
340
- [ 'eight', 'nine' ]
341
- ], <<EOY )
342
- - - - - one
343
- - - two
344
- - three
345
- - four:
346
- - - five:
347
- - six
348
- - - seven
349
- - - eight
350
- - nine
351
- EOY
352
- end
353
-
354
- def test_spec_single_literal
355
- # Literal scalar block
356
- assert_parse_only( [ "\\/|\\/|\n/ | |_\n" ], <<EOY )
357
- - |
358
- \\/|\\/|
359
- / | |_
360
- EOY
361
- end
362
-
363
- def test_spec_single_folded
364
- # Folded scalar block
365
- assert_parse_only(
366
- [ "Mark McGwire's year was crippled by a knee injury.\n" ], <<EOY
367
- - >
368
- Mark McGwire\'s
369
- year was crippled
370
- by a knee injury.
371
- EOY
372
- )
373
- end
374
-
375
- def test_spec_preserve_indent
376
- # Preserve indented spaces
377
- assert_parse_only(
378
- "Sammy Sosa completed another fine season with great stats.\n\n 63 Home Runs\n 0.288 Batting Average\n\nWhat a year!\n", <<EOY
379
- --- >
380
- Sammy Sosa completed another
381
- fine season with great stats.
382
-
383
- 63 Home Runs
384
- 0.288 Batting Average
385
-
386
- What a year!
387
- EOY
388
- )
389
- end
390
-
391
- def test_spec_indentation_determines_scope
392
- assert_parse_only(
393
- { 'name' => 'Mark McGwire', 'accomplishment' => "Mark set a major league home run record in 1998.\n",
394
- 'stats' => "65 Home Runs\n0.278 Batting Average\n" }, <<EOY
395
- name: Mark McGwire
396
- accomplishment: >
397
- Mark set a major league
398
- home run record in 1998.
399
- stats: |
400
- 65 Home Runs
401
- 0.278 Batting Average
402
- EOY
403
- )
404
- end
405
-
406
- def test_spec_multiline_scalars
407
- # Multiline flow scalars
408
- assert_parse_only(
409
- { 'plain' => 'This unquoted scalar spans many lines.',
410
- 'quoted' => "So does this quoted scalar.\n" }, <<EOY
411
- plain: This unquoted
412
- scalar spans
413
- many lines.
414
- quoted: "\\
415
- So does this quoted
416
- scalar.\\n"
417
- EOY
418
- )
419
- end
420
-
421
- def test_spec_type_int
422
- assert_parse_only(
423
- { 'canonical' => 12345, 'decimal' => 12345, 'octal' => '014'.oct, 'hexadecimal' => '0xC'.hex }, <<EOY
424
- canonical: 12345
425
- decimal: +12,345
426
- octal: 014
427
- hexadecimal: 0xC
428
- EOY
429
- )
430
- assert_parse_only(
431
- { 'canonical' => 685230, 'decimal' => 685230, 'octal' => 02472256, 'hexadecimal' => 0x0A74AE, 'sexagesimal' => 685230 }, <<EOY)
432
- canonical: 685230
433
- decimal: +685,230
434
- octal: 02472256
435
- hexadecimal: 0x0A,74,AE
436
- sexagesimal: 190:20:30
437
- EOY
438
- end
439
-
440
- def test_spec_type_float
441
- assert_parse_only(
442
- { 'canonical' => 1230.15, 'exponential' => 1230.15, 'fixed' => 1230.15,
443
- 'negative infinity' => -1.0/0.0 }, <<EOY)
444
- canonical: 1.23015e+3
445
- exponential: 12.3015e+02
446
- fixed: 1,230.15
447
- negative infinity: -.inf
448
- EOY
449
- nan = Psych::load( <<EOY )
450
- not a number: .NaN
451
- EOY
452
- assert( nan['not a number'].nan? )
453
- end
454
-
455
- def test_spec_type_misc
456
- assert_parse_only(
457
- { nil => nil, true => true, false => false, 'string' => '12345' }, <<EOY
458
- null: ~
459
- true: yes
460
- false: no
461
- string: '12345'
462
- EOY
463
- )
464
- end
465
-
466
- def test_spec_complex_invoice
467
- # Complex invoice type
468
- id001 = { 'given' => 'Chris', 'family' => 'Dumars', 'address' =>
469
- { 'lines' => "458 Walkman Dr.\nSuite #292\n", 'city' => 'Royal Oak',
470
- 'state' => 'MI', 'postal' => 48046 } }
471
- assert_parse_only(
472
- { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ),
473
- 'bill-to' => id001, 'ship-to' => id001, 'product' =>
474
- [ { 'sku' => 'BL394D', 'quantity' => 4,
475
- 'description' => 'Basketball', 'price' => 450.00 },
476
- { 'sku' => 'BL4438H', 'quantity' => 1,
477
- 'description' => 'Super Hoop', 'price' => 2392.00 } ],
478
- 'tax' => 251.42, 'total' => 4443.52,
479
- 'comments' => "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.\n" }, <<EOY
480
- invoice: 34843
481
- date : 2001-01-23
482
- bill-to: &id001
483
- given : Chris
484
- family : !str Dumars
485
- address:
486
- lines: |
487
- 458 Walkman Dr.
488
- Suite #292
489
- city : Royal Oak
490
- state : MI
491
- postal : 48046
492
- ship-to: *id001
493
- product:
494
- - !map
495
- sku : BL394D
496
- quantity : 4
497
- description : Basketball
498
- price : 450.00
499
- - sku : BL4438H
500
- quantity : 1
501
- description : Super Hoop
502
- price : 2392.00
503
- tax : 251.42
504
- total: 4443.52
505
- comments: >
506
- Late afternoon is best.
507
- Backup contact is Nancy
508
- Billsmer @ 338-4338.
509
- EOY
510
- )
511
- end
512
-
513
- def test_spec_log_file
514
- doc_ct = 0
515
- Psych::load_documents( <<EOY
516
- ---
517
- Time: 2001-11-23 15:01:42 -05:00
518
- User: ed
519
- Warning: >
520
- This is an error message
521
- for the log file
522
- ---
523
- Time: 2001-11-23 15:02:31 -05:00
524
- User: ed
525
- Warning: >
526
- A slightly different error
527
- message.
528
- ---
529
- Date: 2001-11-23 15:03:17 -05:00
530
- User: ed
531
- Fatal: >
532
- Unknown variable "bar"
533
- Stack:
534
- - file: TopClass.py
535
- line: 23
536
- code: |
537
- x = MoreObject("345\\n")
538
- - file: MoreClass.py
539
- line: 58
540
- code: |-
541
- foo = bar
542
- EOY
543
- ) { |doc|
544
- case doc_ct
545
- when 0
546
- assert_equal( doc, { 'Time' => mktime( 2001, 11, 23, 15, 01, 42, 00, "-05:00" ),
547
- 'User' => 'ed', 'Warning' => "This is an error message for the log file\n" } )
548
- when 1
549
- assert_equal( doc, { 'Time' => mktime( 2001, 11, 23, 15, 02, 31, 00, "-05:00" ),
550
- 'User' => 'ed', 'Warning' => "A slightly different error message.\n" } )
551
- when 2
552
- assert_equal( doc, { 'Date' => mktime( 2001, 11, 23, 15, 03, 17, 00, "-05:00" ),
553
- 'User' => 'ed', 'Fatal' => "Unknown variable \"bar\"\n",
554
- 'Stack' => [
555
- { 'file' => 'TopClass.py', 'line' => 23, 'code' => "x = MoreObject(\"345\\n\")\n" },
556
- { 'file' => 'MoreClass.py', 'line' => 58, 'code' => "foo = bar" } ] } )
557
- end
558
- doc_ct += 1
559
- }
560
- assert_equal( doc_ct, 3 )
561
- end
562
-
563
- def test_spec_root_fold
564
- y = Psych::load( <<EOY
565
- ---
566
- This Psych stream contains a single text value.
567
- The next stream is a log file - a sequence of
568
- log entries. Adding an entry to the log is a
569
- simple matter of appending it at the end.
570
- EOY
571
- )
572
- assert_equal( y, "This Psych stream contains a single text value. The next stream is a log file - a sequence of log entries. Adding an entry to the log is a simple matter of appending it at the end." )
573
- end
574
-
575
- def test_spec_root_mapping
576
- y = Psych::load( <<EOY
577
- # This stream is an example of a top-level mapping.
578
- invoice : 34843
579
- date : 2001-01-23
580
- total : 4443.52
581
- EOY
582
- )
583
- assert_equal( y, { 'invoice' => 34843, 'date' => Date.new( 2001, 1, 23 ), 'total' => 4443.52 } )
584
- end
585
-
586
- def test_spec_oneline_docs
587
- doc_ct = 0
588
- Psych::load_documents( <<EOY
589
- # The following is a sequence of three documents.
590
- # The first contains an empty mapping, the second
591
- # an empty sequence, and the last an empty string.
592
- --- {}
593
- --- [ ]
594
- --- ''
595
- EOY
596
- ) { |doc|
597
- case doc_ct
598
- when 0
599
- assert_equal( doc, {} )
600
- when 1
601
- assert_equal( doc, [] )
602
- when 2
603
- assert_equal( doc, '' )
604
- end
605
- doc_ct += 1
606
- }
607
- assert_equal( doc_ct, 3 )
608
- end
609
-
610
- def test_spec_domain_prefix
611
- customer_proc = proc { |type, val|
612
- if Hash === val
613
- _, _, type = type.split( ':', 3 )
614
- val['type'] = "domain #{type}"
615
- val
616
- else
617
- raise ArgumentError, "Not a Hash in domain.tld,2002/invoice: " + val.inspect
618
- end
619
- }
620
- Psych.add_domain_type( "domain.tld,2002", 'invoice', &customer_proc )
621
- Psych.add_domain_type( "domain.tld,2002", 'customer', &customer_proc )
622
- assert_parse_only( { "invoice"=> { "customers"=> [ { "given"=>"Chris", "type"=>"domain customer", "family"=>"Dumars" } ], "type"=>"domain invoice" } }, <<EOY
623
- # 'http://domain.tld,2002/invoice' is some type family.
624
- invoice: !domain.tld,2002/invoice
625
- # 'seq' is shorthand for 'http://yaml.org/seq'.
626
- # This does not effect '^customer' below
627
- # because it is does not specify a prefix.
628
- customers: !seq
629
- # '^customer' is shorthand for the full
630
- # notation 'http://domain.tld,2002/customer'.
631
- - !customer
632
- given : Chris
633
- family : Dumars
634
- EOY
635
- )
636
- end
637
-
638
- def test_spec_throwaway
639
- assert_parse_only(
640
- {"this"=>"contains three lines of text.\nThe third one starts with a\n# character. This isn't a comment.\n"}, <<EOY
641
- ### These are four throwaway comment ###
642
-
643
- ### lines (the second line is empty). ###
644
- this: | # Comments may trail lines.
645
- contains three lines of text.
646
- The third one starts with a
647
- # character. This isn't a comment.
648
-
649
- # These are three throwaway comment
650
- # lines (the first line is empty).
651
- EOY
652
- )
653
- end
654
-
655
- def test_spec_force_implicit
656
- # Force implicit
657
- assert_parse_only(
658
- { 'integer' => 12, 'also int' => 12, 'string' => '12' }, <<EOY
659
- integer: 12
660
- also int: ! "12"
661
- string: !str 12
662
- EOY
663
- )
664
- end
665
-
666
- ###
667
- # Commenting out this test. This line:
668
- #
669
- # - !domain.tld,2002/type\\x30 value
670
- #
671
- # Is invalid according to the YAML spec:
672
- #
673
- # http://yaml.org/spec/1.1/#id896876
674
- #
675
- # def test_spec_url_escaping
676
- # Psych.add_domain_type( "domain.tld,2002", "type0" ) { |type, val|
677
- # "ONE: #{val}"
678
- # }
679
- # Psych.add_domain_type( "domain.tld,2002", "type%30" ) { |type, val|
680
- # "TWO: #{val}"
681
- # }
682
- # assert_parse_only(
683
- # { 'same' => [ 'ONE: value', 'ONE: value' ], 'different' => [ 'TWO: value' ] }, <<EOY
684
- #same:
685
- # - !domain.tld,2002/type\\x30 value
686
- # - !domain.tld,2002/type0 value
687
- #different: # As far as the Psych parser is concerned
688
- # - !domain.tld,2002/type%30 value
689
- #EOY
690
- # )
691
- # end
692
-
693
- def test_spec_override_anchor
694
- # Override anchor
695
- a001 = "The alias node below is a repeated use of this value.\n"
696
- assert_parse_only(
697
- { 'anchor' => 'This scalar has an anchor.', 'override' => a001, 'alias' => a001 }, <<EOY
698
- anchor : &A001 This scalar has an anchor.
699
- override : &A001 >
700
- The alias node below is a
701
- repeated use of this value.
702
- alias : *A001
703
- EOY
704
- )
705
- end
706
-
707
- def test_spec_explicit_families
708
- Psych.add_domain_type( "somewhere.com,2002", 'type' ) { |type, val|
709
- "SOMEWHERE: #{val}"
710
- }
711
- assert_parse_only(
712
- { 'not-date' => '2002-04-28', 'picture' => "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236i^\020' \202\n\001\000;", 'hmm' => "SOMEWHERE: family above is short for\nhttp://somewhere.com/type\n" }, <<EOY
713
- not-date: !str 2002-04-28
714
- picture: !binary |
715
- R0lGODlhDAAMAIQAAP//9/X
716
- 17unp5WZmZgAAAOfn515eXv
717
- Pz7Y6OjuDg4J+fn5OTk6enp
718
- 56enmleECcgggoBADs=
719
-
720
- hmm: !somewhere.com,2002/type |
721
- family above is short for
722
- http://somewhere.com/type
723
- EOY
724
- )
725
- end
726
-
727
- def test_spec_application_family
728
- # Testing the clarkevans.com graphs
729
- Psych.add_domain_type( "clarkevans.com,2002", 'graph/shape' ) { |type, val|
730
- if Array === val
731
- val << "Shape Container"
732
- val
733
- else
734
- raise ArgumentError, "Invalid graph of type #{val.class}: " + val.inspect
735
- end
736
- }
737
- one_shape_proc = Proc.new { |type, val|
738
- if Hash === val
739
- type = type.split( /:/ )
740
- val['TYPE'] = "Shape: #{type[2]}"
741
- val
742
- else
743
- raise ArgumentError, "Invalid graph of type #{val.class}: " + val.inspect
744
- end
745
- }
746
- Psych.add_domain_type( "clarkevans.com,2002", 'graph/circle', &one_shape_proc )
747
- Psych.add_domain_type( "clarkevans.com,2002", 'graph/line', &one_shape_proc )
748
- Psych.add_domain_type( "clarkevans.com,2002", 'graph/text', &one_shape_proc )
749
- # MODIFIED to remove invalid Psych
750
- assert_parse_only(
751
- [[{"radius"=>7, "center"=>{"x"=>73, "y"=>129}, "TYPE"=>"Shape: graph/circle"}, {"finish"=>{"x"=>89, "y"=>102}, "TYPE"=>"Shape: graph/line", "start"=>{"x"=>73, "y"=>129}}, {"TYPE"=>"Shape: graph/text", "value"=>"Pretty vector drawing.", "start"=>{"x"=>73, "y"=>129}, "color"=>16772795}, "Shape Container"]], <<EOY
752
- - !clarkevans.com,2002/graph/shape
753
- - !/graph/circle
754
- center: &ORIGIN {x: 73, y: 129}
755
- radius: 7
756
- - !/graph/line # !clarkevans.com,2002/graph/line
757
- start: *ORIGIN
758
- finish: { x: 89, y: 102 }
759
- - !/graph/text
760
- start: *ORIGIN
761
- color: 0xFFEEBB
762
- value: Pretty vector drawing.
763
- EOY
764
- )
765
- end
766
-
767
- def test_spec_float_explicit
768
- assert_parse_only(
769
- [ 10.0, 10.0, 10.0, 10.0 ], <<EOY
770
- # All entries in the sequence
771
- # have the same type and value.
772
- - 10.0
773
- - !float 10
774
- - !yaml.org,2002/float '10'
775
- - !yaml.org,2002/float "\\
776
- 1\\
777
- 0"
778
- EOY
779
- )
780
- end
781
-
782
- def test_spec_builtin_seq
783
- # Assortment of sequences
784
- assert_parse_only(
785
- { 'empty' => [], 'in-line' => [ 'one', 'two', 'three', 'four', 'five' ],
786
- 'nested' => [ 'First item in top sequence', [ 'Subordinate sequence entry' ],
787
- "A multi-line sequence entry\n", 'Sixth item in top sequence' ] }, <<EOY
788
- empty: []
789
- in-line: [ one, two, three # May span lines,
790
- , four, # indentation is
791
- five ] # mostly ignored.
792
- nested:
793
- - First item in top sequence
794
- -
795
- - Subordinate sequence entry
796
- - >
797
- A multi-line
798
- sequence entry
799
- - Sixth item in top sequence
800
- EOY
801
- )
802
- end
803
-
804
- def test_spec_builtin_map
805
- # Assortment of mappings
806
- assert_parse_only(
807
- { 'empty' => {}, 'in-line' => { 'one' => 1, 'two' => 2 },
808
- 'spanning' => { 'one' => 1, 'two' => 2 },
809
- 'nested' => { 'first' => 'First entry', 'second' =>
810
- { 'key' => 'Subordinate mapping' }, 'third' =>
811
- [ 'Subordinate sequence', {}, 'Previous mapping is empty.',
812
- { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' },
813
- 'The previous entry is equal to the following one.',
814
- { 'A key' => 'value pair in a sequence.', 'A second' => 'key:value pair.' } ],
815
- 12.0 => 'This key is a float.', "?\n" => 'This key had to be protected.',
816
- "\a" => 'This key had to be escaped.',
817
- "This is a multi-line folded key\n" => "Whose value is also multi-line.\n",
818
- [ 'This key', 'is a sequence' ] => [ 'With a sequence value.' ] } }, <<EOY
819
-
820
- empty: {}
821
- in-line: { one: 1, two: 2 }
822
- spanning: { one: 1,
823
- two: 2 }
824
- nested:
825
- first : First entry
826
- second:
827
- key: Subordinate mapping
828
- third:
829
- - Subordinate sequence
830
- - { }
831
- - Previous mapping is empty.
832
- - A key: value pair in a sequence.
833
- A second: key:value pair.
834
- - The previous entry is equal to the following one.
835
- -
836
- A key: value pair in a sequence.
837
- A second: key:value pair.
838
- !float 12 : This key is a float.
839
- ? >
840
- ?
841
- : This key had to be protected.
842
- "\\a" : This key had to be escaped.
843
- ? >
844
- This is a
845
- multi-line
846
- folded key
847
- : >
848
- Whose value is
849
- also multi-line.
850
- ?
851
- - This key
852
- - is a sequence
853
- :
854
- - With a sequence value.
855
- # The following parses correctly,
856
- # but Ruby 1.6.* fails the comparison!
857
- # ?
858
- # This: key
859
- # is a: mapping
860
- # :
861
- # with a: mapping value.
862
- EOY
863
- )
864
- end
865
-
866
- def test_spec_builtin_literal_blocks
867
- # Assortment of literal scalar blocks
868
- assert_parse_only(
869
- {"both are equal to"=>" This has no newline.", "is equal to"=>"The \\ ' \" characters may be\nfreely used. Leading white\n space is significant.\n\nLine breaks are significant.\nThus this value contains one\nempty line and ends with a\nsingle line break, but does\nnot start with one.\n", "also written as"=>" This has no newline.", "indented and chomped"=>" This has no newline.", "empty"=>"", "literal"=>"The \\ ' \" characters may be\nfreely used. Leading white\n space is significant.\n\nLine breaks are significant.\nThus this value contains one\nempty line and ends with a\nsingle line break, but does\nnot start with one.\n"}, <<EOY
870
- empty: |
871
-
872
- literal: |
873
- The \\\ ' " characters may be
874
- freely used. Leading white
875
- space is significant.
876
-
877
- Line breaks are significant.
878
- Thus this value contains one
879
- empty line and ends with a
880
- single line break, but does
881
- not start with one.
882
-
883
- is equal to: "The \\\\ ' \\" characters may \\
884
- be\\nfreely used. Leading white\\n space \\
885
- is significant.\\n\\nLine breaks are \\
886
- significant.\\nThus this value contains \\
887
- one\\nempty line and ends with a\\nsingle \\
888
- line break, but does\\nnot start with one.\\n"
889
-
890
- # Comments may follow a nested
891
- # scalar value. They must be
892
- # less indented.
893
-
894
- # Modifiers may be combined in any order.
895
- indented and chomped: |2-
896
- This has no newline.
897
-
898
- also written as: |-2
899
- This has no newline.
900
-
901
- both are equal to: " This has no newline."
902
- EOY
903
- )
904
-
905
- str1 = "This has one newline.\n"
906
- str2 = "This has no newline."
907
- str3 = "This has two newlines.\n\n"
908
- assert_parse_only(
909
- { 'clipped' => str1, 'same as "clipped" above' => str1,
910
- 'stripped' => str2, 'same as "stripped" above' => str2,
911
- 'kept' => str3, 'same as "kept" above' => str3 }, <<EOY
912
- clipped: |
913
- This has one newline.
914
-
915
- same as "clipped" above: "This has one newline.\\n"
916
-
917
- stripped: |-
918
- This has no newline.
919
-
920
- same as "stripped" above: "This has no newline."
921
-
922
- kept: |+
923
- This has two newlines.
924
-
925
- same as "kept" above: "This has two newlines.\\n\\n"
926
-
927
- EOY
928
- )
929
- end
930
-
931
- def test_spec_span_single_quote
932
- assert_parse_only( {"third"=>"a single quote ' must be escaped.", "second"=>"! : \\ etc. can be used freely.", "is same as"=>"this contains six spaces\nand one line break", "empty"=>"", "span"=>"this contains six spaces\nand one line break"}, <<EOY
933
- empty: ''
934
- second: '! : \\ etc. can be used freely.'
935
- third: 'a single quote '' must be escaped.'
936
- span: 'this contains
937
- six spaces
938
-
939
- and one
940
- line break'
941
- is same as: "this contains six spaces\\nand one line break"
942
- EOY
943
- )
944
- end
945
-
946
- def test_spec_span_double_quote
947
- assert_parse_only( {"is equal to"=>"this contains four spaces", "third"=>"a \" or a \\ must be escaped.", "second"=>"! : etc. can be used freely.", "empty"=>"", "fourth"=>"this value ends with an LF.\n", "span"=>"this contains four spaces"}, <<EOY
948
- empty: ""
949
- second: "! : etc. can be used freely."
950
- third: "a \\\" or a \\\\ must be escaped."
951
- fourth: "this value ends with an LF.\\n"
952
- span: "this contains
953
- four \\
954
- spaces"
955
- is equal to: "this contains four spaces"
956
- EOY
957
- )
958
- end
959
-
960
- def test_spec_builtin_time
961
- # Time
962
- assert_parse_only(
963
- { "space separated" => mktime( 2001, 12, 14, 21, 59, 43, ".10", "-05:00" ),
964
- "canonical" => mktime( 2001, 12, 15, 2, 59, 43, ".10" ),
965
- "date (noon UTC)" => Date.new( 2002, 12, 14),
966
- "valid iso8601" => mktime( 2001, 12, 14, 21, 59, 43, ".10", "-05:00" ) }, <<EOY
967
- canonical: 2001-12-15T02:59:43.1Z
968
- valid iso8601: 2001-12-14t21:59:43.10-05:00
969
- space separated: 2001-12-14 21:59:43.10 -05:00
970
- date (noon UTC): 2002-12-14
971
- EOY
972
- )
973
- end
974
-
975
- def test_spec_builtin_binary
976
- arrow_gif = "GIF89a\f\000\f\000\204\000\000\377\377\367\365\365\356\351\351\345fff\000\000\000\347\347\347^^^\363\363\355\216\216\216\340\340\340\237\237\237\223\223\223\247\247\247\236\236\236iiiccc\243\243\243\204\204\204\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371\377\376\371!\376\016Made with GIMP\000,\000\000\000\000\f\000\f\000\000\005, \216\2010\236\343@\024\350i\020\304\321\212\010\034\317\200M$z\357\3770\205p\270\2601f\r\e\316\001\303\001\036\020' \202\n\001\000;"
977
- assert_parse_only(
978
- { 'canonical' => arrow_gif, 'base64' => arrow_gif,
979
- 'description' => "The binary value above is a tiny arrow encoded as a gif image.\n" }, <<EOY
980
- canonical: !binary "\\
981
- R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOf\\
982
- n515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlpaW\\
983
- NjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++\\
984
- f/++f/++f/++f/++f/++f/++f/++SH+Dk1hZGUg\\
985
- d2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuN\\
986
- AFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84Bww\\
987
- EeECcgggoBADs="
988
- base64: !binary |
989
- R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOf
990
- n515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlpaW
991
- NjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++
992
- f/++f/++f/++f/++f/++f/++f/++SH+Dk1hZGUg
993
- d2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuN
994
- AFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84Bww
995
- EeECcgggoBADs=
996
- description: >
997
- The binary value above is a tiny arrow
998
- encoded as a gif image.
999
- EOY
1000
- )
1001
- end
1002
- def test_ruby_regexp
1003
- # Test Ruby regular expressions
1004
- assert_to_yaml(
1005
- { 'simple' => /a.b/, 'complex' => %r'\A"((?:[^"]|\")+)"',
1006
- 'case-insensitive' => /George McFly/i }, <<EOY
1007
- case-insensitive: !ruby/regexp "/George McFly/i"
1008
- complex: !ruby/regexp "/\\\\A\\"((?:[^\\"]|\\\\\\")+)\\"/"
1009
- simple: !ruby/regexp "/a.b/"
1010
- EOY
1011
- )
1012
- end
1013
-
1014
- #
1015
- # Test of Ranges
1016
- #
1017
- def test_ranges
1018
-
1019
- # Simple numeric
1020
- assert_to_yaml( 1..3, <<EOY )
1021
- --- !ruby/range 1..3
1022
- EOY
1023
-
1024
- # Simple alphabetic
1025
- assert_to_yaml( 'a'..'z', <<EOY )
1026
- --- !ruby/range a..z
1027
- EOY
1028
-
1029
- # Float
1030
- assert_to_yaml( 10.5...30.3, <<EOY )
1031
- --- !ruby/range 10.5...30.3
1032
- EOY
1033
-
1034
- end
1035
-
1036
- def test_ruby_struct
1037
- # Ruby structures
1038
- book_struct = Struct::new( "MyBookStruct", :author, :title, :year, :isbn )
1039
- assert_to_yaml(
1040
- [ book_struct.new( "Yukihiro Matsumoto", "Ruby in a Nutshell", 2002, "0-596-00214-9" ),
1041
- book_struct.new( [ 'Dave Thomas', 'Andy Hunt' ], "The Pickaxe", 2002,
1042
- book_struct.new( "This should be the ISBN", "but I have another struct here", 2002, "None" )
1043
- ) ], <<EOY
1044
- - !ruby/struct:MyBookStruct
1045
- author: Yukihiro Matsumoto
1046
- title: Ruby in a Nutshell
1047
- year: 2002
1048
- isbn: 0-596-00214-9
1049
- - !ruby/struct:MyBookStruct
1050
- author:
1051
- - Dave Thomas
1052
- - Andy Hunt
1053
- title: The Pickaxe
1054
- year: 2002
1055
- isbn: !ruby/struct:MyBookStruct
1056
- author: This should be the ISBN
1057
- title: but I have another struct here
1058
- year: 2002
1059
- isbn: None
1060
- EOY
1061
- )
1062
-
1063
- assert_to_yaml( Psych_Tests::StructTest.new( 123 ), <<EOY )
1064
- --- !ruby/struct:Psych_Tests::StructTest
1065
- c: 123
1066
- EOY
1067
-
1068
- end
1069
-
1070
- def test_ruby_rational
1071
- assert_to_yaml( Rational(1, 2), <<EOY )
1072
- --- !ruby/object:Rational
1073
- numerator: 1
1074
- denominator: 2
1075
- EOY
1076
-
1077
- # Read Psych dumped by the ruby 1.8.3.
1078
- assert_to_yaml( Rational(1, 2), "!ruby/object:Rational 1/2\n" )
1079
- assert_raises( ArgumentError ) { Psych.load("!ruby/object:Rational INVALID/RATIONAL\n") }
1080
- end
1081
-
1082
- def test_ruby_complex
1083
- assert_to_yaml( Complex(3, 4), <<EOY )
1084
- --- !ruby/object:Complex
1085
- image: 4
1086
- real: 3
1087
- EOY
1088
-
1089
- # Read Psych dumped by the ruby 1.8.3.
1090
- assert_to_yaml( Complex(3, 4), "!ruby/object:Complex 3+4i\n" )
1091
- assert_raises( ArgumentError ) { Psych.load("!ruby/object:Complex INVALID+COMPLEXi\n") }
1092
- end
1093
-
1094
- def test_emitting_indicators
1095
- assert_to_yaml( "Hi, from Object 1. You passed: please, pretty please", <<EOY
1096
- --- "Hi, from Object 1. You passed: please, pretty please"
1097
- EOY
1098
- )
1099
- end
1100
-
1101
- ##
1102
- ## Test the Psych::Stream class -- INACTIVE at the moment
1103
- ##
1104
- #def test_document
1105
- # y = Psych::Stream.new( :Indent => 2, :UseVersion => 0 )
1106
- # y.add(
1107
- # { 'hi' => 'hello', 'map' =>
1108
- # { 'good' => 'two' },
1109
- # 'time' => Time.now,
1110
- # 'try' => /^po(.*)$/,
1111
- # 'bye' => 'goodbye'
1112
- # }
1113
- # )
1114
- # y.add( { 'po' => 'nil', 'oper' => 90 } )
1115
- # y.add( { 'hi' => 'wow!', 'bye' => 'wow!' } )
1116
- # y.add( { [ 'Red Socks', 'Boston' ] => [ 'One', 'Two', 'Three' ] } )
1117
- # y.add( [ true, false, false ] )
1118
- #end
1119
-
1120
- #
1121
- # Test YPath choices parsing
1122
- #
1123
- #def test_ypath_parsing
1124
- # assert_path_segments( "/*/((one|three)/name|place)|//place",
1125
- # [ ["*", "one", "name"],
1126
- # ["*", "three", "name"],
1127
- # ["*", "place"],
1128
- # ["/", "place"] ]
1129
- # )
1130
- #end
1131
-
1132
- #
1133
- # Tests from Tanaka Akira on [ruby-core]
1134
- #
1135
- def test_akira
1136
-
1137
- # Commas in plain scalars [ruby-core:1066]
1138
- assert_to_yaml(
1139
- {"A"=>"A,","B"=>"B"}, <<EOY
1140
- A: "A,"
1141
- B: B
1142
- EOY
1143
- )
1144
-
1145
- # Double-quoted keys [ruby-core:1069]
1146
- assert_to_yaml(
1147
- {"1"=>2, "2"=>3}, <<EOY
1148
- '1': 2
1149
- "2": 3
1150
- EOY
1151
- )
1152
-
1153
- # Anchored mapping [ruby-core:1071]
1154
- assert_to_yaml(
1155
- [{"a"=>"b"}] * 2, <<EOY
1156
- - &id001
1157
- a: b
1158
- - *id001
1159
- EOY
1160
- )
1161
-
1162
- # Stress test [ruby-core:1071]
1163
- # a = []; 1000.times { a << {"a"=>"b", "c"=>"d"} }
1164
- # Psych::load( a.to_yaml )
1165
-
1166
- end
1167
-
1168
- #
1169
- # Test Time.now cycle
1170
- #
1171
- def test_time_now_cycle
1172
- #
1173
- # From Minero Aoki [ruby-core:2305]
1174
- #
1175
- #require 'yaml'
1176
- t = Time.now
1177
- t = Time.at(t.tv_sec, t.tv_usec)
1178
- 5.times do
1179
- assert_cycle(t)
1180
- end
1181
- end
1182
-
1183
- #
1184
- # Test Range cycle
1185
- #
1186
- def test_range_cycle
1187
- #
1188
- # From Minero Aoki [ruby-core:02306]
1189
- #
1190
- assert_cycle("a".."z")
1191
-
1192
- #
1193
- # From Nobu Nakada [ruby-core:02311]
1194
- #
1195
- assert_cycle(0..1)
1196
- assert_cycle(1.0e20 .. 2.0e20)
1197
- assert_cycle("0".."1")
1198
- assert_cycle(".."..."...")
1199
- assert_cycle(".rb"..".pl")
1200
- assert_cycle(".rb"...".pl")
1201
- assert_cycle('"'...".")
1202
- assert_cycle("'"...".")
1203
- end
1204
-
1205
- #
1206
- # Circular references
1207
- #
1208
- def test_circular_references
1209
- a = []; a[0] = a; a[1] = a
1210
- inspect_str = "[[...], [...]]"
1211
- assert_equal( inspect_str, Psych::load(Psych.dump(a)).inspect )
1212
- end
1213
-
1214
- #
1215
- # Test Symbol cycle
1216
- #
1217
- def test_symbol_cycle
1218
- #
1219
- # From Aaron Schrab [ruby-Bugs:2535]
1220
- #
1221
- assert_cycle(:"^foo")
1222
- end
1223
-
1224
- #
1225
- # Test Numeric cycle
1226
- #
1227
- class NumericTest < Numeric
1228
- def initialize(value)
1229
- @value = value
1230
- end
1231
- def ==(other)
1232
- @value == other.instance_eval{ @value }
1233
- end
1234
- end
1235
- def test_numeric_cycle
1236
- assert_cycle(1) # Fixnum
1237
- assert_cycle(111111111111111111111111111111111) # Bignum
1238
- assert_cycle(NumericTest.new(3)) # Subclass of Numeric
1239
- end
1240
-
1241
- #
1242
- # Test empty map/seq in map cycle
1243
- #
1244
- def test_empty_map_key
1245
- #
1246
- # empty seq as key
1247
- #
1248
- assert_cycle({[]=>""})
1249
-
1250
- #
1251
- # empty map as key
1252
- #
1253
- assert_cycle({{}=>""})
1254
- end
1255
-
1256
- #
1257
- # contributed by riley lynch [ruby-Bugs-8548]
1258
- #
1259
- def test_object_id_collision
1260
- omap = Psych::Omap.new
1261
- 1000.times { |i| omap["key_#{i}"] = { "value" => i } }
1262
- raise "id collision in ordered map" if Psych.dump(omap) =~ /id\d+/
1263
- end
1264
-
1265
- def test_date_out_of_range
1266
- Psych::load('1900-01-01T00:00:00+00:00')
1267
- end
1268
-
1269
- def test_normal_exit
1270
- Psych.load("2000-01-01 00:00:00.#{"0"*1000} +00:00\n")
1271
- # '[ruby-core:13735]'
1272
- end
1273
-
1274
- def test_multiline_string_uses_literal_style
1275
- yaml = Psych.dump("multi\nline\nstring")
1276
- assert_match("|", yaml)
1277
- end
1278
-
1279
- def test_string_starting_with_non_word_character_uses_double_quotes_without_exclamation_mark
1280
- yaml = Psych.dump("@123'abc")
1281
- refute_match("!", yaml)
1282
- end
1283
-
1284
- def test_string_dump_with_colon
1285
- yaml = Psych.dump 'x: foo'
1286
- refute_match '!', yaml
1287
- end
1288
-
1289
- def test_string_dump_starting_with_star
1290
- yaml = Psych.dump '*foo'
1291
- refute_match '!', yaml
1292
- end
1293
- end