diff options
Diffstat (limited to 'spec/ruby/core/encoding')
26 files changed, 186 insertions, 133 deletions
diff --git a/spec/ruby/core/encoding/compatible_spec.rb b/spec/ruby/core/encoding/compatible_spec.rb index 80ecab6155..f18d8680a9 100644 --- a/spec/ruby/core/encoding/compatible_spec.rb +++ b/spec/ruby/core/encoding/compatible_spec.rb @@ -7,7 +7,7 @@ require_relative '../../spec_helper' describe "Encoding.compatible? String, String" do describe "when the first's Encoding is valid US-ASCII" do before :each do - @str = "abc".force_encoding Encoding::US_ASCII + @str = "abc".dup.force_encoding Encoding::US_ASCII end it "returns US-ASCII when the second's is US-ASCII" do @@ -33,28 +33,28 @@ describe "Encoding.compatible? String, String" do describe "when the first's Encoding is ASCII compatible and ASCII only" do it "returns the first's Encoding if the second is ASCII compatible and ASCII only" do - [ [Encoding, "abc".force_encoding("UTF-8"), "123".force_encoding("Shift_JIS"), Encoding::UTF_8], - [Encoding, "123".force_encoding("Shift_JIS"), "abc".force_encoding("UTF-8"), Encoding::Shift_JIS] + [ [Encoding, "abc".dup.force_encoding("UTF-8"), "123".dup.force_encoding("Shift_JIS"), Encoding::UTF_8], + [Encoding, "123".dup.force_encoding("Shift_JIS"), "abc".dup.force_encoding("UTF-8"), Encoding::Shift_JIS] ].should be_computed_by(:compatible?) end it "returns the first's Encoding if the second is ASCII compatible and ASCII only" do - [ [Encoding, "abc".force_encoding("BINARY"), "123".force_encoding("US-ASCII"), Encoding::BINARY], - [Encoding, "123".force_encoding("US-ASCII"), "abc".force_encoding("BINARY"), Encoding::US_ASCII] + [ [Encoding, "abc".dup.force_encoding("BINARY"), "123".dup.force_encoding("US-ASCII"), Encoding::BINARY], + [Encoding, "123".dup.force_encoding("US-ASCII"), "abc".dup.force_encoding("BINARY"), Encoding::US_ASCII] ].should be_computed_by(:compatible?) end it "returns the second's Encoding if the second is ASCII compatible but not ASCII only" do - [ [Encoding, "abc".force_encoding("UTF-8"), "\xff".force_encoding("Shift_JIS"), Encoding::Shift_JIS], - [Encoding, "123".force_encoding("Shift_JIS"), "\xff".force_encoding("UTF-8"), Encoding::UTF_8], - [Encoding, "abc".force_encoding("BINARY"), "\xff".force_encoding("US-ASCII"), Encoding::US_ASCII], - [Encoding, "123".force_encoding("US-ASCII"), "\xff".force_encoding("BINARY"), Encoding::BINARY], + [ [Encoding, "abc".dup.force_encoding("UTF-8"), "\xff".dup.force_encoding("Shift_JIS"), Encoding::Shift_JIS], + [Encoding, "123".dup.force_encoding("Shift_JIS"), "\xff".dup.force_encoding("UTF-8"), Encoding::UTF_8], + [Encoding, "abc".dup.force_encoding("BINARY"), "\xff".dup.force_encoding("US-ASCII"), Encoding::US_ASCII], + [Encoding, "123".dup.force_encoding("US-ASCII"), "\xff".dup.force_encoding("BINARY"), Encoding::BINARY], ].should be_computed_by(:compatible?) end it "returns nil if the second's Encoding is not ASCII compatible" do - a = "abc".force_encoding("UTF-8") - b = "1234".force_encoding("UTF-16LE") + a = "abc".dup.force_encoding("UTF-8") + b = "1234".dup.force_encoding("UTF-16LE") Encoding.compatible?(a, b).should be_nil end end @@ -75,7 +75,7 @@ describe "Encoding.compatible? String, String" do describe "when the first's Encoding is not ASCII compatible" do before :each do - @str = "abc".force_encoding Encoding::UTF_7 + @str = "abc".dup.force_encoding Encoding::UTF_7 end it "returns nil when the second String is US-ASCII" do @@ -91,14 +91,14 @@ describe "Encoding.compatible? String, String" do end it "returns the Encoding when the second's Encoding is not ASCII compatible but the same as the first's Encoding" do - encoding = Encoding.compatible?(@str, "def".force_encoding("utf-7")) + encoding = Encoding.compatible?(@str, "def".dup.force_encoding("utf-7")) encoding.should == Encoding::UTF_7 end end describe "when the first's Encoding is invalid" do before :each do - @str = "\xff".force_encoding Encoding::UTF_8 + @str = "\xff".dup.force_encoding Encoding::UTF_8 end it "returns the first's Encoding when the second's Encoding is US-ASCII" do @@ -114,11 +114,11 @@ describe "Encoding.compatible? String, String" do end it "returns nil when the second's Encoding is invalid and ASCII only" do - Encoding.compatible?(@str, "\x7f".force_encoding("utf-16be")).should be_nil + Encoding.compatible?(@str, "\x7f".dup.force_encoding("utf-16be")).should be_nil end it "returns nil when the second's Encoding is invalid and not ASCII only" do - Encoding.compatible?(@str, "\xff".force_encoding("utf-16be")).should be_nil + Encoding.compatible?(@str, "\xff".dup.force_encoding("utf-16be")).should be_nil end it "returns the Encoding when the second's Encoding is invalid but the same as the first" do @@ -129,7 +129,7 @@ describe "Encoding.compatible? String, String" do describe "when the first String is empty and the second is not" do describe "and the first's Encoding is ASCII compatible" do before :each do - @str = "".force_encoding("utf-8") + @str = "".dup.force_encoding("utf-8") end it "returns the first's encoding when the second String is ASCII only" do @@ -143,7 +143,7 @@ describe "Encoding.compatible? String, String" do describe "when the first's Encoding is not ASCII compatible" do before :each do - @str = "".force_encoding Encoding::UTF_7 + @str = "".dup.force_encoding Encoding::UTF_7 end it "returns the second string's encoding" do @@ -154,7 +154,7 @@ describe "Encoding.compatible? String, String" do describe "when the second String is empty" do before :each do - @str = "abc".force_encoding("utf-7") + @str = "abc".dup.force_encoding("utf-7") end it "returns the first Encoding" do @@ -165,7 +165,7 @@ end describe "Encoding.compatible? String, Regexp" do it "returns US-ASCII if both are US-ASCII" do - str = "abc".force_encoding("us-ascii") + str = "abc".dup.force_encoding("us-ascii") Encoding.compatible?(str, /abc/).should == Encoding::US_ASCII end @@ -180,15 +180,15 @@ describe "Encoding.compatible? String, Regexp" do it "returns the String's Encoding if the String is not ASCII only" do [ [Encoding, "\xff", Encoding::BINARY], [Encoding, "\u3042".encode("utf-8"), Encoding::UTF_8], - [Encoding, "\xa4\xa2".force_encoding("euc-jp"), Encoding::EUC_JP], - [Encoding, "\x82\xa0".force_encoding("shift_jis"), Encoding::Shift_JIS], + [Encoding, "\xa4\xa2".dup.force_encoding("euc-jp"), Encoding::EUC_JP], + [Encoding, "\x82\xa0".dup.force_encoding("shift_jis"), Encoding::Shift_JIS], ].should be_computed_by(:compatible?, /abc/) end end describe "Encoding.compatible? String, Symbol" do it "returns US-ASCII if both are ASCII only" do - str = "abc".force_encoding("us-ascii") + str = "abc".dup.force_encoding("us-ascii") Encoding.compatible?(str, :abc).should == Encoding::US_ASCII end @@ -203,8 +203,8 @@ describe "Encoding.compatible? String, Symbol" do it "returns the String's Encoding if the String is not ASCII only" do [ [Encoding, "\xff", Encoding::BINARY], [Encoding, "\u3042".encode("utf-8"), Encoding::UTF_8], - [Encoding, "\xa4\xa2".force_encoding("euc-jp"), Encoding::EUC_JP], - [Encoding, "\x82\xa0".force_encoding("shift_jis"), Encoding::Shift_JIS], + [Encoding, "\xa4\xa2".dup.force_encoding("euc-jp"), Encoding::EUC_JP], + [Encoding, "\x82\xa0".dup.force_encoding("shift_jis"), Encoding::Shift_JIS], ].should be_computed_by(:compatible?, :abc) end end @@ -221,8 +221,8 @@ describe "Encoding.compatible? String, Encoding" do it "returns the String's encoding if the Encoding is US-ASCII" do [ [Encoding, "\xff", Encoding::BINARY], [Encoding, "\u3042".encode("utf-8"), Encoding::UTF_8], - [Encoding, "\xa4\xa2".force_encoding("euc-jp"), Encoding::EUC_JP], - [Encoding, "\x82\xa0".force_encoding("shift_jis"), Encoding::Shift_JIS], + [Encoding, "\xa4\xa2".dup.force_encoding("euc-jp"), Encoding::EUC_JP], + [Encoding, "\x82\xa0".dup.force_encoding("shift_jis"), Encoding::Shift_JIS], ].should be_computed_by(:compatible?, Encoding::US_ASCII) end @@ -242,7 +242,7 @@ end describe "Encoding.compatible? Regexp, String" do it "returns US-ASCII if both are US-ASCII" do - str = "abc".force_encoding("us-ascii") + str = "abc".dup.force_encoding("us-ascii") Encoding.compatible?(/abc/, str).should == Encoding::US_ASCII end @@ -256,8 +256,8 @@ describe "Encoding.compatible? Regexp, Regexp" do it "returns the first's Encoding if it is not US-ASCII and not ASCII only" do [ [Encoding, Regexp.new("\xff"), Encoding::BINARY], [Encoding, Regexp.new("\u3042".encode("utf-8")), Encoding::UTF_8], - [Encoding, Regexp.new("\xa4\xa2".force_encoding("euc-jp")), Encoding::EUC_JP], - [Encoding, Regexp.new("\x82\xa0".force_encoding("shift_jis")), Encoding::Shift_JIS], + [Encoding, Regexp.new("\xa4\xa2".dup.force_encoding("euc-jp")), Encoding::EUC_JP], + [Encoding, Regexp.new("\x82\xa0".dup.force_encoding("shift_jis")), Encoding::Shift_JIS], ].should be_computed_by(:compatible?, /abc/) end end @@ -270,15 +270,15 @@ describe "Encoding.compatible? Regexp, Symbol" do it "returns the first's Encoding if it is not US-ASCII and not ASCII only" do [ [Encoding, Regexp.new("\xff"), Encoding::BINARY], [Encoding, Regexp.new("\u3042".encode("utf-8")), Encoding::UTF_8], - [Encoding, Regexp.new("\xa4\xa2".force_encoding("euc-jp")), Encoding::EUC_JP], - [Encoding, Regexp.new("\x82\xa0".force_encoding("shift_jis")), Encoding::Shift_JIS], + [Encoding, Regexp.new("\xa4\xa2".dup.force_encoding("euc-jp")), Encoding::EUC_JP], + [Encoding, Regexp.new("\x82\xa0".dup.force_encoding("shift_jis")), Encoding::Shift_JIS], ].should be_computed_by(:compatible?, /abc/) end end describe "Encoding.compatible? Symbol, String" do it "returns US-ASCII if both are ASCII only" do - str = "abc".force_encoding("us-ascii") + str = "abc".dup.force_encoding("us-ascii") Encoding.compatible?(str, :abc).should == Encoding::US_ASCII end end @@ -291,8 +291,8 @@ describe "Encoding.compatible? Symbol, Regexp" do it "returns the Regexp's Encoding if it is not US-ASCII and not ASCII only" do a = Regexp.new("\xff") b = Regexp.new("\u3042".encode("utf-8")) - c = Regexp.new("\xa4\xa2".force_encoding("euc-jp")) - d = Regexp.new("\x82\xa0".force_encoding("shift_jis")) + c = Regexp.new("\xa4\xa2".dup.force_encoding("euc-jp")) + d = Regexp.new("\x82\xa0".dup.force_encoding("shift_jis")) [ [Encoding, :abc, a, Encoding::BINARY], [Encoding, :abc, b, Encoding::UTF_8], @@ -310,8 +310,8 @@ describe "Encoding.compatible? Symbol, Symbol" do it "returns the first's Encoding if it is not ASCII only" do [ [Encoding, "\xff".to_sym, Encoding::BINARY], [Encoding, "\u3042".encode("utf-8").to_sym, Encoding::UTF_8], - [Encoding, "\xa4\xa2".force_encoding("euc-jp").to_sym, Encoding::EUC_JP], - [Encoding, "\x82\xa0".force_encoding("shift_jis").to_sym, Encoding::Shift_JIS], + [Encoding, "\xa4\xa2".dup.force_encoding("euc-jp").to_sym, Encoding::EUC_JP], + [Encoding, "\x82\xa0".dup.force_encoding("shift_jis").to_sym, Encoding::Shift_JIS], ].should be_computed_by(:compatible?, :abc) end end diff --git a/spec/ruby/core/encoding/converter/convert_spec.rb b/spec/ruby/core/encoding/converter/convert_spec.rb index 95a9e0b758..7f249d90a3 100644 --- a/spec/ruby/core/encoding/converter/convert_spec.rb +++ b/spec/ruby/core/encoding/converter/convert_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: binary -*- +# frozen_string_literal: true require_relative '../../../spec_helper' describe "Encoding::Converter#convert" do @@ -9,31 +10,31 @@ describe "Encoding::Converter#convert" do it "sets the encoding of the result to the target encoding" do ec = Encoding::Converter.new('ascii', 'utf-8') - str = 'glark'.force_encoding('ascii') + str = 'glark'.dup.force_encoding('ascii') ec.convert(str).encoding.should == Encoding::UTF_8 end it "transcodes the given String to the target encoding" do ec = Encoding::Converter.new("utf-8", "euc-jp") - ec.convert("\u3042".force_encoding('UTF-8')).should == \ - "\xA4\xA2".force_encoding('EUC-JP') + ec.convert("\u3042".dup.force_encoding('UTF-8')).should == \ + "\xA4\xA2".dup.force_encoding('EUC-JP') end it "allows Strings of different encodings to the source encoding" do ec = Encoding::Converter.new('ascii', 'utf-8') - str = 'glark'.force_encoding('SJIS') + str = 'glark'.dup.force_encoding('SJIS') ec.convert(str).encoding.should == Encoding::UTF_8 end it "reuses the given encoding pair if called multiple times" do ec = Encoding::Converter.new('ascii', 'SJIS') - ec.convert('a'.force_encoding('ASCII')).should == 'a'.force_encoding('SJIS') - ec.convert('b'.force_encoding('ASCII')).should == 'b'.force_encoding('SJIS') + ec.convert('a'.dup.force_encoding('ASCII')).should == 'a'.dup.force_encoding('SJIS') + ec.convert('b'.dup.force_encoding('ASCII')).should == 'b'.dup.force_encoding('SJIS') end it "raises UndefinedConversionError if the String contains characters invalid for the target encoding" do ec = Encoding::Converter.new('UTF-8', Encoding.find('macCyrillic')) - -> { ec.convert("\u{6543}".force_encoding('UTF-8')) }.should \ + -> { ec.convert("\u{6543}".dup.force_encoding('UTF-8')) }.should \ raise_error(Encoding::UndefinedConversionError) end diff --git a/spec/ruby/core/encoding/converter/finish_spec.rb b/spec/ruby/core/encoding/converter/finish_spec.rb index 11ca7e8510..239243430b 100644 --- a/spec/ruby/core/encoding/converter/finish_spec.rb +++ b/spec/ruby/core/encoding/converter/finish_spec.rb @@ -16,8 +16,8 @@ describe "Encoding::Converter#finish" do end it "returns the last part of the converted String if it hasn't already" do - @ec.convert("\u{9999}").should == "\e$B9a".force_encoding('iso-2022-jp') - @ec.finish.should == "\e(B".force_encoding('iso-2022-jp') + @ec.convert("\u{9999}").should == "\e$B9a".dup.force_encoding('iso-2022-jp') + @ec.finish.should == "\e(B".dup.force_encoding('iso-2022-jp') end it "returns a String in the destination encoding" do diff --git a/spec/ruby/core/encoding/converter/last_error_spec.rb b/spec/ruby/core/encoding/converter/last_error_spec.rb index 68567737b7..78779be70b 100644 --- a/spec/ruby/core/encoding/converter/last_error_spec.rb +++ b/spec/ruby/core/encoding/converter/last_error_spec.rb @@ -9,45 +9,45 @@ describe "Encoding::Converter#last_error" do it "returns nil when the last conversion did not produce an error" do ec = Encoding::Converter.new('ascii','utf-8') - ec.convert('a'.force_encoding('ascii')) + ec.convert('a'.dup.force_encoding('ascii')) ec.last_error.should be_nil end it "returns nil when #primitive_convert last returned :destination_buffer_full" do ec = Encoding::Converter.new("utf-8", "iso-2022-jp") - ec.primitive_convert("\u{9999}", "", 0, 0, partial_input: false) \ + ec.primitive_convert(+"\u{9999}", +"", 0, 0, partial_input: false) \ .should == :destination_buffer_full ec.last_error.should be_nil end it "returns nil when #primitive_convert last returned :finished" do ec = Encoding::Converter.new("utf-8", "iso-8859-1") - ec.primitive_convert("glark".force_encoding('utf-8'),"").should == :finished + ec.primitive_convert("glark".dup.force_encoding('utf-8'), +"").should == :finished ec.last_error.should be_nil end it "returns nil if the last conversion succeeded but the penultimate failed" do ec = Encoding::Converter.new("utf-8", "iso-8859-1") - ec.primitive_convert("\xf1abcd","").should == :invalid_byte_sequence - ec.primitive_convert("glark".force_encoding('utf-8'),"").should == :finished + ec.primitive_convert(+"\xf1abcd", +"").should == :invalid_byte_sequence + ec.primitive_convert("glark".dup.force_encoding('utf-8'), +"").should == :finished ec.last_error.should be_nil end it "returns an Encoding::InvalidByteSequenceError when #primitive_convert last returned :invalid_byte_sequence" do ec = Encoding::Converter.new("utf-8", "iso-8859-1") - ec.primitive_convert("\xf1abcd","").should == :invalid_byte_sequence + ec.primitive_convert(+"\xf1abcd", +"").should == :invalid_byte_sequence ec.last_error.should be_an_instance_of(Encoding::InvalidByteSequenceError) end it "returns an Encoding::UndefinedConversionError when #primitive_convert last returned :undefined_conversion" do ec = Encoding::Converter.new("utf-8", "iso-8859-1") - ec.primitive_convert("\u{9876}","").should == :undefined_conversion + ec.primitive_convert(+"\u{9876}", +"").should == :undefined_conversion ec.last_error.should be_an_instance_of(Encoding::UndefinedConversionError) end it "returns an Encoding::InvalidByteSequenceError when #primitive_convert last returned :incomplete_input" do ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") - ec.primitive_convert("\xa4", "", nil, 10).should == :incomplete_input + ec.primitive_convert(+"\xa4", +"", nil, 10).should == :incomplete_input ec.last_error.should be_an_instance_of(Encoding::InvalidByteSequenceError) end diff --git a/spec/ruby/core/encoding/converter/new_spec.rb b/spec/ruby/core/encoding/converter/new_spec.rb index 1f7affc72b..db9c3364d7 100644 --- a/spec/ruby/core/encoding/converter/new_spec.rb +++ b/spec/ruby/core/encoding/converter/new_spec.rb @@ -107,7 +107,7 @@ describe "Encoding::Converter.new" do it "sets the replacement String to '\\uFFFD'" do conv = Encoding::Converter.new("us-ascii", "utf-8", replace: nil) - conv.replacement.should == "\u{fffd}".force_encoding("utf-8") + conv.replacement.should == "\u{fffd}".dup.force_encoding("utf-8") end it "sets the replacement String encoding to UTF-8" do diff --git a/spec/ruby/core/encoding/converter/primitive_convert_spec.rb b/spec/ruby/core/encoding/converter/primitive_convert_spec.rb index 802d8e7cb1..63f25eddef 100644 --- a/spec/ruby/core/encoding/converter/primitive_convert_spec.rb +++ b/spec/ruby/core/encoding/converter/primitive_convert_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: binary -*- +# frozen_string_literal: false require_relative '../../../spec_helper' describe "Encoding::Converter#primitive_convert" do @@ -14,6 +15,10 @@ describe "Encoding::Converter#primitive_convert" do -> { @ec.primitive_convert("","") }.should_not raise_error end + it "raises FrozenError when the destination buffer is a frozen String" do + -> { @ec.primitive_convert("", "".freeze) }.should raise_error(FrozenError) + end + it "accepts nil for the destination byte offset" do -> { @ec.primitive_convert("","", nil) }.should_not raise_error end diff --git a/spec/ruby/core/encoding/converter/primitive_errinfo_spec.rb b/spec/ruby/core/encoding/converter/primitive_errinfo_spec.rb index 1f836b259f..668eb9a924 100644 --- a/spec/ruby/core/encoding/converter/primitive_errinfo_spec.rb +++ b/spec/ruby/core/encoding/converter/primitive_errinfo_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: binary -*- +# frozen_string_literal: false require_relative '../../../spec_helper' describe "Encoding::Converter#primitive_errinfo" do diff --git a/spec/ruby/core/encoding/converter/putback_spec.rb b/spec/ruby/core/encoding/converter/putback_spec.rb index c4e0a5da21..e19fe6c314 100644 --- a/spec/ruby/core/encoding/converter/putback_spec.rb +++ b/spec/ruby/core/encoding/converter/putback_spec.rb @@ -4,7 +4,7 @@ require_relative '../../../spec_helper' describe "Encoding::Converter#putback" do before :each do @ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") - @ret = @ec.primitive_convert(@src="abc\xa1def", @dst="", nil, 10) + @ret = @ec.primitive_convert(@src=+"abc\xa1def", @dst=+"", nil, 10) end it "returns a String" do @@ -36,21 +36,21 @@ describe "Encoding::Converter#putback" do it "returns the problematic bytes for UTF-16LE" do ec = Encoding::Converter.new("utf-16le", "iso-8859-1") - src = "\x00\xd8\x61\x00" - dst = "" + src = +"\x00\xd8\x61\x00" + dst = +"" ec.primitive_convert(src, dst).should == :invalid_byte_sequence ec.primitive_errinfo.should == [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "a\x00"] - ec.putback.should == "a\x00".force_encoding("utf-16le") + ec.putback.should == "a\x00".dup.force_encoding("utf-16le") ec.putback.should == "" end it "accepts an integer argument corresponding to the number of bytes to be put back" do ec = Encoding::Converter.new("utf-16le", "iso-8859-1") - src = "\x00\xd8\x61\x00" - dst = "" + src = +"\x00\xd8\x61\x00" + dst = +"" ec.primitive_convert(src, dst).should == :invalid_byte_sequence ec.primitive_errinfo.should == [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "a\x00"] - ec.putback(2).should == "a\x00".force_encoding("utf-16le") + ec.putback(2).should == "a\x00".dup.force_encoding("utf-16le") ec.putback.should == "" end end diff --git a/spec/ruby/core/encoding/converter/replacement_spec.rb b/spec/ruby/core/encoding/converter/replacement_spec.rb index 5ca42e7e5a..ea514ca8dd 100644 --- a/spec/ruby/core/encoding/converter/replacement_spec.rb +++ b/spec/ruby/core/encoding/converter/replacement_spec.rb @@ -13,7 +13,7 @@ describe "Encoding::Converter#replacement" do it "returns \\uFFFD when the destination encoding is UTF-8" do ec = Encoding::Converter.new("us-ascii", "utf-8") - ec.replacement.should == "\u{fffd}".force_encoding('utf-8') + ec.replacement.should == "\u{fffd}".dup.force_encoding('utf-8') ec.replacement.encoding.should == Encoding::UTF_8 end end @@ -38,33 +38,33 @@ describe "Encoding::Converter#replacement=" do it "sets #replacement" do ec = Encoding::Converter.new("us-ascii", "utf-8") - ec.replacement.should == "\u{fffd}".force_encoding('utf-8') + ec.replacement.should == "\u{fffd}".dup.force_encoding('utf-8') ec.replacement = '?'.encode('utf-8') - ec.replacement.should == '?'.force_encoding('utf-8') + ec.replacement.should == '?'.dup.force_encoding('utf-8') end it "raises an UndefinedConversionError is the argument cannot be converted into the destination encoding" do ec = Encoding::Converter.new("sjis", "ascii") - utf8_q = "\u{986}".force_encoding('utf-8') - ec.primitive_convert(utf8_q.dup, "").should == :undefined_conversion + utf8_q = "\u{986}".dup.force_encoding('utf-8') + ec.primitive_convert(utf8_q.dup, +"").should == :undefined_conversion -> { ec.replacement = utf8_q }.should \ raise_error(Encoding::UndefinedConversionError) end it "does not change the replacement character if the argument cannot be converted into the destination encoding" do ec = Encoding::Converter.new("sjis", "ascii") - utf8_q = "\u{986}".force_encoding('utf-8') - ec.primitive_convert(utf8_q.dup, "").should == :undefined_conversion + utf8_q = "\u{986}".dup.force_encoding('utf-8') + ec.primitive_convert(utf8_q.dup, +"").should == :undefined_conversion -> { ec.replacement = utf8_q }.should \ raise_error(Encoding::UndefinedConversionError) - ec.replacement.should == "?".force_encoding('us-ascii') + ec.replacement.should == "?".dup.force_encoding('us-ascii') end it "uses the replacement character" do ec = Encoding::Converter.new("utf-8", "us-ascii", :invalid => :replace, :undef => :replace) ec.replacement = "!" - dest = "" - status = ec.primitive_convert "中文123", dest + dest = +"" + status = ec.primitive_convert(+"中文123", dest) status.should == :finished dest.should == "!!123" diff --git a/spec/ruby/core/encoding/default_external_spec.rb b/spec/ruby/core/encoding/default_external_spec.rb index 682d49d37c..9aae4976e0 100644 --- a/spec/ruby/core/encoding/default_external_spec.rb +++ b/spec/ruby/core/encoding/default_external_spec.rb @@ -18,11 +18,9 @@ describe "Encoding.default_external" do Encoding.default_external.should == Encoding::SHIFT_JIS end - ruby_version_is "3.0" do - platform_is :windows do - it 'is UTF-8 by default on Windows' do - Encoding.default_external.should == Encoding::UTF_8 - end + platform_is :windows do + it 'is UTF-8 by default on Windows' do + Encoding.default_external.should == Encoding::UTF_8 end end end diff --git a/spec/ruby/core/encoding/inspect_spec.rb b/spec/ruby/core/encoding/inspect_spec.rb index 9a930b2a77..df96141db9 100644 --- a/spec/ruby/core/encoding/inspect_spec.rb +++ b/spec/ruby/core/encoding/inspect_spec.rb @@ -5,9 +5,23 @@ describe "Encoding#inspect" do Encoding::UTF_8.inspect.should be_an_instance_of(String) end - it "returns #<Encoding:name> for a non-dummy encoding named 'name'" do - Encoding.list.to_a.reject {|e| e.dummy? }.each do |enc| - enc.inspect.should =~ /#<Encoding:#{enc.name}>/ + ruby_version_is ""..."3.4" do + it "returns #<Encoding:name> for a non-dummy encoding named 'name'" do + Encoding.list.to_a.reject {|e| e.dummy? }.each do |enc| + enc.inspect.should =~ /#<Encoding:#{enc.name}>/ + end + end + end + + ruby_version_is "3.4" do + it "returns #<Encoding:name> for a non-dummy encoding named 'name'" do + Encoding.list.to_a.reject {|e| e.dummy? }.each do |enc| + if enc.name == "ASCII-8BIT" + enc.inspect.should == "#<Encoding:BINARY (ASCII-8BIT)>" + else + enc.inspect.should =~ /#<Encoding:#{enc.name}>/ + end + end end end diff --git a/spec/ruby/core/encoding/invalid_byte_sequence_error/destination_encoding_name_spec.rb b/spec/ruby/core/encoding/invalid_byte_sequence_error/destination_encoding_name_spec.rb index f5fa6f55e3..2b15fc1a0f 100644 --- a/spec/ruby/core/encoding/invalid_byte_sequence_error/destination_encoding_name_spec.rb +++ b/spec/ruby/core/encoding/invalid_byte_sequence_error/destination_encoding_name_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::InvalidByteSequenceError#destination_encoding_name" do diff --git a/spec/ruby/core/encoding/invalid_byte_sequence_error/destination_encoding_spec.rb b/spec/ruby/core/encoding/invalid_byte_sequence_error/destination_encoding_spec.rb index 43be3ddd71..c2ed6de1d8 100644 --- a/spec/ruby/core/encoding/invalid_byte_sequence_error/destination_encoding_spec.rb +++ b/spec/ruby/core/encoding/invalid_byte_sequence_error/destination_encoding_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::InvalidByteSequenceError#destination_encoding" do diff --git a/spec/ruby/core/encoding/invalid_byte_sequence_error/error_bytes_spec.rb b/spec/ruby/core/encoding/invalid_byte_sequence_error/error_bytes_spec.rb index a8f7354b16..d2fc360dce 100644 --- a/spec/ruby/core/encoding/invalid_byte_sequence_error/error_bytes_spec.rb +++ b/spec/ruby/core/encoding/invalid_byte_sequence_error/error_bytes_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: binary -*- +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::InvalidByteSequenceError#error_bytes" do diff --git a/spec/ruby/core/encoding/invalid_byte_sequence_error/incomplete_input_spec.rb b/spec/ruby/core/encoding/invalid_byte_sequence_error/incomplete_input_spec.rb index 94201a9b15..8a3f3de69a 100644 --- a/spec/ruby/core/encoding/invalid_byte_sequence_error/incomplete_input_spec.rb +++ b/spec/ruby/core/encoding/invalid_byte_sequence_error/incomplete_input_spec.rb @@ -8,7 +8,7 @@ describe "Encoding::InvalidByteSequenceError#incomplete_input?" do it "returns true if #primitive_convert returned :incomplete_input for the same data" do ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") - ec.primitive_convert("\xA1",'').should == :incomplete_input + ec.primitive_convert(+"\xA1", +'').should == :incomplete_input begin ec.convert("\xA1") rescue Encoding::InvalidByteSequenceError => e @@ -18,7 +18,7 @@ describe "Encoding::InvalidByteSequenceError#incomplete_input?" do it "returns false if #primitive_convert returned :invalid_byte_sequence for the same data" do ec = Encoding::Converter.new("ascii", "utf-8") - ec.primitive_convert("\xfffffffff",'').should == :invalid_byte_sequence + ec.primitive_convert(+"\xfffffffff", +'').should == :invalid_byte_sequence begin ec.convert("\xfffffffff") rescue Encoding::InvalidByteSequenceError => e diff --git a/spec/ruby/core/encoding/invalid_byte_sequence_error/readagain_bytes_spec.rb b/spec/ruby/core/encoding/invalid_byte_sequence_error/readagain_bytes_spec.rb index 93823b5db4..a5e2824984 100644 --- a/spec/ruby/core/encoding/invalid_byte_sequence_error/readagain_bytes_spec.rb +++ b/spec/ruby/core/encoding/invalid_byte_sequence_error/readagain_bytes_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: binary -*- +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::InvalidByteSequenceError#readagain_bytes" do @@ -14,11 +15,11 @@ describe "Encoding::InvalidByteSequenceError#readagain_bytes" do it "returns the bytes to be read again" do @exception.readagain_bytes.size.should == 1 - @exception.readagain_bytes.should == "a".force_encoding('binary') + @exception.readagain_bytes.should == "a".dup.force_encoding('binary') @exception.readagain_bytes.should == @errinfo[-1] @exception2.readagain_bytes.size.should == 1 - @exception2.readagain_bytes.should == "\xFF".force_encoding('binary') + @exception2.readagain_bytes.should == "\xFF".dup.force_encoding('binary') @exception2.readagain_bytes.should == @errinfo2[-1] end diff --git a/spec/ruby/core/encoding/invalid_byte_sequence_error/source_encoding_name_spec.rb b/spec/ruby/core/encoding/invalid_byte_sequence_error/source_encoding_name_spec.rb index bd3a51cbc5..a9464114a8 100644 --- a/spec/ruby/core/encoding/invalid_byte_sequence_error/source_encoding_name_spec.rb +++ b/spec/ruby/core/encoding/invalid_byte_sequence_error/source_encoding_name_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::UndefinedConversionError#source_encoding_name" do diff --git a/spec/ruby/core/encoding/invalid_byte_sequence_error/source_encoding_spec.rb b/spec/ruby/core/encoding/invalid_byte_sequence_error/source_encoding_spec.rb index f43d6d5830..7fdc0a122b 100644 --- a/spec/ruby/core/encoding/invalid_byte_sequence_error/source_encoding_spec.rb +++ b/spec/ruby/core/encoding/invalid_byte_sequence_error/source_encoding_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::InvalidByteSequenceError#source_encoding" do diff --git a/spec/ruby/core/encoding/name_spec.rb b/spec/ruby/core/encoding/name_spec.rb index 5eadb1d2f5..dce9347978 100644 --- a/spec/ruby/core/encoding/name_spec.rb +++ b/spec/ruby/core/encoding/name_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../spec_helper" require_relative 'shared/name' describe "Encoding#name" do diff --git a/spec/ruby/core/encoding/replicate_spec.rb b/spec/ruby/core/encoding/replicate_spec.rb index 45727a5c0d..e22673db7d 100644 --- a/spec/ruby/core/encoding/replicate_spec.rb +++ b/spec/ruby/core/encoding/replicate_spec.rb @@ -2,66 +2,87 @@ require_relative '../../spec_helper' describe "Encoding#replicate" do - before :all do - @i = 0 - end + ruby_version_is ""..."3.3" do + before :all do + @i = 0 + end - before :each do - @i += 1 - @prefix = "RS#{@i}" - end + before :each do + @i += 1 + @prefix = "RS#{@i}" + end - it "returns a replica of ASCII" do - name = @prefix + '-ASCII' - e = Encoding::ASCII.replicate(name) - e.name.should == name - Encoding.find(name).should == e + it "returns a replica of ASCII" do + name = @prefix + '-ASCII' + e = suppress_warning { Encoding::ASCII.replicate(name) } + e.name.should == name + Encoding.find(name).should == e - "a".force_encoding(e).valid_encoding?.should be_true - "\x80".force_encoding(e).valid_encoding?.should be_false - end + "a".dup.force_encoding(e).valid_encoding?.should be_true + "\x80".dup.force_encoding(e).valid_encoding?.should be_false + end - it "returns a replica of UTF-8" do - name = @prefix + 'UTF-8' - e = Encoding::UTF_8.replicate(name) - e.name.should == name - Encoding.find(name).should == e + it "returns a replica of UTF-8" do + name = @prefix + 'UTF-8' + e = suppress_warning { Encoding::UTF_8.replicate(name) } + e.name.should == name + Encoding.find(name).should == e - "a".force_encoding(e).valid_encoding?.should be_true - "\u3042".force_encoding(e).valid_encoding?.should be_true - "\x80".force_encoding(e).valid_encoding?.should be_false - end + "a".dup.force_encoding(e).valid_encoding?.should be_true + "\u3042".dup.force_encoding(e).valid_encoding?.should be_true + "\x80".dup.force_encoding(e).valid_encoding?.should be_false + end - it "returns a replica of UTF-16BE" do - name = @prefix + 'UTF-16-BE' - e = Encoding::UTF_16BE.replicate(name) - e.name.should == name - Encoding.find(name).should == e + it "returns a replica of UTF-16BE" do + name = @prefix + 'UTF-16-BE' + e = suppress_warning { Encoding::UTF_16BE.replicate(name) } + e.name.should == name + Encoding.find(name).should == e - "a".force_encoding(e).valid_encoding?.should be_false - "\x30\x42".force_encoding(e).valid_encoding?.should be_true - "\x80".force_encoding(e).valid_encoding?.should be_false - end + "a".dup.force_encoding(e).valid_encoding?.should be_false + "\x30\x42".dup.force_encoding(e).valid_encoding?.should be_true + "\x80".dup.force_encoding(e).valid_encoding?.should be_false + end - it "returns a replica of ISO-2022-JP" do - name = @prefix + 'ISO-2022-JP' - e = Encoding::ISO_2022_JP.replicate(name) - Encoding.find(name).should == e + it "returns a replica of ISO-2022-JP" do + name = @prefix + 'ISO-2022-JP' + e = suppress_warning { Encoding::ISO_2022_JP.replicate(name) } + Encoding.find(name).should == e - e.name.should == name - e.dummy?.should be_true + e.name.should == name + e.dummy?.should be_true + end + + # NOTE: it's unclear of the value of this (for the complexity cost of it), + # but it is the current CRuby behavior. + it "can be associated with a String" do + name = @prefix + '-US-ASCII' + e = suppress_warning { Encoding::US_ASCII.replicate(name) } + e.name.should == name + Encoding.find(name).should == e + + s = "abc".dup.force_encoding(e) + s.encoding.should == e + s.encoding.name.should == name + end end - # NOTE: it's unclear of the value of this (for the complexity cost of it), - # but it is the current CRuby behavior. - it "can be associated with a String" do - name = @prefix + '-US-ASCII' - e = Encoding::US_ASCII.replicate(name) - e.name.should == name - Encoding.find(name).should == e + ruby_version_is "3.2"..."3.3" do + it "warns about deprecation" do + -> { + Encoding::US_ASCII.replicate('MY-US-ASCII') + }.should complain(/warning: Encoding#replicate is deprecated and will be removed in Ruby 3.3; use the original encoding instead/) + end + + it "raises EncodingError if too many encodings" do + code = '1_000.times {|i| Encoding::US_ASCII.replicate("R_#{i}") }' + ruby_exe(code, args: "2>&1", exit_status: 1).should.include?('too many encoding (> 256) (EncodingError)') + end + end - s = "abc".force_encoding(e) - s.encoding.should == e - s.encoding.name.should == name + ruby_version_is "3.3" do + it "has been removed" do + Encoding::US_ASCII.should_not.respond_to?(:replicate, true) + end end end diff --git a/spec/ruby/core/encoding/to_s_spec.rb b/spec/ruby/core/encoding/to_s_spec.rb index 82d282386b..bab394a888 100644 --- a/spec/ruby/core/encoding/to_s_spec.rb +++ b/spec/ruby/core/encoding/to_s_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../spec_helper" require_relative 'shared/name' describe "Encoding#to_s" do diff --git a/spec/ruby/core/encoding/undefined_conversion_error/destination_encoding_name_spec.rb b/spec/ruby/core/encoding/undefined_conversion_error/destination_encoding_name_spec.rb index 106fc7ecac..a51a9f46a0 100644 --- a/spec/ruby/core/encoding/undefined_conversion_error/destination_encoding_name_spec.rb +++ b/spec/ruby/core/encoding/undefined_conversion_error/destination_encoding_name_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::UndefinedConversionError#destination_encoding_name" do diff --git a/spec/ruby/core/encoding/undefined_conversion_error/destination_encoding_spec.rb b/spec/ruby/core/encoding/undefined_conversion_error/destination_encoding_spec.rb index c6e24732fd..905556407c 100644 --- a/spec/ruby/core/encoding/undefined_conversion_error/destination_encoding_spec.rb +++ b/spec/ruby/core/encoding/undefined_conversion_error/destination_encoding_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::UndefinedConversionError#destination_encoding" do diff --git a/spec/ruby/core/encoding/undefined_conversion_error/error_char_spec.rb b/spec/ruby/core/encoding/undefined_conversion_error/error_char_spec.rb index 780d81c1ee..9cb55e6d95 100644 --- a/spec/ruby/core/encoding/undefined_conversion_error/error_char_spec.rb +++ b/spec/ruby/core/encoding/undefined_conversion_error/error_char_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::UndefinedConversionError#error_char" do diff --git a/spec/ruby/core/encoding/undefined_conversion_error/source_encoding_name_spec.rb b/spec/ruby/core/encoding/undefined_conversion_error/source_encoding_name_spec.rb index 3b697cb82f..d5e60e78db 100644 --- a/spec/ruby/core/encoding/undefined_conversion_error/source_encoding_name_spec.rb +++ b/spec/ruby/core/encoding/undefined_conversion_error/source_encoding_name_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::UndefinedConversionError#source_encoding_name" do diff --git a/spec/ruby/core/encoding/undefined_conversion_error/source_encoding_spec.rb b/spec/ruby/core/encoding/undefined_conversion_error/source_encoding_spec.rb index 9101d51e11..de456a4b5a 100644 --- a/spec/ruby/core/encoding/undefined_conversion_error/source_encoding_spec.rb +++ b/spec/ruby/core/encoding/undefined_conversion_error/source_encoding_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative '../fixtures/classes' describe "Encoding::UndefinedConversionError#source_encoding" do |