1 # frozen_string_literal: false
4 class TestEncoding < Test::Unit::TestCase
6 # Test basic encoding methods: list, find, name
8 encodings = Encoding.list
9 assert_equal(encodings.empty?, false)
12 assert_equal(e, Encoding.find(e.name))
13 assert_equal(e, Encoding.find(e.name.upcase))
14 assert_equal(e, Encoding.find(e.name.capitalize))
15 assert_equal(e, Encoding.find(e.name.downcase))
16 assert_equal(e, Encoding.find(e))
21 aliases = Encoding.aliases
22 aliases.each do |a, en|
24 assert_equal(e.name, en)
25 assert_include(e.names, a)
29 # Test that Encoding objects can't be copied
30 # And that they can be compared by object_id
32 encodings = Encoding.list
34 assert_raise(TypeError) { e.dup }
35 assert_raise(TypeError) { e.clone }
36 assert_equal(e.object_id, Marshal.load(Marshal.dump(e)).object_id)
41 assert_raise(ArgumentError) { Encoding.find("foobarbazqux") }
42 assert_nothing_raised{Encoding.find("locale")}
43 assert_nothing_raised{Encoding.find("filesystem")}
45 if /(?:ms|dar)win|mingw/ !~ RUBY_PLATFORM
46 # Unix's filesystem encoding is default_external
47 assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS')
48 exit Encoding.find("filesystem") == Encoding::UTF_8
49 Encoding.default_external = Encoding::EUC_JP
50 exit Encoding.find("filesystem") == Encoding::EUC_JP
54 bug5150 = '[ruby-dev:44327]'
55 assert_raise(TypeError, bug5150) {Encoding.find(1)}
59 assert_separately([], "#{<<~'END;'}")
60 assert_instance_of(Encoding, Encoding::UTF_8.replicate("UTF-8-ANOTHER#{Time.now.to_f}"))
61 assert_instance_of(Encoding, Encoding::ISO_2022_JP.replicate("ISO-2022-JP-ANOTHER#{Time.now.to_f}"))
62 bug3127 = '[ruby-dev:40954]'
63 assert_raise(TypeError, bug3127) {Encoding::UTF_8.replicate(0)}
64 assert_raise_with_message(ArgumentError, /\bNUL\b/, bug3127) {Encoding::UTF_8.replicate("\0")}
68 def test_extra_encoding
69 assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
72 Encoding::UTF_8.replicate("dummy#{i}")
74 e = Encoding.list.last
75 format = "%d".force_encoding(e)
76 assert_equal("0", format % 0)
77 assert_equal(e, format.dup.encoding)
78 assert_equal(e, (format*1).encoding)
80 assert_equal(e, (("x"*30).force_encoding(e)*1).encoding)
84 Encoding.list.each do |enc|
85 assert_raise(ArgumentError) {enc.replicate(name)}
92 assert_equal(true, Encoding::ISO_2022_JP.dummy?)
93 assert_equal(false, Encoding::UTF_8.dummy?)
96 def test_ascii_compatible_p
97 assert_equal(true, Encoding::ASCII_8BIT.ascii_compatible?)
98 assert_equal(true, Encoding::UTF_8.ascii_compatible?)
99 assert_equal(false, Encoding::UTF_16BE.ascii_compatible?)
100 assert_equal(false, Encoding::ISO_2022_JP.ascii_compatible?)
104 assert_instance_of(Array, Encoding.name_list)
105 Encoding.name_list.each do |x|
106 assert_instance_of(String, x)
111 assert_instance_of(Hash, Encoding.aliases)
112 Encoding.aliases.each do |k, v|
113 assert_include(Encoding.name_list, k)
114 assert_include(Encoding.name_list, v)
115 assert_instance_of(String, k)
116 assert_instance_of(String, v)
121 str = "".force_encoding("EUC-JP")
122 str2 = Marshal.load(Marshal.dump(str))
123 assert_equal(str, str2)
124 str2 = Marshal.load(Marshal.dump(str2))
125 assert_equal(str, str2, '[ruby-dev:38596]')
128 def test_compatible_p
129 ua = "abc".force_encoding(Encoding::UTF_8)
130 assert_equal(Encoding::UTF_8, Encoding.compatible?(ua, :abc))
131 assert_equal(nil, Encoding.compatible?(ua, 1))
132 bin = "a".force_encoding(Encoding::ASCII_8BIT)
133 asc = "b".force_encoding(Encoding::US_ASCII)
134 assert_equal(Encoding::ASCII_8BIT, Encoding.compatible?(bin, asc))
135 bin = "\xff".force_encoding(Encoding::ASCII_8BIT).to_sym
136 asc = "b".force_encoding(Encoding::ASCII_8BIT)
137 assert_equal(Encoding::ASCII_8BIT, Encoding.compatible?(bin, asc))
138 assert_equal(Encoding::UTF_8, Encoding.compatible?("\u{3042}".to_sym, ua.to_sym))
141 def test_errinfo_after_autoload
142 assert_separately(%w[--disable=gems], "#{<<~"begin;"}\n#{<<~'end;'}")
143 bug9038 = '[ruby-core:57949] [Bug #9038]'
145 e = assert_raise_with_message(SyntaxError, /unknown regexp option - Q/, bug9038) {
148 assert_include(e.message, "/regexp/sQ\n")
152 def test_nonascii_library_path
153 assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}".force_encoding("US-ASCII"))
155 assert_equal(Encoding::US_ASCII, __ENCODING__)
157 assert_raise_with_message(LoadError, /\[Bug #16382\]/) do
158 require "[Bug #16382]"