From 79671ec57e59091260a0bc3d40a31d31d9c72a94 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 28 Apr 2019 23:20:11 +0200 Subject: Update to ruby/spec@7de852d --- spec/ruby/core/string/ascii_only_spec.rb | 126 +++++----- spec/ruby/core/string/b_spec.rb | 30 ++- spec/ruby/core/string/bytes_spec.rb | 26 +- spec/ruby/core/string/bytesize_spec.rb | 48 ++-- spec/ruby/core/string/byteslice_spec.rb | 14 +- spec/ruby/core/string/center_spec.rb | 42 ++-- spec/ruby/core/string/chomp_spec.rb | 88 ++++--- spec/ruby/core/string/chop_spec.rb | 40 ++- spec/ruby/core/string/chr_spec.rb | 78 +++--- spec/ruby/core/string/clear_spec.rb | 58 +++-- spec/ruby/core/string/codepoints_spec.rb | 20 +- spec/ruby/core/string/downcase_spec.rb | 6 +- spec/ruby/core/string/each_codepoint_spec.rb | 8 +- spec/ruby/core/string/element_set_spec.rb | 362 +++++++++++++------------- spec/ruby/core/string/encode_spec.rb | 232 +++++++++-------- spec/ruby/core/string/encoding_spec.rb | 364 +++++++++++++-------------- spec/ruby/core/string/force_encoding_spec.rb | 106 ++++---- spec/ruby/core/string/index_spec.rb | 60 +++-- spec/ruby/core/string/insert_spec.rb | 32 ++- spec/ruby/core/string/ljust_spec.rb | 42 ++-- spec/ruby/core/string/match_spec.rb | 18 +- spec/ruby/core/string/ord_spec.rb | 38 ++- spec/ruby/core/string/reverse_spec.rb | 16 +- spec/ruby/core/string/rindex_spec.rb | 28 +-- spec/ruby/core/string/rjust_spec.rb | 42 ++-- spec/ruby/core/string/shared/chars.rb | 92 ++++--- spec/ruby/core/string/shared/length.rb | 22 +- spec/ruby/core/string/slice_spec.rb | 78 +++--- spec/ruby/core/string/split_spec.rb | 22 +- spec/ruby/core/string/tr_s_spec.rb | 72 +++--- spec/ruby/core/string/tr_spec.rb | 44 ++-- spec/ruby/core/string/valid_encoding_spec.rb | 236 +++++++++-------- 32 files changed, 1200 insertions(+), 1290 deletions(-) (limited to 'spec/ruby/core/string') diff --git a/spec/ruby/core/string/ascii_only_spec.rb b/spec/ruby/core/string/ascii_only_spec.rb index 3dce10fab3..e9f2dc1a36 100644 --- a/spec/ruby/core/string/ascii_only_spec.rb +++ b/spec/ruby/core/string/ascii_only_spec.rb @@ -2,84 +2,82 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -with_feature :encoding do - describe "String#ascii_only?" do - describe "with ASCII only characters" do - it "returns true if the encoding is UTF-8" do - [ ["hello", true], - ["hello".encode('UTF-8'), true], - ["hello".force_encoding('UTF-8'), true], - ].should be_computed_by(:ascii_only?) - end - - it "returns true if the encoding is US-ASCII" do - "hello".force_encoding(Encoding::US_ASCII).ascii_only?.should be_true - "hello".encode(Encoding::US_ASCII).ascii_only?.should be_true - end - - it "returns true for all single-character UTF-8 Strings" do - 0.upto(127) do |n| - n.chr.ascii_only?.should be_true - end - end +describe "String#ascii_only?" do + describe "with ASCII only characters" do + it "returns true if the encoding is UTF-8" do + [ ["hello", true], + ["hello".encode('UTF-8'), true], + ["hello".force_encoding('UTF-8'), true], + ].should be_computed_by(:ascii_only?) end - describe "with non-ASCII only characters" do - it "returns false if the encoding is ASCII-8BIT" do - chr = 128.chr - chr.encoding.should == Encoding::ASCII_8BIT - chr.ascii_only?.should be_false - end - - it "returns false if the String contains any non-ASCII characters" do - [ ["\u{6666}", false], - ["hello, \u{6666}", false], - ["\u{6666}".encode('UTF-8'), false], - ["\u{6666}".force_encoding('UTF-8'), false], - ].should be_computed_by(:ascii_only?) - end + it "returns true if the encoding is US-ASCII" do + "hello".force_encoding(Encoding::US_ASCII).ascii_only?.should be_true + "hello".encode(Encoding::US_ASCII).ascii_only?.should be_true + end - it "returns false if the encoding is US-ASCII" do - [ ["\u{6666}".force_encoding(Encoding::US_ASCII), false], - ["hello, \u{6666}".force_encoding(Encoding::US_ASCII), false], - ].should be_computed_by(:ascii_only?) + it "returns true for all single-character UTF-8 Strings" do + 0.upto(127) do |n| + n.chr.ascii_only?.should be_true end end + end - it "returns true for the empty String with an ASCII-compatible encoding" do - "".ascii_only?.should be_true - "".encode('UTF-8').ascii_only?.should be_true + describe "with non-ASCII only characters" do + it "returns false if the encoding is ASCII-8BIT" do + chr = 128.chr + chr.encoding.should == Encoding::ASCII_8BIT + chr.ascii_only?.should be_false end - it "returns false for the empty String with a non-ASCII-compatible encoding" do - "".force_encoding('UTF-16LE').ascii_only?.should be_false - "".encode('UTF-16BE').ascii_only?.should be_false + it "returns false if the String contains any non-ASCII characters" do + [ ["\u{6666}", false], + ["hello, \u{6666}", false], + ["\u{6666}".encode('UTF-8'), false], + ["\u{6666}".force_encoding('UTF-8'), false], + ].should be_computed_by(:ascii_only?) end - it "returns false for a non-empty String with non-ASCII-compatible encoding" do - "\x78\x00".force_encoding("UTF-16LE").ascii_only?.should be_false + it "returns false if the encoding is US-ASCII" do + [ ["\u{6666}".force_encoding(Encoding::US_ASCII), false], + ["hello, \u{6666}".force_encoding(Encoding::US_ASCII), false], + ].should be_computed_by(:ascii_only?) end + end - it "returns false when interpolating non ascii strings" do - base = "EU currency is" - base.force_encoding(Encoding::US_ASCII) - euro = "\u20AC" - interp = "#{base} #{euro}" - euro.ascii_only?.should be_false - base.ascii_only?.should be_true - interp.ascii_only?.should be_false - end + it "returns true for the empty String with an ASCII-compatible encoding" do + "".ascii_only?.should be_true + "".encode('UTF-8').ascii_only?.should be_true + end - it "returns false after appending non ASCII characters to an empty String" do - ("" << "λ").ascii_only?.should be_false - end + it "returns false for the empty String with a non-ASCII-compatible encoding" do + "".force_encoding('UTF-16LE').ascii_only?.should be_false + "".encode('UTF-16BE').ascii_only?.should be_false + end - it "returns false when concatenating an ASCII and non-ASCII String" do - "".concat("λ").ascii_only?.should be_false - end + it "returns false for a non-empty String with non-ASCII-compatible encoding" do + "\x78\x00".force_encoding("UTF-16LE").ascii_only?.should be_false + end - it "returns false when replacing an ASCII String with a non-ASCII String" do - "".replace("λ").ascii_only?.should be_false - end + it "returns false when interpolating non ascii strings" do + base = "EU currency is" + base.force_encoding(Encoding::US_ASCII) + euro = "\u20AC" + interp = "#{base} #{euro}" + euro.ascii_only?.should be_false + base.ascii_only?.should be_true + interp.ascii_only?.should be_false + end + + it "returns false after appending non ASCII characters to an empty String" do + ("" << "λ").ascii_only?.should be_false + end + + it "returns false when concatenating an ASCII and non-ASCII String" do + "".concat("λ").ascii_only?.should be_false + end + + it "returns false when replacing an ASCII String with a non-ASCII String" do + "".replace("λ").ascii_only?.should be_false end end diff --git a/spec/ruby/core/string/b_spec.rb b/spec/ruby/core/string/b_spec.rb index 6599c23d73..638971f9ce 100644 --- a/spec/ruby/core/string/b_spec.rb +++ b/spec/ruby/core/string/b_spec.rb @@ -2,23 +2,21 @@ require_relative '../../spec_helper' describe "String#b" do - with_feature :encoding do - it "returns an ASCII-8BIT encoded string" do - "Hello".b.should == "Hello".force_encoding(Encoding::ASCII_8BIT) - "こんちには".b.should == "こんちには".force_encoding(Encoding::ASCII_8BIT) - end + it "returns an ASCII-8BIT encoded string" do + "Hello".b.should == "Hello".force_encoding(Encoding::ASCII_8BIT) + "こんちには".b.should == "こんちには".force_encoding(Encoding::ASCII_8BIT) + end - it "returns new string without modifying self" do - str = "こんちには" - str.b.should_not equal(str) - str.should == "こんちには" - end + it "returns new string without modifying self" do + str = "こんちには" + str.b.should_not equal(str) + str.should == "こんちには" + end - it "copies own tainted/untrusted status to the returning value" do - utf_8 = "こんちには".taint.untrust - ret = utf_8.b - ret.tainted?.should be_true - ret.untrusted?.should be_true - end + it "copies own tainted/untrusted status to the returning value" do + utf_8 = "こんちには".taint.untrust + ret = utf_8.b + ret.tainted?.should be_true + ret.untrusted?.should be_true end end diff --git a/spec/ruby/core/string/bytes_spec.rb b/spec/ruby/core/string/bytes_spec.rb index e7d3a7fbd8..96f1ae9cf2 100644 --- a/spec/ruby/core/string/bytes_spec.rb +++ b/spec/ruby/core/string/bytes_spec.rb @@ -36,22 +36,20 @@ describe "String#bytes" do end end -with_feature :encoding do - describe "String#bytes" do - before :each do - @utf8 = "東京" - @ascii = 'Tokyo' - @utf8_ascii = @utf8 + @ascii - end +describe "String#bytes" do + before :each do + @utf8 = "東京" + @ascii = 'Tokyo' + @utf8_ascii = @utf8 + @ascii + end - it "agrees with #getbyte" do - @utf8_ascii.bytes.to_a.each_with_index do |byte,index| - byte.should == @utf8_ascii.getbyte(index) - end + it "agrees with #getbyte" do + @utf8_ascii.bytes.to_a.each_with_index do |byte,index| + byte.should == @utf8_ascii.getbyte(index) end + end - it "is unaffected by #force_encoding" do - @utf8.force_encoding('ASCII').bytes.to_a.should == @utf8.bytes.to_a - end + it "is unaffected by #force_encoding" do + @utf8.force_encoding('ASCII').bytes.to_a.should == @utf8.bytes.to_a end end diff --git a/spec/ruby/core/string/bytesize_spec.rb b/spec/ruby/core/string/bytesize_spec.rb index 527b4a5dd5..b63d718c6c 100644 --- a/spec/ruby/core/string/bytesize_spec.rb +++ b/spec/ruby/core/string/bytesize_spec.rb @@ -2,36 +2,34 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -with_feature :encoding do - describe "#String#bytesize" do - it "needs to be reviewed for spec completeness" +describe "#String#bytesize" do + it "needs to be reviewed for spec completeness" - it "returns the length of self in bytes" do - "hello".bytesize.should == 5 - " ".bytesize.should == 1 - end + it "returns the length of self in bytes" do + "hello".bytesize.should == 5 + " ".bytesize.should == 1 + end - it "works with strings containing single UTF-8 characters" do - "\u{6666}".bytesize.should == 3 - end + it "works with strings containing single UTF-8 characters" do + "\u{6666}".bytesize.should == 3 + end - it "works with pseudo-ASCII strings containing single UTF-8 characters" do - "\u{6666}".force_encoding('ASCII').bytesize.should == 3 - end + it "works with pseudo-ASCII strings containing single UTF-8 characters" do + "\u{6666}".force_encoding('ASCII').bytesize.should == 3 + end - it "works with strings containing UTF-8 characters" do - "c \u{6666}".force_encoding('UTF-8').bytesize.should == 5 - "c \u{6666}".bytesize.should == 5 - end + it "works with strings containing UTF-8 characters" do + "c \u{6666}".force_encoding('UTF-8').bytesize.should == 5 + "c \u{6666}".bytesize.should == 5 + end - it "works with pseudo-ASCII strings containing UTF-8 characters" do - "c \u{6666}".force_encoding('ASCII').bytesize.should == 5 - end + it "works with pseudo-ASCII strings containing UTF-8 characters" do + "c \u{6666}".force_encoding('ASCII').bytesize.should == 5 + end - it "returns 0 for the empty string" do - "".bytesize.should == 0 - "".force_encoding('ASCII').bytesize.should == 0 - "".force_encoding('UTF-8').bytesize.should == 0 - end + it "returns 0 for the empty string" do + "".bytesize.should == 0 + "".force_encoding('ASCII').bytesize.should == 0 + "".force_encoding('UTF-8').bytesize.should == 0 end end diff --git a/spec/ruby/core/string/byteslice_spec.rb b/spec/ruby/core/string/byteslice_spec.rb index df99db95c6..a49da040eb 100644 --- a/spec/ruby/core/string/byteslice_spec.rb +++ b/spec/ruby/core/string/byteslice_spec.rb @@ -17,13 +17,11 @@ describe "String#byteslice with Range" do it_behaves_like :string_slice_range, :byteslice end -with_feature :encoding do - describe "String#byteslice on on non ASCII strings" do - it "returns byteslice of unicode strings" do - "\u3042".byteslice(1).should == "\x81".force_encoding("UTF-8") - "\u3042".byteslice(1, 2).should == "\x81\x82".force_encoding("UTF-8") - "\u3042".byteslice(1..2).should == "\x81\x82".force_encoding("UTF-8") - "\u3042".byteslice(-1).should == "\x82".force_encoding("UTF-8") - end +describe "String#byteslice on on non ASCII strings" do + it "returns byteslice of unicode strings" do + "\u3042".byteslice(1).should == "\x81".force_encoding("UTF-8") + "\u3042".byteslice(1, 2).should == "\x81\x82".force_encoding("UTF-8") + "\u3042".byteslice(1..2).should == "\x81\x82".force_encoding("UTF-8") + "\u3042".byteslice(-1).should == "\x82".force_encoding("UTF-8") end end diff --git a/spec/ruby/core/string/center_spec.rb b/spec/ruby/core/string/center_spec.rb index 145db01b70..3a9e689eac 100644 --- a/spec/ruby/core/string/center_spec.rb +++ b/spec/ruby/core/string/center_spec.rb @@ -104,30 +104,28 @@ describe "String#center with length, padding" do "hello".center(6, 'X'.taint).tainted?.should be_true end - with_feature :encoding do - describe "with width" do - it "returns a String in the same encoding as the original" do - str = "abc".force_encoding Encoding::IBM437 - result = str.center 6 - result.should == " abc " - result.encoding.should equal(Encoding::IBM437) - end + describe "with width" do + it "returns a String in the same encoding as the original" do + str = "abc".force_encoding Encoding::IBM437 + result = str.center 6 + result.should == " abc " + result.encoding.should equal(Encoding::IBM437) + end + end + + describe "with width, pattern" do + it "returns a String in the compatible encoding" do + str = "abc".force_encoding Encoding::IBM437 + result = str.center 6, "あ" + result.should == "あabcああ" + result.encoding.should equal(Encoding::UTF_8) end - describe "with width, pattern" do - it "returns a String in the compatible encoding" do - str = "abc".force_encoding Encoding::IBM437 - result = str.center 6, "あ" - result.should == "あabcああ" - result.encoding.should equal(Encoding::UTF_8) - end - - it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - pat = "ア".encode Encoding::EUC_JP - lambda do - "あれ".center 5, pat - end.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + pat = "ア".encode Encoding::EUC_JP + lambda do + "あれ".center 5, pat + end.should raise_error(Encoding::CompatibilityError) end end end diff --git a/spec/ruby/core/string/chomp_spec.rb b/spec/ruby/core/string/chomp_spec.rb index d6ad710382..7400d71336 100644 --- a/spec/ruby/core/string/chomp_spec.rb +++ b/spec/ruby/core/string/chomp_spec.rb @@ -330,62 +330,60 @@ describe "String#chomp!" do end end -with_feature :encoding do - describe "String#chomp" do - before :each do - @before_separator = $/ - end +describe "String#chomp" do + before :each do + @before_separator = $/ + end - after :each do - $/ = @before_separator - end + after :each do + $/ = @before_separator + end - it "does not modify a multi-byte character" do - "あれ".chomp.should == "あれ" - end + it "does not modify a multi-byte character" do + "あれ".chomp.should == "あれ" + end - it "removes the final carriage return, newline from a multibyte String" do - "あれ\r\n".chomp.should == "あれ" - end + it "removes the final carriage return, newline from a multibyte String" do + "あれ\r\n".chomp.should == "あれ" + end - it "removes the final carriage return, newline from a non-ASCII String" do - str = "abc\r\n".encode "utf-32be" - str.chomp.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String" do + str = "abc\r\n".encode "utf-32be" + str.chomp.should == "abc".encode("utf-32be") + end - it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do - $/ = "\n".encode("utf-8") - str = "abc\r\n".encode "utf-32be" - str.chomp.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do + $/ = "\n".encode("utf-8") + str = "abc\r\n".encode "utf-32be" + str.chomp.should == "abc".encode("utf-32be") end +end - describe "String#chomp!" do - before :each do - @before_separator = $/ - end +describe "String#chomp!" do + before :each do + @before_separator = $/ + end - after :each do - $/ = @before_separator - end + after :each do + $/ = @before_separator + end - it "returns nil when the String is not modified" do - "あれ".chomp!.should be_nil - end + it "returns nil when the String is not modified" do + "あれ".chomp!.should be_nil + end - it "removes the final carriage return, newline from a multibyte String" do - "あれ\r\n".chomp!.should == "あれ" - end + it "removes the final carriage return, newline from a multibyte String" do + "あれ\r\n".chomp!.should == "あれ" + end - it "removes the final carriage return, newline from a non-ASCII String" do - str = "abc\r\n".encode "utf-32be" - str.chomp!.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String" do + str = "abc\r\n".encode "utf-32be" + str.chomp!.should == "abc".encode("utf-32be") + end - it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do - $/ = "\n".encode("utf-8") - str = "abc\r\n".encode "utf-32be" - str.chomp!.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String when the record separator is changed" do + $/ = "\n".encode("utf-8") + str = "abc\r\n".encode "utf-32be" + str.chomp!.should == "abc".encode("utf-32be") end end diff --git a/spec/ruby/core/string/chop_spec.rb b/spec/ruby/core/string/chop_spec.rb index 033a11a95b..f33da3ecc4 100644 --- a/spec/ruby/core/string/chop_spec.rb +++ b/spec/ruby/core/string/chop_spec.rb @@ -27,19 +27,17 @@ describe "String#chop" do "abc\r\n\r\n".chop.should == "abc\r\n" end - with_feature :encoding do - it "removes a multi-byte character" do - "あれ".chop.should == "あ" - end + it "removes a multi-byte character" do + "あれ".chop.should == "あ" + end - it "removes the final carriage return, newline from a multibyte String" do - "あれ\r\n".chop.should == "あれ" - end + it "removes the final carriage return, newline from a multibyte String" do + "あれ\r\n".chop.should == "あれ" + end - it "removes the final carriage return, newline from a non-ASCII String" do - str = "abc\r\n".encode "utf-32be" - str.chop.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String" do + str = "abc\r\n".encode "utf-32be" + str.chop.should == "abc".encode("utf-32be") end it "returns an empty string when applied to an empty string" do @@ -91,19 +89,17 @@ describe "String#chop!" do "abc\r\n\r\n".chop!.should == "abc\r\n" end - with_feature :encoding do - it "removes a multi-byte character" do - "あれ".chop!.should == "あ" - end + it "removes a multi-byte character" do + "あれ".chop!.should == "あ" + end - it "removes the final carriage return, newline from a multibyte String" do - "あれ\r\n".chop!.should == "あれ" - end + it "removes the final carriage return, newline from a multibyte String" do + "あれ\r\n".chop!.should == "あれ" + end - it "removes the final carriage return, newline from a non-ASCII String" do - str = "abc\r\n".encode "utf-32be" - str.chop!.should == "abc".encode("utf-32be") - end + it "removes the final carriage return, newline from a non-ASCII String" do + str = "abc\r\n".encode "utf-32be" + str.chop!.should == "abc".encode("utf-32be") end it "returns self if modifications were made" do diff --git a/spec/ruby/core/string/chr_spec.rb b/spec/ruby/core/string/chr_spec.rb index ca56955866..9ed29542e6 100644 --- a/spec/ruby/core/string/chr_spec.rb +++ b/spec/ruby/core/string/chr_spec.rb @@ -1,44 +1,42 @@ require_relative '../../spec_helper' -with_feature :encoding do - describe "String#chr" do - it "returns a copy of self" do - s = 'e' - s.should_not equal s.chr - end - - it "returns a String" do - 'glark'.chr.should be_an_instance_of(String) - end - - it "returns an empty String if self is an empty String" do - "".chr.should == "" - end - - it "returns a 1-character String" do - "glark".chr.size.should == 1 - end - - it "returns the character at the start of the String" do - "Goodbye, world".chr.should == "G" - end - - it "returns a String in the same encoding as self" do - "\x24".encode(Encoding::US_ASCII).chr.encoding.should == Encoding::US_ASCII - end - - it "understands multi-byte characters" do - s = "\u{9879}" - s.bytesize.should == 3 - s.chr.should == s - end - - it "understands Strings that contain a mixture of character widths" do - three = "\u{8082}" - three.bytesize.should == 3 - four = "\u{77082}" - four.bytesize.should == 4 - "#{three}#{four}".chr.should == three - end +describe "String#chr" do + it "returns a copy of self" do + s = 'e' + s.should_not equal s.chr + end + + it "returns a String" do + 'glark'.chr.should be_an_instance_of(String) + end + + it "returns an empty String if self is an empty String" do + "".chr.should == "" + end + + it "returns a 1-character String" do + "glark".chr.size.should == 1 + end + + it "returns the character at the start of the String" do + "Goodbye, world".chr.should == "G" + end + + it "returns a String in the same encoding as self" do + "\x24".encode(Encoding::US_ASCII).chr.encoding.should == Encoding::US_ASCII + end + + it "understands multi-byte characters" do + s = "\u{9879}" + s.bytesize.should == 3 + s.chr.should == s + end + + it "understands Strings that contain a mixture of character widths" do + three = "\u{8082}" + three.bytesize.should == 3 + four = "\u{77082}" + four.bytesize.should == 4 + "#{three}#{four}".chr.should == three end end diff --git a/spec/ruby/core/string/clear_spec.rb b/spec/ruby/core/string/clear_spec.rb index 8b0ff8a8e6..45d452e947 100644 --- a/spec/ruby/core/string/clear_spec.rb +++ b/spec/ruby/core/string/clear_spec.rb @@ -1,39 +1,37 @@ require_relative '../../spec_helper' -with_feature :encoding do - describe "String#clear" do - before :each do - @s = "Jolene" - end +describe "String#clear" do + before :each do + @s = "Jolene" + end - it "sets self equal to the empty String" do - @s.clear - @s.should == "" - end + it "sets self equal to the empty String" do + @s.clear + @s.should == "" + end - it "returns self after emptying it" do - cleared = @s.clear - cleared.should == "" - cleared.should equal @s - end + it "returns self after emptying it" do + cleared = @s.clear + cleared.should == "" + cleared.should equal @s + end - it "preserves its encoding" do - @s.encode!(Encoding::SHIFT_JIS) - @s.encoding.should == Encoding::SHIFT_JIS - @s.clear.encoding.should == Encoding::SHIFT_JIS - @s.encoding.should == Encoding::SHIFT_JIS - end + it "preserves its encoding" do + @s.encode!(Encoding::SHIFT_JIS) + @s.encoding.should == Encoding::SHIFT_JIS + @s.clear.encoding.should == Encoding::SHIFT_JIS + @s.encoding.should == Encoding::SHIFT_JIS + end - it "works with multibyte Strings" do - s = "\u{9765}\u{876}" - s.clear - s.should == "" - end + it "works with multibyte Strings" do + s = "\u{9765}\u{876}" + s.clear + s.should == "" + end - it "raises a #{frozen_error_class} if self is frozen" do - @s.freeze - lambda { @s.clear }.should raise_error(frozen_error_class) - lambda { "".freeze.clear }.should raise_error(frozen_error_class) - end + it "raises a #{frozen_error_class} if self is frozen" do + @s.freeze + lambda { @s.clear }.should raise_error(frozen_error_class) + lambda { "".freeze.clear }.should raise_error(frozen_error_class) end end diff --git a/spec/ruby/core/string/codepoints_spec.rb b/spec/ruby/core/string/codepoints_spec.rb index bccb3d0484..e5eeb8b69d 100644 --- a/spec/ruby/core/string/codepoints_spec.rb +++ b/spec/ruby/core/string/codepoints_spec.rb @@ -3,18 +3,16 @@ require_relative '../../spec_helper' require_relative 'shared/codepoints' require_relative 'shared/each_codepoint_without_block' -with_feature :encoding do - describe "String#codepoints" do - it_behaves_like :string_codepoints, :codepoints +describe "String#codepoints" do + it_behaves_like :string_codepoints, :codepoints - it "returns an Array when no block is given" do - "abc".codepoints.should == [?a.ord, ?b.ord, ?c.ord] - end + it "returns an Array when no block is given" do + "abc".codepoints.should == [?a.ord, ?b.ord, ?c.ord] + end - it "raises an ArgumentError when no block is given if self has an invalid encoding" do - s = "\xDF".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - lambda { s.codepoints }.should raise_error(ArgumentError) - end + it "raises an ArgumentError when no block is given if self has an invalid encoding" do + s = "\xDF".force_encoding(Encoding::UTF_8) + s.valid_encoding?.should be_false + lambda { s.codepoints }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/string/downcase_spec.rb b/spec/ruby/core/string/downcase_spec.rb index 241bd8c147..9d57ea8e25 100644 --- a/spec/ruby/core/string/downcase_spec.rb +++ b/spec/ruby/core/string/downcase_spec.rb @@ -174,9 +174,7 @@ describe "String#downcase!" do lambda { "hello".freeze.downcase! }.should raise_error(frozen_error_class) end - with_feature :encoding do - it "sets the result String encoding to the source String encoding" do - "ABC".downcase.encoding.should equal(Encoding::UTF_8) - end + it "sets the result String encoding to the source String encoding" do + "ABC".downcase.encoding.should equal(Encoding::UTF_8) end end diff --git a/spec/ruby/core/string/each_codepoint_spec.rb b/spec/ruby/core/string/each_codepoint_spec.rb index 41ca527653..c11cb1beae 100644 --- a/spec/ruby/core/string/each_codepoint_spec.rb +++ b/spec/ruby/core/string/each_codepoint_spec.rb @@ -2,9 +2,7 @@ require_relative '../../spec_helper' require_relative 'shared/codepoints' require_relative 'shared/each_codepoint_without_block' -with_feature :encoding do - describe "String#each_codepoint" do - it_behaves_like :string_codepoints, :each_codepoint - it_behaves_like :string_each_codepoint_without_block, :each_codepoint - end +describe "String#each_codepoint" do + it_behaves_like :string_codepoints, :each_codepoint + it_behaves_like :string_each_codepoint_without_block, :each_codepoint end diff --git a/spec/ruby/core/string/element_set_spec.rb b/spec/ruby/core/string/element_set_spec.rb index 340bd2b9ca..f85256d36e 100644 --- a/spec/ruby/core/string/element_set_spec.rb +++ b/spec/ruby/core/string/element_set_spec.rb @@ -83,68 +83,66 @@ describe "String#[]= with Fixnum index" do lambda { "test"[1] = nil }.should raise_error(TypeError) end - with_feature :encoding do - it "raises a TypeError if passed a Fixnum replacement" do - lambda { "abc"[1] = 65 }.should raise_error(TypeError) - end + it "raises a TypeError if passed a Fixnum replacement" do + lambda { "abc"[1] = 65 }.should raise_error(TypeError) + end - it "raises an IndexError if the index is greater than character size" do - lambda { "あれ"[4] = "a" }.should raise_error(IndexError) - end + it "raises an IndexError if the index is greater than character size" do + lambda { "あれ"[4] = "a" }.should raise_error(IndexError) + end - it "calls #to_int to convert the index" do - index = mock("string element set") - index.should_receive(:to_int).and_return(1) + it "calls #to_int to convert the index" do + index = mock("string element set") + index.should_receive(:to_int).and_return(1) - str = "あれ" - str[index] = "a" - str.should == "あa" - end + str = "あれ" + str[index] = "a" + str.should == "あa" + end - it "raises a TypeError if #to_int does not return an Fixnum" do - index = mock("string element set") - index.should_receive(:to_int).and_return('1') + it "raises a TypeError if #to_int does not return an Fixnum" do + index = mock("string element set") + index.should_receive(:to_int).and_return('1') - lambda { "abc"[index] = "d" }.should raise_error(TypeError) - end + lambda { "abc"[index] = "d" }.should raise_error(TypeError) + end - it "raises an IndexError if #to_int returns a value out of range" do - index = mock("string element set") - index.should_receive(:to_int).and_return(4) + it "raises an IndexError if #to_int returns a value out of range" do + index = mock("string element set") + index.should_receive(:to_int).and_return(4) - lambda { "ab"[index] = "c" }.should raise_error(IndexError) - end + lambda { "ab"[index] = "c" }.should raise_error(IndexError) + end - it "replaces a character with a multibyte character" do - str = "ありがとu" - str[4] = "う" - str.should == "ありがとう" - end + it "replaces a character with a multibyte character" do + str = "ありがとu" + str[4] = "う" + str.should == "ありがとう" + end - it "replaces a multibyte character with a character" do - str = "ありがとう" - str[4] = "u" - str.should == "ありがとu" - end + it "replaces a multibyte character with a character" do + str = "ありがとう" + str[4] = "u" + str.should == "ありがとu" + end - it "replaces a multibyte character with a multibyte character" do - str = "ありがとお" - str[4] = "う" - str.should == "ありがとう" - end + it "replaces a multibyte character with a multibyte character" do + str = "ありがとお" + str[4] = "う" + str.should == "ありがとう" + end - it "encodes the String in an encoding compatible with the replacement" do - str = " ".force_encoding Encoding::US_ASCII - rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT - str[0] = rep - str.encoding.should equal(Encoding::ASCII_8BIT) - end + it "encodes the String in an encoding compatible with the replacement" do + str = " ".force_encoding Encoding::US_ASCII + rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT + str[0] = rep + str.encoding.should equal(Encoding::ASCII_8BIT) + end - it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do - str = "あれ" - rep = "が".encode Encoding::EUC_JP - lambda { str[0] = rep }.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do + str = "あれ" + rep = "が".encode Encoding::EUC_JP + lambda { str[0] = rep }.should raise_error(Encoding::CompatibilityError) end end @@ -172,37 +170,35 @@ describe "String#[]= with String index" do lambda { str["g"] = "h" }.should raise_error(IndexError) end - with_feature :encoding do - it "replaces characters with a multibyte character" do - str = "ありgaとう" - str["ga"] = "が" - str.should == "ありがとう" - end + it "replaces characters with a multibyte character" do + str = "ありgaとう" + str["ga"] = "が" + str.should == "ありがとう" + end - it "replaces multibyte characters with characters" do - str = "ありがとう" - str["が"] = "ga" - str.should == "ありgaとう" - end + it "replaces multibyte characters with characters" do + str = "ありがとう" + str["が"] = "ga" + str.should == "ありgaとう" + end - it "replaces multibyte characters with multibyte characters" do - str = "ありがとう" - str["が"] = "か" - str.should == "ありかとう" - end + it "replaces multibyte characters with multibyte characters" do + str = "ありがとう" + str["が"] = "か" + str.should == "ありかとう" + end - it "encodes the String in an encoding compatible with the replacement" do - str = " ".force_encoding Encoding::US_ASCII - rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT - str[" "] = rep - str.encoding.should equal(Encoding::ASCII_8BIT) - end + it "encodes the String in an encoding compatible with the replacement" do + str = " ".force_encoding Encoding::US_ASCII + rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT + str[" "] = rep + str.encoding.should equal(Encoding::ASCII_8BIT) + end - it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do - str = "あれ" - rep = "が".encode Encoding::EUC_JP - lambda { str["れ"] = rep }.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do + str = "あれ" + rep = "が".encode Encoding::EUC_JP + lambda { str["れ"] = rep }.should raise_error(Encoding::CompatibilityError) end end @@ -287,37 +283,35 @@ describe "String#[]= with a Regexp index" do end end - with_feature :encoding do - it "replaces characters with a multibyte character" do - str = "ありgaとう" - str[/ga/] = "が" - str.should == "ありがとう" - end + it "replaces characters with a multibyte character" do + str = "ありgaとう" + str[/ga/] = "が" + str.should == "ありがとう" + end - it "replaces multibyte characters with characters" do - str = "ありがとう" - str[/が/] = "ga" - str.should == "ありgaとう" - end + it "replaces multibyte characters with characters" do + str = "ありがとう" + str[/が/] = "ga" + str.should == "ありgaとう" + end - it "replaces multibyte characters with multibyte characters" do - str = "ありがとう" - str[/が/] = "か" - str.should == "ありかとう" - end + it "replaces multibyte characters with multibyte characters" do + str = "ありがとう" + str[/が/] = "か" + str.should == "ありかとう" + end - it "encodes the String in an encoding compatible with the replacement" do - str = " ".force_encoding Encoding::US_ASCII - rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT - str[/ /] = rep - str.encoding.should equal(Encoding::ASCII_8BIT) - end + it "encodes the String in an encoding compatible with the replacement" do + str = " ".force_encoding Encoding::US_ASCII + rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT + str[/ /] = rep + str.encoding.should equal(Encoding::ASCII_8BIT) + end - it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do - str = "あれ" - rep = "が".encode Encoding::EUC_JP - lambda { str[/れ/] = rep }.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do + str = "あれ" + rep = "が".encode Encoding::EUC_JP + lambda { str[/れ/] = rep }.should raise_error(Encoding::CompatibilityError) end end @@ -392,55 +386,53 @@ describe "String#[]= with a Range index" do str.should == "abcxd" end - with_feature :encoding do - it "replaces characters with a multibyte character" do - str = "ありgaとう" - str[2..3] = "が" - str.should == "ありがとう" - end + it "replaces characters with a multibyte character" do + str = "ありgaとう" + str[2..3] = "が" + str.should == "ありがとう" + end - it "replaces multibyte characters with characters" do - str = "ありがとう" - str[2...3] = "ga" - str.should == "ありgaとう" - end + it "replaces multibyte characters with characters" do + str = "ありがとう" + str[2...3] = "ga" + str.should == "ありgaとう" + end - it "replaces multibyte characters by negative indexes" do - str = "ありがとう" - str[-3...-2] = "ga" - str.should == "ありgaとう" - end + it "replaces multibyte characters by negative indexes" do + str = "ありがとう" + str[-3...-2] = "ga" + str.should == "ありgaとう" + end - it "replaces multibyte characters with multibyte characters" do - str = "ありがとう" - str[2..2] = "か" - str.should == "ありかとう" - end + it "replaces multibyte characters with multibyte characters" do + str = "ありがとう" + str[2..2] = "か" + str.should == "ありかとう" + end - it "deletes a multibyte character" do - str = "ありとう" - str[2..3] = "" - str.should == "あり" - end + it "deletes a multibyte character" do + str = "ありとう" + str[2..3] = "" + str.should == "あり" + end - it "inserts a multibyte character" do - str = "ありとう" - str[2...2] = "が" - str.should == "ありがとう" - end + it "inserts a multibyte character" do + str = "ありとう" + str[2...2] = "が" + str.should == "ありがとう" + end - it "encodes the String in an encoding compatible with the replacement" do - str = " ".force_encoding Encoding::US_ASCII - rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT - str[0..1] = rep - str.encoding.should equal(Encoding::ASCII_8BIT) - end + it "encodes the String in an encoding compatible with the replacement" do + str = " ".force_encoding Encoding::US_ASCII + rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT + str[0..1] = rep + str.encoding.should equal(Encoding::ASCII_8BIT) + end - it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do - str = "あれ" - rep = "が".encode Encoding::EUC_JP - lambda { str[0..1] = rep }.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do + str = "あれ" + rep = "が".encode Encoding::EUC_JP + lambda { str[0..1] = rep }.should raise_error(Encoding::CompatibilityError) end end @@ -561,52 +553,50 @@ describe "String#[]= with Fixnum index, count" do lambda { "hello"[0, 2] = 33 }.should raise_error(TypeError) end - with_feature :encoding do - it "replaces characters with a multibyte character" do - str = "ありgaとう" - str[2, 2] = "が" - str.should == "ありがとう" - end + it "replaces characters with a multibyte character" do + str = "ありgaとう" + str[2, 2] = "が" + str.should == "ありがとう" + end - it "replaces multibyte characters with characters" do - str = "ありがとう" - str[2, 1] = "ga" - str.should == "ありgaとう" - end + it "replaces multibyte characters with characters" do + str = "ありがとう" + str[2, 1] = "ga" + str.should == "ありgaとう" + end - it "replaces multibyte characters with multibyte characters" do - str = "ありがとう" - str[2, 1] = "か" - str.should == "ありかとう" - end + it "replaces multibyte characters with multibyte characters" do + str = "ありがとう" + str[2, 1] = "か" + str.should == "ありかとう" + end - it "deletes a multibyte character" do - str = "ありとう" - str[2, 2] = "" - str.should == "あり" - end + it "deletes a multibyte character" do + str = "ありとう" + str[2, 2] = "" + str.should == "あり" + end - it "inserts a multibyte character" do - str = "ありとう" - str[2, 0] = "が" - str.should == "ありがとう" - end + it "inserts a multibyte character" do + str = "ありとう" + str[2, 0] = "が" + str.should == "ありがとう" + end - it "raises an IndexError if the character index is out of range of a multibyte String" do - lambda { "あれ"[3, 0] = "り" }.should raise_error(IndexError) - end + it "raises an IndexError if the character index is out of range of a multibyte String" do + lambda { "あれ"[3, 0] = "り" }.should raise_error(IndexError) + end - it "encodes the String in an encoding compatible with the replacement" do - str = " ".force_encoding Encoding::US_ASCII - rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT - str[0, 1] = rep - str.encoding.should equal(Encoding::ASCII_8BIT) - end + it "encodes the String in an encoding compatible with the replacement" do + str = " ".force_encoding Encoding::US_ASCII + rep = [160].pack('C').force_encoding Encoding::ASCII_8BIT + str[0, 1] = rep + str.encoding.should equal(Encoding::ASCII_8BIT) + end - it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do - str = "あれ" - rep = "が".encode Encoding::EUC_JP - lambda { str[0, 1] = rep }.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do + str = "あれ" + rep = "が".encode Encoding::EUC_JP + lambda { str[0, 1] = rep }.should raise_error(Encoding::CompatibilityError) end end diff --git a/spec/ruby/core/string/encode_spec.rb b/spec/ruby/core/string/encode_spec.rb index f582d50794..0be26011ea 100644 --- a/spec/ruby/core/string/encode_spec.rb +++ b/spec/ruby/core/string/encode_spec.rb @@ -2,158 +2,156 @@ require_relative '../../spec_helper' require_relative 'shared/encode' -with_feature :encoding do - describe "String#encode" do - before :each do - @external = Encoding.default_external - @internal = Encoding.default_internal - end +describe "String#encode" do + before :each do + @external = Encoding.default_external + @internal = Encoding.default_internal + end - after :each do - Encoding.default_external = @external - Encoding.default_internal = @internal - end + after :each do + Encoding.default_external = @external + Encoding.default_internal = @internal + end - it_behaves_like :string_encode, :encode + it_behaves_like :string_encode, :encode - describe "when passed no options" do - it "returns a copy when Encoding.default_internal is nil" do - Encoding.default_internal = nil - str = "あ" - str.encode.should_not equal(str) - end + describe "when passed no options" do + it "returns a copy when Encoding.default_internal is nil" do + Encoding.default_internal = nil + str = "あ" + str.encode.should_not equal(str) + end - it "returns a copy for a ASCII-only String when Encoding.default_internal is nil" do - Encoding.default_internal = nil - str = "abc" - str.encode.should_not equal(str) - end + it "returns a copy for a ASCII-only String when Encoding.default_internal is nil" do + Encoding.default_internal = nil + str = "abc" + str.encode.should_not equal(str) + end - it "encodes an ascii substring of a binary string to UTF-8" do - x82 = [0x82].pack('C') - str = "#{x82}foo".force_encoding("ascii-8bit")[1..-1].encode("utf-8") - str.should == "foo".force_encoding("utf-8") - str.encoding.should equal(Encoding::UTF_8) - end + it "encodes an ascii substring of a binary string to UTF-8" do + x82 = [0x82].pack('C') + str = "#{x82}foo".force_encoding("ascii-8bit")[1..-1].encode("utf-8") + str.should == "foo".force_encoding("utf-8") + str.encoding.should equal(Encoding::UTF_8) end + end - describe "when passed to encoding" do - it "returns a copy when passed the same encoding as the String" do - str = "あ" - str.encode(Encoding::UTF_8).should_not equal(str) - end + describe "when passed to encoding" do + it "returns a copy when passed the same encoding as the String" do + str = "あ" + str.encode(Encoding::UTF_8).should_not equal(str) + end - it "round trips a String" do - str = "abc def".force_encoding Encoding::US_ASCII - str.encode("utf-32be").encode("ascii").should == "abc def" - end + it "round trips a String" do + str = "abc def".force_encoding Encoding::US_ASCII + str.encode("utf-32be").encode("ascii").should == "abc def" end + end - describe "when passed options" do - it "returns a copy when Encoding.default_internal is nil" do - Encoding.default_internal = nil - str = "あ" - str.encode(invalid: :replace).should_not equal(str) - end + describe "when passed options" do + it "returns a copy when Encoding.default_internal is nil" do + Encoding.default_internal = nil + str = "あ" + str.encode(invalid: :replace).should_not equal(str) + end - it "normalizes newlines" do - "\r\nfoo".encode(universal_newline: true).should == "\nfoo" + it "normalizes newlines" do + "\r\nfoo".encode(universal_newline: true).should == "\nfoo" - "\rfoo".encode(universal_newline: true).should == "\nfoo" - end + "\rfoo".encode(universal_newline: true).should == "\nfoo" end + end - describe "when passed to, from" do - it "returns a copy in the destination encoding when both encodings are the same" do - str = "あ" - str.force_encoding("ascii-8bit") - encoded = str.encode("utf-8", "utf-8") - - encoded.should_not equal(str) - encoded.encoding.should == Encoding::UTF_8 - end + describe "when passed to, from" do + it "returns a copy in the destination encoding when both encodings are the same" do + str = "あ" + str.force_encoding("ascii-8bit") + encoded = str.encode("utf-8", "utf-8") - it "returns the transcoded string" do - str = "\x00\x00\x00\x1F" - str.encode(Encoding::UTF_8, Encoding::UTF_16BE).should == "\u0000\u001f" - end + encoded.should_not equal(str) + encoded.encoding.should == Encoding::UTF_8 end - describe "when passed to, options" do - it "returns a copy when the destination encoding is the same as the String encoding" do - str = "あ" - str.encode(Encoding::UTF_8, undef: :replace).should_not equal(str) - end + it "returns the transcoded string" do + str = "\x00\x00\x00\x1F" + str.encode(Encoding::UTF_8, Encoding::UTF_16BE).should == "\u0000\u001f" end + end - describe "when passed to, from, options" do - it "returns a copy when both encodings are the same" do - str = "あ" - str.encode("utf-8", "utf-8", invalid: :replace).should_not equal(str) - end + describe "when passed to, options" do + it "returns a copy when the destination encoding is the same as the String encoding" do + str = "あ" + str.encode(Encoding::UTF_8, undef: :replace).should_not equal(str) end end - describe "String#encode!" do - before :each do - @external = Encoding.default_external - @internal = Encoding.default_internal + describe "when passed to, from, options" do + it "returns a copy when both encodings are the same" do + str = "あ" + str.encode("utf-8", "utf-8", invalid: :replace).should_not equal(str) end + end +end - after :each do - Encoding.default_external = @external - Encoding.default_internal = @internal - end +describe "String#encode!" do + before :each do + @external = Encoding.default_external + @internal = Encoding.default_internal + end - it_behaves_like :string_encode, :encode! + after :each do + Encoding.default_external = @external + Encoding.default_internal = @internal + end - it "raises a #{frozen_error_class} when called on a frozen String" do - lambda { "foo".freeze.encode!("euc-jp") }.should raise_error(frozen_error_class) - end + it_behaves_like :string_encode, :encode! - # http://redmine.ruby-lang.org/issues/show/1836 - it "raises a #{frozen_error_class} when called on a frozen String when it's a no-op" do - lambda { "foo".freeze.encode!("utf-8") }.should raise_error(frozen_error_class) - end + it "raises a #{frozen_error_class} when called on a frozen String" do + lambda { "foo".freeze.encode!("euc-jp") }.should raise_error(frozen_error_class) + end - describe "when passed no options" do - it "returns self when Encoding.default_internal is nil" do - Encoding.default_internal = nil - str = "あ" - str.encode!.should equal(str) - end + # http://redmine.ruby-lang.org/issues/show/1836 + it "raises a #{frozen_error_class} when called on a frozen String when it's a no-op" do + lambda { "foo".freeze.encode!("utf-8") }.should raise_error(frozen_error_class) + end + + describe "when passed no options" do + it "returns self when Encoding.default_internal is nil" do + Encoding.default_internal = nil + str = "あ" + str.encode!.should equal(str) + end - it "returns self for a ASCII-only String when Encoding.default_internal is nil" do - Encoding.default_internal = nil - str = "abc" - str.encode!.should equal(str) - end + it "returns self for a ASCII-only String when Encoding.default_internal is nil" do + Encoding.default_internal = nil + str = "abc" + str.encode!.should equal(str) end + end - describe "when passed options" do - it "returns self for ASCII-only String when Encoding.default_internal is nil" do - Encoding.default_internal = nil - str = "abc" - str.encode!(invalid: :replace).should equal(str) - end + describe "when passed options" do + it "returns self for ASCII-only String when Encoding.default_internal is nil" do + Encoding.default_internal = nil + str = "abc" + str.encode!(invalid: :replace).should equal(str) end + end - describe "when passed to encoding" do - it "returns self" do - str = "abc" - result = str.encode!(Encoding::BINARY) - result.encoding.should equal(Encoding::BINARY) - result.should equal(str) - end + describe "when passed to encoding" do + it "returns self" do + str = "abc" + result = str.encode!(Encoding::BINARY) + result.encoding.should equal(Encoding::BINARY) + result.should equal(str) end + end - describe "when passed to, from" do - it "returns self" do - str = "ああ" - result = str.encode!("euc-jp", "utf-8") - result.encoding.should equal(Encoding::EUC_JP) - result.should equal(str) - end + describe "when passed to, from" do + it "returns self" do + str = "ああ" + result = str.encode!("euc-jp", "utf-8") + result.encoding.should equal(Encoding::EUC_JP) + result.should equal(str) end end end diff --git a/spec/ruby/core/string/encoding_spec.rb b/spec/ruby/core/string/encoding_spec.rb index b2861f2264..6182e8eb50 100644 --- a/spec/ruby/core/string/encoding_spec.rb +++ b/spec/ruby/core/string/encoding_spec.rb @@ -2,188 +2,186 @@ require_relative '../../spec_helper' require_relative 'fixtures/iso-8859-9-encoding' -with_feature :encoding do - describe "String#encoding" do - it "returns an Encoding object" do - String.new.encoding.should be_an_instance_of(Encoding) - end - - it "is equal to the source encoding by default" do - s = StringSpecs::ISO88599Encoding.new - s.cedilla.encoding.should == s.source_encoding - end - - it "returns the given encoding if #force_encoding has been called" do - "a".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - end - - it "returns the given encoding if #encode!has been called" do - "a".encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - end - end - - describe "String#encoding for US-ASCII Strings" do - it "returns US-ASCII if self is US-ASCII" do - "a".encoding.should == Encoding::US_ASCII - end - - it "returns US-ASCII if self is US-ASCII only, despite the default internal encoding being different" do - default_internal = Encoding.default_internal - Encoding.default_internal = Encoding::UTF_8 - "a".encoding.should == Encoding::US_ASCII - Encoding.default_internal = default_internal - end - - it "returns US-ASCII if self is US-ASCII only, despite the default external encoding being different" do - default_external = Encoding.default_external - Encoding.default_external = Encoding::UTF_8 - "a".encoding.should == Encoding::US_ASCII - Encoding.default_external = default_external - end - - it "returns US-ASCII if self is US-ASCII only, despite the default internal and external encodings being different" do - default_internal = Encoding.default_internal - default_external = Encoding.default_external - Encoding.default_internal = Encoding::UTF_8 - Encoding.default_external = Encoding::UTF_8 - "a".encoding.should == Encoding::US_ASCII - Encoding.default_external = default_external - Encoding.default_internal = default_internal - end - - it "returns US-ASCII if self is US-ASCII only, despite the default encodings being different" do - default_internal = Encoding.default_internal - default_external = Encoding.default_external - Encoding.default_internal = Encoding::UTF_8 - Encoding.default_external = Encoding::UTF_8 - "a".encoding.should == Encoding::US_ASCII - Encoding.default_external = default_external - Encoding.default_internal = default_internal - end - - end - - describe "String#encoding for Strings with \\u escapes" do - it "returns UTF-8" do - "\u{4040}".encoding.should == Encoding::UTF_8 - end - - it "returns US-ASCII if self is US-ASCII only" do - s = "\u{40}" - s.ascii_only?.should be_true - s.encoding.should == Encoding::US_ASCII - end - - it "returns UTF-8 if self isn't US-ASCII only" do - s = "\u{4076}\u{619}" - s.ascii_only?.should be_false - s.encoding.should == Encoding::UTF_8 - end - - it "is not affected by the default internal encoding" do - default_internal = Encoding.default_internal - Encoding.default_internal = Encoding::ISO_8859_15 - "\u{5050}".encoding.should == Encoding::UTF_8 - "\u{50}".encoding.should == Encoding::US_ASCII - Encoding.default_internal = default_internal - end - - it "is not affected by the default external encoding" do - default_external = Encoding.default_external - Encoding.default_external = Encoding::SHIFT_JIS - "\u{50}".encoding.should == Encoding::US_ASCII - "\u{5050}".encoding.should == Encoding::UTF_8 - Encoding.default_external = default_external - end - - it "is not affected by both the default internal and external encoding being set at the same time" do - default_internal = Encoding.default_internal - default_external = Encoding.default_external - Encoding.default_internal = Encoding::EUC_JP - Encoding.default_external = Encoding::SHIFT_JIS - "\u{50}".encoding.should == Encoding::US_ASCII - "\u{507}".encoding.should == Encoding::UTF_8 - Encoding.default_external = default_external - Encoding.default_internal = default_internal - end - - it "returns the given encoding if #force_encoding has been called" do - "\u{20}".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - "\u{2020}".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - end - - it "returns the given encoding if #encode!has been called" do - "\u{20}".encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - "\u{2020}".encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - end - end - - describe "String#encoding for Strings with \\x escapes" do - - it "returns US-ASCII if self is US-ASCII only" do - s = "\x61" - s.ascii_only?.should be_true - s.encoding.should == Encoding::US_ASCII - end - - it "returns ASCII-8BIT when an escape creates a byte with the 8th bit set if the source encoding is US-ASCII" do - __ENCODING__.should == Encoding::US_ASCII - str = " " - str.encoding.should == Encoding::US_ASCII - str += [0xDF].pack('C') - str.ascii_only?.should be_false - str.encoding.should == Encoding::ASCII_8BIT - end - - # TODO: Deal with case when the byte in question isn't valid in the source - # encoding? - it "returns the source encoding when an escape creates a byte with the 8th bit set if the source encoding isn't US-ASCII" do - fixture = StringSpecs::ISO88599Encoding.new - fixture.source_encoding.should == Encoding::ISO8859_9 - fixture.x_escape.ascii_only?.should be_false - fixture.x_escape.encoding.should == Encoding::ISO8859_9 - end - - it "is not affected by the default internal encoding" do - default_internal = Encoding.default_internal - Encoding.default_internal = Encoding::ISO_8859_15 - "\x50".encoding.should == Encoding::US_ASCII - "\x50".encoding.should == Encoding::US_ASCII - Encoding.default_internal = default_internal - end - - it "is not affected by the default external encoding" do - default_external = Encoding.default_external - Encoding.default_external = Encoding::SHIFT_JIS - "\x50".encoding.should == Encoding::US_ASCII - [0xD4].pack('C').encoding.should == Encoding::ASCII_8BIT - Encoding.default_external = default_external - end - - it "is not affected by both the default internal and external encoding being set at the same time" do - default_internal = Encoding.default_internal - default_external = Encoding.default_external - Encoding.default_internal = Encoding::EUC_JP - Encoding.default_external = Encoding::SHIFT_JIS - x50 = "\x50" - x50.encoding.should == Encoding::US_ASCII - [0xD4].pack('C').encoding.should == Encoding::ASCII_8BIT - Encoding.default_external = default_external - Encoding.default_internal = default_internal - end - - it "returns the given encoding if #force_encoding has been called" do - x50 = "\x50" - x50.force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - xD4 = [212].pack('C') - xD4.force_encoding(Encoding::ISO_8859_9).encoding.should == Encoding::ISO_8859_9 - end - - it "returns the given encoding if #encode!has been called" do - x50 = "\x50" - x50.encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - x00 = "x\00" - x00.encode!(Encoding::UTF_8).encoding.should == Encoding::UTF_8 - end +describe "String#encoding" do + it "returns an Encoding object" do + String.new.encoding.should be_an_instance_of(Encoding) + end + + it "is equal to the source encoding by default" do + s = StringSpecs::ISO88599Encoding.new + s.cedilla.encoding.should == s.source_encoding + end + + it "returns the given encoding if #force_encoding has been called" do + "a".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + end + + it "returns the given encoding if #encode!has been called" do + "a".encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + end +end + +describe "String#encoding for US-ASCII Strings" do + it "returns US-ASCII if self is US-ASCII" do + "a".encoding.should == Encoding::US_ASCII + end + + it "returns US-ASCII if self is US-ASCII only, despite the default internal encoding being different" do + default_internal = Encoding.default_internal + Encoding.default_internal = Encoding::UTF_8 + "a".encoding.should == Encoding::US_ASCII + Encoding.default_internal = default_internal + end + + it "returns US-ASCII if self is US-ASCII only, despite the default external encoding being different" do + default_external = Encoding.default_external + Encoding.default_external = Encoding::UTF_8 + "a".encoding.should == Encoding::US_ASCII + Encoding.default_external = default_external + end + + it "returns US-ASCII if self is US-ASCII only, despite the default internal and external encodings being different" do + default_internal = Encoding.default_internal + default_external = Encoding.default_external + Encoding.default_internal = Encoding::UTF_8 + Encoding.default_external = Encoding::UTF_8 + "a".encoding.should == Encoding::US_ASCII + Encoding.default_external = default_external + Encoding.default_internal = default_internal + end + + it "returns US-ASCII if self is US-ASCII only, despite the default encodings being different" do + default_internal = Encoding.default_internal + default_external = Encoding.default_external + Encoding.default_internal = Encoding::UTF_8 + Encoding.default_external = Encoding::UTF_8 + "a".encoding.should == Encoding::US_ASCII + Encoding.default_external = default_external + Encoding.default_internal = default_internal + end + +end + +describe "String#encoding for Strings with \\u escapes" do + it "returns UTF-8" do + "\u{4040}".encoding.should == Encoding::UTF_8 + end + + it "returns US-ASCII if self is US-ASCII only" do + s = "\u{40}" + s.ascii_only?.should be_true + s.encoding.should == Encoding::US_ASCII + end + + it "returns UTF-8 if self isn't US-ASCII only" do + s = "\u{4076}\u{619}" + s.ascii_only?.should be_false + s.encoding.should == Encoding::UTF_8 + end + + it "is not affected by the default internal encoding" do + default_internal = Encoding.default_internal + Encoding.default_internal = Encoding::ISO_8859_15 + "\u{5050}".encoding.should == Encoding::UTF_8 + "\u{50}".encoding.should == Encoding::US_ASCII + Encoding.default_internal = default_internal + end + + it "is not affected by the default external encoding" do + default_external = Encoding.default_external + Encoding.default_external = Encoding::SHIFT_JIS + "\u{50}".encoding.should == Encoding::US_ASCII + "\u{5050}".encoding.should == Encoding::UTF_8 + Encoding.default_external = default_external + end + + it "is not affected by both the default internal and external encoding being set at the same time" do + default_internal = Encoding.default_internal + default_external = Encoding.default_external + Encoding.default_internal = Encoding::EUC_JP + Encoding.default_external = Encoding::SHIFT_JIS + "\u{50}".encoding.should == Encoding::US_ASCII + "\u{507}".encoding.should == Encoding::UTF_8 + Encoding.default_external = default_external + Encoding.default_internal = default_internal + end + + it "returns the given encoding if #force_encoding has been called" do + "\u{20}".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + "\u{2020}".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + end + + it "returns the given encoding if #encode!has been called" do + "\u{20}".encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + "\u{2020}".encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + end +end + +describe "String#encoding for Strings with \\x escapes" do + + it "returns US-ASCII if self is US-ASCII only" do + s = "\x61" + s.ascii_only?.should be_true + s.encoding.should == Encoding::US_ASCII + end + + it "returns ASCII-8BIT when an escape creates a byte with the 8th bit set if the source encoding is US-ASCII" do + __ENCODING__.should == Encoding::US_ASCII + str = " " + str.encoding.should == Encoding::US_ASCII + str += [0xDF].pack('C') + str.ascii_only?.should be_false + str.encoding.should == Encoding::ASCII_8BIT + end + + # TODO: Deal with case when the byte in question isn't valid in the source + # encoding? + it "returns the source encoding when an escape creates a byte with the 8th bit set if the source encoding isn't US-ASCII" do + fixture = StringSpecs::ISO88599Encoding.new + fixture.source_encoding.should == Encoding::ISO8859_9 + fixture.x_escape.ascii_only?.should be_false + fixture.x_escape.encoding.should == Encoding::ISO8859_9 + end + + it "is not affected by the default internal encoding" do + default_internal = Encoding.default_internal + Encoding.default_internal = Encoding::ISO_8859_15 + "\x50".encoding.should == Encoding::US_ASCII + "\x50".encoding.should == Encoding::US_ASCII + Encoding.default_internal = default_internal + end + + it "is not affected by the default external encoding" do + default_external = Encoding.default_external + Encoding.default_external = Encoding::SHIFT_JIS + "\x50".encoding.should == Encoding::US_ASCII + [0xD4].pack('C').encoding.should == Encoding::ASCII_8BIT + Encoding.default_external = default_external + end + + it "is not affected by both the default internal and external encoding being set at the same time" do + default_internal = Encoding.default_internal + default_external = Encoding.default_external + Encoding.default_internal = Encoding::EUC_JP + Encoding.default_external = Encoding::SHIFT_JIS + x50 = "\x50" + x50.encoding.should == Encoding::US_ASCII + [0xD4].pack('C').encoding.should == Encoding::ASCII_8BIT + Encoding.default_external = default_external + Encoding.default_internal = default_internal + end + + it "returns the given encoding if #force_encoding has been called" do + x50 = "\x50" + x50.force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + xD4 = [212].pack('C') + xD4.force_encoding(Encoding::ISO_8859_9).encoding.should == Encoding::ISO_8859_9 + end + + it "returns the given encoding if #encode!has been called" do + x50 = "\x50" + x50.encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + x00 = "x\00" + x00.encode!(Encoding::UTF_8).encoding.should == Encoding::UTF_8 end end diff --git a/spec/ruby/core/string/force_encoding_spec.rb b/spec/ruby/core/string/force_encoding_spec.rb index 06e04b8d95..83641a37b4 100644 --- a/spec/ruby/core/string/force_encoding_spec.rb +++ b/spec/ruby/core/string/force_encoding_spec.rb @@ -1,73 +1,71 @@ require_relative '../../spec_helper' -with_feature :encoding do - describe "String#force_encoding" do - it "accepts a String as the name of an Encoding" do - "abc".force_encoding('shift_jis').encoding.should == Encoding::Shift_JIS - end - - describe "with a special encoding name" do - before :each do - @original_encoding = Encoding.default_internal - end +describe "String#force_encoding" do + it "accepts a String as the name of an Encoding" do + "abc".force_encoding('shift_jis').encoding.should == Encoding::Shift_JIS + end - after :each do - Encoding.default_internal = @original_encoding - end + describe "with a special encoding name" do + before :each do + @original_encoding = Encoding.default_internal + end - it "accepts valid special encoding names" do - Encoding.default_internal = "US-ASCII" - "abc".force_encoding("internal").encoding.should == Encoding::US_ASCII - end + after :each do + Encoding.default_internal = @original_encoding + end - it "defaults to ASCII-8BIT if special encoding name is not set" do - Encoding.default_internal = nil - "abc".force_encoding("internal").encoding.should == Encoding::ASCII_8BIT - end + it "accepts valid special encoding names" do + Encoding.default_internal = "US-ASCII" + "abc".force_encoding("internal").encoding.should == Encoding::US_ASCII end - it "accepts an Encoding instance" do - "abc".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::Shift_JIS + it "defaults to ASCII-8BIT if special encoding name is not set" do + Encoding.default_internal = nil + "abc".force_encoding("internal").encoding.should == Encoding::ASCII_8BIT end + end - it "calls #to_str to convert an object to an encoding name" do - obj = mock("force_encoding") - obj.should_receive(:to_str).and_return("utf-8") + it "accepts an Encoding instance" do + "abc".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::Shift_JIS + end - "abc".force_encoding(obj).encoding.should == Encoding::UTF_8 - end + it "calls #to_str to convert an object to an encoding name" do + obj = mock("force_encoding") + obj.should_receive(:to_str).and_return("utf-8") - it "raises a TypeError if #to_str does not return a String" do - obj = mock("force_encoding") - obj.should_receive(:to_str).and_return(1) + "abc".force_encoding(obj).encoding.should == Encoding::UTF_8 + end - lambda { "abc".force_encoding(obj) }.should raise_error(TypeError) - end + it "raises a TypeError if #to_str does not return a String" do + obj = mock("force_encoding") + obj.should_receive(:to_str).and_return(1) - it "raises a TypeError if passed nil" do - lambda { "abc".force_encoding(nil) }.should raise_error(TypeError) - end + lambda { "abc".force_encoding(obj) }.should raise_error(TypeError) + end - it "returns self" do - str = "abc" - str.force_encoding('utf-8').should equal(str) - end + it "raises a TypeError if passed nil" do + lambda { "abc".force_encoding(nil) }.should raise_error(TypeError) + end - it "sets the encoding even if the String contents are invalid in that encoding" do - str = "\u{9765}" - str.force_encoding('euc-jp') - str.encoding.should == Encoding::EUC_JP - str.valid_encoding?.should be_false - end + it "returns self" do + str = "abc" + str.force_encoding('utf-8').should equal(str) + end - it "does not transcode self" do - str = "\u{8612}" - str.dup.force_encoding('utf-16le').should_not == str.encode('utf-16le') - end + it "sets the encoding even if the String contents are invalid in that encoding" do + str = "\u{9765}" + str.force_encoding('euc-jp') + str.encoding.should == Encoding::EUC_JP + str.valid_encoding?.should be_false + end - it "raises a #{frozen_error_class} if self is frozen" do - str = "abcd".freeze - lambda { str.force_encoding(str.encoding) }.should raise_error(frozen_error_class) - end + it "does not transcode self" do + str = "\u{8612}" + str.dup.force_encoding('utf-16le').should_not == str.encode('utf-16le') + end + + it "raises a #{frozen_error_class} if self is frozen" do + str = "abcd".freeze + lambda { str.force_encoding(str.encoding) }.should raise_error(frozen_error_class) end end diff --git a/spec/ruby/core/string/index_spec.rb b/spec/ruby/core/string/index_spec.rb index 3ed27034e1..fb5f4e75e7 100644 --- a/spec/ruby/core/string/index_spec.rb +++ b/spec/ruby/core/string/index_spec.rb @@ -140,25 +140,23 @@ describe "String#index with String" do "I’ve got a multibyte character.\n".index("\n\n").should == nil end - with_feature :encoding do - it "returns the character index of a multibyte character" do - "ありがとう".index("が").should == 2 - end + it "returns the character index of a multibyte character" do + "ありがとう".index("が").should == 2 + end - it "returns the character index after offset" do - "われわれ".index("わ", 1).should == 2 - end + it "returns the character index after offset" do + "われわれ".index("わ", 1).should == 2 + end - it "returns the character index after a partial first match" do - "