ya2yaml 0.29.2 → 0.30

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