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
|
|
|
|
|
|
|
module Psych
|
|
|
|
class TestException < TestCase
|
|
|
|
class Wups < Exception
|
|
|
|
attr_reader :foo, :bar
|
|
|
|
def initialize *args
|
|
|
|
super
|
|
|
|
@foo = 1
|
|
|
|
@bar = 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@wups = Wups.new
|
2018-10-20 00:25:04 -04:00
|
|
|
|
|
|
|
@orig_verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$VERBOSE = @orig_verbose
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
2019-01-09 16:34:51 -05:00
|
|
|
def make_ex msg = 'oh no!'
|
|
|
|
begin
|
|
|
|
raise msg
|
|
|
|
rescue ::Exception => e
|
|
|
|
e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_backtrace
|
|
|
|
err = make_ex
|
|
|
|
new_err = Psych.load(Psych.dump(err))
|
|
|
|
assert_equal err.backtrace, new_err.backtrace
|
|
|
|
end
|
|
|
|
|
2014-08-29 17:41:02 -04:00
|
|
|
def test_naming_exception
|
|
|
|
err = String.xxx rescue $!
|
|
|
|
new_err = Psych.load(Psych.dump(err))
|
|
|
|
assert_equal err.message, new_err.message
|
|
|
|
end
|
|
|
|
|
2011-12-06 18:12:37 -05:00
|
|
|
def test_load_takes_file
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
|
|
|
Psych.load '--- `'
|
|
|
|
end
|
|
|
|
assert_nil ex.file
|
|
|
|
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
2018-08-26 20:44:04 -04:00
|
|
|
Psych.load '--- `', filename: 'meow'
|
2011-12-06 18:12:37 -05:00
|
|
|
end
|
|
|
|
assert_equal 'meow', ex.file
|
2018-08-26 20:44:04 -04:00
|
|
|
|
|
|
|
# deprecated interface
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
|
|
|
Psych.load '--- `', 'deprecated'
|
|
|
|
end
|
|
|
|
assert_equal 'deprecated', ex.file
|
2011-12-06 18:12:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_psych_parse_stream_takes_file
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
|
|
|
Psych.parse_stream '--- `'
|
|
|
|
end
|
|
|
|
assert_nil ex.file
|
|
|
|
assert_match '(<unknown>)', ex.message
|
|
|
|
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
2018-08-26 20:44:04 -04:00
|
|
|
Psych.parse_stream '--- `', filename: 'omg!'
|
2011-12-06 18:12:37 -05:00
|
|
|
end
|
|
|
|
assert_equal 'omg!', ex.file
|
|
|
|
assert_match 'omg!', ex.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_stream_takes_file
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
|
|
|
Psych.load_stream '--- `'
|
|
|
|
end
|
|
|
|
assert_nil ex.file
|
|
|
|
assert_match '(<unknown>)', ex.message
|
|
|
|
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
2018-08-26 20:44:04 -04:00
|
|
|
Psych.load_stream '--- `', filename: 'omg!'
|
2011-12-06 18:12:37 -05:00
|
|
|
end
|
|
|
|
assert_equal 'omg!', ex.file
|
2018-08-26 20:44:04 -04:00
|
|
|
|
|
|
|
# deprecated interface
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
|
|
|
Psych.load_stream '--- `', 'deprecated'
|
|
|
|
end
|
|
|
|
assert_equal 'deprecated', ex.file
|
2011-12-06 18:12:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_file_exception
|
* 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(['parsefile', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write '--- `'
|
|
|
|
t.close
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
|
|
|
Psych.parse_file t.path
|
|
|
|
end
|
|
|
|
assert_equal t.path, ex.file
|
|
|
|
}
|
2011-12-06 18:12:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_file_exception
|
* 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(['loadfile', 'yml']) {|t|
|
|
|
|
t.binmode
|
|
|
|
t.write '--- `'
|
|
|
|
t.close
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
|
|
|
Psych.load_file t.path
|
|
|
|
end
|
|
|
|
assert_equal t.path, ex.file
|
|
|
|
}
|
2011-12-06 18:12:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_psych_parse_takes_file
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
|
|
|
Psych.parse '--- `'
|
|
|
|
end
|
|
|
|
assert_match '(<unknown>)', ex.message
|
|
|
|
assert_nil ex.file
|
|
|
|
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
2018-08-26 20:44:04 -04:00
|
|
|
Psych.parse '--- `', filename: 'omg!'
|
2011-12-06 18:12:37 -05:00
|
|
|
end
|
|
|
|
assert_match 'omg!', ex.message
|
2018-08-26 20:44:04 -04:00
|
|
|
|
|
|
|
# deprecated interface
|
|
|
|
ex = assert_raises(Psych::SyntaxError) do
|
|
|
|
Psych.parse '--- `', 'deprecated'
|
|
|
|
end
|
|
|
|
assert_match 'deprecated', ex.message
|
2011-12-06 18:12:37 -05:00
|
|
|
end
|
|
|
|
|
2011-10-04 13:53:41 -04:00
|
|
|
def test_attributes
|
|
|
|
e = assert_raises(Psych::SyntaxError) {
|
|
|
|
Psych.load '--- `foo'
|
|
|
|
}
|
|
|
|
|
2011-12-06 18:12:37 -05:00
|
|
|
assert_nil e.file
|
2011-10-04 13:53:41 -04:00
|
|
|
assert_equal 1, e.line
|
|
|
|
assert_equal 5, e.column
|
|
|
|
# FIXME: offset isn't being set correctly by libyaml
|
|
|
|
# assert_equal 5, e.offset
|
|
|
|
|
|
|
|
assert e.problem
|
|
|
|
assert e.context
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
def test_convert
|
|
|
|
w = Psych.load(Psych.dump(@wups))
|
|
|
|
assert_equal @wups, w
|
|
|
|
assert_equal 1, w.foo
|
|
|
|
assert_equal 2, w.bar
|
|
|
|
end
|
|
|
|
|
2012-10-22 17:25:02 -04:00
|
|
|
def test_psych_syntax_error
|
* 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(['parsefile', 'yml']) do |t|
|
2012-10-22 17:25:02 -04:00
|
|
|
t.binmode
|
|
|
|
t.write '--- `'
|
|
|
|
t.close
|
|
|
|
|
|
|
|
begin
|
|
|
|
Psych.parse_file t.path
|
|
|
|
rescue StandardError
|
|
|
|
assert true # count assertion
|
|
|
|
ensure
|
|
|
|
return unless $!
|
|
|
|
|
|
|
|
ancestors = $!.class.ancestors.inspect
|
|
|
|
|
|
|
|
flunk "Psych::SyntaxError not rescued by StandardError: #{ancestors}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
end
|