1 # frozen_string_literal: true
10 class TestCase < Test::Unit::TestCase
11 def self.suppress_warning
12 verbose, $VERBOSE = $VERBOSE, nil
18 def with_default_external(enc)
19 verbose, $VERBOSE = $VERBOSE, nil
20 origenc, Encoding.default_external = Encoding.default_external, enc
24 verbose, $VERBOSE = $VERBOSE, nil
25 Encoding.default_external = origenc
29 def with_default_internal(enc)
30 verbose, $VERBOSE = $VERBOSE, nil
31 origenc, Encoding.default_internal = Encoding.default_internal, enc
35 verbose, $VERBOSE = $VERBOSE, nil
36 Encoding.default_internal = origenc
41 # Convert between Psych and the object to verify correct parsing and
44 def assert_to_yaml( obj, yaml, loader = :load )
45 assert_equal( obj, Psych.send(loader, yaml) )
46 assert_equal( obj, Psych::parse( yaml ).transform )
47 assert_equal( obj, Psych.send(loader, obj.to_yaml) )
48 assert_equal( obj, Psych::parse( obj.to_yaml ).transform )
49 assert_equal( obj, Psych.send(loader,
51 :UseVersion => true, :UseHeader => true, :SortKeys => true
54 rescue Psych::DisallowedClass, Psych::BadAlias, Psych::AliasesNotEnabled
55 assert_to_yaml obj, yaml, :unsafe_load
61 def assert_parse_only( obj, yaml )
63 assert_equal obj, Psych::load( yaml )
64 rescue Psych::DisallowedClass, Psych::BadAlias, Psych::AliasesNotEnabled
65 assert_equal obj, Psych::unsafe_load( yaml )
67 assert_equal obj, Psych::parse( yaml ).transform
70 def assert_cycle( obj )
71 v = Visitors::YAMLTree.create
74 assert_nil Psych.load(v.tree.yaml)
75 assert_nil Psych::load(Psych.dump(obj))
76 assert_nil Psych::load(obj.to_yaml)
79 assert_equal(obj, Psych.load(v.tree.yaml))
80 assert_equal(obj, Psych::load(Psych.dump(obj)))
81 assert_equal(obj, Psych::load(obj.to_yaml))
82 rescue Psych::DisallowedClass, Psych::BadAlias, Psych::AliasesNotEnabled
83 assert_equal(obj, Psych.unsafe_load(v.tree.yaml))
84 assert_equal(obj, Psych::unsafe_load(Psych.dump(obj)))
85 assert_equal(obj, Psych::unsafe_load(obj.to_yaml))
91 # Make a time with the time zone
93 def mktime( year, mon, day, hour, min, sec, usec, zone = "Z" )
94 usec = Rational(usec.to_s) * 1000000
95 val = Time::utc( year.to_i, mon.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i, usec )
97 hour = zone[0,3].to_i * 3600
98 min = zone[3,2].to_i * 60
100 val = Time.at( val.tv_sec - ofs, val.tv_nsec / 1000.0 )
107 # backport so that tests will run on 2.0.0
108 unless Tempfile.respond_to? :create
109 def Tempfile.create(basename, *rest)
111 Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts|
112 mode = File::RDWR|File::CREAT|File::EXCL
115 mode |= opts.delete(:mode) || 0
121 tmpfile = File.open(tmpname, mode, opts)
127 tmpfile.close if !tmpfile.closed?