2017-07-14 02:15:58 -04:00
|
|
|
# frozen_string_literal: true
|
2010-03-28 17:49:37 -04:00
|
|
|
require 'minitest/autorun'
|
|
|
|
require 'stringio'
|
|
|
|
require 'tempfile'
|
|
|
|
require 'date'
|
2015-08-21 13:55:23 -04:00
|
|
|
|
2011-09-07 06:36:11 -04:00
|
|
|
require 'psych'
|
2010-03-28 17:49:37 -04:00
|
|
|
|
|
|
|
module Psych
|
2015-02-17 13:17:46 -05:00
|
|
|
superclass = if defined?(Minitest::Test)
|
|
|
|
Minitest::Test
|
|
|
|
else
|
|
|
|
MiniTest::Unit::TestCase
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestCase < superclass
|
2013-05-14 13:07:51 -04:00
|
|
|
def self.suppress_warning
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_default_external(enc)
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
origenc, Encoding.default_external = Encoding.default_external, enc
|
|
|
|
$VERBOSE = verbose
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
Encoding.default_external = origenc
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_default_internal(enc)
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
origenc, Encoding.default_internal = Encoding.default_internal, enc
|
|
|
|
$VERBOSE = verbose
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
Encoding.default_internal = origenc
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
#
|
|
|
|
# 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 )
|
2017-04-05 09:16:32 -04:00
|
|
|
assert_equal( obj, Psych::load( obj.to_yaml ) )
|
|
|
|
assert_equal( obj, Psych::parse( obj.to_yaml ).transform )
|
2010-03-28 17:49:37 -04:00
|
|
|
assert_equal( obj, Psych::load(
|
2017-04-05 09:16:32 -04:00
|
|
|
obj.to_yaml(
|
2010-03-28 17:49:37 -04:00
|
|
|
: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 )
|
2013-05-14 13:26:41 -04:00
|
|
|
v = Visitors::YAMLTree.create
|
2010-03-28 17:49:37 -04:00
|
|
|
v << obj
|
2017-04-05 09:16:32 -04:00
|
|
|
if obj.nil?
|
|
|
|
assert_nil Psych.load(v.tree.yaml)
|
|
|
|
assert_nil Psych::load(Psych.dump(obj))
|
|
|
|
assert_nil Psych::load(obj.to_yaml)
|
|
|
|
else
|
|
|
|
assert_equal(obj, Psych.load(v.tree.yaml))
|
|
|
|
assert_equal(obj, Psych::load(Psych.dump(obj)))
|
|
|
|
assert_equal(obj, Psych::load(obj.to_yaml))
|
|
|
|
end
|
2010-03-28 17:49:37 -04:00
|
|
|
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
|
2013-08-28 13:55:50 -04:00
|
|
|
|
2015-12-07 11:58:10 -05:00
|
|
|
# backport so that tests will run on 2.0.0
|
2013-08-28 13:55:50 -04:00
|
|
|
unless Tempfile.respond_to? :create
|
|
|
|
def Tempfile.create(basename, *rest)
|
|
|
|
tmpfile = nil
|
|
|
|
Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts|
|
|
|
|
mode = File::RDWR|File::CREAT|File::EXCL
|
|
|
|
perm = 0600
|
|
|
|
if opts
|
|
|
|
mode |= opts.delete(:mode) || 0
|
|
|
|
opts[:perm] = perm
|
|
|
|
perm = nil
|
|
|
|
else
|
|
|
|
opts = perm
|
|
|
|
end
|
|
|
|
tmpfile = File.open(tmpname, mode, opts)
|
|
|
|
end
|
|
|
|
if block_given?
|
|
|
|
begin
|
|
|
|
yield tmpfile
|
|
|
|
ensure
|
|
|
|
tmpfile.close if !tmpfile.closed?
|
|
|
|
File.unlink tmpfile
|
|
|
|
end
|
|
|
|
else
|
|
|
|
tmpfile
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|