diff options
author | Jeremy Evans <code@jeremyevans.net> | 2022-08-23 12:22:24 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2023-01-01 09:00:24 -0800 |
commit | 0903a251796c2b4086804a94420c231c04e3cea1 (patch) | |
tree | 15a05d6357a257df60c80cf8166e6410413f87e2 /test/ruby | |
parent | a4e9606da425a5f6bb5089b1037a3afd8cfa585b (diff) |
Make IO#set_encoding with binary external encoding use nil internal encoding
This was already the behavior when a single `'external:internal'`
encoding specifier string was passed. This makes the behavior
consistent for the case where separate external and internal
encoding specifiers are provided.
While here, fix the IO#set_encoding method documentation to
state that either the first or second argument can be a string
with an encoding name, and describe the behavior when the
external encoding is binary.
Fixes [Bug #18899]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/6280
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_io_m17n.rb | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index 9c14087eba..267975d4ac 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -1158,6 +1158,78 @@ EOT end end + def test_set_encoding_argument_parsing + File.open(File::NULL) do |f| + f.set_encoding('binary') + assert_equal(Encoding::ASCII_8BIT, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding(Encoding.find('binary')) + assert_equal(Encoding::ASCII_8BIT, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding('binary:utf-8') + assert_equal(nil, f.internal_encoding) + assert_equal(Encoding::ASCII_8BIT, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding('binary', 'utf-8') + assert_equal(nil, f.internal_encoding) + assert_equal(Encoding::ASCII_8BIT, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding(Encoding.find('binary'), Encoding.find('utf-8')) + assert_equal(nil, f.internal_encoding) + assert_equal(Encoding::ASCII_8BIT, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding('binary', Encoding.find('utf-8')) + assert_equal(nil, f.internal_encoding) + assert_equal(Encoding::ASCII_8BIT, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding(Encoding.find('binary'), 'utf-8') + assert_equal(nil, f.internal_encoding) + assert_equal(Encoding::ASCII_8BIT, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding('iso-8859-1:utf-8') + assert_equal(Encoding::UTF_8, f.internal_encoding) + assert_equal(Encoding::ISO_8859_1, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding('iso-8859-1', 'utf-8') + assert_equal(Encoding::UTF_8, f.internal_encoding) + assert_equal(Encoding::ISO_8859_1, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding(Encoding.find('iso-8859-1'), Encoding.find('utf-8')) + assert_equal(Encoding::UTF_8, f.internal_encoding) + assert_equal(Encoding::ISO_8859_1, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding('iso-8859-1', Encoding.find('utf-8')) + assert_equal(Encoding::UTF_8, f.internal_encoding) + assert_equal(Encoding::ISO_8859_1, f.external_encoding) + end + + File.open(File::NULL) do |f| + f.set_encoding(Encoding.find('iso-8859-1'), 'utf-8') + assert_equal(Encoding::UTF_8, f.internal_encoding) + assert_equal(Encoding::ISO_8859_1, f.external_encoding) + end + end + def test_textmode_twice assert_raise(ArgumentError) { open(__FILE__, "rt", textmode: true) {|f| |