1require 'minitest/autorun'
2require 'stringio'
3require 'tempfile'
4require 'date'
5require 'psych'
6
7module Psych
8  class TestCase < MiniTest::Unit::TestCase
9    #
10    # Convert between Psych and the object to verify correct parsing and
11    # emitting
12    #
13    def assert_to_yaml( obj, yaml )
14      assert_equal( obj, Psych::load( yaml ) )
15      assert_equal( obj, Psych::parse( yaml ).transform )
16      assert_equal( obj, Psych::load( obj.psych_to_yaml ) )
17      assert_equal( obj, Psych::parse( obj.psych_to_yaml ).transform )
18      assert_equal( obj, Psych::load(
19        obj.psych_to_yaml(
20          :UseVersion => true, :UseHeader => true, :SortKeys => true
21        )
22      ))
23    end
24
25    #
26    # Test parser only
27    #
28    def assert_parse_only( obj, yaml )
29      assert_equal( obj, Psych::load( yaml ) )
30      assert_equal( obj, Psych::parse( yaml ).transform )
31    end
32
33    def assert_cycle( obj )
34      v = Visitors::YAMLTree.new
35      v << obj
36      assert_equal(obj, Psych.load(v.tree.yaml))
37      assert_equal( obj, Psych::load(Psych.dump(obj)))
38      assert_equal( obj, Psych::load( obj.psych_to_yaml ) )
39    end
40
41    #
42    # Make a time with the time zone
43    #
44    def mktime( year, mon, day, hour, min, sec, usec, zone = "Z" )
45      usec = Rational(usec.to_s) * 1000000
46      val = Time::utc( year.to_i, mon.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i, usec )
47      if zone != "Z"
48        hour = zone[0,3].to_i * 3600
49        min = zone[3,2].to_i * 60
50        ofs = (hour + min)
51        val = Time.at( val.tv_sec - ofs, val.tv_nsec / 1000.0 )
52      end
53      return val
54    end
55  end
56end
57