ya2yaml 0.26 → 0.29.2

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 +2 -1
  2. data/lib/ya2yaml.rb +27 -21
  3. data/test/test.rb +108 -49
  4. metadata +53 -34
data/README CHANGED
@@ -8,7 +8,8 @@ Ya2YAML is "yet another to_yaml". It emits YAML document with complete UTF8 supp
8
8
 
9
9
  *code*:
10
10
 
11
- $KCODE = 'UTF8'
11
+ # encoding: UTF-8
12
+ $KCODE = 'UTF8' unless RUBY_VERSION >= '1.9'
12
13
  require 'ya2yaml'
13
14
 
14
15
  obj = [
@@ -1,6 +1,6 @@
1
- #!/usr/bin/env ruby
1
+ # encoding: UTF-8
2
2
 
3
- # $Id: ya2yaml.rb,v 0.26 2007-01-19 20:42:42+09 funai Exp funai $
3
+ # $Id: ya2yaml.rb,v 0.29 2009/02/09 09:01:30 funai Exp funai $
4
4
  #
5
5
  # Author:: Akira FUNAI
6
6
  # Copyright:: Copyright (c) 2006 Akira FUNAI
@@ -8,7 +8,8 @@
8
8
 
9
9
  class Ya2YAML
10
10
 
11
- def initialize(options = {})
11
+ def initialize(opts = {})
12
+ options = opts.dup
12
13
  options[:indent_size] = 2 if options[:indent_size].to_i <= 0
13
14
  options[:minimum_block_length] = 0 if options[:minimum_block_length].to_i <= 0
14
15
  options.update(
@@ -23,15 +24,15 @@ class Ya2YAML
23
24
  end
24
25
 
25
26
  def _ya2yaml(obj)
26
- throw 'set $KCODE to "UTF8".' if $KCODE != 'UTF8'
27
+ throw 'set $KCODE to "UTF8".' if (RUBY_VERSION < '1.9.0') && ($KCODE != 'UTF8')
27
28
  '--- ' + emit(obj,1) + "\n"
28
29
  end
29
30
 
30
31
  private
31
32
 
32
33
  def emit(obj,level)
33
- case obj.class.to_s
34
- when 'Array'
34
+ case obj
35
+ when Array
35
36
  if (obj.length == 0)
36
37
  '[]'
37
38
  else
@@ -40,7 +41,7 @@ class Ya2YAML
40
41
  indent + '- ' + emit(o,level + 1)
41
42
  }.join('')
42
43
  end
43
- when 'Hash'
44
+ when Hash
44
45
  if (obj.length == 0)
45
46
  '{}'
46
47
  else
@@ -69,17 +70,17 @@ class Ya2YAML
69
70
  end
70
71
  }.join('')
71
72
  end
72
- when 'NilClass'
73
+ when NilClass
73
74
  '~'
74
- when 'String'
75
+ when String
75
76
  emit_string(obj,level)
76
- when 'TrueClass','FalseClass'
77
+ when TrueClass,FalseClass
77
78
  obj.to_s
78
- when 'Fixnum','Bignum','Float'
79
+ when Fixnum,Bignum,Float
79
80
  obj.to_s
80
- when 'Date'
81
+ when Date
81
82
  obj.to_s
82
- when 'Time'
83
+ when Time
83
84
  offset = obj.gmtoff
84
85
  off_hm = sprintf(
85
86
  '%+.2d:%.2d',
@@ -88,11 +89,11 @@ class Ya2YAML
88
89
  )
89
90
  u_sec = (obj.usec != 0) ? sprintf(".%.6d",obj.usec) : ''
90
91
  obj.strftime("%Y-%m-%d %H:%M:%S#{u_sec} #{off_hm}")
91
- when 'Symbol'
92
- '!ruby/symbol ' + obj.to_s
93
- when 'Range'
92
+ when Symbol
93
+ '!ruby/symbol ' + emit_string(obj.to_s,level)
94
+ when Range
94
95
  '!ruby/range ' + obj.to_s
95
- when 'Regexp'
96
+ when Regexp
96
97
  '!ruby/regexp ' + obj.inspect
97
98
  else
98
99
  case
@@ -105,7 +106,7 @@ class Ya2YAML
105
106
  # serialized as a generic object
106
107
  object_members = {}
107
108
  obj.instance_variables.each{|k,v|
108
- object_members[k.sub(/^@/,'')] = obj.instance_variable_get(k)
109
+ object_members[k.to_s.sub(/^@/,'')] = obj.instance_variable_get(k)
109
110
  }
110
111
  '!ruby/object:' + obj.class.to_s + ' ' +
