diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-04-09 08:36:24 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-04-09 08:36:24 -0700 |
commit | 6faec131cb140948003fff0a89064f737e3b1b4d (patch) | |
tree | 0423d8e7b70bda84c129e42929a8a2dbde7774af | |
parent | 30f530721879c79154c628cc450eada400742ad3 (diff) | |
download | psych-6faec131cb140948003fff0a89064f737e3b1b4d.tar.gz |
merging from ruby to psych
-rw-r--r--[-rwxr-xr-x] | ext/psych/emitter.c | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | ext/psych/parser.c | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | ext/psych/psych.c | 0 | ||||
-rw-r--r-- | lib/psych.rb | 11 | ||||
-rw-r--r-- | lib/psych/coder.rb | 11 | ||||
-rw-r--r-- | lib/psych/core_ext.rb | 5 | ||||
-rw-r--r-- | lib/psych/nodes/node.rb | 10 | ||||
-rw-r--r-- | test/psych/.swp | bin | 0 -> 12288 bytes | |||
-rw-r--r-- | test/psych/test_coder.rb | 25 | ||||
-rw-r--r-- | test/psych/test_emitter.rb | 2 | ||||
-rw-r--r-- | test/psych/test_engine_manager.rb | 57 | ||||
-rw-r--r-- | test/psych/test_psych.rb | 19 |
12 files changed, 130 insertions, 10 deletions
diff --git a/ext/psych/emitter.c b/ext/psych/emitter.c index befa98e..befa98e 100755..100644 --- a/ext/psych/emitter.c +++ b/ext/psych/emitter.c diff --git a/ext/psych/parser.c b/ext/psych/parser.c index dd64c25..dd64c25 100755..100644 --- a/ext/psych/parser.c +++ b/ext/psych/parser.c diff --git a/ext/psych/psych.c b/ext/psych/psych.c index 69ff1d8..69ff1d8 100755..100644 --- a/ext/psych/psych.c +++ b/ext/psych/psych.c diff --git a/lib/psych.rb b/lib/psych.rb index c142593..9b4b215 100644 --- a/lib/psych.rb +++ b/lib/psych.rb @@ -1,4 +1,4 @@ -require 'psych/psych' +require 'psych.so' require 'psych/nodes' require 'psych/visitors' require 'psych/handler' @@ -153,10 +153,15 @@ module Psych # Example: # # Psych.dump(['a', 'b']) # => "---\n- a\n- b\n" - def self.dump o, options = {} + def self.dump o, io = nil, options = {} + if Hash === io + options = io + io = nil + end + visitor = Psych::Visitors::YAMLTree.new options visitor << o - visitor.tree.to_yaml + visitor.tree.to_yaml io end ### diff --git a/lib/psych/coder.rb b/lib/psych/coder.rb index 79e46da..eff0cc3 100644 --- a/lib/psych/coder.rb +++ b/lib/psych/coder.rb @@ -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 diff --git a/lib/psych/core_ext.rb b/lib/psych/core_ext.rb index bd22219..9c55c70 100644 --- a/lib/psych/core_ext.rb +++ b/lib/psych/core_ext.rb @@ -12,11 +12,14 @@ class Object def psych_to_yaml options = {} Psych.dump self, options end + remove_method :to_yaml rescue nil alias :to_yaml :psych_to_yaml end module Kernel - def y *objects + def psych_y *objects puts Psych.dump_stream(*objects) end + remove_method :y rescue nil + alias y psych_y end diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb index 0de768c..3ab9aca 100644 --- a/lib/psych/nodes/node.rb +++ b/lib/psych/nodes/node.rb @@ -30,10 +30,12 @@ module Psych # Convert this node to YAML. # # See also Psych::Visitors::Emitter - def to_yaml - io = StringIO.new - Visitors::Emitter.new(io).accept self - io.string + def to_yaml io = nil + real_io = io || StringIO.new + + Visitors::Emitter.new(real_io).accept self + return real_io.string unless io + io end end end diff --git a/test/psych/.swp b/test/psych/.swp Binary files differnew file mode 100644 index 0000000..ea9da4d --- /dev/null +++ b/test/psych/.swp diff --git a/test/psych/test_coder.rb b/test/psych/test_coder.rb index deef6f2..0fa01ca 100644 --- a/test/psych/test_coder.rb +++ b/test/psych/test_coder.rb @@ -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) diff --git a/test/psych/test_emitter.rb b/test/psych/test_emitter.rb index c907653..3738c1a 100644 --- a/test/psych/test_emitter.rb +++ b/test/psych/test_emitter.rb @@ -6,7 +6,7 @@ module Psych class TestEmitter < TestCase def setup super - @out = StringIO.new + @out = StringIO.new('') @emitter = Psych::Emitter.new @out end diff --git a/test/psych/test_engine_manager.rb b/test/psych/test_engine_manager.rb new file mode 100644 index 0000000..8bd7d6c --- /dev/null +++ b/test/psych/test_engine_manager.rb @@ -0,0 +1,57 @@ +require_relative 'helper' +require 'yaml' + +module Psych + class TestEngineManager < TestCase + def teardown + YAML::ENGINE.yamler = 'syck' + end + + def test_bad_engine + assert_raises(ArgumentError) do + YAML::ENGINE.yamler = 'foooo' + end + end + + def test_set_psych + YAML::ENGINE.yamler = 'psych' + assert_equal Psych, YAML + assert_equal 'psych', YAML::ENGINE.yamler + end + + def test_set_syck + YAML::ENGINE.yamler = 'syck' + assert_equal Syck, YAML + assert_equal 'syck', YAML::ENGINE.yamler + end + + A = Struct.new(:name) + + def test_dump_types + YAML::ENGINE.yamler = 'psych' + + assert_to_yaml ::Object.new + assert_to_yaml Time.now + assert_to_yaml Date.today + assert_to_yaml('a' => 'b') + assert_to_yaml A.new('foo') + assert_to_yaml %w{a b} + assert_to_yaml Exception.new('foo') + assert_to_yaml "hello!" + assert_to_yaml :fooo + assert_to_yaml(1..10) + assert_to_yaml(/hello!~/) + assert_to_yaml 1 + assert_to_yaml 1.2 + assert_to_yaml Rational(1, 2) + assert_to_yaml Complex(1, 2) + assert_to_yaml true + assert_to_yaml false + assert_to_yaml nil + end + + def assert_to_yaml obj + assert obj.to_yaml, "#{obj.class} to_yaml works" + end + end +end diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb index 88fe83c..22baa14 100644 --- a/test/psych/test_psych.rb +++ b/test/psych/test_psych.rb @@ -1,5 +1,8 @@ require_relative 'helper' +require 'stringio' +require 'tempfile' + class TestPsych < Psych::TestCase def test_dump_stream things = [22, "foo \n", {}] @@ -7,6 +10,22 @@ class TestPsych < Psych::TestCase assert_equal things, Psych.load_stream(stream) end + def test_dump_file + hash = {'hello' => 'TGIF!'} + Tempfile.open('fun.yml') do |io| + assert_equal io, Psych.dump(hash, io) + io.rewind + assert_equal Psych.dump(hash), io.read + end + end + + def test_dump_io + hash = {'hello' => 'TGIF!'} + stringio = StringIO.new '' + assert_equal stringio, Psych.dump(hash, stringio) + assert_equal Psych.dump(hash), stringio.string + end + def test_simple assert_equal 'foo', Psych.load("--- foo\n") end |