2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-10-14 04:31:51 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestEncoding < Test::Unit::TestCase
|
|
|
|
|
|
|
|
# Test basic encoding methods: list, find, name
|
|
|
|
def test_encoding
|
|
|
|
encodings = Encoding.list
|
|
|
|
assert_equal(encodings.empty?, false)
|
|
|
|
|
|
|
|
encodings.each do |e|
|
|
|
|
assert_equal(e, Encoding.find(e.name))
|
|
|
|
assert_equal(e, Encoding.find(e.name.upcase))
|
|
|
|
assert_equal(e, Encoding.find(e.name.capitalize))
|
|
|
|
assert_equal(e, Encoding.find(e.name.downcase))
|
2011-04-26 12:30:27 -04:00
|
|
|
assert_equal(e, Encoding.find(e))
|
2007-10-14 04:31:51 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-09 03:33:55 -04:00
|
|
|
def test_enc_names
|
|
|
|
aliases = Encoding.aliases
|
|
|
|
aliases.each do |a, en|
|
|
|
|
e = Encoding.find(a)
|
|
|
|
assert_equal(e.name, en)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_include(e.names, a)
|
2008-10-09 03:33:55 -04:00
|
|
|
end
|
|
|
|
end
|
2007-10-14 04:31:51 -04:00
|
|
|
|
|
|
|
# Test that Encoding objects can't be copied
|
|
|
|
# And that they can be compared by object_id
|
|
|
|
def test_singleton
|
|
|
|
encodings = Encoding.list
|
|
|
|
encodings.each do |e|
|
|
|
|
assert_raise(TypeError) { e.dup }
|
|
|
|
assert_raise(TypeError) { e.clone }
|
|
|
|
assert_equal(e.object_id, Marshal.load(Marshal.dump(e)).object_id)
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2007-10-14 04:31:51 -04:00
|
|
|
end
|
2008-06-03 09:34:48 -04:00
|
|
|
|
|
|
|
def test_find
|
|
|
|
assert_raise(ArgumentError) { Encoding.find("foobarbazqux") }
|
2009-10-28 03:34:24 -04:00
|
|
|
assert_nothing_raised{Encoding.find("locale")}
|
|
|
|
assert_nothing_raised{Encoding.find("filesystem")}
|
2009-11-17 22:38:20 -05:00
|
|
|
|
2011-09-03 00:49:43 -04:00
|
|
|
if /(?:ms|dar)win|mingw/ !~ RUBY_PLATFORM
|
2009-11-17 22:38:20 -05:00
|
|
|
# Unix's filesystem encoding is default_external
|
|
|
|
assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS')
|
|
|
|
exit Encoding.find("filesystem") == Encoding::UTF_8
|
|
|
|
Encoding.default_external = Encoding::EUC_JP
|
|
|
|
exit Encoding.find("filesystem") == Encoding::EUC_JP
|
|
|
|
EOS
|
|
|
|
end
|
2011-08-02 18:46:34 -04:00
|
|
|
|
|
|
|
bug5150 = '[ruby-dev:44327]'
|
|
|
|
assert_raise(TypeError, bug5150) {Encoding.find(1)}
|
2008-06-03 09:34:48 -04:00
|
|
|
end
|
|
|
|
|
2009-12-12 11:35:10 -05:00
|
|
|
def test_replicate
|
2010-04-29 01:46:04 -04:00
|
|
|
assert_instance_of(Encoding, Encoding::UTF_8.replicate('UTF-8-ANOTHER'))
|
|
|
|
assert_instance_of(Encoding, Encoding::ISO_2022_JP.replicate('ISO-2022-JP-ANOTHER'))
|
2010-04-10 23:21:47 -04:00
|
|
|
bug3127 = '[ruby-dev:40954]'
|
|
|
|
assert_raise(TypeError, bug3127) {Encoding::UTF_8.replicate(0)}
|
|
|
|
assert_raise(ArgumentError, bug3127) {Encoding::UTF_8.replicate("\0")}
|
2009-12-12 11:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-06-03 09:34:48 -04:00
|
|
|
def test_dummy_p
|
|
|
|
assert_equal(true, Encoding::ISO_2022_JP.dummy?)
|
|
|
|
assert_equal(false, Encoding::UTF_8.dummy?)
|
|
|
|
end
|
|
|
|
|
2009-08-08 19:25:32 -04:00
|
|
|
def test_ascii_compatible_p
|
|
|
|
assert_equal(true, Encoding::ASCII_8BIT.ascii_compatible?)
|
|
|
|
assert_equal(true, Encoding::UTF_8.ascii_compatible?)
|
|
|
|
assert_equal(false, Encoding::UTF_16BE.ascii_compatible?)
|
|
|
|
assert_equal(false, Encoding::ISO_2022_JP.ascii_compatible?)
|
|
|
|
end
|
|
|
|
|
2008-06-03 09:34:48 -04:00
|
|
|
def test_name_list
|
|
|
|
assert_instance_of(Array, Encoding.name_list)
|
|
|
|
Encoding.name_list.each do |x|
|
|
|
|
assert_instance_of(String, x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_aliases
|
|
|
|
assert_instance_of(Hash, Encoding.aliases)
|
|
|
|
Encoding.aliases.each do |k, v|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_include(Encoding.name_list, k)
|
|
|
|
assert_include(Encoding.name_list, v)
|
2008-06-03 09:34:48 -04:00
|
|
|
assert_instance_of(String, k)
|
|
|
|
assert_instance_of(String, v)
|
|
|
|
end
|
|
|
|
end
|
2009-06-12 20:58:30 -04:00
|
|
|
|
|
|
|
def test_marshal
|
|
|
|
str = "".force_encoding("EUC-JP")
|
|
|
|
str2 = Marshal.load(Marshal.dump(str))
|
|
|
|
assert_equal(str, str2)
|
|
|
|
str2 = Marshal.load(Marshal.dump(str2))
|
|
|
|
assert_equal(str, str2, '[ruby-dev:38596]')
|
|
|
|
end
|
2011-09-06 00:15:49 -04:00
|
|
|
|
2012-01-21 07:57:37 -05:00
|
|
|
def test_compatible_p
|
|
|
|
ua = "abc".force_encoding(Encoding::UTF_8)
|
|
|
|
assert_equal(Encoding::UTF_8, Encoding.compatible?(ua, :abc))
|
|
|
|
assert_equal(nil, Encoding.compatible?(ua, 1))
|
2012-02-05 10:17:50 -05:00
|
|
|
bin = "a".force_encoding(Encoding::ASCII_8BIT)
|
|
|
|
asc = "b".force_encoding(Encoding::US_ASCII)
|
|
|
|
assert_equal(Encoding::ASCII_8BIT, Encoding.compatible?(bin, asc))
|
2014-03-26 04:24:09 -04:00
|
|
|
bin = "\xff".force_encoding(Encoding::ASCII_8BIT).to_sym
|
|
|
|
asc = "b".force_encoding(Encoding::ASCII_8BIT)
|
|
|
|
assert_equal(Encoding::ASCII_8BIT, Encoding.compatible?(bin, asc))
|
2014-03-26 07:14:44 -04:00
|
|
|
assert_equal(Encoding::UTF_8, Encoding.compatible?("\u{3042}".to_sym, ua.to_sym))
|
2012-01-21 07:57:37 -05:00
|
|
|
end
|
2013-10-21 10:57:55 -04:00
|
|
|
|
|
|
|
def test_errinfo_after_autoload
|
2017-08-29 03:11:52 -04:00
|
|
|
assert_separately(%w[--disable=gems], "#{<<~"begin;"}\n#{<<~'end;'}")
|
2013-10-21 10:57:55 -04:00
|
|
|
bug9038 = '[ruby-core:57949] [Bug #9038]'
|
2017-08-29 03:11:52 -04:00
|
|
|
begin;
|
2019-03-12 08:04:03 -04:00
|
|
|
e = assert_raise_with_message(SyntaxError, /unknown regexp option - Q/, bug9038) {
|
2013-10-21 10:57:55 -04:00
|
|
|
eval("/regexp/sQ")
|
|
|
|
}
|
2019-03-12 08:04:03 -04:00
|
|
|
assert_include(e.message, "/regexp/sQ\n")
|
2013-10-21 10:57:55 -04:00
|
|
|
end;
|
|
|
|
end
|
2019-12-02 18:12:57 -05:00
|
|
|
|
|
|
|
def test_nonascii_library_path
|
|
|
|
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}".force_encoding("US-ASCII"))
|
|
|
|
begin;
|
|
|
|
assert_equal(Encoding::US_ASCII, __ENCODING__)
|
|
|
|
$:.unshift("/\x80")
|
|
|
|
assert_raise_with_message(LoadError, /\[Bug #16382\]/) do
|
|
|
|
$:.resolve_feature_path "[Bug #16382]"
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
end
|
2007-10-14 04:31:51 -04:00
|
|
|
end
|