2017-07-14 02:15:58 -04:00
|
|
|
# frozen_string_literal: true
|
2013-03-26 10:55:04 -04:00
|
|
|
require_relative 'helper'
|
2010-03-28 17:49:37 -04:00
|
|
|
|
2010-03-31 17:09:58 -04:00
|
|
|
require 'stringio'
|
|
|
|
require 'tempfile'
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
class TestPsych < Psych::TestCase
|
2018-10-20 00:25:04 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@orig_verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
end
|
|
|
|
|
2010-04-24 00:11:27 -04:00
|
|
|
def teardown
|
|
|
|
Psych.domain_types.clear
|
2018-10-20 00:25:04 -04:00
|
|
|
$VERBOSE = @orig_verbose
|
2010-04-24 00:11:27 -04:00
|
|
|
end
|
|
|
|
|
2015-12-07 11:58:10 -05:00
|
|
|
def test_line_width_invalid
|
2021-05-10 06:09:17 -04:00
|
|
|
assert_raise(ArgumentError) { Psych.dump('x', { :line_width => -2 }) }
|
2015-12-07 11:58:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_line_width_no_limit
|
|
|
|
data = { 'a' => 'a b' * 50}
|
|
|
|
expected = "---\na: #{'a b' * 50}\n"
|
|
|
|
assert_equal(expected, Psych.dump(data, { :line_width => -1 }))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_line_width_limit
|
2010-07-08 12:10:10 -04:00
|
|
|
yml = Psych.dump('123456 7', { :line_width => 5 })
|
|
|
|
assert_match(/^\s*7/, yml)
|
|
|
|
end
|
|
|
|
|
2010-07-07 19:05:45 -04:00
|
|
|
def test_indent
|
|
|
|
yml = Psych.dump({:a => {'b' => 'c'}}, {:indentation => 5})
|
|
|
|
assert_match(/^[ ]{5}b/, yml)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_canonical
|
|
|
|
yml = Psych.dump({:a => {'b' => 'c'}}, {:canonical => true})
|
2013-02-07 18:54:29 -05:00
|
|
|
assert_match(/\? "b/, yml)
|
2010-07-07 19:05:45 -04:00
|
|
|
end
|
|
|
|
|
2010-07-07 19:18:27 -04:00
|
|
|
def test_header
|
|
|
|
yml = Psych.dump({:a => {'b' => 'c'}}, {:header => true})
|
|
|
|
assert_match(/YAML/, yml)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_version_array
|
|
|
|
yml = Psych.dump({:a => {'b' => 'c'}}, {:version => [1,1]})
|
|
|
|
assert_match(/1.1/, yml)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_version_string
|
|
|
|
yml = Psych.dump({:a => {'b' => 'c'}}, {:version => '1.1'})
|
|
|
|
assert_match(/1.1/, yml)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_version_bool
|
|
|
|
yml = Psych.dump({:a => {'b' => 'c'}}, {:version => true})
|
|
|
|
assert_match(/1.1/, yml)
|
|
|
|
end
|
|
|
|
|
2010-04-09 12:09:55 -04:00
|
|
|
def test_load_argument_error
|
2021-05-10 06:09:17 -04:00
|
|
|
assert_raise(TypeError) do
|
2010-04-09 12:09:55 -04:00
|
|
|
Psych.load nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-26 20:44:04 -04:00
|
|
|
def test_parse
|
|
|
|
assert_equal %w[a b], Psych.parse("- a\n- b").to_ruby
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_default_fallback
|
|
|
|
assert_equal false, Psych.parse("")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_raises_on_bad_input
|
2021-05-10 06:09:17 -04:00
|
|
|
assert_raise(Psych::SyntaxError) { Psych.parse("--- `") }
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
2010-06-18 14:38:19 -04:00
|
|
|
def test_non_existing_class_on_deserialize
|
2021-05-10 06:09:17 -04:00
|
|
|
e = assert_raise(ArgumentError) do
|
2021-05-10 12:50:06 -04:00
|
|
|
Psych.unsafe_load("--- !ruby/object:NonExistent\nfoo: 1")
|
2010-06-18 14:38:19 -04:00
|
|
|
end
|
|
|
|
assert_equal 'undefined class/module NonExistent', e.message
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
def test_dump_stream
|
|
|
|
things = [22, "foo \n", {}]
|
|
|
|
stream = Psych.dump_stream(*things)
|
|
|
|
assert_equal things, Psych.load_stream(stream)
|
|
|
|
end
|
|
|
|
|
2010-03-31 17:09:58 -04:00
|
|
|
def test_dump_file
|
|
|
|
hash = {'hello' => 'TGIF!'}
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create('fun.yml') do |io|
|
2010-03-31 17:09:58 -04:00
|
|
|
assert_equal io, Psych.dump(hash, io)
|
|
|
|
io.rewind
|
|
|
|
assert_equal Psych.dump(hash), io.read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dump_io
|
|
|
|
hash = {'hello' => 'TGIF!'}
|
2017-07-14 02:15:58 -04:00
|
|
|
stringio = StringIO.new ''.dup
|
2010-03-31 17:09:58 -04:00
|
|
|
assert_equal stringio, Psych.dump(hash, stringio)
|
|
|
|
assert_equal Psych.dump(hash), stringio.string
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
def test_simple
|
|
|
|
assert_equal 'foo', Psych.load("--- foo\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_libyaml_version
|
|
|
|
assert Psych.libyaml_version
|
|
|
|
assert_equal Psych.libyaml_version.join('.'), Psych::LIBYAML_VERSION
|
|
|
|
end
|
|
|
|
|
2017-04-05 09:16:32 -04:00
|
|
|
def test_load_stream
|
|
|
|
docs = Psych.load_stream("--- foo\n...\n--- bar\n...")
|
2010-03-28 17:49:37 -04:00
|
|
|
assert_equal %w{ foo bar }, docs
|
|
|
|
end
|
|
|
|
|
2020-09-10 09:12:11 -04:00
|
|
|
def test_load_stream_freeze
|
|
|
|
docs = Psych.load_stream("--- foo\n...\n--- bar\n...", freeze: true)
|
|
|
|
assert_equal %w{ foo bar }, docs
|
|
|
|
docs.each do |string|
|
|
|
|
assert_predicate string, :frozen?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_stream_symbolize_names
|
|
|
|
docs = Psych.load_stream("---\nfoo: bar", symbolize_names: true)
|
|
|
|
assert_equal [{foo: 'bar'}], docs
|
|
|
|
end
|
|
|
|
|
2018-08-26 20:44:04 -04:00
|
|
|
def test_load_stream_default_fallback
|
|
|
|
assert_equal [], Psych.load_stream("")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_stream_raises_on_bad_input
|
2021-05-10 06:09:17 -04:00
|
|
|
assert_raise(Psych::SyntaxError) { Psych.load_stream("--- `") }
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
def test_parse_stream
|
|
|
|
docs = Psych.parse_stream("--- foo\n...\n--- bar\n...")
|
2018-08-26 20:44:04 -04:00
|
|
|
assert_equal(%w[foo bar], docs.children.map(&:transform))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_stream_with_block
|
|
|
|
docs = []
|
|
|
|
Psych.parse_stream("--- foo\n...\n--- bar\n...") do |node|
|
|
|
|
docs << node
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal %w[foo bar], docs.map(&:to_ruby)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_stream_default_fallback
|
|
|
|
docs = Psych.parse_stream("")
|
|
|
|
assert_equal [], docs.children.map(&:to_ruby)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_stream_with_block_default_fallback
|
|
|
|
docs = []
|
|
|
|
Psych.parse_stream("") do |node|
|
|
|
|
docs << node
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [], docs.map(&:to_ruby)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_stream_raises_on_bad_input
|
2021-05-10 06:09:17 -04:00
|
|
|
assert_raise(Psych::SyntaxError) { Psych.parse_stream("--- `") }
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_builtin_type
|
|
|
|
got = nil
|
2011-03-29 23:52:07 -04:00
|
|
|
Psych.add_builtin_type 'omap' do |type, val|
|
2010-03-28 17:49:37 -04:00
|
|
|
got = val
|
|
|
|
end
|
2010-04-24 00:11:27 -04:00
|
|
|
Psych.load('--- !!omap hello')
|
2010-03-28 17:49:37 -04:00
|
|
|
assert_equal 'hello', got
|
|
|
|
ensure
|
|
|
|
Psych.remove_type 'omap'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_domain_types
|
|
|
|
got = nil
|
2020-06-03 13:18:34 -04:00
|
|
|
Psych.add_domain_type 'foo.bar/2002', 'foo' do |type, val|
|
2010-03-28 17:49:37 -04:00
|
|
|
got = val
|
|
|
|
end
|
|
|
|
|
2020-06-03 13:18:34 -04:00
|
|
|
Psych.load('--- !foo.bar/2002:foo hello')
|
2010-03-28 17:49:37 -04:00
|
|
|
assert_equal 'hello', got
|
|
|
|
|
2020-06-03 13:18:34 -04:00
|
|
|
Psych.load("--- !foo.bar/2002:foo\n- hello\n- world")
|
2010-03-28 17:49:37 -04:00
|
|
|
assert_equal %w{ hello world }, got
|
|
|
|
|
2020-06-03 13:18:34 -04:00
|
|
|
Psych.load("--- !foo.bar/2002:foo\nhello: world")
|
2010-03-28 17:49:37 -04:00
|
|
|
assert_equal({ 'hello' => 'world' }, got)
|
|
|
|
end
|
|
|
|
|
2019-07-24 16:01:20 -04:00
|
|
|
def test_load_freeze
|
|
|
|
data = Psych.load("--- {foo: ['a']}", freeze: true)
|
|
|
|
assert_predicate data, :frozen?
|
|
|
|
assert_predicate data['foo'], :frozen?
|
|
|
|
assert_predicate data['foo'].first, :frozen?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_freeze_deduplication
|
|
|
|
unless String.method_defined?(:-@) && (-("a" * 20)).equal?((-("a" * 20)))
|
2021-05-10 04:54:06 -04:00
|
|
|
pend "This Ruby implementation doesn't support string deduplication"
|
2019-07-24 16:01:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
data = Psych.load("--- ['a']", freeze: true)
|
|
|
|
assert_same 'a', data.first
|
|
|
|
end
|
|
|
|
|
2018-08-26 20:44:04 -04:00
|
|
|
def test_load_default_fallback
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal false, Psych.unsafe_load("")
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_with_fallback
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal 42, Psych.load("", filename: "file", fallback: 42)
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_with_fallback_nil_or_false
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_nil Psych.load("", filename: "file", fallback: nil)
|
|
|
|
assert_equal false, Psych.load("", filename: "file", fallback: false)
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_with_fallback_hash
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal Hash.new, Psych.load("", filename: "file", fallback: Hash.new)
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_with_fallback_for_nil
|
2021-05-11 16:49:20 -04:00
|
|
|
assert_nil Psych.unsafe_load("--- null", filename: "file", fallback: 42)
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_with_fallback_for_false
|
2021-05-11 16:49:20 -04:00
|
|
|
assert_equal false, Psych.unsafe_load("--- false", filename: "file", fallback: 42)
|
2018-08-26 20:44:04 -04:00
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
def test_load_file
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['yikes', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write('--- hello world')
|
|
|
|
t.close
|
|
|
|
assert_equal 'hello world', Psych.load_file(t.path)
|
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
2020-09-10 09:12:11 -04:00
|
|
|
def test_load_file_freeze
|
|
|
|
Tempfile.create(['yikes', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write('--- hello world')
|
|
|
|
t.close
|
|
|
|
|
|
|
|
object = Psych.load_file(t.path, freeze: true)
|
|
|
|
assert_predicate object, :frozen?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_file_symbolize_names
|
|
|
|
Tempfile.create(['yikes', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write("---\nfoo: bar")
|
|
|
|
t.close
|
|
|
|
|
|
|
|
assert_equal({foo: 'bar'}, Psych.load_file(t.path, symbolize_names: true))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-08-26 20:44:04 -04:00
|
|
|
def test_load_file_default_fallback
|
2018-04-27 21:47:58 -04:00
|
|
|
Tempfile.create(['empty', 'yml']) {|t|
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal false, Psych.unsafe_load_file(t.path)
|
2018-04-27 21:47:58 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-06-24 05:06:08 -04:00
|
|
|
def test_load_file_with_fallback
|
2018-04-27 21:47:58 -04:00
|
|
|
Tempfile.create(['empty', 'yml']) {|t|
|
|
|
|
assert_equal 42, Psych.load_file(t.path, fallback: 42)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_file_with_fallback_nil_or_false
|
|
|
|
Tempfile.create(['empty', 'yml']) {|t|
|
|
|
|
assert_nil Psych.load_file(t.path, fallback: nil)
|
|
|
|
assert_equal false, Psych.load_file(t.path, fallback: false)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_file_with_fallback_hash
|
2016-08-22 21:34:42 -04:00
|
|
|
Tempfile.create(['empty', 'yml']) {|t|
|
2017-12-19 04:44:33 -05:00
|
|
|
assert_equal Hash.new, Psych.load_file(t.path, fallback: Hash.new)
|
2016-08-22 21:34:42 -04:00
|
|
|
}
|
2016-06-24 05:06:08 -04:00
|
|
|
end
|
|
|
|
|
2018-04-27 21:47:58 -04:00
|
|
|
def test_load_file_with_fallback_for_nil
|
|
|
|
Tempfile.create(['nil', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write('--- null')
|
|
|
|
t.close
|
|
|
|
assert_nil Psych.load_file(t.path, fallback: 42)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_file_with_fallback_for_false
|
|
|
|
Tempfile.create(['false', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write('--- false')
|
|
|
|
t.close
|
|
|
|
assert_equal false, Psych.load_file(t.path, fallback: 42)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-12-14 06:13:12 -05:00
|
|
|
def test_safe_load_file_with_permitted_classe
|
|
|
|
Tempfile.create(['false', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write("--- !ruby/range\nbegin: 0\nend: 42\nexcl: false\n")
|
|
|
|
t.close
|
|
|
|
assert_equal 0..42, Psych.safe_load_file(t.path, permitted_classes: [Range])
|
2021-05-10 06:09:17 -04:00
|
|
|
assert_raise(Psych::DisallowedClass) {
|
2020-12-14 06:13:12 -05:00
|
|
|
Psych.safe_load_file(t.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
def test_parse_file
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(['yikes', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write('--- hello world')
|
|
|
|
t.close
|
|
|
|
assert_equal 'hello world', Psych.parse_file(t.path).transform
|
|
|
|
}
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
2010-03-29 19:57:25 -04:00
|
|
|
|
2018-08-26 20:44:04 -04:00
|
|
|
def test_parse_file_default_fallback
|
|
|
|
Tempfile.create(['empty', 'yml']) do |t|
|
|
|
|
assert_equal false, Psych.parse_file(t.path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-29 19:57:25 -04:00
|
|
|
def test_degenerate_strings
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal false, Psych.unsafe_load(' ')
|
2010-03-29 19:57:25 -04:00
|
|
|
assert_equal false, Psych.parse(' ')
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal false, Psych.unsafe_load('')
|
2010-03-29 19:57:25 -04:00
|
|
|
assert_equal false, Psych.parse('')
|
|
|
|
end
|
2010-04-24 00:11:27 -04:00
|
|
|
|
|
|
|
def test_callbacks
|
|
|
|
types = []
|
|
|
|
appender = lambda { |*args| types << args }
|
|
|
|
|
2020-06-03 13:18:34 -04:00
|
|
|
Psych.add_domain_type('example.com:2002', 'foo', &appender)
|
2010-04-24 00:11:27 -04:00
|
|
|
Psych.load <<-eoyml
|
2020-06-03 13:18:34 -04:00
|
|
|
- !tag:example.com:2002:foo bar
|
2010-04-24 00:11:27 -04:00
|
|
|
eoyml
|
|
|
|
|
|
|
|
assert_equal [
|
2020-06-03 13:18:34 -04:00
|
|
|
["tag:example.com:2002:foo", "bar"]
|
2010-04-24 00:11:27 -04:00
|
|
|
], types
|
|
|
|
end
|
2017-11-26 22:11:18 -05:00
|
|
|
|
|
|
|
def test_symbolize_names
|
2017-11-30 20:52:26 -05:00
|
|
|
yaml = <<-eoyml
|
2017-11-26 22:11:18 -05:00
|
|
|
foo:
|
|
|
|
bar: baz
|
2021-02-25 12:36:15 -05:00
|
|
|
1: 2
|
2017-11-26 22:11:18 -05:00
|
|
|
hoge:
|
|
|
|
- fuga: piyo
|
|
|
|
eoyml
|
2017-11-30 20:52:26 -05:00
|
|
|
|
|
|
|
result = Psych.load(yaml)
|
2021-02-25 12:36:15 -05:00
|
|
|
assert_equal result, { "foo" => { "bar" => "baz", 1 => 2 }, "hoge" => [{ "fuga" => "piyo" }] }
|
2017-11-26 22:11:18 -05:00
|
|
|
|
2017-11-30 20:52:26 -05:00
|
|
|
result = Psych.load(yaml, symbolize_names: true)
|
2021-02-25 12:36:15 -05:00
|
|
|
assert_equal result, { foo: { bar: "baz", 1 => 2 }, hoge: [{ fuga: "piyo" }] }
|
2017-11-30 20:52:26 -05:00
|
|
|
|
|
|
|
result = Psych.safe_load(yaml, symbolize_names: true)
|
2021-02-25 12:36:15 -05:00
|
|
|
assert_equal result, { foo: { bar: "baz", 1 => 2 }, hoge: [{ fuga: "piyo" }] }
|
2017-11-26 22:11:18 -05:00
|
|
|
end
|
2021-05-19 10:07:24 -04:00
|
|
|
|
|
|
|
def test_safe_dump_defaults
|
|
|
|
yaml = <<-eoyml
|
|
|
|
---
|
|
|
|
array:
|
|
|
|
- 1
|
|
|
|
float: 13.12
|
|
|
|
booleans:
|
|
|
|
- true
|
|
|
|
- false
|
|
|
|
eoyml
|
|
|
|
|
2021-06-07 06:24:20 -04:00
|
|
|
payload = Psych.safe_dump({
|
2021-05-19 10:07:24 -04:00
|
|
|
"array" => [1],
|
|
|
|
"float" => 13.12,
|
|
|
|
"booleans" => [true, false],
|
|
|
|
})
|
|
|
|
assert_equal yaml, payload
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_dump_unpermitted_class
|
2021-06-07 06:21:41 -04:00
|
|
|
error = assert_raise Psych::DisallowedClass do
|
2021-06-07 06:24:20 -04:00
|
|
|
Psych.safe_dump(Object.new)
|
2021-05-19 10:07:24 -04:00
|
|
|
end
|
|
|
|
assert_equal "Tried to dump unspecified class: Object", error.message
|
|
|
|
|
|
|
|
hash_subclass = Class.new(Hash)
|
2021-06-07 06:21:41 -04:00
|
|
|
error = assert_raise Psych::DisallowedClass do
|
2021-06-07 06:24:20 -04:00
|
|
|
Psych.safe_dump(hash_subclass.new)
|
2021-05-19 10:07:24 -04:00
|
|
|
end
|
|
|
|
assert_equal "Tried to dump unspecified class: #{hash_subclass.inspect}", error.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_dump_extra_permitted_classes
|
2021-06-07 06:24:20 -04:00
|
|
|
assert_equal "--- !ruby/object {}\n", Psych.safe_dump(Object.new, permitted_classes: [Object])
|
2021-05-19 10:07:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_dump_symbols
|
2021-06-07 06:21:41 -04:00
|
|
|
error = assert_raise Psych::DisallowedClass do
|
2021-06-07 06:24:20 -04:00
|
|
|
Psych.safe_dump(:foo, permitted_classes: [Symbol])
|
2021-05-19 10:07:24 -04:00
|
|
|
end
|
|
|
|
assert_equal "Tried to dump unspecified class: Symbol(:foo)", error.message
|
|
|
|
|
2021-06-07 09:56:47 -04:00
|
|
|
assert_match(/\A--- :foo\n(?:\.\.\.\n)?\z/, Psych.safe_dump(:foo, permitted_classes: [Symbol], permitted_symbols: [:foo]))
|
2021-05-19 10:07:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_dump_aliases
|
|
|
|
x = []
|
|
|
|
x << x
|
2021-06-07 06:21:41 -04:00
|
|
|
error = assert_raise Psych::BadAlias do
|
2021-06-07 06:24:20 -04:00
|
|
|
Psych.safe_dump(x)
|
2021-05-19 10:07:24 -04:00
|
|
|
end
|
|
|
|
assert_equal "Tried to dump an aliased object", error.message
|
|
|
|
|
2021-06-07 06:24:20 -04:00
|
|
|
assert_equal "--- &1\n" + "- *1\n", Psych.safe_dump(x, aliases: true)
|
2021-05-19 10:07:24 -04:00
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|