111
112
  emit(object_members,level + 1)
@@ -183,6 +184,9 @@ class Ya2YAML
183
184
  end
184
185
 
185
186
  def string_type(str)
187
+ if str.respond_to?(:valid_encoding?) && !str.valid_encoding?
188
+ return false,false,false,false
189
+ end
186
190
  (ucs_codes = str.unpack('U*')) rescue (
187
191
  # ArgumentError -> binary data
188
192
  return false,false,false,false
@@ -192,10 +196,10 @@ class Ya2YAML
192
196
  str =~ /\A#{REX_ANY_LB}* | #{REX_ANY_LB}*\z|#{REX_ANY_LB}{2}\z/
193
197
  )
194
198
  # detour Syck bug
195
- return true,false,is_one_line?(str),false
199
+ return true,false,nil,false
196
200
  end
197
201
  ucs_codes.each {|ucs_code|
198
- return true,false,is_one_line?(str),false unless is_printable?(ucs_code)
202
+ return true,false,nil,false unless is_printable?(ucs_code)
199
203
  }
200
204
  return true,true,is_one_line?(str),is_one_plain_line?(str)
201
205
  end
@@ -249,7 +253,9 @@ class Ya2YAML
249
253
  when is_printable?(ucs_code)
250
254
  c
251
255
  when @options[:escape_as_utf8]
252
- '\\x' + c.unpack('H2' * c.size).join('\\x')
256
+ c.respond_to?(:bytes) ?
257
+ c.bytes.collect {|b| '\\x%.2x' % b }.join :
258
+ '\\x' + c.unpack('H2' * c.size).join('\\x')
253
259
  when ucs_code == 0x2028 || ucs_code == 0x2029
254
260
  ESCAPE_SEQ_LB[c]
255
261
  when ucs_code <= 0x7f
@@ -1,17 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: UTF-8
2
3
 
3
- # $Id: test.rb,v 1.4 2007-01-19 20:41:37+09 funai Exp funai $
4
+ # $Id: test.rb,v 1.6 2009/02/09 09:00:57 funai Exp funai $
4
5
 
5
- $: << (File.dirname(__FILE__) + '/../lib')
6
+ $:.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
6
7
 
7
8
  Dir.chdir(File.dirname(__FILE__))
8
9
 
9
- $KCODE = 'UTF8'
10
+ $KCODE = 'UTF8' if RUBY_VERSION < '1.9.0'
10
11
  require 'ya2yaml'
11
12
 
12
13
  require 'yaml'
13
14
  require 'test/unit'
14
15
 
16
+ # There's an incompatibility in how ruby handles struct dumps
17
+ # between versions that's beyond our scope.
18
+ # (One uses strings internally, the other symbols.)
19
+ # This just enables tests to pass.
20
+ class << Struct
21
+ alias yaml_new_without_indifferent_keys yaml_new
22
+ def yaml_new(klass, tag, val)
23
+ val.keys.each { |k, v| val[k.to_sym] = val.delete(k) }
24
+ yaml_new_without_indifferent_keys(klass, tag, val)
25
+ end
26
+ end if RUBY_VERSION >= "1.9"
27
+
15
28
  class TC_Ya2YAML < Test::Unit::TestCase
16
29
 
17
30
  @@struct_klass = Struct::new('Foo',:bar,:buz)
@@ -30,16 +43,21 @@ class TC_Ya2YAML < Test::Unit::TestCase
30
43
  puts "test may take minutes. please wait.\n"
31
44
 
32
45
  def setup
33
- @text = ''
34
- @gif = ''
35
- File.open('./t.yaml','r') {|f| @text = f.read}
36
- File.open('./t.gif','r') {|f| @gif = f.read}
37
-
38
- @struct = @@struct_klass.new('barbarbar',@@struct_klass.new('baaaar',12345))
39
- @klass = Moo.new('boobooboo',Time.new())
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))
40
50
  end
41
51
 
42
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
+
43
61
  [
44
62
  [
45
63
  {},
@@ -75,9 +93,11 @@ class TC_Ya2YAML < Test::Unit::TestCase
75
93
  ],
76
94
  ].each {|opt,yaml|
77
95
  y = ["\xc2\x86","a\xe2\x80\xa8b\xe2\x80\xa9c"," abc\nxyz"].ya2yaml(opt)
78
- # puts y
79
-
80
- assert_equal(y,yaml)
96
+ assert_equal(
97
+ yaml,
98
+ y,
99
+ "option #{opt.inspect} should be recognized"
100
+ )
81
101
  }
82
102
  end
83
103
 
