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
|
2010-04-24 00:11:27 -04:00
|
|
|
def teardown
|
|
|
|
Psych.domain_types.clear
|
|
|
|
end
|
|
|
|
|
2010-07-08 12:10:10 -04:00
|
|
|
def test_line_width
|
|
|
|
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
|
|
|
|
assert_raises(TypeError) do
|
|
|
|
Psych.load nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-18 14:38:19 -04:00
|
|
|
def test_non_existing_class_on_deserialize
|
|
|
|
e = assert_raises(ArgumentError) do
|
|
|
|
Psych.load("--- !ruby/object:NonExistent\nfoo: 1")
|
|
|
|
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!'}
|
|
|
|
stringio = StringIO.new ''
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_load_documents
|
|
|
|
docs = Psych.load_documents("--- foo\n...\n--- bar\n...")
|
|
|
|
assert_equal %w{ foo bar }, docs
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_stream
|
|
|
|
docs = Psych.parse_stream("--- foo\n...\n--- bar\n...")
|
|
|
|
assert_equal %w{ foo bar }, docs.children.map { |x| x.transform }
|
|
|
|
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
|
|
|
|
Psych.add_domain_type 'foo.bar,2002', 'foo' do |type, val|
|
|
|
|
got = val
|
|
|
|
end
|
|
|
|
|
|
|
|
Psych.load('--- !foo.bar,2002/foo hello')
|
|
|
|
assert_equal 'hello', got
|
|
|
|
|
|
|
|
Psych.load("--- !foo.bar,2002/foo\n- hello\n- world")
|
|
|
|
assert_equal %w{ hello world }, got
|
|
|
|
|
|
|
|
Psych.load("--- !foo.bar,2002/foo\nhello: world")
|
|
|
|
assert_equal({ 'hello' => 'world' }, got)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def test_degenerate_strings
|
|
|
|
assert_equal false, Psych.load(' ')
|
|
|
|
assert_equal false, Psych.parse(' ')
|
|
|
|
assert_equal false, Psych.load('')
|
|
|
|
assert_equal false, Psych.parse('')
|
|
|
|
end
|
2010-04-24 00:11:27 -04:00
|
|
|
|
|
|
|
def test_callbacks
|
|
|
|
types = []
|
|
|
|
appender = lambda { |*args| types << args }
|
|
|
|
|
|
|
|
Psych.add_builtin_type('foo', &appender)
|
|
|
|
Psych.add_domain_type('example.com,2002', 'foo', &appender)
|
|
|
|
Psych.load <<-eoyml
|
|
|
|
- !tag:yaml.org,2002:foo bar
|
|
|
|
- !tag:example.com,2002:foo bar
|
|
|
|
eoyml
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
["tag:yaml.org,2002:foo", "bar"],
|
|
|
|
["tag:example.com,2002:foo", "bar"]
|
|
|
|
], types
|
|
|
|
end
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|