ya2yaml 0.29.2 → 0.30
Sign up to get free protection for your applications and to get access to all the features.
- data/{README → README.rdoc} +0 -0
- data/lib/ya2yaml.rb +359 -359
- data/test/test.rb +443 -403
- metadata +6 -7
data/test/test.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: UTF-8
|
3
3
|
|
4
|
-
#
|
4
|
+
# Author:: Akira FUNAI
|
5
|
+
# Copyright:: Copyright (c) 2006-2010 Akira FUNAI
|
6
|
+
# License:: MIT License
|
5
7
|
|
6
8
|
$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
|
7
9
|
|
@@ -27,415 +29,453 @@ end if RUBY_VERSION >= "1.9"
|
|
27
29
|
|
28
30
|
class TC_Ya2YAML < Test::Unit::TestCase
|
29
31
|
|
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
|
-
|
32
|
+
@@struct_klass = Struct::new('Foo', :bar, :buz)
|
33
|
+
class Moo
|
34
|
+
attr_accessor :val1, :val2
|
35
|
+
def initialize(val1, val2)
|
36
|
+
@val1 = val1
|
37
|
+
@val2 = val2
|
38
|
+
end
|
39
|
+
def ==(k)
|
40
|
+
(k.class == self.class) &&
|
41
|
+
(k.val1 == self.val1) &&
|
42
|
+
(k.val2 == self.val2)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
puts "test may take minutes. please wait.\n"
|
46
|
+
|
47
|
+
def setup
|
48
|
+
@text = File.open('./t.yaml', 'r') { |f| f.read }
|
49
|
+
@gif = File.open('./t.gif', 'rb') { |f| f.read }
|
50
|
+
@struct = @@struct_klass.new('barbarbar', @@struct_klass.new('baaaar', 12345))
|
51
|
+
@klass = Moo.new('boobooboo', Time.local(2009, 2, 9, 16, 44, 10))
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_options
|
55
|
+
opt = {:syck_compatible => true}
|
56
|
+
'foobar'.ya2yaml(opt)
|
57
|
+
assert_equal(
|
58
|
+
{:syck_compatible => true},
|
59
|
+
opt,
|
60
|
+
'ya2yaml should not change the option hash'
|
61
|
+
)
|
62
|
+
|
63
|
+
[
|
64
|
+
[
|
65
|
+
{},
|
66
|
+
"--- \n- \"\\u0086\"\n- |-\n a\xe2\x80\xa8 b\xe2\x80\xa9 c\n- |4-\n abc\n xyz\n",
|
67
|
+
],
|
68
|
+
[
|
69
|
+
{:indent_size => 4},
|
70
|
+
"--- \n- \"\\u0086\"\n- |-\n a\xe2\x80\xa8 b\xe2\x80\xa9 c\n- |8-\n abc\n xyz\n",
|
71
|
+
],
|
72
|
+
[
|
73
|
+
{:minimum_block_length => 16},
|
74
|
+
"--- \n- \"\\u0086\"\n- \"a\\Lb\\Pc\"\n- \" abc\\nxyz\"\n",
|
75
|
+
],
|
74
76
|
# [
|
75
77
|
# {:emit_c_document_end => true},
|
76
78
|
# "--- \n- \"\\u0086\"\n- |-\n a\xe2\x80\xa8 b\xe2\x80\xa9 c\n- |4-\n abc\n xyz\n...\n",
|
77
79
|
# ],
|
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
|
-
|
80
|
+
[
|
81
|
+
{:printable_with_syck => true},
|
82
|
+
"--- \n- \"\\u0086\"\n- |-\n a\xe2\x80\xa8 b\xe2\x80\xa9 c\n- \" abc\\n\\\n xyz\"\n",
|
83
|
+
],
|
84
|
+
[
|
85
|
+
{:escape_b_specific => true},
|
86
|
+
"--- \n- \"\\u0086\"\n- \"a\\Lb\\Pc\"\n- |4-\n abc\n xyz\n",
|
87
|
+
],
|
88
|
+
[
|
89
|
+
{:escape_as_utf8 => true},
|
90
|
+
"--- \n- \"\\xc2\\x86\"\n- |-\n a\xe2\x80\xa8 b\xe2\x80\xa9 c\n- |4-\n abc\n xyz\n",
|
91
|
+
],
|
92
|
+
[
|
93
|
+
{:syck_compatible => true},
|
94
|
+
"--- \n- \"\\xc2\\x86\"\n- \"a\\xe2\\x80\\xa8b\\xe2\\x80\\xa9c\"\n- \" abc\\n\\\n xyz\"\n",
|
95
|
+
],
|
96
|
+
].each {|opt, yaml|
|
97
|
+
y = ["\xc2\x86", "a\xe2\x80\xa8b\xe2\x80\xa9c", " abc\nxyz"].ya2yaml(opt)
|
98
|
+
assert_equal(
|
99
|
+
yaml,
|
100
|
+
y,
|
101
|
+
"option #{opt.inspect} should be recognized"
|
102
|
+
)
|
103
|
+
}
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_hash_order
|
107
|
+
[
|
108
|
+
[
|
109
|
+
nil,
|
110
|
+
"--- \na: 1\nb: 2\nc: 3\n",
|
111
|
+
],
|
112
|
+
[
|
113
|
+
[],
|
114
|
+
"--- \na: 1\nb: 2\nc: 3\n",
|
115
|
+
],
|
116
|
+
[
|
117
|
+
['c', 'b', 'a'],
|
118
|
+
"--- \nc: 3\nb: 2\na: 1\n",
|
119
|
+
],
|
120
|
+
[
|
121
|
+
['b'],
|
122
|
+
"--- \nb: 2\na: 1\nc: 3\n",
|
123
|
+
],
|
124
|
+
].each {|hash_order, yaml|
|
125
|
+
y = {
|
126
|
+
'a' => 1,
|
127
|
+
'c' => 3,
|
128
|
+
'b' => 2,
|
129
|
+
}.ya2yaml(
|
130
|
+
:hash_order => hash_order
|
131
|
+
)
|
132
|
+
assert_equal(
|
133
|
+
yaml,
|
134
|
+
y,
|
135
|
+
'hash order should be kept when :hash_order provided'
|
136
|
+
)
|
137
|
+
}
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_normalize_line_breaks
|
141
|
+
[
|
142
|
+
["\n\n\n\n", "--- \"\\n\\n\\n\\n\"\n"],
|
143
|
+
["\r\n\r\n\r\n", "--- \"\\n\\n\\n\"\n"],
|
144
|
+
["\r\n\n\n", "--- \"\\n\\n\\n\"\n"],
|
145
|
+
["\n\r\n\n", "--- \"\\n\\n\\n\"\n"],
|
146
|
+
["\n\n\r\n", "--- \"\\n\\n\\n\"\n"],
|
147
|
+
["\n\n\n\r", "--- \"\\n\\n\\n\\n\"\n"],
|
148
|
+
["\r\r\n\r", "--- \"\\n\\n\\n\"\n"],
|
149
|
+
["\r\r\r\r", "--- \"\\n\\n\\n\\n\"\n"],
|
150
|
+
["\r\xc2\x85\r\n", "--- \"\\n\\n\\n\"\n"],
|
151
|
+
["\r\xe2\x80\xa8\r\n", "--- \"\\n\\L\\n\"\n"],
|
152
|
+
["\r\xe2\x80\xa9\r\n", "--- \"\\n\\P\\n\"\n"],
|
153
|
+
].each {|src, yaml|
|
154
|
+
y = src.ya2yaml(
|
155
|
+
:minimum_block_length => 16
|
156
|
+
)
|
157
|
+
assert_equal(
|
158
|
+
yaml,
|
159
|
+
y,
|
160
|
+
'line breaks should be normalized to fit the format.'
|
161
|
+
)
|
162
|
+
}
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_structs
|
166
|
+
[
|
167
|
+
[Struct.new('Hoge', :foo).new(123), "--- !ruby/struct:Hoge \n foo: 123\n", ],
|
168
|
+
[Struct.new(:foo).new(123), "--- !ruby/struct: \n foo: 123\n", ],
|
169
|
+
].each {|src, yaml|
|
170
|
+
y = src.ya2yaml()
|
171
|
+
assert_equal(
|
172
|
+
yaml,
|
173
|
+
y,
|
174
|
+
'ruby struct should be serialized properly'
|
175
|
+
)
|
176
|
+
}
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_roundtrip_single_byte_char
|
180
|
+
("\x00".."\x7f").each {|c|
|
181
|
+
y = c.ya2yaml()
|
182
|
+
r = YAML.load(y)
|
183
|
+
assert_equal(
|
184
|
+
(c == "\r" ? "\n" : c), # "\r" is normalized as "\n"
|
185
|
+
r,
|
186
|
+
'single byte characters should round-trip properly'
|
187
|
+
)
|
188
|
+
}
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_roundtrip_multi_byte_char
|
192
|
+
[
|
193
|
+
0x80,
|
194
|
+
0x85,
|
195
|
+
0xa0,
|
196
|
+
0x07ff,
|
197
|
+
0x0800,
|
198
|
+
0x0fff,
|
199
|
+
0x1000,
|
200
|
+
0x2028,
|
201
|
+
0x2029,
|
202
|
+
0xcfff,
|
203
|
+
0xd000,
|
204
|
+
0xd7ff,
|
205
|
+
0xe000,
|
206
|
+
0xfffd,
|
207
|
+
0x10000,
|
208
|
+
0x3ffff,
|
209
|
+
0x40000,
|
210
|
+
0xfffff,
|
211
|
+
0x100000,
|
212
|
+
0x10ffff,
|
213
|
+
].each {|ucs_code|
|
214
|
+
[-1, 0, 1].each {|ofs|
|
215
|
+
(c = [ucs_code + ofs].pack('U'))
|
216
|
+
next unless c.valid_encoding? if c.respond_to? :valid_encoding?
|
217
|
+
c_hex = c.unpack('H8')
|
218
|
+
y = c.ya2yaml(
|
219
|
+
:escape_b_specific => true,
|
220
|
+
:escape_as_utf8 => true
|
221
|
+
)
|
222
|
+
r = YAML.load(y)
|
223
|
+
assert_equal(
|
224
|
+
(c == "\xc2\x85" ? "\n" : c), # "\N" is normalized as "\n"
|
225
|
+
r,
|
226
|
+
"multi byte characters #{c_hex} should round-trip properly"
|
227
|
+
)
|
228
|
+
}
|
229
|
+
}
|
230
|
+
end
|
231
|
+
|
232
|
+
def test_roundtrip_ambiguous_string
|
233
|
+
[
|
234
|
+
'true',
|
235
|
+
'false',
|
236
|
+
'TRUE',
|
237
|
+
'FALSE',
|
238
|
+
'Y',
|
239
|
+
'N',
|
240
|
+
'y',
|
241
|
+
'n',
|
242
|
+
'on',
|
243
|
+
'off',
|
244
|
+
true,
|
245
|
+
false,
|
246
|
+
'0b0101',
|
247
|
+
'-0b0101',
|
248
|
+
0b0101,
|
249
|
+
-0b0101,
|
250
|
+
'031',
|
251
|
+
'-031',
|
252
|
+
031,
|
253
|
+
-031,
|
254
|
+
'123.001e-1',
|
255
|
+
'123.01',
|
256
|
+
'123',
|
257
|
+
123.001e-1,
|
258
|
+
123.01,
|
259
|
+
123,
|
260
|
+
'-123.001e-1',
|
261
|
+
'-123.01',
|
262
|
+
'-123',
|
263
|
+
-123.001e-1,
|
264
|
+
-123.01,
|
265
|
+
-123,
|
266
|
+
'INF',
|
267
|
+
'inf',
|
268
|
+
'NaN',
|
269
|
+
'nan',
|
270
|
+
'0xfe2a',
|
271
|
+
'-0xfe2a',
|
272
|
+
0xfe2a,
|
273
|
+
-0xfe2a,
|
274
|
+
'1:23:32.0200',
|
275
|
+
'1:23:32',
|
276
|
+
'-1:23:32.0200',
|
277
|
+
'-1:23:32',
|
278
|
+
'<<',
|
279
|
+
'~',
|
280
|
+
'null',
|
281
|
+
'nUll',
|
282
|
+
'Null',
|
283
|
+
'NULL',
|
284
|
+
'',
|
285
|
+
nil,
|
286
|
+
'2006-09-12',
|
287
|
+
'2006-09-11T17:28:07Z',
|
288
|
+
'2006-09-11T17:28:07+09:00',
|
289
|
+
'2006-09-11 17:28:07.662694 +09:00',
|
290
|
+
'=',
|
291
|
+
].each {|c|
|
292
|
+
['', 'hoge'].each {|ext|
|
293
|
+
src = (c.class == String) ? (c + ext) : c
|
294
|
+
y = src.ya2yaml(
|
295
|
+
:escape_as_utf8 => true
|
296
|
+
)
|
297
|
+
r = YAML.load(y)
|
298
|
+
assert_equal(
|
299
|
+
src,
|
300
|
+
r,
|
301
|
+
'ambiguous elements should round-trip properly'
|
302
|
+
)
|
303
|
+
}
|
304
|
+
}
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_roundtrip_string
|
308
|
+
chars = "aあ\t\-\?,\[\{\#&\*!\|>'\"\%\@\`.\\ \n\xc2\xa0\xe2\x80\xa8".split('')
|
309
|
+
|
310
|
+
chars.each {|i|
|
311
|
+
chars.each {|j|
|
312
|
+
chars.each {|k|
|
313
|
+
src = i + j + k
|
314
|
+
y = src.ya2yaml(
|
315
|
+
:printable_with_syck => true,
|
316
|
+
:escape_b_specific => true,
|
317
|
+
:escape_as_utf8 => true
|
318
|
+
)
|
319
|
+
r = YAML.load(y)
|
320
|
+
assert_equal(
|
321
|
+
src,
|
322
|
+
r,
|
323
|
+
'string of special characters should round-trip properly'
|
324
|
+
)
|
325
|
+
}
|
326
|
+
}
|
327
|
+
}
|
328
|
+
end
|
329
|
+
|
330
|
+
# patch by pawel.j.radecki at gmail.com. thanks!
|
331
|
+
def test_roundtrip_symbols
|
332
|
+
symbol1 = :"Batman: The Dark Knight - Why So Serious?!"
|
333
|
+
result_symbol1 = YAML.load(symbol1.ya2yaml)
|
334
|
+
assert_equal(symbol1, result_symbol1)
|
335
|
+
|
336
|
+
symbol2 = :"Batman: The Dark Knight - \"Why So Serious?!\""
|
337
|
+
result_symbol2 = YAML.load(symbol2.ya2yaml)
|
338
|
+
assert_equal(symbol2, result_symbol2)
|
337
339
|
|
338
340
|
# # YAML.load problem: the quotes within the symbol are lost here
|
339
341
|
# symbol3 = :"\"Batman: The Dark Knight - Why So Serious?!\""
|
340
342
|
# result_symbol3 = YAML.load(symbol3.ya2yaml)
|
341
|
-
# assert_equal(symbol3,result_symbol3)
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
343
|
+
# assert_equal(symbol3, result_symbol3)
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_roundtrip_types
|
347
|
+
objects = [
|
348
|
+
[],
|
349
|
+
[1],
|
350
|
+
{},
|
351
|
+
{'foo' => 'bar'},
|
352
|
+
nil,
|
353
|
+
'hoge',
|
354
|
+
"abc\nxyz\n",
|
355
|
+
(s = "\xff\xff"),
|
356
|
+
true,
|
357
|
+
false,
|
358
|
+
1000,
|
359
|
+
1000.1,
|
360
|
+
-1000,
|
361
|
+
-1000.1,
|
362
|
+
Date.new(2009, 2, 9),
|
363
|
+
Time.local(2009, 2, 9, 16, 35, 22),
|
364
|
+
:foo,
|
365
|
+
1..10,
|
366
|
+
/abc\nxyz/i,
|
367
|
+
@struct,
|
368
|
+
@klass,
|
369
|
+
]
|
370
|
+
s.force_encoding("BINARY") if s.respond_to? :force_encoding
|
371
|
+
|
372
|
+
objects.each {|obj|
|
373
|
+
src = case obj.class.to_s
|
374
|
+
when 'Array'
|
375
|
+
(obj.length) == 0 ? [] : objects
|
376
|
+
when 'Hash'
|
377
|
+
if (obj.length) == 0
|
378
|
+
{}
|
379
|
+
else
|
380
|
+
h = {}
|
381
|
+
c = 0
|
382
|
+
objects.each {|val|
|
383
|
+
h[c] = {}
|
384
|
+
objects.each {|key|
|
385
|
+
h[c][key] = val unless (key.class == Hash || key.class == Moo)
|
386
|
+
}
|
387
|
+
c += 1
|
388
|
+
}
|
389
|
+
h
|
390
|
+
end
|
391
|
+
else
|
392
|
+
obj
|
393
|
+
end
|
394
|
+
y = src.ya2yaml(
|
395
|
+
:syck_compatible => true
|
396
|
+
)
|
397
|
+
|
398
|
+
r = YAML.load(y)
|
399
|
+
assert_equal(
|
400
|
+
src,
|
401
|
+
r,
|
402
|
+
'types other than String should round-trip properly'
|
403
|
+
)
|
404
|
+
}
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_roundtrip_various
|
408
|
+
[
|
409
|
+
[1, 2, ['c', 'd', [[['e']], []], 'f'], 3, Time.local(2009, 2, 9, 17, 9), [[:foo]], nil, true, false, [], {}, {[123, 223]=>456}, {[1]=>2, 'a'=>'b', 'c' => [9, 9, 9], Time.local(2009, 2, 9, 17, 10) => 'hoge'}, ],
|
410
|
+
[],
|
411
|
+
{[123, 223]=>456},
|
412
|
+
{},
|
413
|
+
{'foo' => {1 => {2=>3, 4=>5}, 6 => [7, 8]}},
|
414
|
+
"abc",
|
415
|
+
" abc\n def\ndef\ndef\ndef\ndef\n",
|
416
|
+
"abc\n def\ndef\n",
|
417
|
+
"abc\n def\ndef\n\n",
|
418
|
+
"abc\n def\ndef\n\n ",
|
419
|
+
"abc\n def\ndef\n \n",
|
420
|
+
"abc\n def\ndef \n \n",
|
421
|
+
"abc\n def\ndef \n \n ",
|
422
|
+
' ほげほげほげ',
|
423
|
+
{"ほげ\nほげ\n ほげ" => 123},
|
424
|
+
[["ほげ\nほげ\n ほげ"]],
|
425
|
+
"ほげh\x4fge\nほげ\nほげ",
|
426
|
+
[{'ほげ'=>'abc', "ほげ\nほげ"=>'ほげ'}, 'ほげ', @text],
|
427
|
+
[Date.today, -9.011, 0.023, 4, -5, {1=>-2, -1=>@text, '_foo'=>'bar', 'ぬお-ぬお'=>321}],
|
428
|
+
{1=>-2, -1=>@gif, '_foo'=>'bar', 'ぬお-ぬお'=>321},
|
429
|
+
].each {|src|
|
430
|
+
y = src.ya2yaml(
|
431
|
+
:syck_compatible => true
|
432
|
+
)
|
433
|
+
|
434
|
+
r = YAML.load(y)
|
435
|
+
assert_equal(
|
436
|
+
src,
|
437
|
+
r,
|
438
|
+
'various types should round-trip properly'
|
439
|
+
)
|
440
|
+
}
|
441
|
+
end
|
442
|
+
|
443
|
+
def test_circular_reference
|
444
|
+
a = []
|
445
|
+
a << a
|
446
|
+
assert_raise(
|
447
|
+
ArgumentError,
|
448
|
+
'Object#ya2yaml should raise ArgumentError when the object includes a circular reference'
|
449
|
+
) {
|
450
|
+
a.ya2yaml
|
451
|
+
}
|
452
|
+
end
|
453
|
+
|
454
|
+
def test_binary
|
455
|
+
return if RUBY_VERSION < '1.9.0'
|
456
|
+
|
457
|
+
y = nil
|
458
|
+
assert_nothing_raised(
|
459
|
+
"Ya2YAML#string_type should dump 'ASCII-8BIT' strings as '!binary'"
|
460
|
+
) {
|
461
|
+
y = '日本語'.force_encoding('ASCII-8BIT').ya2yaml
|
462
|
+
}
|
463
|
+
assert_equal(
|
464
|
+
"--- !binary |\n 5pel5pys6Kqe\n\n",
|
465
|
+
y,
|
466
|
+
"Ya2YAML#string_type should dump 'ASCII-8BIT' strings as '!binary'"
|
467
|
+
)
|
468
|
+
|
469
|
+
assert_nothing_raised(
|
470
|
+
"Ya2YAML#string_type should dump strings with invalid encodings as '!binary'"
|
471
|
+
) {
|
472
|
+
y = '日本語'.encode('EUC-JP').force_encoding('UTF-8').ya2yaml
|
473
|
+
}
|
474
|
+
assert_equal(
|
475
|
+
"--- !binary |\n xvzL3Ljs\n\n",
|
476
|
+
y,
|
477
|
+
"Ya2YAML#string_type should dump strings with invalid encodings as '!binary'"
|
478
|
+
)
|
479
|
+
end
|
440
480
|
|
441
481
|
end
|