@@ -107,8 +127,11 @@ class TC_Ya2YAML < Test::Unit::TestCase
107
127
  }.ya2yaml(
108
128
  :hash_order => hash_order
109
129
  )
110
- # p y
111
- assert_equal(y,yaml)
130
+ assert_equal(
131
+ yaml,
132
+ y,
133
+ 'hash order should be kept when :hash_order provided'
134
+ )
112
135
  }
113
136
  end
114
137
 
@@ -129,8 +152,11 @@ class TC_Ya2YAML < Test::Unit::TestCase
129
152
  y = src.ya2yaml(
130
153
  :minimum_block_length => 16
131
154
  )
132
- # p y
133
- assert_equal(y,yaml)
155
+ assert_equal(
156
+ yaml,
157
+ y,
158
+ 'line breaks should be normalized to fit the format.'
159
+ )
134
160
  }
135
161
  end
136
162
 
@@ -140,16 +166,23 @@ class TC_Ya2YAML < Test::Unit::TestCase
140
166
  [Struct.new(:foo).new(123), "--- !ruby/struct: \n foo: 123\n",],
141
167
  ].each {|src,yaml|
142
168
  y = src.ya2yaml()
143
- assert_equal(y,yaml)
169
+ assert_equal(
170
+ yaml,
171
+ y,
172
+ 'ruby struct should be serialized properly'
173
+ )
144
174
  }
145
175
  end
146
176
 
147
177
  def test_roundtrip_single_byte_char
148
178
  ("\x00".."\x7f").each {|c|
149
179
  y = c.ya2yaml()
150
- # puts y
151
180
  r = YAML.load(y)
152
- assert_equal((c == "\r" ? "\n" : c),r) # "\r" is normalized as "\n".
181
+ assert_equal(
182
+ (c == "\r" ? "\n" : c), # "\r" is normalized as "\n"
183
+ r,
184
+ 'single byte characters should round-trip properly'
185
+ )
153
186
  }
154
187
  end
155
188
 
@@ -177,18 +210,19 @@ class TC_Ya2YAML < Test::Unit::TestCase
177
210
  0x10ffff,
178
211
  ].each {|ucs_code|
179
212
  [-1,0,1].each {|ofs|
180
- (c = [ucs_code + ofs].pack('U')) rescue next
213
+ (c = [ucs_code + ofs].pack('U'))
214
+ next unless c.valid_encoding? if c.respond_to? :valid_encoding?
181
215
  c_hex = c.unpack('H8')
182
216
  y = c.ya2yaml(
183
217
  :escape_b_specific => true,
184
218
  :escape_as_utf8 => true
185
219
  )
186
- # puts y
187
220
  r = YAML.load(y)
188
221
  assert_equal(
189
- [c_hex,(c =~ /\xc2\x85/u ? "\n" : c)],
190
- [c_hex,r]
191
- ) # "\N" is normalized as "\n".
222
+ (c == "\xc2\x85" ? "\n" : c), # "\N" is normalized as "\n"
223
+ r,
224
+ "multi byte characters #{c_hex} should round-trip properly"
225
+ )
192
226
  }
193
227
  }
194
228
  end
@@ -254,13 +288,16 @@ class TC_Ya2YAML < Test::Unit::TestCase
254
288
  '=',
255
289
  ].each {|c|
256
290
  ['','hoge'].each {|ext|
257
- src = c.class == String ? (c + ext) : c
291
+ src = (c.class == String) ? (c + ext) : c
258
292
  y = src.ya2yaml(
259
293
  :escape_as_utf8 => true
260
294
  )
261
- # puts y
262
295
  r = YAML.load(y)
263
- assert_equal(src,r)
296
+ assert_equal(
297
+ src,
298
+ r,
299
+ 'ambiguous elements should round-trip properly'
300
+ )
264
301
  }
265
302
  }
266
303
  end
@@ -271,22 +308,39 @@ class TC_Ya2YAML < Test::Unit::TestCase
271
308
  chars.each {|i|
272
309
  chars.each {|j|
273
310
  chars.each {|k|
274
- chars.each {|l|
275
- src = i + j + k + l
276
- y = src.ya2yaml(
277
- :printable_with_syck => true,
278
- :escape_b_specific => true,
279
- :escape_as_utf8 => true
280
- )
281
- # puts y
282
- r = YAML.load(y)
283
- assert_equal(src,r)
284
- }
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
+ )
285
323
  }
286
324
  }
287
325
  }
288
326
  end
289
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)
337
+
338
+ # # YAML.load problem: the quotes within the symbol are lost here
339
+ # symbol3 = :"\"Batman: The Dark Knight - Why So Serious?!\""
340
+ # result_symbol3 = YAML.load(symbol3.ya2yaml)
341
+ # assert_equal(symbol3,result_symbol3)
342
+ end
343
+
290
344
  def test_roundtrip_types
