psych 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. data/.autotest +18 -0
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.rdoc +3 -0
  4. data/Manifest.txt +87 -0
  5. data/README.rdoc +50 -0
  6. data/Rakefile +66 -0
  7. data/ext/psych/emitter.c +517 -0
  8. data/ext/psych/emitter.h +8 -0
  9. data/ext/psych/extconf.rb +22 -0
  10. data/ext/psych/parser.c +384 -0
  11. data/ext/psych/parser.h +6 -0
  12. data/ext/psych/psych.c +34 -0
  13. data/ext/psych/psych.h +20 -0
  14. data/ext/psych/to_ruby.c +41 -0
  15. data/ext/psych/to_ruby.h +8 -0
  16. data/ext/psych/yaml_tree.c +24 -0
  17. data/ext/psych/yaml_tree.h +8 -0
  18. data/lib/psych.rb +263 -0
  19. data/lib/psych/coder.rb +94 -0
  20. data/lib/psych/core_ext.rb +39 -0
  21. data/lib/psych/deprecated.rb +82 -0
  22. data/lib/psych/handler.rb +221 -0
  23. data/lib/psych/json.rb +6 -0
  24. data/lib/psych/json/ruby_events.rb +19 -0
  25. data/lib/psych/json/stream.rb +15 -0
  26. data/lib/psych/json/tree_builder.rb +12 -0
  27. data/lib/psych/json/yaml_events.rb +29 -0
  28. data/lib/psych/nodes.rb +77 -0
  29. data/lib/psych/nodes/alias.rb +18 -0
  30. data/lib/psych/nodes/document.rb +60 -0
  31. data/lib/psych/nodes/mapping.rb +56 -0
  32. data/lib/psych/nodes/node.rb +52 -0
  33. data/lib/psych/nodes/scalar.rb +67 -0
  34. data/lib/psych/nodes/sequence.rb +81 -0
  35. data/lib/psych/nodes/stream.rb +37 -0
  36. data/lib/psych/omap.rb +4 -0
  37. data/lib/psych/parser.rb +47 -0
  38. data/lib/psych/scalar_scanner.rb +105 -0
  39. data/lib/psych/set.rb +4 -0
  40. data/lib/psych/stream.rb +36 -0
  41. data/lib/psych/streaming.rb +22 -0
  42. data/lib/psych/tree_builder.rb +94 -0
  43. data/lib/psych/visitors.rb +6 -0
  44. data/lib/psych/visitors/depth_first.rb +26 -0
  45. data/lib/psych/visitors/emitter.rb +44 -0
  46. data/lib/psych/visitors/json_tree.rb +21 -0
  47. data/lib/psych/visitors/to_ruby.rb +267 -0
  48. data/lib/psych/visitors/visitor.rb +19 -0
  49. data/lib/psych/visitors/yaml_tree.rb +373 -0
  50. data/test/psych/helper.rb +63 -0
  51. data/test/psych/json/test_stream.rb +109 -0
  52. data/test/psych/nodes/test_enumerable.rb +43 -0
  53. data/test/psych/test_alias_and_anchor.rb +26 -0
  54. data/test/psych/test_array.rb +19 -0
  55. data/test/psych/test_boolean.rb +36 -0
  56. data/test/psych/test_class.rb +17 -0
  57. data/test/psych/test_coder.rb +184 -0
  58. data/test/psych/test_date_time.rb +17 -0
  59. data/test/psych/test_deprecated.rb +210 -0
  60. data/test/psych/test_document.rb +46 -0
  61. data/test/psych/test_emitter.rb +94 -0
  62. data/test/psych/test_encoding.rb +179 -0
  63. data/test/psych/test_engine_manager.rb +57 -0
  64. data/test/psych/test_exception.rb +39 -0
  65. data/test/psych/test_hash.rb +30 -0
  66. data/test/psych/test_json_tree.rb +65 -0
  67. data/test/psych/test_merge_keys.rb +72 -0
  68. data/test/psych/test_nil.rb +18 -0
  69. data/test/psych/test_null.rb +19 -0
  70. data/test/psych/test_object.rb +27 -0
  71. data/test/psych/test_omap.rb +68 -0
  72. data/test/psych/test_parser.rb +297 -0
  73. data/test/psych/test_psych.rb +168 -0
  74. data/test/psych/test_scalar.rb +11 -0
  75. data/test/psych/test_scalar_scanner.rb +69 -0
  76. data/test/psych/test_serialize_subclasses.rb +38 -0
  77. data/test/psych/test_set.rb +49 -0
  78. data/test/psych/test_stream.rb +49 -0
  79. data/test/psych/test_string.rb +49 -0
  80. data/test/psych/test_struct.rb +51 -0
  81. data/test/psych/test_symbol.rb +17 -0
  82. data/test/psych/test_to_yaml_properties.rb +63 -0
  83. data/test/psych/test_tree_builder.rb +79 -0
  84. data/test/psych/test_yaml.rb +1256 -0
  85. data/test/psych/visitors/test_depth_first.rb +49 -0
  86. data/test/psych/visitors/test_emitter.rb +144 -0
  87. data/test/psych/visitors/test_to_ruby.rb +325 -0
  88. data/test/psych/visitors/test_yaml_tree.rb +155 -0
  89. metadata +232 -0
