summaryrefslogtreecommitdiff
path: root/test/psych/visitors
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-28 21:49:37 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-28 21:49:37 +0000
commitb9b923ca942096ddb1062be2deb9e6de9a65f346 (patch)
treebc8466746efbe763c7e8a54390d9b34db1aa63c4 /test/psych/visitors
parenta8a99a7379fa8e07f217fc7b24e3a9208a967898 (diff)
* ext/psych/*: importing Psych to trunk
* test/psych/*: ditto * lib/psych/*: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27079 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/psych/visitors')
-rw-r--r--test/psych/visitors/test_emitter.rb124
-rw-r--r--test/psych/visitors/test_to_ruby.rb323
-rw-r--r--test/psych/visitors/test_yaml_tree.rb140
3 files changed, 587 insertions, 0 deletions
diff --git a/test/psych/visitors/test_emitter.rb b/test/psych/visitors/test_emitter.rb
new file mode 100644
index 0000000000..42987d113c
--- /dev/null
+++ b/test/psych/visitors/test_emitter.rb
@@ -0,0 +1,124 @@
+require 'test/psych/helper'
+
+module Psych
+ module Visitors
+ class TestEmitter < TestCase
+ def setup
+ super
+ @io = StringIO.new
+ @visitor = Visitors::Emitter.new @io
+ end
+
+ def test_stream
+ s = Nodes::Stream.new
+ @visitor.accept s
+ assert_equal '', @io.string
+ end
+
+ def test_document
+ s = Nodes::Stream.new
+ doc = Nodes::Document.new [1,1]
+ scalar = Nodes::Scalar.new 'hello world'
+
+ doc.children << scalar
+ s.children << doc
+
+ @visitor.accept s
+
+ assert_match(/1.1/, @io.string)
+ assert_equal @io.string, s.to_yaml
+ end
+
+ def test_document_implicit_end
+ s = Nodes::Stream.new
+ doc = Nodes::Document.new
+ mapping = Nodes::Mapping.new
+ mapping.children << Nodes::Scalar.new('key')
+ mapping.children << Nodes::Scalar.new('value')
+ doc.children << mapping
+ s.children << doc
+
+ @visitor.accept s
+
+ assert_match(/key: value/, @io.string)
+ assert_equal @io.string, s.to_yaml
+ assert(/\.\.\./ !~ s.to_yaml)
+ end
+
+ def test_scalar
+ s = Nodes::Stream.new
+ doc = Nodes::Document.new
+ scalar = Nodes::Scalar.new 'hello world'
+
+ doc.children << scalar
+ s.children << doc
+
+ @visitor.accept s
+
+ assert_match(/hello/, @io.string)
+ assert_equal @io.string, s.to_yaml
+ end
+
+ def test_scalar_with_tag
+ s = Nodes::Stream.new
+ doc = Nodes::Document.new
+ scalar = Nodes::Scalar.new 'hello world', nil, '!str', false, false, 5
+
+ doc.children << scalar
+ s.children << doc
+
+ @visitor.accept s
+
+ assert_match(/str/, @io.string)
+ assert_match(/hello/, @io.string)
+ assert_equal @io.string, s.to_yaml
+ end
+
+ def test_sequence
+ s = Nodes::Stream.new
+ doc = Nodes::Document.new
+ scalar = Nodes::Scalar.new 'hello world'
+ seq = Nodes::Sequence.new
+
+ seq.children << scalar
+ doc.children << seq
+ s.children << doc
+
+ @visitor.accept s
+
+ assert_match(/- hello/, @io.string)
+ assert_equal @io.string, s.to_yaml
+ end
+
+ def test_mapping
+ s = Nodes::Stream.new
+ doc = Nodes::Document.new
+ mapping = Nodes::Mapping.new
+ mapping.children << Nodes::Scalar.new('key')
+ mapping.children << Nodes::Scalar.new('value')
+ doc.children << mapping
+ s.children << doc
+
+ @visitor.accept s
+
+ assert_match(/key: value/, @io.string)
+ assert_equal @io.string, s.to_yaml
+ end
+
+ def test_alias
+ s = Nodes::Stream.new
+ doc = Nodes::Document.new
+ mapping = Nodes::Mapping.new
+ mapping.children << Nodes::Scalar.new('key', 'A')
+ mapping.children << Nodes::Alias.new('A')
+ doc.children << mapping
+ s.children << doc
+
+ @visitor.accept s
+
+ assert_match(/&A key: \*A/, @io.string)
+ assert_equal @io.string, s.to_yaml
+ end
+ end
+ end
+end
diff --git a/test/psych/visitors/test_to_ruby.rb b/test/psych/visitors/test_to_ruby.rb
new file mode 100644
index 0000000000..dc02fbc29d
--- /dev/null
+++ b/test/psych/visitors/test_to_ruby.rb
@@ -0,0 +1,323 @@
+require 'test/psych/helper'
+
+module Psych
+ module Visitors
+ class TestToRuby < TestCase
+ def setup
+ super
+ @visitor = ToRuby.new
+ end
+
+ def test_object
+ mapping = Nodes::Mapping.new nil, "!ruby/object"
+ mapping.children << Nodes::Scalar.new('foo')
+ mapping.children << Nodes::Scalar.new('bar')
+
+ o = mapping.to_ruby
+ assert_equal 'bar', o.instance_variable_get(:@foo)
+ end
+
+ def test_awesome
+ Psych.load('1900-01-01T00:00:00+00:00')
+ end
+
+ def test_legacy_struct
+ foo = Struct.new('AWESOME', :bar)
+ assert_equal foo.new('baz'), Psych.load(<<-eoyml)
+!ruby/struct:AWESOME
+ bar: baz
+ eoyml
+ end
+
+ def test_binary
+ gif = "GIF89a\f\x00\f\x00\x84\x00\x00\xFF\xFF\xF7\xF5\xF5\xEE\xE9\xE9\xE5fff\x00\x00\x00\xE7\xE7\xE7^^^\xF3\xF3\xED\x8E\x8E\x8E\xE0\xE0\xE0\x9F\x9F\x9F\x93\x93\x93\xA7\xA7\xA7\x9E\x9E\x9Eiiiccc\xA3\xA3\xA3\x84\x84\x84\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9!\xFE\x0EMade with GIMP\x00,\x00\x00\x00\x00\f\x00\f\x00\x00\x05, \x8E\x810\x9E\xE3@\x14\xE8i\x10\xC4\xD1\x8A\b\x1C\xCF\x80M$z\xEF\xFF0\x85p\xB8\xB01f\r\e\xCE\x01\xC3\x01\x1E\x10' \x82\n\x01\x00;"
+
+ hash = Psych.load(<<-'eoyaml')
+canonical: !!binary "\
+ R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\
+ OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\
+ +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\
+ AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="
+generic: !binary |
+ R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
+ OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
+ +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
+ AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
+description:
+ The binary value above is a tiny arrow encoded as a gif image.
+ eoyaml
+ assert_equal gif, hash['canonical']
+ assert_equal gif, hash['generic']
+ end
+
+ A = Struct.new(:foo)
+
+ def test_struct
+ s = A.new('bar')
+
+ mapping = Nodes::Mapping.new nil, "!ruby/struct:#{s.class}"
+ mapping.children << Nodes::Scalar.new('foo')
+ mapping.children << Nodes::Scalar.new('bar')
+
+ ruby = mapping.to_ruby
+
+ assert_equal s.class, ruby.class
+ assert_equal s.foo, ruby.foo
+ assert_equal s, ruby
+ end
+
+ def test_anon_struct_legacy
+ s = Struct.new(:foo).new('bar')
+
+ mapping = Nodes::Mapping.new nil, '!ruby/struct:'
+ mapping.children << Nodes::Scalar.new('foo')
+ mapping.children << Nodes::Scalar.new('bar')
+
+ assert_equal s.foo, mapping.to_ruby.foo
+ end
+
+ def test_anon_struct
+ s = Struct.new(:foo).new('bar')
+
+ mapping = Nodes::Mapping.new nil, '!ruby/struct'
+ mapping.children << Nodes::Scalar.new('foo')
+ mapping.children << Nodes::Scalar.new('bar')
+
+ assert_equal s.foo, mapping.to_ruby.foo
+ end
+
+ def test_exception
+ exc = Exception.new 'hello'
+
+ mapping = Nodes::Mapping.new nil, '!ruby/exception'
+ mapping.children << Nodes::Scalar.new('message')
+ mapping.children << Nodes::Scalar.new('hello')
+
+ ruby = mapping.to_ruby
+
+ assert_equal exc.class, ruby.class
+ assert_equal exc.message, ruby.message
+ end
+
+ def test_regexp
+ node = Nodes::Scalar.new('/foo/', nil, '!ruby/regexp')
+ assert_equal(/foo/, node.to_ruby)
+
+ node = Nodes::Scalar.new('/foo/m', nil, '!ruby/regexp')
+ assert_equal(/foo/m, node.to_ruby)
+
+ node = Nodes::Scalar.new('/foo/ix', nil, '!ruby/regexp')
+ assert_equal(/foo/ix, node.to_ruby)
+ end
+
+ def test_time
+ now = Time.now
+ formatted = now.strftime("%Y-%m-%d %H:%M:%S") +
+ ".%06d %+.2d:00" % [now.usec, now.gmt_offset / 3600]
+
+ assert_equal now, Nodes::Scalar.new(formatted).to_ruby
+ end
+
+ def test_time_utc
+ now = Time.now.utc
+ formatted = now.strftime("%Y-%m-%d %H:%M:%S") +
+ ".%06dZ" % [now.usec]
+
+ assert_equal now, Nodes::Scalar.new(formatted).to_ruby
+ end
+
+ def test_time_utc_no_z
+ now = Time.now.utc
+ formatted = now.strftime("%Y-%m-%d %H:%M:%S") +
+ ".%06d" % [now.usec]
+
+ assert_equal now, Nodes::Scalar.new(formatted).to_ruby
+ end
+
+ def test_date
+ d = '1980-12-16'
+ actual = Date.strptime(d, '%Y-%m-%d')
+
+ date = Nodes::Scalar.new(d, nil, 'tag:yaml.org,2002:timestamp', false)
+
+ assert_equal actual, date.to_ruby
+ end
+
+ def test_rational
+ mapping = Nodes::Mapping.new nil, '!ruby/object:Rational'
+ mapping.children << Nodes::Scalar.new('denominator')
+ mapping.children << Nodes::Scalar.new('2')
+ mapping.children << Nodes::Scalar.new('numerator')
+ mapping.children << Nodes::Scalar.new('1')
+
+ assert_equal Rational(1,2), mapping.to_ruby
+ end
+
+ def test_complex
+ mapping = Nodes::Mapping.new nil, '!ruby/object:Complex'
+ mapping.children << Nodes::Scalar.new('image')
+ mapping.children << Nodes::Scalar.new('2')
+ mapping.children << Nodes::Scalar.new('real')
+ mapping.children << Nodes::Scalar.new('1')
+
+ assert_equal Complex(1,2), mapping.to_ruby
+ end
+
+ if RUBY_VERSION >= '1.9'
+ def test_complex_string
+ node = Nodes::Scalar.new '3+4i', nil, "!ruby/object:Complex"
+ assert_equal Complex(3, 4), node.to_ruby
+ end
+
+ def test_rational_string
+ node = Nodes::Scalar.new '1/2', nil, "!ruby/object:Rational"
+ assert_equal Rational(1, 2), node.to_ruby
+ end
+ end
+
+ def test_range_string
+ node = Nodes::Scalar.new '1..2', nil, "!ruby/range"
+ assert_equal 1..2, node.to_ruby
+ end
+
+ def test_range_string_triple
+ node = Nodes::Scalar.new '1...3', nil, "!ruby/range"
+ assert_equal 1...3, node.to_ruby
+ end
+
+ def test_integer
+ i = Nodes::Scalar.new('1', nil, 'tag:yaml.org,2002:int')
+ assert_equal 1, i.to_ruby
+
+ assert_equal 1, Nodes::Scalar.new('1').to_ruby
+
+ i = Nodes::Scalar.new('-1', nil, 'tag:yaml.org,2002:int')
+ assert_equal(-1, i.to_ruby)
+
+ assert_equal(-1, Nodes::Scalar.new('-1').to_ruby)
+ assert_equal 1, Nodes::Scalar.new('+1').to_ruby
+ end
+
+ def test_int_ignore
+ ['1,000', '1_000'].each do |num|
+ i = Nodes::Scalar.new(num, nil, 'tag:yaml.org,2002:int')
+ assert_equal 1000, i.to_ruby
+
+ assert_equal 1000, Nodes::Scalar.new(num).to_ruby
+ end
+ end
+
+ def test_float_ignore
+ ['1,000.3', '1_000.3'].each do |num|
+ i = Nodes::Scalar.new(num, nil, 'tag:yaml.org,2002:float')
+ assert_equal 1000.3, i.to_ruby
+
+ i = Nodes::Scalar.new(num, nil, '!float')
+ assert_equal 1000.3, i.to_ruby
+
+ assert_equal 1000.3, Nodes::Scalar.new(num).to_ruby
+ end
+ end
+
+ # http://yaml.org/type/bool.html
+ def test_boolean_true
+ %w{ yes Yes YES true True TRUE on On ON }.each do |t|
+ i = Nodes::Scalar.new(t, nil, 'tag:yaml.org,2002:bool')
+ assert_equal true, i.to_ruby
+ assert_equal true, Nodes::Scalar.new(t).to_ruby
+ end
+ end
+
+ # http://yaml.org/type/bool.html
+ def test_boolean_false
+ %w{ no No NO false False FALSE off Off OFF }.each do |t|
+ i = Nodes::Scalar.new(t, nil, 'tag:yaml.org,2002:bool')
+ assert_equal false, i.to_ruby
+ assert_equal false, Nodes::Scalar.new(t).to_ruby
+ end
+ end
+
+ def test_float
+ i = Nodes::Scalar.new('12', nil, 'tag:yaml.org,2002:float')
+ assert_equal 12.0, i.to_ruby
+
+ i = Nodes::Scalar.new('1.2', nil, 'tag:yaml.org,2002:float')
+ assert_equal 1.2, i.to_ruby
+
+ i = Nodes::Scalar.new('1.2')
+ assert_equal 1.2, i.to_ruby
+
+ assert_equal 1, Nodes::Scalar.new('.Inf').to_ruby.infinite?
+ assert_equal 1, Nodes::Scalar.new('.inf').to_ruby.infinite?
+ assert_equal 1, Nodes::Scalar.new('.Inf', nil, 'tag:yaml.org,2002:float').to_ruby.infinite?
+
+ assert_equal(-1, Nodes::Scalar.new('-.inf').to_ruby.infinite?)
+ assert_equal(-1, Nodes::Scalar.new('-.Inf').to_ruby.infinite?)
+ assert_equal(-1, Nodes::Scalar.new('-.Inf', nil, 'tag:yaml.org,2002:float').to_ruby.infinite?)
+
+ assert Nodes::Scalar.new('.NaN').to_ruby.nan?
+ assert Nodes::Scalar.new('.NaN', nil, 'tag:yaml.org,2002:float').to_ruby.nan?
+ end
+
+ def test_exp_float
+ exp = 1.2e+30
+
+ i = Nodes::Scalar.new(exp.to_s, nil, 'tag:yaml.org,2002:float')
+ assert_equal exp, i.to_ruby
+
+ assert_equal exp, Nodes::Scalar.new(exp.to_s).to_ruby
+ end
+
+ def test_scalar
+ scalar = Nodes::Scalar.new('foo')
+ assert_equal 'foo', @visitor.accept(scalar)
+ assert_equal 'foo', scalar.to_ruby
+ end
+
+ def test_sequence
+ seq = Nodes::Sequence.new
+ seq.children << Nodes::Scalar.new('foo')
+ seq.children << Nodes::Scalar.new('bar')
+
+ assert_equal %w{ foo bar }, seq.to_ruby
+ end
+
+ def test_mapping
+ mapping = Nodes::Mapping.new
+ mapping.children << Nodes::Scalar.new('foo')
+ mapping.children << Nodes::Scalar.new('bar')
+ assert_equal({'foo' => 'bar'}, mapping.to_ruby)
+ end
+
+ def test_document
+ doc = Nodes::Document.new
+ doc.children << Nodes::Scalar.new('foo')
+ assert_equal 'foo', doc.to_ruby
+ end
+
+ def test_stream
+ a = Nodes::Document.new
+ a.children << Nodes::Scalar.new('foo')
+
+ b = Nodes::Document.new
+ b.children << Nodes::Scalar.new('bar')
+
+ stream = Nodes::Stream.new
+ stream.children << a
+ stream.children << b
+
+ assert_equal %w{ foo bar }, stream.to_ruby
+ end
+
+ def test_alias
+ seq = Nodes::Sequence.new
+ seq.children << Nodes::Scalar.new('foo', 'A')
+ seq.children << Nodes::Alias.new('A')
+
+ list = seq.to_ruby
+ assert_equal %w{ foo foo }, list
+ assert_equal list[0].object_id, list[1].object_id
+ end
+ end
+ end
+end
diff --git a/test/psych/visitors/test_yaml_tree.rb b/test/psych/visitors/test_yaml_tree.rb
new file mode 100644
index 0000000000..a0a2336e75
--- /dev/null
+++ b/test/psych/visitors/test_yaml_tree.rb
@@ -0,0 +1,140 @@
+require 'test/psych/helper'
+
+module Psych
+ module Visitors
+ class TestYAMLTree < TestCase
+ def setup
+ super
+ @v = Visitors::YAMLTree.new
+ end
+
+ def test_object_has_no_class
+ yaml = Psych.dump(Object.new)
+ assert(Psych.dump(Object.new) !~ /Object/, yaml)
+ end
+
+ def test_struct_const
+ foo = Struct.new("Foo", :bar)
+ assert_cycle foo.new('bar')
+ end
+
+ A = Struct.new(:foo)
+
+ def test_struct
+ assert_cycle A.new('bar')
+ end
+
+ def test_struct_anon
+ s = Struct.new(:foo).new('bar')
+ obj = Psych.load(Psych.dump(s))
+ assert_equal s.foo, obj.foo
+ end
+
+ def test_exception
+ ex = Exception.new 'foo'
+ loaded = Psych.load(Psych.dump(ex))
+
+ assert_equal ex.message, loaded.message
+ assert_equal ex.class, loaded.class
+ end
+
+ def test_regexp
+ assert_cycle(/foo/)
+ assert_cycle(/foo/i)
+ assert_cycle(/foo/mx)
+ end
+
+ def test_time
+ assert_cycle Time.now
+ end
+
+ def test_date
+ date = Date.strptime('2002-12-14', '%Y-%m-%d')
+ assert_cycle date
+ end
+
+ def test_rational
+ assert_cycle Rational(1,2)
+ end
+
+ def test_complex
+ assert_cycle Complex(1,2)
+ end
+
+ def test_scalar
+ assert_cycle 'foo'
+ assert_cycle ':foo'
+ assert_cycle ''
+ assert_cycle ':'
+ end
+
+ def test_boolean
+ assert_cycle true
+ assert_cycle 'true'
+ assert_cycle false
+ assert_cycle 'false'
+ end
+
+ def test_range_inclusive
+ assert_cycle 1..2
+ end
+
+ def test_range_exclusive
+ assert_cycle 1...2
+ end
+
+ def test_anon_class
+ assert_raises(TypeError) do
+ @v.accept Class.new
+ end
+
+ assert_raises(TypeError) do
+ Psych.dump(Class.new)
+ end
+ end
+
+ def test_hash
+ assert_cycle('a' => 'b')
+ end
+
+ def test_list
+ assert_cycle(%w{ a b })
+ assert_cycle([1, 2.2])
+ end
+
+ def test_symbol
+ assert_cycle :foo
+ end
+
+ def test_int
+ assert_cycle 1
+ assert_cycle(-1)
+ assert_cycle '1'
+ assert_cycle '-1'
+ end
+
+ def test_float
+ assert_cycle 1.2
+ assert_cycle '1.2'
+
+ assert Psych.load(Psych.dump(0.0 / 0.0)).nan?
+ assert_equal 1, Psych.load(Psych.dump(1 / 0.0)).infinite?
+ assert_equal(-1, Psych.load(Psych.dump(-1 / 0.0)).infinite?)
+ end
+
+ # http://yaml.org/type/null.html
+ def test_nil
+ assert_cycle nil
+ assert_equal nil, Psych.load('null')
+ assert_equal nil, Psych.load('Null')
+ assert_equal nil, Psych.load('NULL')
+ assert_equal nil, Psych.load('~')
+ assert_equal({'foo' => nil}, Psych.load('foo: '))
+
+ assert_cycle 'null'
+ assert_cycle 'nUll'
+ assert_cycle '~'
+ end
+ end
+ end
+end