1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
# coding: utf-8
require_relative 'helper'
module Psych
class TestParser < TestCase
class EventCatcher < Handler
attr_accessor :parser
attr_reader :calls, :marks
def initialize
@parser = nil
@calls = []
@marks = []
end
(Handler.instance_methods(true) -
Object.instance_methods).each do |m|
class_eval %{
def #{m} *args
super
@marks << @parser.mark if @parser
@calls << [:#{m}, args]
end
}
end
end
def setup
super
@handler = EventCatcher.new
@parser = Psych::Parser.new @handler
@handler.parser = @parser
end
def test_ast_roundtrip
parser = Psych.parser
parser.parse('null')
ast = parser.handler.root
assert_match(/^null/, ast.yaml)
end
def test_exception_memory_leak
yaml = <<-eoyaml
%YAML 1.1
%TAG ! tag:tenderlovemaking.com,2009:
--- &ponies
- first element
- *ponies
- foo: bar
...
eoyaml
[:start_stream, :start_document, :end_document, :alias, :scalar,
:start_sequence, :end_sequence, :start_mapping, :end_mapping,
:end_stream].each do |method|
klass = Class.new(Psych::Handler) do
define_method(method) do |*args|
raise
end
end
parser = Psych::Parser.new klass.new
2.times {
assert_raises(RuntimeError, method.to_s) do
parser.parse yaml
end
}
end
end
def test_multiparse
3.times do
@parser.parse '--- foo'
end
end
def test_filename
ex = assert_raises(Psych::SyntaxError) do
@parser.parse '--- `', 'omg!'
end
assert_match 'omg!', ex.message
end
def test_line_numbers
assert_equal 0, @parser.mark.line
@parser.parse "---\n- hello\n- world"
line_calls = @handler.marks.map(&:line).zip(@handler.calls.map(&:first))
assert_equal [[0, :start_stream],
[0, :start_document],
[1, :start_sequence],
[2, :scalar],
[3, :scalar],
[3, :end_sequence],
[3, :end_document],
[3, :end_stream]], line_calls
assert_equal 3, @parser.mark.line
end
def test_column_numbers
assert_equal 0, @parser.mark.column
@parser.parse "---\n- hello\n- world"
col_calls = @handler.marks.map(&:column).zip(@handler.calls.map(&:first))
assert_equal [[0, :start_stream],
[3, :start_document],
[1, :start_sequence],
[0, :scalar],
[0, :scalar],
[0, :end_sequence],
[0, :end_document],
[0, :end_stream]], col_calls
assert_equal 0, @parser.mark.column
end
def test_index_numbers
assert_equal 0, @parser.mark.index
@parser.parse "---\n- hello\n- world"
idx_calls = @handler.marks.map(&:index).zip(@handler.calls.map(&:first))
assert_equal [[0, :start_stream],
[3, :start_document],
[5, :start_sequence],
[12, :scalar],
[19, :scalar],
[19, :end_sequence],
[19, :end_document],
[19, :end_stream]], idx_calls
assert_equal 19, @parser.mark.index
end
def test_bom
tadpole = 'おたまじゃくし'
# BOM + text
yml = "\uFEFF#{tadpole}".encode('UTF-16LE')
@parser.parse yml
assert_equal tadpole, @parser.handler.calls[2][1].first
end
def test_external_encoding
tadpole = 'おたまじゃくし'
@parser.external_encoding = Psych::Parser::UTF16LE
@parser.parse tadpole.encode 'UTF-16LE'
assert_equal tadpole, @parser.handler.calls[2][1].first
end
def test_bogus_io
o = Object.new
def o.external_encoding; nil end
def o.read len; self end
assert_raises(TypeError) do
@parser.parse o
end
end
def test_parse_io
@parser.parse StringIO.new("--- a")
assert_called :start_stream
assert_called :scalar
assert_called :end_stream
end
def test_syntax_error
assert_raises(Psych::SyntaxError) do
@parser.parse("---\n\"foo\"\n\"bar\"\n")
end
end
def test_syntax_error_twice
assert_raises(Psych::SyntaxError) do
@parser.parse("---\n\"foo\"\n\"bar\"\n")
end
assert_raises(Psych::SyntaxError) do
@parser.parse("---\n\"foo\"\n\"bar\"\n")
end
end
def test_syntax_error_has_path_for_string
e = assert_raises(Psych::SyntaxError) do
@parser.parse("---\n\"foo\"\n\"bar\"\n")
end
assert_match '(<unknown>):', e.message
end
def test_syntax_error_has_path_for_io
io = StringIO.new "---\n\"foo\"\n\"bar\"\n"
def io.path; "hello!"; end
e = assert_raises(Psych::SyntaxError) do
@parser.parse(io)
end
assert_match "(#{io.path}):", e.message
end
def test_mapping_end
@parser.parse("---\n!!map { key: value }")
assert_called :end_mapping
end
def test_mapping_tag
@parser.parse("---\n!!map { key: value }")
assert_called :start_mapping, ["tag:yaml.org,2002:map", false, Nodes::Mapping::FLOW]
end
def test_mapping_anchor
@parser.parse("---\n&A { key: value }")
assert_called :start_mapping, ['A', true, Nodes::Mapping::FLOW]
end
def test_mapping_block
@parser.parse("---\n key: value")
assert_called :start_mapping, [true, Nodes::Mapping::BLOCK]
end
def test_mapping_start
@parser.parse("---\n{ key: value }")
assert_called :start_mapping
assert_called :start_mapping, [true, Nodes::Mapping::FLOW]
end
def test_sequence_end
@parser.parse("---\n&A [1, 2]")
assert_called :end_sequence
end
def test_sequence_start_anchor
@parser.parse("---\n&A [1, 2]")
assert_called :start_sequence, ["A", true, Nodes::Sequence::FLOW]
end
def test_sequence_start_tag
@parser.parse("---\n!!seq [1, 2]")
assert_called :start_sequence, ["tag:yaml.org,2002:seq", false, Nodes::Sequence::FLOW]
end
def test_sequence_start_flow
@parser.parse("---\n[1, 2]")
assert_called :start_sequence, [true, Nodes::Sequence::FLOW]
end
def test_sequence_start_block
@parser.parse("---\n - 1\n - 2")
assert_called :start_sequence, [true, Nodes::Sequence::BLOCK]
end
def test_literal_scalar
@parser.parse(<<-eoyml)
%YAML 1.1
---
"literal\n\
\ttext\n"
eoyml
assert_called :scalar, ['literal text ', false, true, Nodes::Scalar::DOUBLE_QUOTED]
end
def test_scalar
@parser.parse("--- foo\n")
assert_called :scalar, ['foo', true, false, Nodes::Scalar::PLAIN]
end
def test_scalar_with_tag
@parser.parse("---\n!!str foo\n")
assert_called :scalar, ['foo', 'tag:yaml.org,2002:str', false, false, Nodes::Scalar::PLAIN]
end
def test_scalar_with_anchor
@parser.parse("---\n&A foo\n")
assert_called :scalar, ['foo', 'A', true, false, Nodes::Scalar::PLAIN]
end
def test_scalar_plain_implicit
@parser.parse("---\n&A foo\n")
assert_called :scalar, ['foo', 'A', true, false, Nodes::Scalar::PLAIN]
end
def test_alias
@parser.parse(<<-eoyml)
%YAML 1.1
---
!!seq [
!!str "Without properties",
&A !!str "Anchored",
!!str "Tagged",
*A,
!!str "",
]
eoyml
assert_called :alias, ['A']
end
def test_end_stream
@parser.parse("--- foo\n")
assert_called :end_stream
end
def test_start_stream
@parser.parse("--- foo\n")
assert_called :start_stream
end
def test_end_document_implicit
@parser.parse("\"foo\"\n")
assert_called :end_document, [true]
end
def test_end_document_explicit
@parser.parse("\"foo\"\n...")
assert_called :end_document, [false]
end
def test_start_document_version
@parser.parse("%YAML 1.1\n---\n\"foo\"\n")
assert_called :start_document, [[1,1], [], false]
end
def test_start_document_tag
@parser.parse("%TAG !yaml! tag:yaml.org,2002\n---\n!yaml!str \"foo\"\n")
assert_called :start_document, [[], [['!yaml!', 'tag:yaml.org,2002']], false]
end
def assert_called call, with = nil, parser = @parser
if with
call = parser.handler.calls.find { |x|
x.first == call && x.last.compact == with
}
assert(call,
"#{[call,with].inspect} not in #{parser.handler.calls.inspect}"
)
else
assert parser.handler.calls.any? { |x| x.first == call }
end
end
end
end
|