psych-shopifork 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.autotest +18 -0
- data/.gemtest +0 -0
- data/.travis.yml +9 -0
- data/CHANGELOG.rdoc +414 -0
- data/Manifest.txt +113 -0
- data/README.rdoc +71 -0
- data/Rakefile +72 -0
- data/ext/psych/depend +3 -0
- data/ext/psych/extconf.rb +36 -0
- data/ext/psych/psych.c +34 -0
- data/ext/psych/psych.h +20 -0
- data/ext/psych/psych_emitter.c +538 -0
- data/ext/psych/psych_emitter.h +8 -0
- data/ext/psych/psych_parser.c +579 -0
- data/ext/psych/psych_parser.h +6 -0
- data/ext/psych/psych_to_ruby.c +43 -0
- data/ext/psych/psych_to_ruby.h +8 -0
- data/ext/psych/psych_yaml_tree.c +24 -0
- data/ext/psych/psych_yaml_tree.h +8 -0
- data/ext/psych/yaml/LICENSE +19 -0
- data/ext/psych/yaml/api.c +1392 -0
- data/ext/psych/yaml/config.h +11 -0
- data/ext/psych/yaml/dumper.c +394 -0
- data/ext/psych/yaml/emitter.c +2335 -0
- data/ext/psych/yaml/loader.c +432 -0
- data/ext/psych/yaml/parser.c +1374 -0
- data/ext/psych/yaml/reader.c +465 -0
- data/ext/psych/yaml/scanner.c +3570 -0
- data/ext/psych/yaml/writer.c +141 -0
- data/ext/psych/yaml/yaml.h +1971 -0
- data/ext/psych/yaml/yaml_private.h +643 -0
- data/lib/psych.rb +497 -0
- data/lib/psych/class_loader.rb +101 -0
- data/lib/psych/coder.rb +94 -0
- data/lib/psych/core_ext.rb +35 -0
- data/lib/psych/deprecated.rb +85 -0
- data/lib/psych/exception.rb +13 -0
- data/lib/psych/handler.rb +249 -0
- data/lib/psych/handlers/document_stream.rb +22 -0
- data/lib/psych/handlers/recorder.rb +39 -0
- data/lib/psych/json/ruby_events.rb +19 -0
- data/lib/psych/json/stream.rb +16 -0
- data/lib/psych/json/tree_builder.rb +12 -0
- data/lib/psych/json/yaml_events.rb +29 -0
- data/lib/psych/nodes.rb +77 -0
- data/lib/psych/nodes/alias.rb +18 -0
- data/lib/psych/nodes/document.rb +60 -0
- data/lib/psych/nodes/mapping.rb +56 -0
- data/lib/psych/nodes/node.rb +55 -0
- data/lib/psych/nodes/scalar.rb +67 -0
- data/lib/psych/nodes/sequence.rb +81 -0
- data/lib/psych/nodes/stream.rb +37 -0
- data/lib/psych/omap.rb +4 -0
- data/lib/psych/parser.rb +51 -0
- data/lib/psych/scalar_scanner.rb +149 -0
- data/lib/psych/set.rb +4 -0
- data/lib/psych/stream.rb +37 -0
- data/lib/psych/streaming.rb +27 -0
- data/lib/psych/syntax_error.rb +21 -0
- data/lib/psych/tree_builder.rb +96 -0
- data/lib/psych/visitors.rb +6 -0
- data/lib/psych/visitors/depth_first.rb +26 -0
- data/lib/psych/visitors/emitter.rb +51 -0
- data/lib/psych/visitors/json_tree.rb +24 -0
- data/lib/psych/visitors/to_ruby.rb +372 -0
- data/lib/psych/visitors/visitor.rb +19 -0
- data/lib/psych/visitors/yaml_tree.rb +507 -0
- data/lib/psych/y.rb +9 -0
- data/test/psych/handlers/test_recorder.rb +25 -0
- data/test/psych/helper.rb +114 -0
- data/test/psych/json/test_stream.rb +109 -0
- data/test/psych/nodes/test_enumerable.rb +43 -0
- data/test/psych/test_alias_and_anchor.rb +96 -0
- data/test/psych/test_array.rb +57 -0
- data/test/psych/test_boolean.rb +36 -0
- data/test/psych/test_class.rb +36 -0
- data/test/psych/test_coder.rb +184 -0
- data/test/psych/test_date_time.rb +25 -0
- data/test/psych/test_deprecated.rb +214 -0
- data/test/psych/test_document.rb +46 -0
- data/test/psych/test_emitter.rb +94 -0
- data/test/psych/test_encoding.rb +254 -0
- data/test/psych/test_engine_manager.rb +47 -0
- data/test/psych/test_exception.rb +151 -0
- data/test/psych/test_hash.rb +44 -0
- data/test/psych/test_json_tree.rb +65 -0
- data/test/psych/test_merge_keys.rb +132 -0
- data/test/psych/test_nil.rb +18 -0
- data/test/psych/test_null.rb +19 -0
- data/test/psych/test_numeric.rb +45 -0
- data/test/psych/test_object.rb +44 -0
- data/test/psych/test_object_references.rb +67 -0
- data/test/psych/test_omap.rb +75 -0
- data/test/psych/test_parser.rb +339 -0
- data/test/psych/test_psych.rb +168 -0
- data/test/psych/test_safe_load.rb +97 -0
- data/test/psych/test_scalar.rb +11 -0
- data/test/psych/test_scalar_scanner.rb +106 -0
- data/test/psych/test_serialize_subclasses.rb +38 -0
- data/test/psych/test_set.rb +49 -0
- data/test/psych/test_stream.rb +93 -0
- data/test/psych/test_string.rb +153 -0
- data/test/psych/test_struct.rb +49 -0
- data/test/psych/test_symbol.rb +17 -0
- data/test/psych/test_tainted.rb +130 -0
- data/test/psych/test_to_yaml_properties.rb +63 -0
- data/test/psych/test_tree_builder.rb +79 -0
- data/test/psych/test_yaml.rb +1289 -0
- data/test/psych/test_yamldbm.rb +197 -0
- data/test/psych/test_yamlstore.rb +87 -0
- data/test/psych/visitors/test_depth_first.rb +49 -0
- data/test/psych/visitors/test_emitter.rb +144 -0
- data/test/psych/visitors/test_to_ruby.rb +326 -0
- data/test/psych/visitors/test_yaml_tree.rb +173 -0
- metadata +257 -0
@@ -0,0 +1,339 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require_relative 'helper'
|
4
|
+
|
5
|
+
module Psych
|
6
|
+
class TestParser < TestCase
|
7
|
+
class EventCatcher < Handler
|
8
|
+
attr_accessor :parser
|
9
|
+
attr_reader :calls, :marks
|
10
|
+
def initialize
|
11
|
+
@parser = nil
|
12
|
+
@calls = []
|
13
|
+
@marks = []
|
14
|
+
end
|
15
|
+
|
16
|
+
(Handler.instance_methods(true) -
|
17
|
+
Object.instance_methods).each do |m|
|
18
|
+
class_eval %{
|
19
|
+
def #{m} *args
|
20
|
+
super
|
21
|
+
@marks << @parser.mark if @parser
|
22
|
+
@calls << [:#{m}, args]
|
23
|
+
end
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def setup
|
29
|
+
super
|
30
|
+
@handler = EventCatcher.new
|
31
|
+
@parser = Psych::Parser.new @handler
|
32
|
+
@handler.parser = @parser
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_ast_roundtrip
|
36
|
+
parser = Psych.parser
|
37
|
+
parser.parse('null')
|
38
|
+
ast = parser.handler.root
|
39
|
+
assert_match(/^null/, ast.yaml)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_exception_memory_leak
|
43
|
+
yaml = <<-eoyaml
|
44
|
+
%YAML 1.1
|
45
|
+
%TAG ! tag:tenderlovemaking.com,2009:
|
46
|
+
--- &ponies
|
47
|
+
- first element
|
48
|
+
- *ponies
|
49
|
+
- foo: bar
|
50
|
+
...
|
51
|
+
eoyaml
|
52
|
+
|
53
|
+
[:start_stream, :start_document, :end_document, :alias, :scalar,
|
54
|
+
:start_sequence, :end_sequence, :start_mapping, :end_mapping,
|
55
|
+
:end_stream].each do |method|
|
56
|
+
|
57
|
+
klass = Class.new(Psych::Handler) do
|
58
|
+
define_method(method) do |*args|
|
59
|
+
raise
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
parser = Psych::Parser.new klass.new
|
64
|
+
2.times {
|
65
|
+
assert_raises(RuntimeError, method.to_s) do
|
66
|
+
parser.parse yaml
|
67
|
+
end
|
68
|
+
}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_multiparse
|
73
|
+
3.times do
|
74
|
+
@parser.parse '--- foo'
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_filename
|
79
|
+
ex = assert_raises(Psych::SyntaxError) do
|
80
|
+
@parser.parse '--- `', 'omg!'
|
81
|
+
end
|
82
|
+
assert_match 'omg!', ex.message
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_line_numbers
|
86
|
+
assert_equal 0, @parser.mark.line
|
87
|
+
@parser.parse "---\n- hello\n- world"
|
88
|
+
line_calls = @handler.marks.map(&:line).zip(@handler.calls.map(&:first))
|
89
|
+
assert_equal [[0, :start_stream],
|
90
|
+
[0, :start_document],
|
91
|
+
[1, :start_sequence],
|
92
|
+
[2, :scalar],
|
93
|
+
[3, :scalar],
|
94
|
+
[3, :end_sequence],
|
95
|
+
[3, :end_document],
|
96
|
+
[3, :end_stream]], line_calls
|
97
|
+
|
98
|
+
assert_equal 3, @parser.mark.line
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_column_numbers
|
102
|
+
assert_equal 0, @parser.mark.column
|
103
|
+
@parser.parse "---\n- hello\n- world"
|
104
|
+
col_calls = @handler.marks.map(&:column).zip(@handler.calls.map(&:first))
|
105
|
+
assert_equal [[0, :start_stream],
|
106
|
+
[3, :start_document],
|
107
|
+
[1, :start_sequence],
|
108
|
+
[0, :scalar],
|
109
|
+
[0, :scalar],
|
110
|
+
[0, :end_sequence],
|
111
|
+
[0, :end_document],
|
112
|
+
[0, :end_stream]], col_calls
|
113
|
+
|
114
|
+
assert_equal 0, @parser.mark.column
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_index_numbers
|
118
|
+
assert_equal 0, @parser.mark.index
|
119
|
+
@parser.parse "---\n- hello\n- world"
|
120
|
+
idx_calls = @handler.marks.map(&:index).zip(@handler.calls.map(&:first))
|
121
|
+
assert_equal [[0, :start_stream],
|
122
|
+
[3, :start_document],
|
123
|
+
[5, :start_sequence],
|
124
|
+
[12, :scalar],
|
125
|
+
[19, :scalar],
|
126
|
+
[19, :end_sequence],
|
127
|
+
[19, :end_document],
|
128
|
+
[19, :end_stream]], idx_calls
|
129
|
+
|
130
|
+
assert_equal 19, @parser.mark.index
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_bom
|
134
|
+
tadpole = 'おたまじゃくし'
|
135
|
+
|
136
|
+
# BOM + text
|
137
|
+
yml = "\uFEFF#{tadpole}".encode('UTF-16LE')
|
138
|
+
@parser.parse yml
|
139
|
+
assert_equal tadpole, @parser.handler.calls[2][1].first
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_external_encoding
|
143
|
+
tadpole = 'おたまじゃくし'
|
144
|
+
|
145
|
+
@parser.external_encoding = Psych::Parser::UTF16LE
|
146
|
+
@parser.parse tadpole.encode 'UTF-16LE'
|
147
|
+
assert_equal tadpole, @parser.handler.calls[2][1].first
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_bogus_io
|
151
|
+
o = Object.new
|
152
|
+
def o.external_encoding; nil end
|
153
|
+
def o.read len; self end
|
154
|
+
|
155
|
+
assert_raises(TypeError) do
|
156
|
+
@parser.parse o
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_parse_io
|
161
|
+
@parser.parse StringIO.new("--- a")
|
162
|
+
assert_called :start_stream
|
163
|
+
assert_called :scalar
|
164
|
+
assert_called :end_stream
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_syntax_error
|
168
|
+
assert_raises(Psych::SyntaxError) do
|
169
|
+
@parser.parse("---\n\"foo\"\n\"bar\"\n")
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_syntax_error_twice
|
174
|
+
assert_raises(Psych::SyntaxError) do
|
175
|
+
@parser.parse("---\n\"foo\"\n\"bar\"\n")
|
176
|
+
end
|
177
|
+
|
178
|
+
assert_raises(Psych::SyntaxError) do
|
179
|
+
@parser.parse("---\n\"foo\"\n\"bar\"\n")
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_syntax_error_has_path_for_string
|
184
|
+
e = assert_raises(Psych::SyntaxError) do
|
185
|
+
@parser.parse("---\n\"foo\"\n\"bar\"\n")
|
186
|
+
end
|
187
|
+
assert_match '(<unknown>):', e.message
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_syntax_error_has_path_for_io
|
191
|
+
io = StringIO.new "---\n\"foo\"\n\"bar\"\n"
|
192
|
+
def io.path; "hello!"; end
|
193
|
+
|
194
|
+
e = assert_raises(Psych::SyntaxError) do
|
195
|
+
@parser.parse(io)
|
196
|
+
end
|
197
|
+
assert_match "(#{io.path}):", e.message
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_mapping_end
|
201
|
+
@parser.parse("---\n!!map { key: value }")
|
202
|
+
assert_called :end_mapping
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_mapping_tag
|
206
|
+
@parser.parse("---\n!!map { key: value }")
|
207
|
+
assert_called :start_mapping, ["tag:yaml.org,2002:map", false, Nodes::Mapping::FLOW]
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_mapping_anchor
|
211
|
+
@parser.parse("---\n&A { key: value }")
|
212
|
+
assert_called :start_mapping, ['A', true, Nodes::Mapping::FLOW]
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_mapping_block
|
216
|
+
@parser.parse("---\n key: value")
|
217
|
+
assert_called :start_mapping, [true, Nodes::Mapping::BLOCK]
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_mapping_start
|
221
|
+
@parser.parse("---\n{ key: value }")
|
222
|
+
assert_called :start_mapping
|
223
|
+
assert_called :start_mapping, [true, Nodes::Mapping::FLOW]
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_sequence_end
|
227
|
+
@parser.parse("---\n&A [1, 2]")
|
228
|
+
assert_called :end_sequence
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_sequence_start_anchor
|
232
|
+
@parser.parse("---\n&A [1, 2]")
|
233
|
+
assert_called :start_sequence, ["A", true, Nodes::Sequence::FLOW]
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_sequence_start_tag
|
237
|
+
@parser.parse("---\n!!seq [1, 2]")
|
238
|
+
assert_called :start_sequence, ["tag:yaml.org,2002:seq", false, Nodes::Sequence::FLOW]
|
239
|
+
end
|
240
|
+
|
241
|
+
def test_sequence_start_flow
|
242
|
+
@parser.parse("---\n[1, 2]")
|
243
|
+
assert_called :start_sequence, [true, Nodes::Sequence::FLOW]
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_sequence_start_block
|
247
|
+
@parser.parse("---\n - 1\n - 2")
|
248
|
+
assert_called :start_sequence, [true, Nodes::Sequence::BLOCK]
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_literal_scalar
|
252
|
+
@parser.parse(<<-eoyml)
|
253
|
+
%YAML 1.1
|
254
|
+
---
|
255
|
+
"literal\n\
|
256
|
+
\ttext\n"
|
257
|
+
eoyml
|
258
|
+
assert_called :scalar, ['literal text ', false, true, Nodes::Scalar::DOUBLE_QUOTED]
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_scalar
|
262
|
+
@parser.parse("--- foo\n")
|
263
|
+
assert_called :scalar, ['foo', true, false, Nodes::Scalar::PLAIN]
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_scalar_with_tag
|
267
|
+
@parser.parse("---\n!!str foo\n")
|
268
|
+
assert_called :scalar, ['foo', 'tag:yaml.org,2002:str', false, false, Nodes::Scalar::PLAIN]
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_scalar_with_anchor
|
272
|
+
@parser.parse("---\n&A foo\n")
|
273
|
+
assert_called :scalar, ['foo', 'A', true, false, Nodes::Scalar::PLAIN]
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_scalar_plain_implicit
|
277
|
+
@parser.parse("---\n&A foo\n")
|
278
|
+
assert_called :scalar, ['foo', 'A', true, false, Nodes::Scalar::PLAIN]
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_alias
|
282
|
+
@parser.parse(<<-eoyml)
|
283
|
+
%YAML 1.1
|
284
|
+
---
|
285
|
+
!!seq [
|
286
|
+
!!str "Without properties",
|
287
|
+
&A !!str "Anchored",
|
288
|
+
!!str "Tagged",
|
289
|
+
*A,
|
290
|
+
!!str "",
|
291
|
+
]
|
292
|
+
eoyml
|
293
|
+
assert_called :alias, ['A']
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_end_stream
|
297
|
+
@parser.parse("--- foo\n")
|
298
|
+
assert_called :end_stream
|
299
|
+
end
|
300
|
+
|
301
|
+
def test_start_stream
|
302
|
+
@parser.parse("--- foo\n")
|
303
|
+
assert_called :start_stream
|
304
|
+
end
|
305
|
+
|
306
|
+
def test_end_document_implicit
|
307
|
+
@parser.parse("\"foo\"\n")
|
308
|
+
assert_called :end_document, [true]
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_end_document_explicit
|
312
|
+
@parser.parse("\"foo\"\n...")
|
313
|
+
assert_called :end_document, [false]
|
314
|
+
end
|
315
|
+
|
316
|
+
def test_start_document_version
|
317
|
+
@parser.parse("%YAML 1.1\n---\n\"foo\"\n")
|
318
|
+
assert_called :start_document, [[1,1], [], false]
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_start_document_tag
|
322
|
+
@parser.parse("%TAG !yaml! tag:yaml.org,2002\n---\n!yaml!str \"foo\"\n")
|
323
|
+
assert_called :start_document, [[], [['!yaml!', 'tag:yaml.org,2002']], false]
|
324
|
+
end
|
325
|
+
|
326
|
+
def assert_called call, with = nil, parser = @parser
|
327
|
+
if with
|
328
|
+
call = parser.handler.calls.find { |x|
|
329
|
+
x.first == call && x.last.compact == with
|
330
|
+
}
|
331
|
+
assert(call,
|
332
|
+
"#{[call,with].inspect} not in #{parser.handler.calls.inspect}"
|
333
|
+
)
|
334
|
+
else
|
335
|
+
assert parser.handler.calls.any? { |x| x.first == call }
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|
339
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
require 'tempfile'
|
5
|
+
|
6
|
+
class TestPsych < Psych::TestCase
|
7
|
+
def teardown
|
8
|
+
Psych.domain_types.clear
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_line_width
|
12
|
+
yml = Psych.dump('123456 7', { :line_width => 5 })
|
13
|
+
assert_match(/^\s*7/, yml)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_indent
|
17
|
+
yml = Psych.dump({:a => {'b' => 'c'}}, {:indentation => 5})
|
18
|
+
assert_match(/^[ ]{5}b/, yml)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_canonical
|
22
|
+
yml = Psych.dump({:a => {'b' => 'c'}}, {:canonical => true})
|
23
|
+
assert_match(/\? "b/, yml)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_header
|
27
|
+
yml = Psych.dump({:a => {'b' => 'c'}}, {:header => true})
|
28
|
+
assert_match(/YAML/, yml)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_version_array
|
32
|
+
yml = Psych.dump({:a => {'b' => 'c'}}, {:version => [1,1]})
|
33
|
+
assert_match(/1.1/, yml)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_version_string
|
37
|
+
yml = Psych.dump({:a => {'b' => 'c'}}, {:version => '1.1'})
|
38
|
+
assert_match(/1.1/, yml)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_version_bool
|
42
|
+
yml = Psych.dump({:a => {'b' => 'c'}}, {:version => true})
|
43
|
+
assert_match(/1.1/, yml)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_load_argument_error
|
47
|
+
assert_raises(TypeError) do
|
48
|
+
Psych.load nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_non_existing_class_on_deserialize
|
53
|
+
e = assert_raises(ArgumentError) do
|
54
|
+
Psych.load("--- !ruby/object:NonExistent\nfoo: 1")
|
55
|
+
end
|
56
|
+
assert_equal 'undefined class/module NonExistent', e.message
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_dump_stream
|
60
|
+
things = [22, "foo \n", {}]
|
61
|
+
stream = Psych.dump_stream(*things)
|
62
|
+
assert_equal things, Psych.load_stream(stream)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_dump_file
|
66
|
+
hash = {'hello' => 'TGIF!'}
|
67
|
+
Tempfile.create('fun.yml') do |io|
|
68
|
+
assert_equal io, Psych.dump(hash, io)
|
69
|
+
io.rewind
|
70
|
+
assert_equal Psych.dump(hash), io.read
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_dump_io
|
75
|
+
hash = {'hello' => 'TGIF!'}
|
76
|
+
stringio = StringIO.new ''
|
77
|
+
assert_equal stringio, Psych.dump(hash, stringio)
|
78
|
+
assert_equal Psych.dump(hash), stringio.string
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_simple
|
82
|
+
assert_equal 'foo', Psych.load("--- foo\n")
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_libyaml_version
|
86
|
+
assert Psych.libyaml_version
|
87
|
+
assert_equal Psych.libyaml_version.join('.'), Psych::LIBYAML_VERSION
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_load_documents
|
91
|
+
docs = Psych.load_documents("--- foo\n...\n--- bar\n...")
|
92
|
+
assert_equal %w{ foo bar }, docs
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_parse_stream
|
96
|
+
docs = Psych.parse_stream("--- foo\n...\n--- bar\n...")
|
97
|
+
assert_equal %w{ foo bar }, docs.children.map { |x| x.transform }
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_add_builtin_type
|
101
|
+
got = nil
|
102
|
+
Psych.add_builtin_type 'omap' do |type, val|
|
103
|
+
got = val
|
104
|
+
end
|
105
|
+
Psych.load('--- !!omap hello')
|
106
|
+
assert_equal 'hello', got
|
107
|
+
ensure
|
108
|
+
Psych.remove_type 'omap'
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_domain_types
|
112
|
+
got = nil
|
113
|
+
Psych.add_domain_type 'foo.bar,2002', 'foo' do |type, val|
|
114
|
+
got = val
|
115
|
+
end
|
116
|
+
|
117
|
+
Psych.load('--- !foo.bar,2002/foo hello')
|
118
|
+
assert_equal 'hello', got
|
119
|
+
|
120
|
+
Psych.load("--- !foo.bar,2002/foo\n- hello\n- world")
|
121
|
+
assert_equal %w{ hello world }, got
|
122
|
+
|
123
|
+
Psych.load("--- !foo.bar,2002/foo\nhello: world")
|
124
|
+
assert_equal({ 'hello' => 'world' }, got)
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_load_file
|
128
|
+
Tempfile.create(['yikes', 'yml']) {|t|
|
129
|
+
t.binmode
|
130
|
+
t.write('--- hello world')
|
131
|
+
t.close
|
132
|
+
assert_equal 'hello world', Psych.load_file(t.path)
|
133
|
+
}
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_parse_file
|
137
|
+
Tempfile.create(['yikes', 'yml']) {|t|
|
138
|
+
t.binmode
|
139
|
+
t.write('--- hello world')
|
140
|
+
t.close
|
141
|
+
assert_equal 'hello world', Psych.parse_file(t.path).transform
|
142
|
+
}
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_degenerate_strings
|
146
|
+
assert_equal false, Psych.load(' ')
|
147
|
+
assert_equal false, Psych.parse(' ')
|
148
|
+
assert_equal false, Psych.load('')
|
149
|
+
assert_equal false, Psych.parse('')
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_callbacks
|
153
|
+
types = []
|
154
|
+
appender = lambda { |*args| types << args }
|
155
|
+
|
156
|
+
Psych.add_builtin_type('foo', &appender)
|
157
|
+
Psych.add_domain_type('example.com,2002', 'foo', &appender)
|
158
|
+
Psych.load <<-eoyml
|
159
|
+
- !tag:yaml.org,2002:foo bar
|
160
|
+
- !tag:example.com,2002:foo bar
|
161
|
+
eoyml
|
162
|
+
|
163
|
+
assert_equal [
|
164
|
+
["tag:yaml.org,2002:foo", "bar"],
|
165
|
+
["tag:example.com,2002:foo", "bar"]
|
166
|
+
], types
|
167
|
+
end
|
168
|
+
end
|