291
345
  objects = [
292
346
  [],
@@ -296,21 +350,22 @@ class TC_Ya2YAML < Test::Unit::TestCase
296
350
  nil,
297
351
  'hoge',
298
352
  "abc\nxyz\n",
299
- "\xff\xff",
353
+ (s = "\xff\xff"),
300
354
  true,
301
355
  false,
302
356
  1000,
303
357
  1000.1,
304
358
  -1000,
305
359
  -1000.1,
306
- Date.today(),
307
- Time.new(),
360
+ Date.new(2009,2,9),
361
+ Time.local(2009,2,9,16,35,22),
308
362
  :foo,
309
363
  1..10,
310
364
  /abc\nxyz/i,
311
365
  @struct,
312
366
  @klass,
313
367
  ]
368
+ s.force_encoding("BINARY") if s.respond_to? :force_encoding
314
369
 
315
370
  objects.each {|obj|
316
371
  src = case obj.class.to_s
@@ -337,16 +392,19 @@ class TC_Ya2YAML < Test::Unit::TestCase
337
392
  y = src.ya2yaml(
338
393
  :syck_compatible => true
339
394
  )
340
- # puts y
341
395
 
342
396
  r = YAML.load(y)
343
- assert_equal(src,r)
397
+ assert_equal(
398
+ src,
399
+ r,
400
+ 'types other than String should round-trip properly'
401
+ )
344
402
  }
345
403
  end
346
404
 
347
405
  def test_roundtrip_various
348
406
  [
349
- [1,2,['c','d',[[['e']],[]],'f'],3,Time.new(),[[:foo]],nil,true,false,[],{},{[123,223]=>456},{[1]=>2,'a'=>'b','c' => [9,9,9],Time.new() => 'hoge'},],
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'},],
350
408
  [],
351
409
  {[123,223]=>456},
352
410
  {},
@@ -370,13 +428,14 @@ class TC_Ya2YAML < Test::Unit::TestCase
370
428
  y = src.ya2yaml(
371
429
  :syck_compatible => true
372
430
  )
373
- # puts y
374
431
 
375
432
  r = YAML.load(y)
376
- assert_equal(src,r)
433
+ assert_equal(
434
+ src,
435
+ r,
436
+ 'various types should round-trip properly'
437
+ )
377
438
  }
378
439
  end
379
440
 
380
441
  end
381
-
382
- __END__
metadata CHANGED
@@ -1,33 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
3
- specification_version: 1
4
2
  name: ya2yaml
5
3
  version: !ruby/object:Gem::Version
6
- version: "0.26"
7
- date: 2007-01-19 00:00:00 +09:00
8
- summary: An UTF8 safe YAML dumper.
9
- require_paths:
10
- - lib
11
- email: funai.akira@gmail.com
12
- homepage: http://rubyforge.org/projects/ya2yaml/
13
- rubyforge_project:
14
- description:
15
- autorequire: ya2yaml
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 29
8
+ - 2
9
+ version: 0.29.2
25
10
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
11
  authors:
30
12
  - Akira FUNAI
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain:
16
+ date: 2010-05-04 00:00:00 +09:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: |
21
+ Ya2YAML is "yet another to_yaml". It emits YAML document with complete UTF8 support (string/binary detection, "\u" escape sequences and Unicode specific line breaks).
22
+
23
+ email: akira@funai.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README
31
30
  files:
32
31
  - lib/ya2yaml.rb
33
32
  - README
@@ -35,20 +34,40 @@ files:
35
34
  - test/t.gif
36
35
  - test/t.yaml
37
36
  - test/test.rb
38
- test_files:
39
- - test/test.rb
37
+ has_rdoc: true
38
+ homepage: http://rubyforge.org/projects/ya2yaml/
39
+ licenses: []
40
+
41
+ post_install_message:
40
42
  rdoc_options:
41
43
  - --main
42
44
  - README
43
45
  - --charset
44
46
  - UTF8
45
- extra_rdoc_files:
46
- - README
47
- executables: []
48
-
49
- extensions: []
50
-
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">"
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ - 0
56
+ - 0
57
+ version: 0.0.0
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
51
65
  requirements: []
52
66
 
53
- dependencies: []
54
-
67
+ rubyforge_project: ya2yaml
68
+ rubygems_version: 1.3.6
69
+ signing_key:
70
+ specification_version: 1
71
+ summary: An UTF8 safe YAML dumper.
72
+ test_files:
73
+ - test/test.rb