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
|
# frozen_string_literal: true
require_relative 'helper'
module Psych
class TestCoder < TestCase
class InitApi
attr_accessor :implicit
attr_accessor :style
attr_accessor :tag
attr_accessor :a, :b, :c
def initialize
@a = 1
@b = 2
@c = 3
end
def init_with coder
@a = coder['aa']
@b = coder['bb']
@implicit = coder.implicit
@tag = coder.tag
@style = coder.style
end
def encode_with coder
coder['aa'] = @a
coder['bb'] = @b
end
end
class TaggingCoder < InitApi
def encode_with coder
super
coder.tag = coder.tag.sub(/!/, '!hello')
coder.implicit = false
coder.style = Psych::Nodes::Mapping::FLOW
end
end
class ScalarCoder
def encode_with coder
coder.scalar = "foo"
end
end
class Represent
yaml_tag 'foo'
def encode_with coder
coder.represent_scalar 'foo', 'bar'
end
end
class RepresentWithInit
yaml_tag name
attr_accessor :str
def init_with coder
@str = coder.scalar
end
def encode_with coder
coder.represent_scalar self.class.name, 'bar'
end
end
class RepresentWithSeq
yaml_tag name
attr_accessor :seq
def init_with coder
@seq = coder.seq
end
def encode_with coder
coder.represent_seq self.class.name, %w{ foo bar }
end
end
class RepresentWithMap
yaml_tag name
attr_accessor :map
def init_with coder
@map = coder.map
end
def encode_with coder
coder.represent_map self.class.name, { "string" => 'a', :symbol => 'b' }
end
end
class RepresentWithObject
def encode_with coder
coder.represent_object self.class.name, 20
end
end
class Referential
attr_reader :a
def initialize
@a = self
end
def encode_with(c)
c['a'] = @a
end
def init_with(c)
@a = c['a']
end
end
class CustomEncode
def initialize(**opts)
@opts = opts
end
def encode_with(coder)
@opts.each { |k,v| coder.public_send :"#{k}=", v }
end
end
def test_self_referential
x = Referential.new
copy = Psych.unsafe_load Psych.dump x
assert_equal copy, copy.a
end
def test_represent_with_object
thing = Psych.load(Psych.dump(RepresentWithObject.new))
assert_equal 20, thing
end
def test_json_dump_exclude_tag
refute_match('TestCoder::InitApi', Psych.to_json(InitApi.new))
end
def test_map_takes_block
coder = Psych::Coder.new 'foo'
tag = coder.tag
style = coder.style
coder.map { |map| map.add 'foo', 'bar' }
assert_equal 'bar', coder['foo']
assert_equal tag, coder.tag
assert_equal style, coder.style
end
def test_map_with_tag
coder = Psych::Coder.new 'foo'
coder.map('hello') { |map| map.add 'foo', 'bar' }
assert_equal 'bar', coder['foo']
assert_equal 'hello', coder.tag
end
def test_map_with_tag_and_style
coder = Psych::Coder.new 'foo'
coder.map('hello', 'world') { |map| map.add 'foo', 'bar' }
assert_equal 'bar', coder['foo']
assert_equal 'hello', coder.tag
assert_equal 'world', coder.style
end
def test_represent_map
thing = Psych.unsafe_load(Psych.dump(RepresentWithMap.new))
assert_equal({ "string" => 'a', :symbol => 'b' }, thing.map)
end
def test_represent_sequence
thing = Psych.unsafe_load(Psych.dump(RepresentWithSeq.new))
assert_equal %w{ foo bar }, thing.seq
end
def test_represent_with_init
thing = Psych.unsafe_load(Psych.dump(RepresentWithInit.new))
assert_equal 'bar', thing.str
end
def test_represent!
assert_match(/foo/, Psych.dump(Represent.new))
assert_instance_of(Represent, Psych.unsafe_load(Psych.dump(Represent.new)))
end
def test_scalar_coder
foo = Psych.load(Psych.dump(ScalarCoder.new))
assert_equal 'foo', foo
end
def test_load_dumped_tagging
foo = InitApi.new
bar = Psych.unsafe_load(Psych.dump(foo))
assert_equal false, bar.implicit
assert_equal "!ruby/object:Psych::TestCoder::InitApi", bar.tag
assert_equal Psych::Nodes::Mapping::BLOCK, bar.style
end
def test_dump_with_tag
foo = TaggingCoder.new
assert_match(/hello/, Psych.dump(foo))
assert_match(/\{aa/, Psych.dump(foo))
end
def test_dump_encode_with
foo = InitApi.new
assert_match(/aa/, Psych.dump(foo))
end
def test_dump_init_with
foo = InitApi.new
bar = Psych.unsafe_load(Psych.dump(foo))
assert_equal foo.a, bar.a
assert_equal foo.b, bar.b
assert_nil bar.c
end
def test_coder_style_map_default
foo = Psych.dump a: 1, b: 2
assert_equal "---\n:a: 1\n:b: 2\n", foo
end
def test_coder_style_map_any
pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
foo = Psych.dump CustomEncode.new \
map: {a: 1, b: 2},
style: Psych::Nodes::Mapping::ANY,
tag: nil
assert_equal "---\n:a: 1\n:b: 2\n", foo
end
def test_coder_style_map_block
pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
foo = Psych.dump CustomEncode.new \
map: {a: 1, b: 2},
style: Psych::Nodes::Mapping::BLOCK,
tag: nil
assert_equal "---\n:a: 1\n:b: 2\n", foo
end
def test_coder_style_map_flow
pend "Failing on JRuby" if RUBY_PLATFORM =~ /java/
foo = Psych.dump CustomEncode.new \
map: { a: 1, b: 2 },
style: Psych::Nodes::Mapping::FLOW,
tag: nil
assert_equal "--- {! ':a': 1, ! ':b': 2}\n", foo
end
def test_coder_style_seq_default
foo = Psych.dump [ 1, 2, 3 ]
assert_equal "---\n- 1\n- 2\n- 3\n", foo
end
def test_coder_style_seq_any
foo = Psych.dump CustomEncode.new \
seq: [ 1, 2, 3 ],
style: Psych::Nodes::Sequence::ANY,
tag: nil
assert_equal "---\n- 1\n- 2\n- 3\n", foo
end
def test_coder_style_seq_block
foo = Psych.dump CustomEncode.new \
seq: [ 1, 2, 3 ],
style: Psych::Nodes::Sequence::BLOCK,
tag: nil
assert_equal "---\n- 1\n- 2\n- 3\n", foo
end
def test_coder_style_seq_flow
foo = Psych.dump CustomEncode.new \
seq: [ 1, 2, 3 ],
style: Psych::Nodes::Sequence::FLOW,
tag: nil
assert_equal "--- [1, 2, 3]\n", foo
end
def test_coder_style_scalar_default
foo = Psych.dump 'some scalar'
assert_match(/\A--- some scalar\n(?:\.\.\.\n)?\z/, foo)
end
def test_coder_style_scalar_any
foo = Psych.dump CustomEncode.new \
scalar: 'some scalar',
style: Psych::Nodes::Scalar::ANY,
tag: nil
assert_match(/\A--- some scalar\n(?:\.\.\.\n)?\z/, foo)
end
def test_coder_style_scalar_plain
foo = Psych.dump CustomEncode.new \
scalar: 'some scalar',
style: Psych::Nodes::Scalar::PLAIN,
tag: nil
assert_match(/\A--- some scalar\n(?:\.\.\.\n)?\z/, foo)
end
def test_coder_style_scalar_single_quoted
foo = Psych.dump CustomEncode.new \
scalar: 'some scalar',
style: Psych::Nodes::Scalar::SINGLE_QUOTED,
tag: nil
assert_equal "--- ! 'some scalar'\n", foo
end
def test_coder_style_scalar_double_quoted
foo = Psych.dump CustomEncode.new \
scalar: 'some scalar',
style: Psych::Nodes::Scalar::DOUBLE_QUOTED,
tag: nil
assert_equal %Q'--- ! "some scalar"\n', foo
end
def test_coder_style_scalar_literal
foo = Psych.dump CustomEncode.new \
scalar: 'some scalar',
style: Psych::Nodes::Scalar::LITERAL,
tag: nil
assert_equal "--- ! |-\n some scalar\n", foo
end
def test_coder_style_scalar_folded
foo = Psych.dump CustomEncode.new \
scalar: 'some scalar',
style: Psych::Nodes::Scalar::FOLDED,
tag: nil
assert_equal "--- ! >-\n some scalar\n", foo
end
end
end
|