psych 2.0.12 → 5.2.3
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.
- checksums.yaml +5 -5
- data/CONTRIBUTING.md +24 -0
- data/{ext/psych/yaml/LICENSE → LICENSE} +9 -7
- data/README.md +80 -0
- data/ext/psych/depend +14 -0
- data/ext/psych/extconf.rb +43 -28
- data/ext/psych/psych.c +6 -4
- data/ext/psych/psych.h +0 -3
- data/ext/psych/psych_emitter.c +165 -132
- data/ext/psych/psych_parser.c +298 -331
- data/ext/psych/psych_to_ruby.c +0 -5
- data/ext/psych/psych_yaml_tree.c +0 -13
- data/lib/psych/class_loader.rb +11 -8
- data/lib/psych/coder.rb +1 -0
- data/lib/psych/core_ext.rb +3 -19
- data/lib/psych/exception.rb +17 -2
- data/lib/psych/handler.rb +8 -2
- data/lib/psych/handlers/document_stream.rb +2 -1
- data/lib/psych/handlers/recorder.rb +2 -1
- data/lib/psych/json/ruby_events.rb +1 -0
- data/lib/psych/json/stream.rb +3 -2
- data/lib/psych/json/tree_builder.rb +2 -1
- data/lib/psych/json/yaml_events.rb +1 -0
- data/lib/psych/nodes/alias.rb +3 -0
- data/lib/psych/nodes/document.rb +3 -0
- data/lib/psych/nodes/mapping.rb +3 -0
- data/lib/psych/nodes/node.rb +25 -5
- data/lib/psych/nodes/scalar.rb +4 -1
- data/lib/psych/nodes/sequence.rb +3 -0
- data/lib/psych/nodes/stream.rb +3 -0
- data/lib/psych/nodes.rb +8 -7
- data/lib/psych/omap.rb +1 -0
- data/lib/psych/parser.rb +14 -0
- data/lib/psych/scalar_scanner.rb +41 -49
- data/lib/psych/set.rb +1 -0
- data/lib/psych/stream.rb +1 -0
- data/lib/psych/streaming.rb +1 -0
- data/lib/psych/syntax_error.rb +2 -1
- data/lib/psych/tree_builder.rb +48 -7
- data/lib/psych/versions.rb +10 -0
- data/lib/psych/visitors/depth_first.rb +1 -0
- data/lib/psych/visitors/emitter.rb +1 -0
- data/lib/psych/visitors/json_tree.rb +2 -1
- data/lib/psych/visitors/to_ruby.rb +64 -33
- data/lib/psych/visitors/visitor.rb +18 -3
- data/lib/psych/visitors/yaml_tree.rb +128 -149
- data/lib/psych/visitors.rb +7 -6
- data/lib/psych/y.rb +1 -0
- data/lib/psych.rb +360 -95
- metadata +36 -169
- data/.autotest +0 -18
- data/.gemtest +0 -0
- data/.travis.yml +0 -11
- data/CHANGELOG.rdoc +0 -562
- data/Manifest.txt +0 -112
- data/README.rdoc +0 -71
- data/Rakefile +0 -74
- data/ext/psych/yaml/api.c +0 -1415
- data/ext/psych/yaml/config.h +0 -10
- data/ext/psych/yaml/dumper.c +0 -394
- data/ext/psych/yaml/emitter.c +0 -2329
- data/ext/psych/yaml/loader.c +0 -459
- data/ext/psych/yaml/parser.c +0 -1370
- data/ext/psych/yaml/reader.c +0 -469
- data/ext/psych/yaml/scanner.c +0 -3583
- data/ext/psych/yaml/writer.c +0 -141
- data/ext/psych/yaml/yaml.h +0 -1971
- data/ext/psych/yaml/yaml_private.h +0 -664
- data/lib/psych/deprecated.rb +0 -85
- data/test/psych/handlers/test_recorder.rb +0 -25
- data/test/psych/helper.rb +0 -114
- data/test/psych/json/test_stream.rb +0 -109
- data/test/psych/nodes/test_enumerable.rb +0 -43
- data/test/psych/test_alias_and_anchor.rb +0 -96
- data/test/psych/test_array.rb +0 -57
- data/test/psych/test_boolean.rb +0 -36
- data/test/psych/test_class.rb +0 -36
- data/test/psych/test_coder.rb +0 -184
- data/test/psych/test_date_time.rb +0 -38
- data/test/psych/test_deprecated.rb +0 -214
- data/test/psych/test_document.rb +0 -46
- data/test/psych/test_emitter.rb +0 -93
- data/test/psych/test_encoding.rb +0 -259
- data/test/psych/test_exception.rb +0 -157
- data/test/psych/test_hash.rb +0 -94
- data/test/psych/test_json_tree.rb +0 -65
- data/test/psych/test_marshalable.rb +0 -54
- data/test/psych/test_merge_keys.rb +0 -180
- data/test/psych/test_nil.rb +0 -18
- data/test/psych/test_null.rb +0 -19
- data/test/psych/test_numeric.rb +0 -45
- data/test/psych/test_object.rb +0 -44
- data/test/psych/test_object_references.rb +0 -71
- data/test/psych/test_omap.rb +0 -75
- data/test/psych/test_parser.rb +0 -339
- data/test/psych/test_psych.rb +0 -168
- data/test/psych/test_safe_load.rb +0 -97
- data/test/psych/test_scalar.rb +0 -11
- data/test/psych/test_scalar_scanner.rb +0 -106
- data/test/psych/test_serialize_subclasses.rb +0 -38
- data/test/psych/test_set.rb +0 -49
- data/test/psych/test_stream.rb +0 -93
- data/test/psych/test_string.rb +0 -226
- data/test/psych/test_struct.rb +0 -49
- data/test/psych/test_symbol.rb +0 -25
- data/test/psych/test_tainted.rb +0 -130
- data/test/psych/test_to_yaml_properties.rb +0 -63
- data/test/psych/test_tree_builder.rb +0 -79
- data/test/psych/test_yaml.rb +0 -1288
- data/test/psych/test_yamldbm.rb +0 -193
- data/test/psych/test_yamlstore.rb +0 -85
- data/test/psych/visitors/test_depth_first.rb +0 -49
- data/test/psych/visitors/test_emitter.rb +0 -144
- data/test/psych/visitors/test_to_ruby.rb +0 -326
- data/test/psych/visitors/test_yaml_tree.rb +0 -173
data/test/psych/test_encoding.rb
DELETED
@@ -1,259 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
require_relative 'helper'
|
4
|
-
|
5
|
-
module Psych
|
6
|
-
class TestEncoding < TestCase
|
7
|
-
class EncodingCatcher < Handler
|
8
|
-
attr_reader :strings
|
9
|
-
def initialize
|
10
|
-
@strings = []
|
11
|
-
end
|
12
|
-
|
13
|
-
(Handler.instance_methods(true) -
|
14
|
-
Object.instance_methods).each do |m|
|
15
|
-
class_eval %{
|
16
|
-
def #{m} *args
|
17
|
-
@strings += args.flatten.find_all { |a|
|
18
|
-
String === a
|
19
|
-
}
|
20
|
-
end
|
21
|
-
}
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def setup
|
26
|
-
super
|
27
|
-
@buffer = StringIO.new
|
28
|
-
@handler = EncodingCatcher.new
|
29
|
-
@parser = Psych::Parser.new @handler
|
30
|
-
@utf8 = Encoding.find('UTF-8')
|
31
|
-
@emitter = Psych::Emitter.new @buffer
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_dump_load_encoding_object
|
35
|
-
assert_cycle Encoding::US_ASCII
|
36
|
-
assert_cycle Encoding::UTF_8
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_transcode_shiftjis
|
40
|
-
str = "こんにちは!"
|
41
|
-
loaded = Psych.load("--- こんにちは!".encode('SHIFT_JIS'))
|
42
|
-
assert_equal str, loaded
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_transcode_utf16le
|
46
|
-
str = "こんにちは!"
|
47
|
-
loaded = Psych.load("--- こんにちは!".encode('UTF-16LE'))
|
48
|
-
assert_equal str, loaded
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_transcode_utf16be
|
52
|
-
str = "こんにちは!"
|
53
|
-
loaded = Psych.load("--- こんにちは!".encode('UTF-16BE'))
|
54
|
-
assert_equal str, loaded
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_io_shiftjis
|
58
|
-
Tempfile.create(['shiftjis', 'yml'], :encoding => 'SHIFT_JIS') {|t|
|
59
|
-
t.write '--- こんにちは!'
|
60
|
-
t.close
|
61
|
-
|
62
|
-
# If the external encoding isn't utf8, utf16le, or utf16be, we cannot
|
63
|
-
# process the file.
|
64
|
-
File.open(t.path, 'r', :encoding => 'SHIFT_JIS') do |f|
|
65
|
-
assert_raises Psych::SyntaxError do
|
66
|
-
Psych.load(f)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
}
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_io_utf16le
|
73
|
-
Tempfile.create(['utf16le', 'yml']) {|t|
|
74
|
-
t.binmode
|
75
|
-
t.write '--- こんにちは!'.encode('UTF-16LE')
|
76
|
-
t.close
|
77
|
-
|
78
|
-
File.open(t.path, 'rb', :encoding => 'UTF-16LE') do |f|
|
79
|
-
assert_equal "こんにちは!", Psych.load(f)
|
80
|
-
end
|
81
|
-
}
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_io_utf16be
|
85
|
-
Tempfile.create(['utf16be', 'yml']) {|t|
|
86
|
-
t.binmode
|
87
|
-
t.write '--- こんにちは!'.encode('UTF-16BE')
|
88
|
-
t.close
|
89
|
-
|
90
|
-
File.open(t.path, 'rb', :encoding => 'UTF-16BE') do |f|
|
91
|
-
assert_equal "こんにちは!", Psych.load(f)
|
92
|
-
end
|
93
|
-
}
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_io_utf8
|
97
|
-
Tempfile.create(['utf8', 'yml']) {|t|
|
98
|
-
t.binmode
|
99
|
-
t.write '--- こんにちは!'.encode('UTF-8')
|
100
|
-
t.close
|
101
|
-
|
102
|
-
File.open(t.path, 'rb', :encoding => 'UTF-8') do |f|
|
103
|
-
assert_equal "こんにちは!", Psych.load(f)
|
104
|
-
end
|
105
|
-
}
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_emit_alias
|
109
|
-
@emitter.start_stream Psych::Parser::UTF8
|
110
|
-
@emitter.start_document [], [], true
|
111
|
-
e = assert_raises(RuntimeError) do
|
112
|
-
@emitter.alias 'ドラえもん'.encode('EUC-JP')
|
113
|
-
end
|
114
|
-
assert_match(/alias value/, e.message)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_to_yaml_is_valid
|
118
|
-
with_default_external(Encoding::US_ASCII) do
|
119
|
-
with_default_internal(nil) do
|
120
|
-
s = "こんにちは!"
|
121
|
-
# If no encoding is specified, use UTF-8
|
122
|
-
assert_equal Encoding::UTF_8, Psych.dump(s).encoding
|
123
|
-
assert_equal s, Psych.load(Psych.dump(s))
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_start_mapping
|
129
|
-
foo = 'foo'
|
130
|
-
bar = 'バー'
|
131
|
-
|
132
|
-
@emitter.start_stream Psych::Parser::UTF8
|
133
|
-
@emitter.start_document [], [], true
|
134
|
-
@emitter.start_mapping(
|
135
|
-
foo.encode('Shift_JIS'),
|
136
|
-
bar.encode('UTF-16LE'),
|
137
|
-
false, Nodes::Sequence::ANY)
|
138
|
-
@emitter.end_mapping
|
139
|
-
@emitter.end_document false
|
140
|
-
@emitter.end_stream
|
141
|
-
|
142
|
-
@parser.parse @buffer.string
|
143
|
-
assert_encodings @utf8, @handler.strings
|
144
|
-
assert_equal [foo, bar], @handler.strings
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_start_sequence
|
148
|
-
foo = 'foo'
|
149
|
-
bar = 'バー'
|
150
|
-
|
151
|
-
@emitter.start_stream Psych::Parser::UTF8
|
152
|
-
@emitter.start_document [], [], true
|
153
|
-
@emitter.start_sequence(
|
154
|
-
foo.encode('Shift_JIS'),
|
155
|
-
bar.encode('UTF-16LE'),
|
156
|
-
false, Nodes::Sequence::ANY)
|
157
|
-
@emitter.end_sequence
|
158
|
-
@emitter.end_document false
|
159
|
-
@emitter.end_stream
|
160
|
-
|
161
|
-
@parser.parse @buffer.string
|
162
|
-
assert_encodings @utf8, @handler.strings
|
163
|
-
assert_equal [foo, bar], @handler.strings
|
164
|
-
end
|
165
|
-
|
166
|
-
def test_doc_tag_encoding
|
167
|
-
key = '鍵'
|
168
|
-
@emitter.start_stream Psych::Parser::UTF8
|
169
|
-
@emitter.start_document(
|
170
|
-
[1, 1],
|
171
|
-
[['!'.encode('EUC-JP'), key.encode('EUC-JP')]],
|
172
|
-
true
|
173
|
-
)
|
174
|
-
@emitter.scalar 'foo', nil, nil, true, false, Nodes::Scalar::ANY
|
175
|
-
@emitter.end_document false
|
176
|
-
@emitter.end_stream
|
177
|
-
|
178
|
-
@parser.parse @buffer.string
|
179
|
-
assert_encodings @utf8, @handler.strings
|
180
|
-
assert_equal key, @handler.strings[1]
|
181
|
-
end
|
182
|
-
|
183
|
-
def test_emitter_encoding
|
184
|
-
str = "壁に耳あり、障子に目あり"
|
185
|
-
thing = Psych.load Psych.dump str.encode('EUC-JP')
|
186
|
-
assert_equal str, thing
|
187
|
-
end
|
188
|
-
|
189
|
-
def test_default_internal
|
190
|
-
with_default_internal(Encoding::EUC_JP) do
|
191
|
-
str = "壁に耳あり、障子に目あり"
|
192
|
-
assert_equal @utf8, str.encoding
|
193
|
-
|
194
|
-
@parser.parse str
|
195
|
-
assert_encodings Encoding::EUC_JP, @handler.strings
|
196
|
-
assert_equal str, @handler.strings.first.encode('UTF-8')
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
def test_scalar
|
201
|
-
@parser.parse("--- a")
|
202
|
-
assert_encodings @utf8, @handler.strings
|
203
|
-
end
|
204
|
-
|
205
|
-
def test_alias
|
206
|
-
@parser.parse(<<-eoyml)
|
207
|
-
%YAML 1.1
|
208
|
-
---
|
209
|
-
!!seq [
|
210
|
-
!!str "Without properties",
|
211
|
-
&A !!str "Anchored",
|
212
|
-
!!str "Tagged",
|
213
|
-
*A,
|
214
|
-
!!str "",
|
215
|
-
]
|
216
|
-
eoyml
|
217
|
-
assert_encodings @utf8, @handler.strings
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_list_anchor
|
221
|
-
list = %w{ a b }
|
222
|
-
list << list
|
223
|
-
@parser.parse(Psych.dump(list))
|
224
|
-
assert_encodings @utf8, @handler.strings
|
225
|
-
end
|
226
|
-
|
227
|
-
def test_map_anchor
|
228
|
-
h = {}
|
229
|
-
h['a'] = h
|
230
|
-
@parser.parse(Psych.dump(h))
|
231
|
-
assert_encodings @utf8, @handler.strings
|
232
|
-
end
|
233
|
-
|
234
|
-
def test_map_tag
|
235
|
-
@parser.parse(<<-eoyml)
|
236
|
-
%YAML 1.1
|
237
|
-
---
|
238
|
-
!!map { a : b }
|
239
|
-
eoyml
|
240
|
-
assert_encodings @utf8, @handler.strings
|
241
|
-
end
|
242
|
-
|
243
|
-
def test_doc_tag
|
244
|
-
@parser.parse(<<-eoyml)
|
245
|
-
%YAML 1.1
|
246
|
-
%TAG ! tag:tenderlovemaking.com,2009:
|
247
|
-
--- !fun
|
248
|
-
eoyml
|
249
|
-
assert_encodings @utf8, @handler.strings
|
250
|
-
end
|
251
|
-
|
252
|
-
private
|
253
|
-
def assert_encodings encoding, strings
|
254
|
-
strings.each do |str|
|
255
|
-
assert_equal encoding, str.encoding, str
|
256
|
-
end
|
257
|
-
end
|
258
|
-
end
|
259
|
-
end
|
@@ -1,157 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
module Psych
|
4
|
-
class TestException < TestCase
|
5
|
-
class Wups < Exception
|
6
|
-
attr_reader :foo, :bar
|
7
|
-
def initialize *args
|
8
|
-
super
|
9
|
-
@foo = 1
|
10
|
-
@bar = 2
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def setup
|
15
|
-
super
|
16
|
-
@wups = Wups.new
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_naming_exception
|
20
|
-
err = String.xxx rescue $!
|
21
|
-
new_err = Psych.load(Psych.dump(err))
|
22
|
-
assert_equal err.message, new_err.message
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_load_takes_file
|
26
|
-
ex = assert_raises(Psych::SyntaxError) do
|
27
|
-
Psych.load '--- `'
|
28
|
-
end
|
29
|
-
assert_nil ex.file
|
30
|
-
|
31
|
-
ex = assert_raises(Psych::SyntaxError) do
|
32
|
-
Psych.load '--- `', 'meow'
|
33
|
-
end
|
34
|
-
assert_equal 'meow', ex.file
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_psych_parse_stream_takes_file
|
38
|
-
ex = assert_raises(Psych::SyntaxError) do
|
39
|
-
Psych.parse_stream '--- `'
|
40
|
-
end
|
41
|
-
assert_nil ex.file
|
42
|
-
assert_match '(<unknown>)', ex.message
|
43
|
-
|
44
|
-
ex = assert_raises(Psych::SyntaxError) do
|
45
|
-
Psych.parse_stream '--- `', 'omg!'
|
46
|
-
end
|
47
|
-
assert_equal 'omg!', ex.file
|
48
|
-
assert_match 'omg!', ex.message
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_load_stream_takes_file
|
52
|
-
ex = assert_raises(Psych::SyntaxError) do
|
53
|
-
Psych.load_stream '--- `'
|
54
|
-
end
|
55
|
-
assert_nil ex.file
|
56
|
-
assert_match '(<unknown>)', ex.message
|
57
|
-
|
58
|
-
ex = assert_raises(Psych::SyntaxError) do
|
59
|
-
Psych.load_stream '--- `', 'omg!'
|
60
|
-
end
|
61
|
-
assert_equal 'omg!', ex.file
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_parse_file_exception
|
65
|
-
Tempfile.create(['parsefile', 'yml']) {|t|
|
66
|
-
t.binmode
|
67
|
-
t.write '--- `'
|
68
|
-
t.close
|
69
|
-
ex = assert_raises(Psych::SyntaxError) do
|
70
|
-
Psych.parse_file t.path
|
71
|
-
end
|
72
|
-
assert_equal t.path, ex.file
|
73
|
-
}
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_load_file_exception
|
77
|
-
Tempfile.create(['loadfile', 'yml']) {|t|
|
78
|
-
t.binmode
|
79
|
-
t.write '--- `'
|
80
|
-
t.close
|
81
|
-
ex = assert_raises(Psych::SyntaxError) do
|
82
|
-
Psych.load_file t.path
|
83
|
-
end
|
84
|
-
assert_equal t.path, ex.file
|
85
|
-
}
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_psych_parse_takes_file
|
89
|
-
ex = assert_raises(Psych::SyntaxError) do
|
90
|
-
Psych.parse '--- `'
|
91
|
-
end
|
92
|
-
assert_match '(<unknown>)', ex.message
|
93
|
-
assert_nil ex.file
|
94
|
-
|
95
|
-
ex = assert_raises(Psych::SyntaxError) do
|
96
|
-
Psych.parse '--- `', 'omg!'
|
97
|
-
end
|
98
|
-
assert_match 'omg!', ex.message
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_attributes
|
102
|
-
e = assert_raises(Psych::SyntaxError) {
|
103
|
-
Psych.load '--- `foo'
|
104
|
-
}
|
105
|
-
|
106
|
-
assert_nil e.file
|
107
|
-
assert_equal 1, e.line
|
108
|
-
assert_equal 5, e.column
|
109
|
-
# FIXME: offset isn't being set correctly by libyaml
|
110
|
-
# assert_equal 5, e.offset
|
111
|
-
|
112
|
-
assert e.problem
|
113
|
-
assert e.context
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_convert
|
117
|
-
w = Psych.load(Psych.dump(@wups))
|
118
|
-
assert_equal @wups, w
|
119
|
-
assert_equal 1, w.foo
|
120
|
-
assert_equal 2, w.bar
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_to_yaml_properties
|
124
|
-
class << @wups
|
125
|
-
def to_yaml_properties
|
126
|
-
[:@foo]
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
w = Psych.load(Psych.dump(@wups))
|
131
|
-
assert_equal @wups, w
|
132
|
-
assert_equal 1, w.foo
|
133
|
-
assert_nil w.bar
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_psych_syntax_error
|
137
|
-
Tempfile.create(['parsefile', 'yml']) do |t|
|
138
|
-
t.binmode
|
139
|
-
t.write '--- `'
|
140
|
-
t.close
|
141
|
-
|
142
|
-
begin
|
143
|
-
Psych.parse_file t.path
|
144
|
-
rescue StandardError
|
145
|
-
assert true # count assertion
|
146
|
-
ensure
|
147
|
-
return unless $!
|
148
|
-
|
149
|
-
ancestors = $!.class.ancestors.inspect
|
150
|
-
|
151
|
-
flunk "Psych::SyntaxError not rescued by StandardError: #{ancestors}"
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
end
|
157
|
-
end
|
data/test/psych/test_hash.rb
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
module Psych
|
4
|
-
class TestHash < TestCase
|
5
|
-
class X < Hash
|
6
|
-
end
|
7
|
-
|
8
|
-
class HashWithCustomInit < Hash
|
9
|
-
attr_reader :obj
|
10
|
-
def initialize(obj)
|
11
|
-
@obj = obj
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class HashWithCustomInitNoIvar < Hash
|
16
|
-
def initialize(obj)
|
17
|
-
# *shrug*
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def setup
|
22
|
-
super
|
23
|
-
@hash = { :a => 'b' }
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_referenced_hash_with_ivar
|
27
|
-
a = [1,2,3,4,5]
|
28
|
-
t1 = [HashWithCustomInit.new(a)]
|
29
|
-
t1 << t1.first
|
30
|
-
assert_cycle t1
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_custom_initialized
|
34
|
-
a = [1,2,3,4,5]
|
35
|
-
t1 = HashWithCustomInit.new(a)
|
36
|
-
t2 = Psych.load(Psych.dump(t1))
|
37
|
-
assert_equal t1, t2
|
38
|
-
assert_cycle t1
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_custom_initialize_no_ivar
|
42
|
-
t1 = HashWithCustomInitNoIvar.new(nil)
|
43
|
-
t2 = Psych.load(Psych.dump(t1))
|
44
|
-
assert_equal t1, t2
|
45
|
-
assert_cycle t1
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_hash_subclass_with_ivars
|
49
|
-
x = X.new
|
50
|
-
x[:a] = 'b'
|
51
|
-
x.instance_variable_set :@foo, 'bar'
|
52
|
-
dup = Psych.load Psych.dump x
|
53
|
-
assert_cycle x
|
54
|
-
assert_equal 'bar', dup.instance_variable_get(:@foo)
|
55
|
-
assert_equal X, dup.class
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_load_with_class_syck_compatibility
|
59
|
-
hash = Psych.load "--- !ruby/object:Hash\n:user_id: 7\n:username: Lucas\n"
|
60
|
-
assert_equal({ user_id: 7, username: 'Lucas'}, hash)
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_empty_subclass
|
64
|
-
assert_match "!ruby/hash:#{X}", Psych.dump(X.new)
|
65
|
-
x = Psych.load Psych.dump X.new
|
66
|
-
assert_equal X, x.class
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_map
|
70
|
-
x = Psych.load "--- !map:#{X} { }\n"
|
71
|
-
assert_equal X, x.class
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_self_referential
|
75
|
-
@hash['self'] = @hash
|
76
|
-
assert_cycle(@hash)
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_cycles
|
80
|
-
assert_cycle(@hash)
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_ref_append
|
84
|
-
hash = Psych.load(<<-eoyml)
|
85
|
-
---
|
86
|
-
foo: &foo
|
87
|
-
hello: world
|
88
|
-
bar:
|
89
|
-
<<: *foo
|
90
|
-
eoyml
|
91
|
-
assert_equal({"foo"=>{"hello"=>"world"}, "bar"=>{"hello"=>"world"}}, hash)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
@@ -1,65 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
module Psych
|
4
|
-
class TestJSONTree < TestCase
|
5
|
-
def test_string
|
6
|
-
assert_match(/"foo"/, Psych.to_json("foo"))
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_symbol
|
10
|
-
assert_match(/"foo"/, Psych.to_json(:foo))
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_nil
|
14
|
-
assert_match(/^null/, Psych.to_json(nil))
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_int
|
18
|
-
assert_match(/^10/, Psych.to_json(10))
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_float
|
22
|
-
assert_match(/^1.2/, Psych.to_json(1.2))
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_hash
|
26
|
-
hash = { 'one' => 'two' }
|
27
|
-
json = Psych.to_json(hash)
|
28
|
-
assert_match(/}$/, json)
|
29
|
-
assert_match(/^\{/, json)
|
30
|
-
assert_match(/['"]one['"]/, json)
|
31
|
-
assert_match(/['"]two['"]/, json)
|
32
|
-
end
|
33
|
-
|
34
|
-
class Bar
|
35
|
-
def encode_with coder
|
36
|
-
coder.represent_seq 'omg', %w{ a b c }
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_json_list_dump_exclude_tag
|
41
|
-
json = Psych.to_json Bar.new
|
42
|
-
refute_match('omg', json)
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_list_to_json
|
46
|
-
list = %w{ one two }
|
47
|
-
json = Psych.to_json(list)
|
48
|
-
assert_match(/\]$/, json)
|
49
|
-
assert_match(/^\[/, json)
|
50
|
-
assert_match(/"one"/, json)
|
51
|
-
assert_match(/"two"/, json)
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_time
|
55
|
-
time = Time.utc(2010, 10, 10)
|
56
|
-
assert_equal "{\"a\": \"2010-10-10 00:00:00.000000000 Z\"}\n",
|
57
|
-
Psych.to_json({'a' => time })
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_datetime
|
61
|
-
time = Time.new(2010, 10, 10).to_datetime
|
62
|
-
assert_equal "{\"a\": \"#{time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")}\"}\n", Psych.to_json({'a' => time })
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
require 'delegate'
|
3
|
-
|
4
|
-
module Psych
|
5
|
-
class TestMarshalable < TestCase
|
6
|
-
def test_objects_defining_marshal_dump_and_marshal_load_can_be_dumped
|
7
|
-
sd = SimpleDelegator.new(1)
|
8
|
-
loaded = Psych.load(Psych.dump(sd))
|
9
|
-
|
10
|
-
assert_instance_of(SimpleDelegator, loaded)
|
11
|
-
assert_equal(sd, loaded)
|
12
|
-
end
|
13
|
-
|
14
|
-
class PsychCustomMarshalable < BasicObject
|
15
|
-
attr_reader :foo
|
16
|
-
|
17
|
-
def initialize(foo)
|
18
|
-
@foo = foo
|
19
|
-
end
|
20
|
-
|
21
|
-
def marshal_dump
|
22
|
-
[foo]
|
23
|
-
end
|
24
|
-
|
25
|
-
def mashal_load(data)
|
26
|
-
@foo = data[0]
|
27
|
-
end
|
28
|
-
|
29
|
-
def init_with(coder)
|
30
|
-
@foo = coder['foo']
|
31
|
-
end
|
32
|
-
|
33
|
-
def encode_with(coder)
|
34
|
-
coder['foo'] = 2
|
35
|
-
end
|
36
|
-
|
37
|
-
def respond_to?(method)
|
38
|
-
[:marshal_dump, :marshal_load, :init_with, :encode_with].include?(method)
|
39
|
-
end
|
40
|
-
|
41
|
-
def class
|
42
|
-
PsychCustomMarshalable
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_init_with_takes_priority_over_marshal_methods
|
47
|
-
obj = PsychCustomMarshalable.new(1)
|
48
|
-
loaded = Psych.load(Psych.dump(obj))
|
49
|
-
|
50
|
-
assert(PsychCustomMarshalable === loaded)
|
51
|
-
assert_equal(2, loaded.foo)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|