1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
# frozen_string_literal: false
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))
assert_equal(e, Encoding.find(e))
end
end
def test_enc_names
aliases = Encoding.aliases
aliases.each do |a, en|
e = Encoding.find(a)
assert_equal(e.name, en)
assert_include(e.names, a)
end
end
# 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)
end
end
def test_find
assert_raise(ArgumentError) { Encoding.find("foobarbazqux") }
assert_nothing_raised{Encoding.find("locale")}
assert_nothing_raised{Encoding.find("filesystem")}
if /(?:ms|dar)win|mingw/ !~ RUBY_PLATFORM
# 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
bug5150 = '[ruby-dev:44327]'
assert_raise(TypeError, bug5150) {Encoding.find(1)}
end
def test_replicate
assert_separately([], "#{<<~'END;'}")
assert_instance_of(Encoding, Encoding::UTF_8.replicate("UTF-8-ANOTHER#{Time.now.to_f}"))
assert_instance_of(Encoding, Encoding::ISO_2022_JP.replicate("ISO-2022-JP-ANOTHER#{Time.now.to_f}"))
bug3127 = '[ruby-dev:40954]'
assert_raise(TypeError, bug3127) {Encoding::UTF_8.replicate(0)}
assert_raise_with_message(ArgumentError, /\bNUL\b/, bug3127) {Encoding::UTF_8.replicate("\0")}
END;
end
def test_extra_encoding
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;
200.times {|i|
Encoding::UTF_8.replicate("dummy#{i}")
}
e = Encoding.list.last
format = "%d".force_encoding(e)
assert_equal("0", format % 0)
assert_equal(e, format.dup.encoding)
assert_equal(e, (format*1).encoding)
assert_equal(e, (("x"*30).force_encoding(e)*1).encoding)
GC.start
name = "A" * 64
Encoding.list.each do |enc|
assert_raise(ArgumentError) {enc.replicate(name)}
name.succ!
end
end;
end
def test_dummy_p
assert_equal(true, Encoding::ISO_2022_JP.dummy?)
assert_equal(false, Encoding::UTF_8.dummy?)
end
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
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|
assert_include(Encoding.name_list, k)
assert_include(Encoding.name_list, v)
assert_instance_of(String, k)
assert_instance_of(String, v)
end
end
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
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))
bin = "a".force_encoding(Encoding::ASCII_8BIT)
asc = "b".force_encoding(Encoding::US_ASCII)
assert_equal(Encoding::ASCII_8BIT, Encoding.compatible?(bin, asc))
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))
assert_equal(Encoding::UTF_8, Encoding.compatible?("\u{3042}".to_sym, ua.to_sym))
end
def test_errinfo_after_autoload
assert_separately(%w[--disable=gems], "#{<<~"begin;"}\n#{<<~'end;'}")
bug9038 = '[ruby-core:57949] [Bug #9038]'
begin;
e = assert_raise_with_message(SyntaxError, /unknown regexp option - Q/, bug9038) {
eval("/regexp/sQ")
}
assert_include(e.message, "/regexp/sQ\n")
end;
end
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
require "[Bug #16382]"
end
end;
end
end
|