Permalink
Browse files

merging from ruby to psych

  • Loading branch information...
tenderlove committed Apr 9, 2010
1 parent 30f5307 commit 6faec131cb140948003fff0a89064f737e3b1b4d
View
0 ext/psych/emitter.c 100755 → 100644
No changes.
View
0 ext/psych/parser.c 100755 → 100644
No changes.
View
0 ext/psych/psych.c 100755 → 100644
No changes.
View
@@ -1,4 +1,4 @@
-require 'psych/psych'
+require 'psych.so'
require 'psych/nodes'
require 'psych/visitors'
require 'psych/handler'
@@ -153,10 +153,15 @@ def self.parse_stream yaml
# 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
###
View
@@ -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 @@ def initialize tag
@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 @@ def []= k, v
@type = :map
@map[k] = v
end
+ alias :add :[]=
def [] k
@type = :map
View
@@ -12,11 +12,14 @@ def self.yaml_tag url
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
View
@@ -30,10 +30,12 @@ def to_ruby
# 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
View
Binary file not shown.
View
@@ -89,6 +89,31 @@ def encode_with coder
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)
@@ -6,7 +6,7 @@ module Psych
class TestEmitter < TestCase
def setup
super
- @out = StringIO.new
+ @out = StringIO.new('')
@emitter = Psych::Emitter.new @out
end
@@ -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
View
@@ -1,12 +1,31 @@
require_relative 'helper'
+require 'stringio'
+require 'tempfile'
+
class TestPsych < Psych::TestCase
def test_dump_stream
things = [22, "foo \n", {}]
stream = Psych.dump_stream(*things)
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

0 comments on commit 6faec13

Please sign in to comment.