@@ -0,0 +1,49 @@
1
+ require 'psych/helper'
2
+
3
+ module Psych
4
+ module Visitors
5
+ class TestDepthFirst < TestCase
6
+ class Collector < Struct.new(:calls)
7
+ def initialize(calls = [])
8
+ super
9
+ end
10
+
11
+ def call obj
12
+ calls << obj
13
+ end
14
+ end
15
+
16
+ def test_scalar
17
+ collector = Collector.new
18
+ visitor = Visitors::DepthFirst.new collector
19
+ visitor.accept Psych.parse_stream '--- hello'
20
+
21
+ assert_equal 3, collector.calls.length
22
+ end
23
+
24
+ def test_sequence
25
+ collector = Collector.new
26
+ visitor = Visitors::DepthFirst.new collector
27
+ visitor.accept Psych.parse_stream "---\n- hello"
28
+
29
+ assert_equal 4, collector.calls.length
30
+ end
31
+
32
+ def test_mapping
33
+ collector = Collector.new
34
+ visitor = Visitors::DepthFirst.new collector
35
+ visitor.accept Psych.parse_stream "---\nhello: world"
36
+
37
+ assert_equal 5, collector.calls.length
38
+ end
39
+
40
+ def test_alias
41
+ collector = Collector.new
42
+ visitor = Visitors::DepthFirst.new collector
43
+ visitor.accept Psych.parse_stream "--- &yay\n- foo\n- *yay\n"
44
+
45
+ assert_equal 5, collector.calls.length
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,144 @@
1
+ require 'psych/helper'
2
+
3
+ module Psych
4
+ module Visitors
5
+ class TestEmitter < TestCase
6
+ def setup
7
+ super
8
+ @io = StringIO.new
9
+ @visitor = Visitors::Emitter.new @io
10
+ end
11
+
12
+ def test_options
13
+ io = StringIO.new
14
+ visitor = Visitors::Emitter.new io, :indentation => 3
15
+
16
+ s = Nodes::Stream.new
17
+ doc = Nodes::Document.new
18
+ mapping = Nodes::Mapping.new
19
+ m2 = Nodes::Mapping.new
20
+ m2.children << Nodes::Scalar.new('a')
21
+ m2.children << Nodes::Scalar.new('b')
22
+
23
+ mapping.children << Nodes::Scalar.new('key')
24
+ mapping.children << m2
25
+ doc.children << mapping
26
+ s.children << doc
27
+
28
+ visitor.accept s
29
+ assert_match(/^[ ]{3}a/, io.string)
30
+ end
31
+
32
+ def test_stream
33
+ s = Nodes::Stream.new
34
+ @visitor.accept s
35
+ assert_equal '', @io.string
36
+ end
37
+
38
+ def test_document
39
+ s = Nodes::Stream.new
40
+ doc = Nodes::Document.new [1,1]
41
+ scalar = Nodes::Scalar.new 'hello world'
42
+
43
+ doc.children << scalar
44
+ s.children << doc
45
+
46
+ @visitor.accept s
47
+
48
+ assert_match(/1.1/, @io.string)
49
+ assert_equal @io.string, s.to_yaml
50
+ end
51
+
52
+ def test_document_implicit_end
53
+ s = Nodes::Stream.new
54
+ doc = Nodes::Document.new
55
+ mapping = Nodes::Mapping.new
56
+ mapping.children << Nodes::Scalar.new('key')
57
+ mapping.children << Nodes::Scalar.new('value')
58
+ doc.children << mapping
59
+ s.children << doc
60
+
61
+ @visitor.accept s
62
+
63
+ assert_match(/key: value/, @io.string)
64
+ assert_equal @io.string, s.to_yaml
65
+ assert(/\.\.\./ !~ s.to_yaml)
66
+ end
67
+
68
+ def test_scalar
69
+ s = Nodes::Stream.new
70
+ doc = Nodes::Document.new
71
+ scalar = Nodes::Scalar.new 'hello world'
72
+
73
+ doc.children << scalar
74
+ s.children << doc
75
+
76
+ @visitor.accept s
77
+
78
+ assert_match(/hello/, @io.string)
79
+ assert_equal @io.string, s.to_yaml
80
+ end
81
+
82
+ def test_scalar_with_tag
83
+ s = Nodes::Stream.new
84
+ doc = Nodes::Document.new
85
+ scalar = Nodes::Scalar.new 'hello world', nil, '!str', false, false, 5
86
+
87
+ doc.children << scalar
88
+ s.children << doc
89
+
90
+ @visitor.accept s
91
+
92
+ assert_match(/str/, @io.string)
93
+ assert_match(/hello/, @io.string)
94
+ assert_equal @io.string, s.to_yaml
95
+ end
96
+
97
+ def test_sequence
98
+ s = Nodes::Stream.new
99
+ doc = Nodes::Document.new
100
+ scalar = Nodes::Scalar.new 'hello world'
101
+ seq = Nodes::Sequence.new
102
+
103
+ seq.children << scalar
104
+ doc.children << seq
105
+ s.children << doc
106
+
107
+ @visitor.accept s
108
+
109
+ assert_match(/- hello/, @io.string)
110
+ assert_equal @io.string, s.to_yaml
111
+ end
112
+
113
+ def test_mapping
114
+ s = Nodes::Stream.new
115
+ doc = Nodes::Document.new
116
+ mapping = Nodes::Mapping.new
117
+ mapping.children << Nodes::Scalar.new('key')
118
+ mapping.children << Nodes::Scalar.new('value')
119
+ doc.children << mapping
120
+ s.children << doc
121
+
122
+ @visitor.accept s
123
+
124
+ assert_match(/key: value/, @io.string)
125
+ assert_equal @io.string, s.to_yaml
126
+ end
127
+
128
+ def test_alias
129
+ s = Nodes::Stream.new
130
+ doc = Nodes::Document.new
131
+ mapping = Nodes::Mapping.new
132
+ mapping.children << Nodes::Scalar.new('key', 'A')
133
+ mapping.children << Nodes::Alias.new('A')
134
+ doc.children << mapping
135
+ s.children << doc
136
+
137
+ @visitor.accept s
138
+
139
+ assert_match(/&A key: \*A/, @io.string)
140
+ assert_equal @io.string, s.to_yaml
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,325 @@
1
+ require 'psych/helper'
2
+
3
+ module Psych
4
+ module Visitors
5
+ class TestToRuby < TestCase
6
+ def setup
7
+ super
8
+ @visitor = ToRuby.new
9
+ end
10
+
11
+ def test_object
12
+ mapping = Nodes::Mapping.new nil, "!ruby/object"
13
+ mapping.children << Nodes::Scalar.new('foo')
14
+ mapping.children << Nodes::Scalar.new('bar')
15
+
16
+ o = mapping.to_ruby
17
+ assert_equal 'bar', o.instance_variable_get(:@foo)
18
+ end
19
+
20
+ def test_awesome
21
+ Psych.load('1900-01-01T00:00:00+00:00')
22
+ end
23
+
24
+ def test_legacy_struct
25
+ foo = Struct.new('AWESOME', :bar)
26
+ assert_equal foo.new('baz'), Psych.load(<<-eoyml)
27
+ !ruby/struct:AWESOME
28
+ bar: baz
29
+ eoyml
30
+ end
31
+
32
+ def test_binary
33
+ gif = "GIF89a\f\x00\f\x00\x84\x00\x00\xFF\xFF\xF7\xF5\xF5\xEE\xE9\xE9\xE5fff\x00\x00\x00\xE7\xE7\xE7^^^\xF3\xF3\xED\x8E\x8E\x8E\xE0\xE0\xE0\x9F\x9F\x9F\x93\x93\x93\xA7\xA7\xA7\x9E\x9E\x9Eiiiccc\xA3\xA3\xA3\x84\x84\x84\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9!\xFE\x0EMade with GIMP\x00,\x00\x00\x00\x00\f\x00\f\x00\x00\x05, \x8E\x810\x9E\xE3@\x14\xE8i\x10\xC4\xD1\x8A\b\x1C\xCF\x80M$z\xEF\xFF0\x85p\xB8\xB01f\r\e\xCE\x01\xC3\x01\x1E\x10' \x82\n\x01\x00;"
34
+
35
+ hash = Psych.load(<<-'eoyaml')
36
+ canonical: !!binary "\
37
+ R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\
38
+ OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\
39
+ +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\
40
+ AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="
41
+ generic: !binary |
42
+ R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
43
+ OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
44
+ +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
45
+ AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
46
+ description:
47
+ The binary value above is a tiny arrow encoded as a gif image.
48
+ eoyaml
49
+ assert_equal gif, hash['canonical']
50
+ assert_equal gif, hash['generic']
51
+ end
52
+
53
+ A = Struct.new(:foo)
54
+
55
+ def test_struct
56
+ s = A.new('bar')
57
+
58
+ mapping = Nodes::Mapping.new nil, "!ruby/struct:#{s.class}"
59
+ mapping.children << Nodes::Scalar.new('foo')
60
+ mapping.children << Nodes::Scalar.new('bar')
61
+
62
+ ruby = mapping.to_ruby
63
+
64
+ assert_equal s.class, ruby.class
65
+ assert_equal s.foo, ruby.foo
66
+ assert_equal s, ruby
67
+ end
68
+
69
+ def test_anon_struct_legacy
70
+ s = Struct.new(:foo).new('bar')
71
+
72
+ mapping = Nodes::Mapping.new nil, '!ruby/struct:'
73
+ mapping.children << Nodes::Scalar.new('foo')
74
+ mapping.children << Nodes::Scalar.new('bar')
75
+
76
+ assert_equal s.foo, mapping.to_ruby.foo
77
+ end
78
+
79
+ def test_anon_struct
80
+ s = Struct.new(:foo).new('bar')
81
+
82
+ mapping = Nodes::Mapping.new nil, '!ruby/struct'
83
+ mapping.children << Nodes::Scalar.new('foo')
84
+ mapping.children << Nodes::Scalar.new('bar')
85
+
86
+ assert_equal s.foo, mapping.to_ruby.foo
87
+ end
88
+
89
+ def test_exception
90
+ exc = Exception.new 'hello'
91
+
92
+ mapping = Nodes::Mapping.new nil, '!ruby/exception'
93
+ mapping.children << Nodes::Scalar.new('message')
94
+ mapping.children << Nodes::Scalar.new('hello')
95
+
96
+ ruby = mapping.to_ruby
97
+
98
+ assert_equal exc.class, ruby.class
99
+ assert_equal exc.message, ruby.message
100
+ end
101
+
102
+ def test_regexp
103
+ node = Nodes::Scalar.new('/foo/', nil, '!ruby/regexp')
104
+ assert_equal(/foo/, node.to_ruby)
105
+
106
+ node = Nodes::Scalar.new('/foo/m', nil, '!ruby/regexp')
107
+ assert_equal(/foo/m, node.to_ruby)
108
+
109
+ node = Nodes::Scalar.new('/foo/ix', nil, '!ruby/regexp')
110
+ assert_equal(/foo/ix, node.to_ruby)
111
+ end
112
+
113
+ def test_time
114
+ now = Time.now
115
+ zone = now.strftime('%z')
116
+ zone = " #{zone[0,3]}:#{zone[3,5]}"
117
+
118
+ formatted = now.strftime("%Y-%m-%d %H:%M:%S.%9N") + zone
119
+
120
+ assert_equal now, Nodes::Scalar.new(formatted).to_ruby
121
+ end
122
+
123
+ def test_time_utc
124
+ now = Time.now.utc
125
+ formatted = now.strftime("%Y-%m-%d %H:%M:%S") +
126
+ ".%09dZ" % [now.nsec]
127
+
128
+ assert_equal now, Nodes::Scalar.new(formatted).to_ruby
129
+ end
130
+
131
+ def test_time_utc_no_z
132
+ now = Time.now.utc
133
+ formatted = now.strftime("%Y-%m-%d %H:%M:%S") +
134
+ ".%09d" % [now.nsec]
135
+
136
+ assert_equal now, Nodes::Scalar.new(formatted).to_ruby
137
+ end
138
+
139
+ def test_date
140
+ d = '1980-12-16'
141
+ actual = Date.strptime(d, '%Y-%m-%d')
142
+
143
+ date = Nodes::Scalar.new(d, nil, 'tag:yaml.org,2002:timestamp', false)
144
+
145
+ assert_equal actual, date.to_ruby
146
+ end
147
+
148
+ def test_rational
149
+ mapping = Nodes::Mapping.new nil, '!ruby/object:Rational'
150
+ mapping.children << Nodes::Scalar.new('denominator')
151
+ mapping.children << Nodes::Scalar.new('2')
152
+ mapping.children << Nodes::Scalar.new('numerator')
153
+ mapping.children << Nodes::Scalar.new('1')
154
+
155
+ assert_equal Rational(1,2), mapping.to_ruby
156
+ end
157
+
158
+ def test_complex
159
+ mapping = Nodes::Mapping.new nil, '!ruby/object:Complex'
160
+ mapping.children << Nodes::Scalar.new('image')
161
+ mapping.children << Nodes::Scalar.new('2')
162
+ mapping.children << Nodes::Scalar.new('real')
163
+ mapping.children << Nodes::Scalar.new('1')
164
+
165
+ assert_equal Complex(1,2), mapping.to_ruby
166
+ end
167
+
168
+ if RUBY_VERSION >= '1.9'
169
+ def test_complex_string
170
+ node = Nodes::Scalar.new '3+4i', nil, "!ruby/object:Complex"
171
+ assert_equal Complex(3, 4), node.to_ruby
172
+ end
173
+
174
+ def test_rational_string
175
+ node = Nodes::Scalar.new '1/2', nil, "!ruby/object:Rational"
176
+ assert_equal Rational(1, 2), node.to_ruby
177
+ end
178
+ end
179
+
180
+ def test_range_string
181
+ node = Nodes::Scalar.new '1..2', nil, "!ruby/range"
182
+ assert_equal 1..2, node.to_ruby
183
+ end
184
+
185
+ def test_range_string_triple
186
+ node = Nodes::Scalar.new '1...3', nil, "!ruby/range"
187
+ assert_equal 1...3, node.to_ruby
188
+ end
189
+
190
+ def test_integer
191
+ i = Nodes::Scalar.new('1', nil, 'tag:yaml.org,2002:int')
192
+ assert_equal 1, i.to_ruby
193
+
194
+ assert_equal 1, Nodes::Scalar.new('1').to_ruby
195
+
196
+ i = Nodes::Scalar.new('-1', nil, 'tag:yaml.org,2002:int')
197
+ assert_equal(-1, i.to_ruby)
198
+
199
+ assert_equal(-1, Nodes::Scalar.new('-1').to_ruby)
200
+ assert_equal 1, Nodes::Scalar.new('+1').to_ruby
201
+ end
202
+
203
+ def test_int_ignore
204
+ ['1,000', '1_000'].each do |num|
205
+ i = Nodes::Scalar.new(num, nil, 'tag:yaml.org,2002:int')
206
+ assert_equal 1000, i.to_ruby
207
+
208
+ assert_equal 1000, Nodes::Scalar.new(num).to_ruby
209
+ end
210
+ end
211
+
212
+ def test_float_ignore
213
+ ['1,000.3', '1_000.3'].each do |num|
214
+ i = Nodes::Scalar.new(num, nil, 'tag:yaml.org,2002:float')
215
+ assert_equal 1000.3, i.to_ruby
216
+
217
+ i = Nodes::Scalar.new(num, nil, '!float')
218
+ assert_equal 1000.3, i.to_ruby
219
+
220
+ assert_equal 1000.3, Nodes::Scalar.new(num).to_ruby
221
+ end
222
+ end
223
+
224
+ # http://yaml.org/type/bool.html
225
+ def test_boolean_true
226
+ %w{ yes Yes YES true True TRUE on On ON }.each do |t|
227
+ i = Nodes::Scalar.new(t, nil, 'tag:yaml.org,2002:bool')
228
+ assert_equal true, i.to_ruby
229
+ assert_equal true, Nodes::Scalar.new(t).to_ruby
230
+ end
231
+ end
232
+
233
+ # http://yaml.org/type/bool.html
234
+ def test_boolean_false
235
+ %w{ no No NO false False FALSE off Off OFF }.each do |t|
236
+ i = Nodes::Scalar.new(t, nil, 'tag:yaml.org,2002:bool')
237
+ assert_equal false, i.to_ruby
238
+ assert_equal false, Nodes::Scalar.new(t).to_ruby
239
+ end
240
+ end
241
+
242
+ def test_float
243
+ i = Nodes::Scalar.new('12', nil, 'tag:yaml.org,2002:float')
244
+ assert_equal 12.0, i.to_ruby
245
+
246
+ i = Nodes::Scalar.new('1.2', nil, 'tag:yaml.org,2002:float')
247
+ assert_equal 1.2, i.to_ruby
248
+
249
+ i = Nodes::Scalar.new('1.2')
250
+ assert_equal 1.2, i.to_ruby
251
+
252
+ assert_equal 1, Nodes::Scalar.new('.Inf').to_ruby.infinite?
253
+ assert_equal 1, Nodes::Scalar.new('.inf').to_ruby.infinite?
254
+ assert_equal 1, Nodes::Scalar.new('.Inf', nil, 'tag:yaml.org,2002:float').to_ruby.infinite?
255
+
256
+ assert_equal(-1, Nodes::Scalar.new('-.inf').to_ruby.infinite?)
257
+ assert_equal(-1, Nodes::Scalar.new('-.Inf').to_ruby.infinite?)
258
+ assert_equal(-1, Nodes::Scalar.new('-.Inf', nil, 'tag:yaml.org,2002:float').to_ruby.infinite?)
259
+
260
+ assert Nodes::Scalar.new('.NaN').to_ruby.nan?
261
+ assert Nodes::Scalar.new('.NaN', nil, 'tag:yaml.org,2002:float').to_ruby.nan?
262
+ end
263
+
264
+ def test_exp_float
265
+ exp = 1.2e+30
266
+
267
+ i = Nodes::Scalar.new(exp.to_s, nil, 'tag:yaml.org,2002:float')
268
+ assert_equal exp, i.to_ruby
269
+
270
+ assert_equal exp, Nodes::Scalar.new(exp.to_s).to_ruby
271
+ end
272
+
273
+ def test_scalar
274
+ scalar = Nodes::Scalar.new('foo')
275
+ assert_equal 'foo', @visitor.accept(scalar)
276
+ assert_equal 'foo', scalar.to_ruby
277
+ end
278
+
279
+ def test_sequence
280
+ seq = Nodes::Sequence.new
281
+ seq.children << Nodes::Scalar.new('foo')
282
+ seq.children << Nodes::Scalar.new('bar')
283
+
284
+ assert_equal %w{ foo bar }, seq.to_ruby
285
+ end
286
+
287
+ def test_mapping
288
+ mapping = Nodes::Mapping.new
289
+ mapping.children << Nodes::Scalar.new('foo')
290
+ mapping.children << Nodes::Scalar.new('bar')
291
+ assert_equal({'foo' => 'bar'}, mapping.to_ruby)
292
+ end
293
+
294
+ def test_document
295
+ doc = Nodes::Document.new
296
+ doc.children << Nodes::Scalar.new('foo')
297
+ assert_equal 'foo', doc.to_ruby
298
+ end
299
+
300
+ def test_stream
301
+ a = Nodes::Document.new
302
+ a.children << Nodes::Scalar.new('foo')
303
+
304
+ b = Nodes::Document.new
305
+ b.children << Nodes::Scalar.new('bar')
306
+
307
+ stream = Nodes::Stream.new
308
+ stream.children << a
309
+ stream.children << b
310
+
311
+ assert_equal %w{ foo bar }, stream.to_ruby
312
+ end
313
+
314
+ def test_alias
315
+ seq = Nodes::Sequence.new
316
+ seq.children << Nodes::Scalar.new('foo', 'A')
317
+ seq.children << Nodes::Alias.new('A')
318
+
319
+ list = seq.to_ruby
320
+ assert_equal %w{ foo foo }, list
321
+ assert_equal list[0].object_id, list[1].object_id
322
+ end
323
+ end
324
+ end
325
+ end