mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/psych/lib/psych/coder.rb: Adding Syck compatibility to the yaml coder
* test/psych/test_coder.rb: test for syck compatibility git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27130 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
6024074540
commit
69c6ee0b45
2 changed files with 35 additions and 1 deletions
|
@ -7,7 +7,7 @@ module Psych
|
|||
# called, respectively.
|
||||
class Coder
|
||||
attr_accessor :tag, :style, :implicit
|
||||
attr_reader :type, :map, :scalar, :seq
|
||||
attr_reader :type, :scalar, :seq
|
||||
|
||||
def initialize tag
|
||||
@map = {}
|
||||
|
@ -19,6 +19,14 @@ module Psych
|
|||
@scalar = nil
|
||||
end
|
||||
|
||||
# Emit a map. The coder will be yielded to the block.
|
||||
def map tag = @tag, style = @style
|
||||
@tag = tag
|
||||
@style = style
|
||||
yield self if block_given?
|
||||
@map
|
||||
end
|
||||
|
||||
# Emit a scalar with +value+ and +tag+
|
||||
def represent_scalar tag, value
|
||||
self.tag = tag
|
||||
|
@ -53,6 +61,7 @@ module Psych
|
|||
@type = :map
|
||||
@map[k] = v
|
||||
end
|
||||
alias :add :[]=
|
||||
|
||||
def [] k
|
||||
@type = :map
|
||||
|
|
|
@ -89,6 +89,31 @@ module Psych
|
|||
end
|
||||
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.load(Psych.dump(RepresentWithMap.new))
|
||||
assert_equal({ 'a' => 'b' }, thing.map)
|
||||
|
|
Loading…
Reference in a new issue