summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/psych/core_ext.rb3
-rw-r--r--test/psych/helper.rb49
-rw-r--r--test/psych/test_array.rb10
-rw-r--r--test/psych/test_class.rb4
-rw-r--r--test/psych/test_exception.rb11
-rw-r--r--test/psych/test_hash.rb10
-rw-r--r--test/psych/test_omap.rb9
-rw-r--r--test/psych/test_set.rb8
-rw-r--r--test/psych/test_string.rb5
-rw-r--r--test/psych/test_symbol.rb10
-rw-r--r--test/psych/test_yaml.rb69
-rw-r--r--test/psych/visitors/test_yaml_tree.rb93
12 files changed, 112 insertions, 169 deletions
diff --git a/lib/psych/core_ext.rb b/lib/psych/core_ext.rb
index d8f79eb..1cf79af 100644
--- a/lib/psych/core_ext.rb
+++ b/lib/psych/core_ext.rb
@@ -13,7 +13,8 @@ class Object
Psych.add_tag(url, self)
end
- def to_yaml options = {}
+ def psych_to_yaml options = {}
Psych.dump self, options
end
+ #alias :to_yaml :psych_to_yaml
end
diff --git a/test/psych/helper.rb b/test/psych/helper.rb
index 184fac1..04d3547 100644
--- a/test/psych/helper.rb
+++ b/test/psych/helper.rb
@@ -1,4 +1,3 @@
-require 'psych'
require 'minitest/autorun'
require 'stringio'
require 'tempfile'
@@ -6,6 +5,52 @@ require 'date'
module Psych
class TestCase < MiniTest::Unit::TestCase
- # Yes... blank for now. But we will use this.
+ #
+ # Convert between Psych and the object to verify correct parsing and
+ # emitting
+ #
+ def assert_to_yaml( obj, yaml )
+ assert_equal( obj, Psych::load( yaml ) )
+ assert_equal( obj, Psych::parse( yaml ).transform )
+ assert_equal( obj, Psych::load( obj.psych_to_yaml ) )
+ assert_equal( obj, Psych::parse( obj.psych_to_yaml ).transform )
+ assert_equal( obj, Psych::load(
+ obj.psych_to_yaml(
+ :UseVersion => true, :UseHeader => true, :SortKeys => true
+ )
+ ))
+ end
+
+ #
+ # Test parser only
+ #
+ def assert_parse_only( obj, yaml )
+ assert_equal( obj, Psych::load( yaml ) )
+ assert_equal( obj, Psych::parse( yaml ).transform )
+ end
+
+ def assert_cycle( obj )
+ v = Visitors::YAMLTree.new
+ v << obj
+ assert_equal(obj, Psych.load(v.tree.to_yaml))
+ assert_equal( obj, Psych::load(Psych.dump(obj)))
+ assert_equal( obj, Psych::load( obj.psych_to_yaml ) )
+ end
+
+ #
+ # Make a time with the time zone
+ #
+ def mktime( year, mon, day, hour, min, sec, usec, zone = "Z" )
+ usec = Rational(usec.to_s) * 1000000
+ val = Time::utc( year.to_i, mon.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i, usec )
+ if zone != "Z"
+ hour = zone[0,3].to_i * 3600
+ min = zone[3,2].to_i * 60
+ ofs = (hour + min)
+ val = Time.at( val.tv_sec - ofs, val.tv_nsec / 1000.0 )
+ end
+ return val
+ end
end
end
+require 'psych'
diff --git a/test/psych/test_array.rb b/test/psych/test_array.rb
index 0158f20..5686ab9 100644
--- a/test/psych/test_array.rb
+++ b/test/psych/test_array.rb
@@ -9,15 +9,11 @@ module Psych
def test_self_referential
@list << @list
- assert_equal @list, Psych.load(@list.to_yaml)
+ assert_cycle(@list)
end
- def test_to_yaml
- assert_equal @list, Psych.load(@list.to_yaml)
- end
-
- def test_dump
- assert_equal @list, Psych.load(Psych.dump(@list))
+ def test_cycle
+ assert_cycle(@list)
end
end
end
diff --git a/test/psych/test_class.rb b/test/psych/test_class.rb
index 48000da..02c4c1e 100644
--- a/test/psych/test_class.rb
+++ b/test/psych/test_class.rb
@@ -2,9 +2,9 @@ require 'test/psych/helper'
module Psych
class TestClass < TestCase
- def test_to_yaml
+ def test_cycle
assert_raises(::TypeError) do
- TestClass.to_yaml
+ assert_cycle(TestClass)
end
end
diff --git a/test/psych/test_exception.rb b/test/psych/test_exception.rb
index efc5747..dd06168 100644
--- a/test/psych/test_exception.rb
+++ b/test/psych/test_exception.rb
@@ -16,15 +16,8 @@ module Psych
@wups = Wups.new
end
- def test_to_yaml
- w = Psych.load(@wups.to_yaml)
- assert_equal @wups, w
- assert_equal 1, w.foo
- assert_equal 2, w.bar
- end
-
- def test_dump
- w = Psych.load(@wups.to_yaml)
+ def test_convert
+ w = Psych.load(Psych.dump(@wups))
assert_equal @wups, w
assert_equal 1, w.foo
assert_equal 2, w.bar
diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb
index 7b6cb82..84de492 100644
--- a/test/psych/test_hash.rb
+++ b/test/psych/test_hash.rb
@@ -9,15 +9,11 @@ module Psych
def test_self_referential
@hash['self'] = @hash
- assert_equal @hash, Psych.load(Psych.dump(@hash))
+ assert_cycle(@hash)
end
- def test_to_yaml
- assert_equal @hash, Psych.load(@hash.to_yaml)
- end
-
- def test_dump
- assert_equal @hash, Psych.load(Psych.dump(@hash))
+ def test_cycles
+ assert_cycle(@hash)
end
def test_ref_append
diff --git a/test/psych/test_omap.rb b/test/psych/test_omap.rb
index b9a3c86..06750c8 100644
--- a/test/psych/test_omap.rb
+++ b/test/psych/test_omap.rb
@@ -30,9 +30,9 @@ module Psych
assert_equal 'c', map['b']
end
- def test_to_yaml
+ def test_dump
map = Psych::Omap['a', 'b', 'c', 'd']
- yaml = map.to_yaml
+ yaml = Psych.dump(map)
assert_match('!omap', yaml)
assert_match('- a: b', yaml)
assert_match('- c: d', yaml)
@@ -41,10 +41,7 @@ module Psych
def test_round_trip
list = [["a", "b"], ["b", "c"]]
map = Psych::Omap[*list.flatten]
- loaded = Psych.load(Psych.dump(map))
-
- assert_equal map, loaded
- assert_equal list, loaded.to_a
+ assert_cycle(map)
end
def test_load
diff --git a/test/psych/test_set.rb b/test/psych/test_set.rb
index 6cff8dd..1d15573 100644
--- a/test/psych/test_set.rb
+++ b/test/psych/test_set.rb
@@ -9,12 +9,12 @@ module Psych
@set['bar'] = 'baz'
end
- def test_to_yaml
- assert_match(/!set/, @set.to_yaml)
+ def test_dump
+ assert_match(/!set/, Psych.dump(@set))
end
def test_roundtrip
- assert_equal(@set, Psych.load(Psych.dump(@set)))
+ assert_cycle(@set)
end
###
@@ -43,7 +43,7 @@ bar: baz
def test_set_self_reference
@set['self'] = @set
- assert_equal(@set, Psych.load(Psych.dump(@set)))
+ assert_cycle(@set)
end
end
end
diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb
index 5e4b1ad..9f5bc27 100644
--- a/test/psych/test_string.rb
+++ b/test/psych/test_string.rb
@@ -32,6 +32,11 @@ module Psych
assert_equal ivar, food.instance_variable_get(:@we_built_this_city)
end
+ def test_binary
+ string = [0, 123,22, 44, 9, 32, 34, 39].pack('C*')
+ assert_cycle string
+ end
+
def binary_string percentage = 0.31, length = 100
string = ''
(percentage * length).to_i.times do |i|
diff --git a/test/psych/test_symbol.rb b/test/psych/test_symbol.rb
index 8f821e4..83d8ca0 100644
--- a/test/psych/test_symbol.rb
+++ b/test/psych/test_symbol.rb
@@ -2,16 +2,12 @@ require 'test/psych/helper'
module Psych
class TestSymbol < TestCase
- def test_to_yaml
- assert_equal :a, Psych.load(:a.to_yaml)
- end
-
- def test_dump
- assert_equal :a, Psych.load(Psych.dump(:a))
+ def test_cycle
+ assert_cycle :a
end
def test_stringy
- assert_equal :"1", Psych.load(Psych.dump(:"1"))
+ assert_cycle :"1"
end
def test_load_quoted
diff --git a/test/psych/test_yaml.rb b/test/psych/test_yaml.rb
index 583cf12..a0b15ca 100644
--- a/test/psych/test_yaml.rb
+++ b/test/psych/test_yaml.rb
@@ -11,54 +11,6 @@ end
class Psych_Unit_Tests < Psych::TestCase
#
- # Convert between Psych and the object to verify correct parsing and
- # emitting
- #
- def assert_to_yaml( obj, yaml )
- assert_equal( obj, Psych::load( yaml ) )
- assert_equal( obj, Psych::parse( yaml ).transform )
- assert_equal( obj, Psych::load( obj.to_yaml ) )
- assert_equal( obj, Psych::parse( obj.to_yaml ).transform )
- assert_equal( obj, Psych::load(
- obj.to_yaml( :UseVersion => true, :UseHeader => true, :SortKeys => true )
- ) )
- end
-
- #
- # Test parser only
- #
- def assert_parse_only( obj, yaml )
- assert_equal( obj, Psych::load( yaml ) )
- assert_equal( obj, Psych::parse( yaml ).transform )
- end
-
- def assert_cycle( obj )
- assert_equal( obj, Psych::load( obj.to_yaml ) )
- end
-
- #def assert_path_segments( path, segments )
- # Psych::YPath.each_path( path ) { |choice|
- # assert_equal( choice.segments, segments.shift )
- # }
- # assert_equal( segments.length, 0, "Some segments leftover: #{ segments.inspect }" )
- #end
-
- #
- # Make a time with the time zone
- #
- def mktime( year, mon, day, hour, min, sec, usec, zone = "Z" )
- usec = Rational(usec.to_s) * 1000000
- val = Time::utc( year.to_i, mon.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i, usec )
- if zone != "Z"
- hour = zone[0,3].to_i * 3600
- min = zone[3,2].to_i * 60
- ofs = (hour + min)
- val = Time.at( val.tv_sec - ofs, val.tv_nsec / 1000.0 )
- end
- return val
- end
-
- #
# Tests modified from 00basic.t in Psych.pm
#
def test_basic_map
@@ -1231,7 +1183,7 @@ EOY
def test_circular_references
a = []; a[0] = a; a[1] = a
inspect_str = "[[...], [...]]"
- assert_equal( inspect_str, Psych::load( a.to_yaml ).inspect )
+ assert_equal( inspect_str, Psych::load(Psych.dump(a)).inspect )
end
#
@@ -1268,16 +1220,12 @@ EOY
#
# empty seq as key
#
- o = Psych.load({[]=>""}.to_yaml)
- assert_equal(Hash, o.class)
- assert_equal([[]], o.keys)
+ assert_cycle({[]=>""})
#
# empty map as key
#
- o = Psych.load({{}=>""}.to_yaml)
- assert_equal(Hash, o.class)
- assert_equal([{}], o.keys)
+ assert_cycle({{}=>""})
end
#
@@ -1286,7 +1234,7 @@ EOY
def test_object_id_collision
omap = Psych::Omap.new
1000.times { |i| omap["key_#{i}"] = { "value" => i } }
- raise "id collision in ordered map" if omap.to_yaml =~ /id\d+/
+ raise "id collision in ordered map" if Psych.dump(omap) =~ /id\d+/
end
def test_date_out_of_range
@@ -1298,12 +1246,3 @@ EOY
# '[ruby-core:13735]'
end
end
-
-#if $0 == __FILE__
-# suite = Test::Unit::TestSuite.new('Psych')
-# ObjectSpace.each_object(Class) do |klass|
-# suite << klass.suite if (Test::Unit::TestCase > klass)
-# end
-# require 'test/unit/ui/console/testrunner'
-# Test::Unit::UI::Console::TestRunner.run(suite).passed?
-#end
diff --git a/test/psych/visitors/test_yaml_tree.rb b/test/psych/visitors/test_yaml_tree.rb
index 2a8c5c0..a0a2336 100644
--- a/test/psych/visitors/test_yaml_tree.rb
+++ b/test/psych/visitors/test_yaml_tree.rb
@@ -15,13 +15,13 @@ module Psych
def test_struct_const
foo = Struct.new("Foo", :bar)
- assert_round_trip foo.new('bar')
+ assert_cycle foo.new('bar')
end
A = Struct.new(:foo)
def test_struct
- assert_round_trip A.new('bar')
+ assert_cycle A.new('bar')
end
def test_struct_anon
@@ -39,65 +39,48 @@ module Psych
end
def test_regexp
- assert_round_trip(/foo/)
- assert_round_trip(/foo/i)
- assert_round_trip(/foo/mx)
+ assert_cycle(/foo/)
+ assert_cycle(/foo/i)
+ assert_cycle(/foo/mx)
end
def test_time
- assert_round_trip Time.now
+ assert_cycle Time.now
end
def test_date
date = Date.strptime('2002-12-14', '%Y-%m-%d')
- assert_round_trip date
+ assert_cycle date
end
def test_rational
- assert_round_trip Rational(1,2)
+ assert_cycle Rational(1,2)
end
def test_complex
- assert_round_trip Complex(1,2)
- end
-
- def test_circular_list
- a = []
- 2.times { a << a }
- assert_equal a.inspect, Psych.load(a.to_yaml).inspect
- end
-
- def test_circular_map
- a = {}
- a[a] = a
- assert_equal a.inspect, Psych.load(a.to_yaml).inspect
+ assert_cycle Complex(1,2)
end
def test_scalar
- assert_round_trip 'foo'
- assert_round_trip ':foo'
- assert_round_trip ''
- assert_round_trip ':'
+ assert_cycle 'foo'
+ assert_cycle ':foo'
+ assert_cycle ''
+ assert_cycle ':'
end
def test_boolean
- assert_round_trip true
- assert_round_trip 'true'
- assert_round_trip false
- assert_round_trip 'false'
+ assert_cycle true
+ assert_cycle 'true'
+ assert_cycle false
+ assert_cycle 'false'
end
def test_range_inclusive
- assert_round_trip 1..2
+ assert_cycle 1..2
end
def test_range_exclusive
- assert_round_trip 1...2
- end
-
- def test_binary
- string = [0, 123,22, 44, 9, 32, 34, 39].pack('C*')
- assert_equal string, Psych.load(string.to_yaml)
+ assert_cycle 1...2
end
def test_anon_class
@@ -106,33 +89,33 @@ module Psych
end
assert_raises(TypeError) do
- Class.new.to_yaml
+ Psych.dump(Class.new)
end
end
def test_hash
- assert_round_trip('a' => 'b')
+ assert_cycle('a' => 'b')
end
def test_list
- assert_round_trip(%w{ a b })
- assert_round_trip([1, 2.2])
+ assert_cycle(%w{ a b })
+ assert_cycle([1, 2.2])
end
def test_symbol
- assert_round_trip :foo
+ assert_cycle :foo
end
def test_int
- assert_round_trip 1
- assert_round_trip(-1)
- assert_round_trip '1'
- assert_round_trip '-1'
+ assert_cycle 1
+ assert_cycle(-1)
+ assert_cycle '1'
+ assert_cycle '-1'
end
def test_float
- assert_round_trip 1.2
- assert_round_trip '1.2'
+ 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?
@@ -141,24 +124,16 @@ module Psych
# http://yaml.org/type/null.html
def test_nil
- assert_round_trip 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_round_trip 'null'
- assert_round_trip 'nUll'
- assert_round_trip '~'
- end
-
- def assert_round_trip obj
- v = Visitors::YAMLTree.new
- v << obj
- assert_equal(obj, Psych.load(v.tree.to_yaml))
- assert_equal(obj, Psych.load(obj.to_yaml))
- assert_equal(obj, Psych.load(Psych.dump(obj)))
+ assert_cycle 'null'
+ assert_cycle 'nUll'
+ assert_cycle '~'
end
end
end