diff options
author | Benoit Daloze <eregontp@gmail.com> | 2019-04-28 23:20:11 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2019-04-28 23:20:11 +0200 |
commit | 79671ec57e59091260a0bc3d40a31d31d9c72a94 (patch) | |
tree | 2f59a8727b8f63f9e79d50352fa4f78a7cc00234 /spec/ruby/core/string | |
parent | 994833085ae06afbe94d30ab183d80e0234fbe14 (diff) | |
download | ruby-79671ec57e59091260a0bc3d40a31d31d9c72a94.tar.gz |
Update to ruby/spec@7de852d
Diffstat (limited to 'spec/ruby/core/string')
32 files changed, 1200 insertions, 1290 deletions
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 - "</</h".index("</h").should == 2 - end + it "returns the character index after a partial first match" do + "</</h".index("</h").should == 2 + end - it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - char = "れ".encode Encoding::EUC_JP - lambda do - "あれ".index char - end.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + char = "れ".encode Encoding::EUC_JP + lambda do + "あれ".index char + end.should raise_error(Encoding::CompatibilityError) end end @@ -293,24 +291,22 @@ describe "String#index with Regexp" do "RWOARW".index(/R./, obj).should == 4 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 "treats the offset as a character index" do - "われわわれ".index(/わ/, 3).should == 3 - end + it "treats the offset as a character index" do + "われわわれ".index(/わ/, 3).should == 3 + end - it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - re = Regexp.new "れ".encode(Encoding::EUC_JP) - lambda do - "あれ".index re - end.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + re = Regexp.new "れ".encode(Encoding::EUC_JP) + lambda do + "あれ".index re + end.should raise_error(Encoding::CompatibilityError) end end diff --git a/spec/ruby/core/string/insert_spec.rb b/spec/ruby/core/string/insert_spec.rb index 2c72bb5ffd..d5b9026f3d 100644 --- a/spec/ruby/core/string/insert_spec.rb +++ b/spec/ruby/core/string/insert_spec.rb @@ -63,22 +63,20 @@ describe "String#insert with index, other" do lambda { str.insert(4, 'X') }.should raise_error(frozen_error_class) end - with_feature :encoding do - it "inserts a character into a multibyte encoded string" do - "ありがとう".insert(1, 'ü').should == "あüりがとう" - end - - it "returns a String in the compatible encoding" do - str = "".force_encoding(Encoding::US_ASCII) - str.insert(0, "ありがとう") - str.encoding.should == Encoding::UTF_8 - end - - it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - pat = "ア".encode Encoding::EUC_JP - lambda do - "あれ".insert 0, pat - end.should raise_error(Encoding::CompatibilityError) - end + it "inserts a character into a multibyte encoded string" do + "ありがとう".insert(1, 'ü').should == "あüりがとう" + end + + it "returns a String in the compatible encoding" do + str = "".force_encoding(Encoding::US_ASCII) + str.insert(0, "ありがとう") + str.encoding.should == Encoding::UTF_8 + end + + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + pat = "ア".encode Encoding::EUC_JP + lambda do + "あれ".insert 0, pat + end.should raise_error(Encoding::CompatibilityError) end end diff --git a/spec/ruby/core/string/ljust_spec.rb b/spec/ruby/core/string/ljust_spec.rb index 5838a0c9f9..a8160c727b 100644 --- a/spec/ruby/core/string/ljust_spec.rb +++ b/spec/ruby/core/string/ljust_spec.rb @@ -87,30 +87,28 @@ describe "String#ljust with length, padding" do "hello".ljust(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.ljust 5 - 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.ljust 5 + 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.ljust 5, "あ" + 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.ljust 5, "あ" - 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 - "あれ".ljust 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 + "あれ".ljust 5, pat + end.should raise_error(Encoding::CompatibilityError) end end end diff --git a/spec/ruby/core/string/match_spec.rb b/spec/ruby/core/string/match_spec.rb index 67e015f23d..c5505fd382 100644 --- a/spec/ruby/core/string/match_spec.rb +++ b/spec/ruby/core/string/match_spec.rb @@ -43,10 +43,8 @@ describe "String#=~" do $~.should == nil end - with_feature :encoding do - it "returns the character index of a found match" do - ("こにちわ" =~ /に/).should == 1 - end + it "returns the character index of a found match" do + ("こにちわ" =~ /に/).should == 1 end end @@ -64,10 +62,8 @@ describe "String#match" do "01234".match(/(.).(.)/, 1).captures.should == ["1", "3"] end - with_feature :encoding do - it "uses the start as a character offset" do - "零一二三四".match(/(.).(.)/, 1).captures.should == ["一", "三"] - end + it "uses the start as a character offset" do + "零一二三四".match(/(.).(.)/, 1).captures.should == ["一", "三"] end end @@ -76,10 +72,8 @@ describe "String#match" do "01234".match(/(.).(.)/, -4).captures.should == ["1", "3"] end - with_feature :encoding do - it "uses the start as a character offset" do - "零一二三四".match(/(.).(.)/, -4).captures.should == ["一", "三"] - end + it "uses the start as a character offset" do + "零一二三四".match(/(.).(.)/, -4).captures.should == ["一", "三"] end end end diff --git a/spec/ruby/core/string/ord_spec.rb b/spec/ruby/core/string/ord_spec.rb index 8ba110c973..63f9815ea9 100644 --- a/spec/ruby/core/string/ord_spec.rb +++ b/spec/ruby/core/string/ord_spec.rb @@ -1,30 +1,28 @@ require_relative '../../spec_helper' -with_feature :encoding do - describe "String#ord" do - it "returns a Fixnum" do - 'a'.ord.should be_an_instance_of(Fixnum) - end +describe "String#ord" do + it "returns a Fixnum" do + 'a'.ord.should be_an_instance_of(Fixnum) + end - it "returns the codepoint of the first character in the String" do - 'a'.ord.should == 97 - end + it "returns the codepoint of the first character in the String" do + 'a'.ord.should == 97 + end - it "ignores subsequent characters" do - "\u{287}a".ord.should == "\u{287}".ord - end + it "ignores subsequent characters" do + "\u{287}a".ord.should == "\u{287}".ord + end - it "understands multibyte characters" do - "\u{9879}".ord.should == 39033 - end + it "understands multibyte characters" do + "\u{9879}".ord.should == 39033 + end - it "is equivalent to #codepoints.first" do - "\u{981}\u{982}".ord.should == "\u{981}\u{982}".codepoints.first - end + it "is equivalent to #codepoints.first" do + "\u{981}\u{982}".ord.should == "\u{981}\u{982}".codepoints.first + end - it "raises an ArgumentError if called on an empty String" do - lambda { ''.ord }.should raise_error(ArgumentError) - end + it "raises an ArgumentError if called on an empty String" do + lambda { ''.ord }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/string/reverse_spec.rb b/spec/ruby/core/string/reverse_spec.rb index 7cb171cb81..f2a8b64d47 100644 --- a/spec/ruby/core/string/reverse_spec.rb +++ b/spec/ruby/core/string/reverse_spec.rb @@ -15,10 +15,8 @@ describe "String#reverse" do "m".taint.reverse.tainted?.should == true end - with_feature :encoding do - it "reverses a string with multi byte characters" do - "微軟正黑體".reverse.should == "體黑正軟微" - end + it "reverses a string with multi byte characters" do + "微軟正黑體".reverse.should == "體黑正軟微" end end @@ -42,11 +40,9 @@ describe "String#reverse!" do lambda { "".freeze.reverse! }.should raise_error(frozen_error_class) end - with_feature :encoding do - it "reverses a string with multi byte characters" do - str = "微軟正黑體" - str.reverse! - str.should == "體黑正軟微" - end + it "reverses a string with multi byte characters" do + str = "微軟正黑體" + str.reverse! + str.should == "體黑正軟微" end end diff --git a/spec/ruby/core/string/rindex_spec.rb b/spec/ruby/core/string/rindex_spec.rb index 98e36785e3..f74c0115cc 100644 --- a/spec/ruby/core/string/rindex_spec.rb +++ b/spec/ruby/core/string/rindex_spec.rb @@ -347,22 +347,20 @@ describe "String#rindex with Regexp" do lambda { "str".rindex(/../, nil) }.should raise_error(TypeError) end - with_feature :encoding do - it "returns the reverse character index of a multibyte character" do - "ありがりがとう".rindex("が").should == 4 - "ありがりがとう".rindex(/が/).should == 4 - end + it "returns the reverse character index of a multibyte character" do + "ありがりがとう".rindex("が").should == 4 + "ありがりがとう".rindex(/が/).should == 4 + end - it "returns the character index before the finish" do - "ありがりがとう".rindex("が", 3).should == 2 - "ありがりがとう".rindex(/が/, 3).should == 2 - end + it "returns the character index before the finish" do + "ありがりがとう".rindex("が", 3).should == 2 + "ありがりがとう".rindex(/が/, 3).should == 2 + end - it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - re = Regexp.new "れ".encode(Encoding::EUC_JP) - lambda do - "あれ".rindex re - end.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + re = Regexp.new "れ".encode(Encoding::EUC_JP) + lambda do + "あれ".rindex re + end.should raise_error(Encoding::CompatibilityError) end end diff --git a/spec/ruby/core/string/rjust_spec.rb b/spec/ruby/core/string/rjust_spec.rb index c2cfae7b66..2821b018e8 100644 --- a/spec/ruby/core/string/rjust_spec.rb +++ b/spec/ruby/core/string/rjust_spec.rb @@ -87,30 +87,28 @@ describe "String#rjust with length, padding" do "hello".rjust(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.rjust 5 - 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.rjust 5 + 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.rjust 5, "あ" + 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.rjust 5, "あ" - 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 - "あれ".rjust 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 + "あれ".rjust 5, pat + end.should raise_error(Encoding::CompatibilityError) end end end diff --git a/spec/ruby/core/string/shared/chars.rb b/spec/ruby/core/string/shared/chars.rb index 6a2fdb483f..d37bc50675 100644 --- a/spec/ruby/core/string/shared/chars.rb +++ b/spec/ruby/core/string/shared/chars.rb @@ -20,61 +20,59 @@ describe :string_chars, shared: true do ["\303\207", "\342\210\202", "\303\251", "\306\222", "g"] end - with_feature :encoding do - it "returns characters in the same encoding as self" do - "&%".force_encoding('Shift_JIS').send(@method).to_a.all? {|c| c.encoding.name.should == 'Shift_JIS'} - "&%".encode('ASCII-8BIT').send(@method).to_a.all? {|c| c.encoding.name.should == 'ASCII-8BIT'} - end + it "returns characters in the same encoding as self" do + "&%".force_encoding('Shift_JIS').send(@method).to_a.all? {|c| c.encoding.name.should == 'Shift_JIS'} + "&%".encode('ASCII-8BIT').send(@method).to_a.all? {|c| c.encoding.name.should == 'ASCII-8BIT'} + end - it "works with multibyte characters" do - s = "\u{8987}".force_encoding("UTF-8") - s.bytesize.should == 3 - s.send(@method).to_a.should == [s] - end + it "works with multibyte characters" do + s = "\u{8987}".force_encoding("UTF-8") + s.bytesize.should == 3 + s.send(@method).to_a.should == [s] + end - it "works if the String's contents is invalid for its encoding" do - xA4 = [0xA4].pack('C') - xA4.force_encoding('UTF-8') - xA4.valid_encoding?.should be_false - xA4.send(@method).to_a.should == [xA4.force_encoding("UTF-8")] - end + it "works if the String's contents is invalid for its encoding" do + xA4 = [0xA4].pack('C') + xA4.force_encoding('UTF-8') + xA4.valid_encoding?.should be_false + xA4.send(@method).to_a.should == [xA4.force_encoding("UTF-8")] + end - it "returns a different character if the String is transcoded" do - s = "\u{20AC}".force_encoding('UTF-8') - s.encode('UTF-8').send(@method).to_a.should == ["\u{20AC}".force_encoding('UTF-8')] - s.encode('iso-8859-15').send(@method).to_a.should == [[0xA4].pack('C').force_encoding('iso-8859-15')] - s.encode('iso-8859-15').encode('UTF-8').send(@method).to_a.should == ["\u{20AC}".force_encoding('UTF-8')] - end + it "returns a different character if the String is transcoded" do + s = "\u{20AC}".force_encoding('UTF-8') + s.encode('UTF-8').send(@method).to_a.should == ["\u{20AC}".force_encoding('UTF-8')] + s.encode('iso-8859-15').send(@method).to_a.should == [[0xA4].pack('C').force_encoding('iso-8859-15')] + s.encode('iso-8859-15').encode('UTF-8').send(@method).to_a.should == ["\u{20AC}".force_encoding('UTF-8')] + end - it "uses the String's encoding to determine what characters it contains" do - s = "\u{24B62}" + it "uses the String's encoding to determine what characters it contains" do + s = "\u{24B62}" - s.force_encoding('UTF-8').send(@method).to_a.should == [ - s.force_encoding('UTF-8') - ] - s.force_encoding('BINARY').send(@method).to_a.should == [ - [0xF0].pack('C').force_encoding('BINARY'), - [0xA4].pack('C').force_encoding('BINARY'), - [0xAD].pack('C').force_encoding('BINARY'), - [0xA2].pack('C').force_encoding('BINARY') - ] - s.force_encoding('SJIS').send(@method).to_a.should == [ - [0xF0,0xA4].pack('CC').force_encoding('SJIS'), - [0xAD].pack('C').force_encoding('SJIS'), - [0xA2].pack('C').force_encoding('SJIS') - ] - end + s.force_encoding('UTF-8').send(@method).to_a.should == [ + s.force_encoding('UTF-8') + ] + s.force_encoding('BINARY').send(@method).to_a.should == [ + [0xF0].pack('C').force_encoding('BINARY'), + [0xA4].pack('C').force_encoding('BINARY'), + [0xAD].pack('C').force_encoding('BINARY'), + [0xA2].pack('C').force_encoding('BINARY') + ] + s.force_encoding('SJIS').send(@method).to_a.should == [ + [0xF0,0xA4].pack('CC').force_encoding('SJIS'), + [0xAD].pack('C').force_encoding('SJIS'), + [0xA2].pack('C').force_encoding('SJIS') + ] + end - it "taints resulting strings when self is tainted" do - str = "hello" + it "taints resulting strings when self is tainted" do + str = "hello" - str.send(@method) do |x| - x.tainted?.should == false - end + str.send(@method) do |x| + x.tainted?.should == false + end - str.dup.taint.send(@method) do |x| - x.tainted?.should == true - end + str.dup.taint.send(@method) do |x| + x.tainted?.should == true end end end diff --git a/spec/ruby/core/string/shared/length.rb b/spec/ruby/core/string/shared/length.rb index 0e6e66ee1c..cea287d7a2 100644 --- a/spec/ruby/core/string/shared/length.rb +++ b/spec/ruby/core/string/shared/length.rb @@ -10,19 +10,17 @@ describe :string_length, shared: true do "four".send(@method).should == 4 end - with_feature :encoding do - it "returns the length of a string in different encodings" do - utf8_str = 'こにちわ' * 100 - utf8_str.size.should == 400 - utf8_str.encode(Encoding::UTF_32BE).size.should == 400 - utf8_str.encode(Encoding::SHIFT_JIS).size.should == 400 - end + it "returns the length of a string in different encodings" do + utf8_str = 'こにちわ' * 100 + utf8_str.size.should == 400 + utf8_str.encode(Encoding::UTF_32BE).size.should == 400 + utf8_str.encode(Encoding::SHIFT_JIS).size.should == 400 + end - it "returns the length of the new self after encoding is changed" do - str = 'こにちわ' - str.send(@method) + it "returns the length of the new self after encoding is changed" do + str = 'こにちわ' + str.send(@method) - str.force_encoding('ASCII-8BIT').send(@method).should == 12 - end + str.force_encoding('ASCII-8BIT').send(@method).should == 12 end end diff --git a/spec/ruby/core/string/slice_spec.rb b/spec/ruby/core/string/slice_spec.rb index a3e9d6b74f..e969f85937 100644 --- a/spec/ruby/core/string/slice_spec.rb +++ b/spec/ruby/core/string/slice_spec.rb @@ -72,15 +72,13 @@ describe "String#slice! with index" do "hello".slice!(obj).should == ?e end - with_feature :encoding do - - it "returns the character given by the character index" do - "hellö there".slice!(1).should == "e" - "hellö there".slice!(4).should == "ö" - "hellö there".slice!(6).should == "t" - end + it "returns the character given by the character index" do + "hellö there".slice!(1).should == "e" + "hellö there".slice!(4).should == "ö" + "hellö there".slice!(6).should == "t" end + end describe "String#slice! with index, length" do @@ -148,19 +146,17 @@ describe "String#slice! with index, length" do s.slice!(0, 4).should be_an_instance_of(StringSpecs::MyString) end - with_feature :encoding do - it "returns the substring given by the character offsets" do - "hellö there".slice!(1,0).should == "" - "hellö there".slice!(1,3).should == "ell" - "hellö there".slice!(1,6).should == "ellö t" - "hellö there".slice!(1,9).should == "ellö ther" - end + it "returns the substring given by the character offsets" do + "hellö there".slice!(1,0).should == "" + "hellö there".slice!(1,3).should == "ell" + "hellö there".slice!(1,6).should == "ellö t" + "hellö there".slice!(1,9).should == "ellö ther" + end - it "treats invalid bytes as single bytes" do - xE6xCB = [0xE6,0xCB].pack('CC').force_encoding('utf-8') - "a#{xE6xCB}b".slice!(1, 2).should == xE6xCB - end + it "treats invalid bytes as single bytes" do + xE6xCB = [0xE6,0xCB].pack('CC').force_encoding('utf-8') + "a#{xE6xCB}b".slice!(1, 2).should == xE6xCB end end @@ -236,20 +232,18 @@ describe "String#slice! Range" do a.slice!(range_incl).should == "OO" end - with_feature :encoding do - - it "returns the substring given by the character offsets of the range" do - "hellö there".slice!(1..1).should == "e" - "hellö there".slice!(1..3).should == "ell" - "hellö there".slice!(1...3).should == "el" - "hellö there".slice!(-4..-2).should == "her" - "hellö there".slice!(-4...-2).should == "he" - "hellö there".slice!(5..-1).should == " there" - "hellö there".slice!(5...-1).should == " ther" - end + it "returns the substring given by the character offsets of the range" do + "hellö there".slice!(1..1).should == "e" + "hellö there".slice!(1..3).should == "ell" + "hellö there".slice!(1...3).should == "el" + "hellö there".slice!(-4..-2).should == "her" + "hellö there".slice!(-4...-2).should == "he" + "hellö there".slice!(5..-1).should == " there" + "hellö there".slice!(5...-1).should == " ther" end + it "raises a #{frozen_error_class} on a frozen instance that is modified" do lambda { "hello".freeze.slice!(1..3) }.should raise_error(frozen_error_class) end @@ -305,11 +299,9 @@ describe "String#slice! with Regexp" do s.slice!(/../).should be_an_instance_of(StringSpecs::MyString) end - with_feature :encoding do - it "returns the matching portion of self with a multi byte character" do - "hëllo there".slice!(/[ë](.)\1/).should == "ëll" - "".slice!(//).should == "" - end + it "returns the matching portion of self with a multi byte character" do + "hëllo there".slice!(/[ë](.)\1/).should == "ëll" + "".slice!(//).should == "" end it "sets $~ to MatchData when there is a match and nil when there's none" do @@ -389,16 +381,14 @@ describe "String#slice! with Regexp, index" do s.slice!(/(.)(.)/, 1).should be_an_instance_of(StringSpecs::MyString) end - with_feature :encoding do - it "returns the encoding aware capture for the given index" do - "hår".slice!(/(.)(.)(.)/, 0).should == "hår" - "hår".slice!(/(.)(.)(.)/, 1).should == "h" - "hår".slice!(/(.)(.)(.)/, 2).should == "å" - "hår".slice!(/(.)(.)(.)/, 3).should == "r" - "hår".slice!(/(.)(.)(.)/, -1).should == "r" - "hår".slice!(/(.)(.)(.)/, -2).should == "å" - "hår".slice!(/(.)(.)(.)/, -3).should == "h" - end + it "returns the encoding aware capture for the given index" do + "hår".slice!(/(.)(.)(.)/, 0).should == "hår" + "hår".slice!(/(.)(.)(.)/, 1).should == "h" + "hår".slice!(/(.)(.)(.)/, 2).should == "å" + "hår".slice!(/(.)(.)(.)/, 3).should == "r" + "hår".slice!(/(.)(.)(.)/, -1).should == "r" + "hår".slice!(/(.)(.)(.)/, -2).should == "å" + "hår".slice!(/(.)(.)(.)/, -3).should == "h" end it "sets $~ to MatchData when there is a match and nil when there's none" do diff --git a/spec/ruby/core/string/split_spec.rb b/spec/ruby/core/string/split_spec.rb index 82f261b8df..6c666deb6c 100644 --- a/spec/ruby/core/string/split_spec.rb +++ b/spec/ruby/core/string/split_spec.rb @@ -3,19 +3,17 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' describe "String#split with String" do - with_feature :encoding do - it "throws an ArgumentError if the pattern is not a valid string" do - str = 'проверка' - broken_str = 'проверка' - broken_str.force_encoding('binary') - broken_str.chop! - broken_str.force_encoding('utf-8') - lambda { str.split(broken_str) }.should raise_error(ArgumentError) - end + it "throws an ArgumentError if the pattern is not a valid string" do + str = 'проверка' + broken_str = 'проверка' + broken_str.force_encoding('binary') + broken_str.chop! + broken_str.force_encoding('utf-8') + lambda { str.split(broken_str) }.should raise_error(ArgumentError) + end - it "splits on multibyte characters" do - "ありがりがとう".split("が").should == ["あり", "り", "とう"] - end + it "splits on multibyte characters" do + "ありがりがとう".split("が").should == ["あり", "り", "とう"] end it "returns an array of substrings based on splitting on the given string" do diff --git a/spec/ruby/core/string/tr_s_spec.rb b/spec/ruby/core/string/tr_s_spec.rb index 87635acf2c..b05211abc4 100644 --- a/spec/ruby/core/string/tr_s_spec.rb +++ b/spec/ruby/core/string/tr_s_spec.rb @@ -60,49 +60,47 @@ describe "String#tr_s" do end end - with_feature :encoding do - # http://redmine.ruby-lang.org/issues/show/1839 - it "can replace a 7-bit ASCII character with a multibyte one" do - a = "uber" - a.encoding.should == Encoding::UTF_8 - b = a.tr_s("u","ü") - b.should == "über" - b.encoding.should == Encoding::UTF_8 - end - - it "can replace multiple 7-bit ASCII characters with a multibyte one" do - a = "uuuber" - a.encoding.should == Encoding::UTF_8 - b = a.tr_s("u","ü") - b.should == "über" - b.encoding.should == Encoding::UTF_8 - end + # http://redmine.ruby-lang.org/issues/show/1839 + it "can replace a 7-bit ASCII character with a multibyte one" do + a = "uber" + a.encoding.should == Encoding::UTF_8 + b = a.tr_s("u","ü") + b.should == "über" + b.encoding.should == Encoding::UTF_8 + end - it "can replace a multibyte character with a single byte one" do - a = "über" - a.encoding.should == Encoding::UTF_8 - b = a.tr_s("ü","u") - b.should == "uber" - b.encoding.should == Encoding::UTF_8 - end + it "can replace multiple 7-bit ASCII characters with a multibyte one" do + a = "uuuber" + a.encoding.should == Encoding::UTF_8 + b = a.tr_s("u","ü") + b.should == "über" + b.encoding.should == Encoding::UTF_8 + end - it "can replace multiple multibyte characters with a single byte one" do - a = "üüüber" - a.encoding.should == Encoding::UTF_8 - b = a.tr_s("ü","u") - b.should == "uber" - b.encoding.should == Encoding::UTF_8 - end + it "can replace a multibyte character with a single byte one" do + a = "über" + a.encoding.should == Encoding::UTF_8 + b = a.tr_s("ü","u") + b.should == "uber" + b.encoding.should == Encoding::UTF_8 + end - it "does not replace a multibyte character where part of the bytes match the tr string" do - str = "椎名深夏" - a = "\u0080\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008E\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009E\u009F" - b = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ" - str.tr_s(a, b).should == "椎名深夏" - end + it "can replace multiple multibyte characters with a single byte one" do + a = "üüüber" + a.encoding.should == Encoding::UTF_8 + b = a.tr_s("ü","u") + b.should == "uber" + b.encoding.should == Encoding::UTF_8 + end + it "does not replace a multibyte character where part of the bytes match the tr string" do + str = "椎名深夏" + a = "\u0080\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008E\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009E\u009F" + b = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ" + str.tr_s(a, b).should == "椎名深夏" end + end describe "String#tr_s!" do diff --git a/spec/ruby/core/string/tr_spec.rb b/spec/ruby/core/string/tr_spec.rb index efd5a7f638..8a33dd24f5 100644 --- a/spec/ruby/core/string/tr_spec.rb +++ b/spec/ruby/core/string/tr_spec.rb @@ -72,32 +72,30 @@ describe "String#tr" do end end - with_feature :encoding do - # http://redmine.ruby-lang.org/issues/show/1839 - it "can replace a 7-bit ASCII character with a multibyte one" do - a = "uber" - a.encoding.should == Encoding::UTF_8 - b = a.tr("u","ü") - b.should == "über" - b.encoding.should == Encoding::UTF_8 - end - - it "can replace a multibyte character with a single byte one" do - a = "über" - a.encoding.should == Encoding::UTF_8 - b = a.tr("ü","u") - b.should == "uber" - b.encoding.should == Encoding::UTF_8 - end + # http://redmine.ruby-lang.org/issues/show/1839 + it "can replace a 7-bit ASCII character with a multibyte one" do + a = "uber" + a.encoding.should == Encoding::UTF_8 + b = a.tr("u","ü") + b.should == "über" + b.encoding.should == Encoding::UTF_8 + end - it "does not replace a multibyte character where part of the bytes match the tr string" do - str = "椎名深夏" - a = "\u0080\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008E\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009E\u009F" - b = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ" - str.tr(a, b).should == "椎名深夏" - end + it "can replace a multibyte character with a single byte one" do + a = "über" + a.encoding.should == Encoding::UTF_8 + b = a.tr("ü","u") + b.should == "uber" + b.encoding.should == Encoding::UTF_8 + end + it "does not replace a multibyte character where part of the bytes match the tr string" do + str = "椎名深夏" + a = "\u0080\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008E\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009E\u009F" + b = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ" + str.tr(a, b).should == "椎名深夏" end + end describe "String#tr!" do diff --git a/spec/ruby/core/string/valid_encoding_spec.rb b/spec/ruby/core/string/valid_encoding_spec.rb index c8cbbcf5ec..54109386ba 100644 --- a/spec/ruby/core/string/valid_encoding_spec.rb +++ b/spec/ruby/core/string/valid_encoding_spec.rb @@ -1,129 +1,127 @@ require_relative '../../spec_helper' -with_feature :encoding do - describe "String#valid_encoding?" do - it "returns true if the String's encoding is valid" do - "a".valid_encoding?.should be_true - "\u{8365}\u{221}".valid_encoding?.should be_true - end +describe "String#valid_encoding?" do + it "returns true if the String's encoding is valid" do + "a".valid_encoding?.should be_true + "\u{8365}\u{221}".valid_encoding?.should be_true + end - it "returns true if self is valid in the current encoding and other encodings" do - str = "\x77" - str.force_encoding('utf-8').valid_encoding?.should be_true - str.force_encoding('ascii-8bit').valid_encoding?.should be_true - end + it "returns true if self is valid in the current encoding and other encodings" do + str = "\x77" + str.force_encoding('utf-8').valid_encoding?.should be_true + str.force_encoding('ascii-8bit').valid_encoding?.should be_true + end - it "returns true for all encodings self is valid in" do - str = "\u{6754}" - str.force_encoding('ASCII-8BIT').valid_encoding?.should be_true - str.force_encoding('UTF-8').valid_encoding?.should be_true - str.force_encoding('US-ASCII').valid_encoding?.should be_false - str.force_encoding('Big5').valid_encoding?.should be_false - str.force_encoding('CP949').valid_encoding?.should be_false - str.force_encoding('Emacs-Mule').valid_encoding?.should be_false - str.force_encoding('EUC-JP').valid_encoding?.should be_false - str.force_encoding('EUC-KR').valid_encoding?.should be_false - str.force_encoding('EUC-TW').valid_encoding?.should be_false - str.force_encoding('GB18030').valid_encoding?.should be_false - str.force_encoding('GBK').valid_encoding?.should be_false - str.force_encoding('ISO-8859-1').valid_encoding?.should be_true - str.force_encoding('ISO-8859-2').valid_encoding?.should be_true - str.force_encoding('ISO-8859-3').valid_encoding?.should be_true - str.force_encoding('ISO-8859-4').valid_encoding?.should be_true - str.force_encoding('ISO-8859-5').valid_encoding?.should be_true - str.force_encoding('ISO-8859-6').valid_encoding?.should be_true - str.force_encoding('ISO-8859-7').valid_encoding?.should be_true - str.force_encoding('ISO-8859-8').valid_encoding?.should be_true - str.force_encoding('ISO-8859-9').valid_encoding?.should be_true - str.force_encoding('ISO-8859-10').valid_encoding?.should be_true - str.force_encoding('ISO-8859-11').valid_encoding?.should be_true - str.force_encoding('ISO-8859-13').valid_encoding?.should be_true - str.force_encoding('ISO-8859-14').valid_encoding?.should be_true - str.force_encoding('ISO-8859-15').valid_encoding?.should be_true - str.force_encoding('ISO-8859-16').valid_encoding?.should be_true - str.force_encoding('KOI8-R').valid_encoding?.should be_true - str.force_encoding('KOI8-U').valid_encoding?.should be_true - str.force_encoding('Shift_JIS').valid_encoding?.should be_false - str.force_encoding('UTF-16BE').valid_encoding?.should be_false - str.force_encoding('UTF-16LE').valid_encoding?.should be_false - str.force_encoding('UTF-32BE').valid_encoding?.should be_false - str.force_encoding('UTF-32LE').valid_encoding?.should be_false - str.force_encoding('Windows-1251').valid_encoding?.should be_true - str.force_encoding('IBM437').valid_encoding?.should be_true - str.force_encoding('IBM737').valid_encoding?.should be_true - str.force_encoding('IBM775').valid_encoding?.should be_true - str.force_encoding('CP850').valid_encoding?.should be_true - str.force_encoding('IBM852').valid_encoding?.should be_true - str.force_encoding('CP852').valid_encoding?.should be_true - str.force_encoding('IBM855').valid_encoding?.should be_true - str.force_encoding('CP855').valid_encoding?.should be_true - str.force_encoding('IBM857').valid_encoding?.should be_true - str.force_encoding('IBM860').valid_encoding?.should be_true - str.force_encoding('IBM861').valid_encoding?.should be_true - str.force_encoding('IBM862').valid_encoding?.should be_true - str.force_encoding('IBM863').valid_encoding?.should be_true - str.force_encoding('IBM864').valid_encoding?.should be_true - str.force_encoding('IBM865').valid_encoding?.should be_true - str.force_encoding('IBM866').valid_encoding?.should be_true - str.force_encoding('IBM869').valid_encoding?.should be_true - str.force_encoding('Windows-1258').valid_encoding?.should be_true - str.force_encoding('GB1988').valid_encoding?.should be_true - str.force_encoding('macCentEuro').valid_encoding?.should be_true - str.force_encoding('macCroatian').valid_encoding?.should be_true - str.force_encoding('macCyrillic').valid_encoding?.should be_true - str.force_encoding('macGreek').valid_encoding?.should be_true - str.force_encoding('macIceland').valid_encoding?.should be_true - str.force_encoding('macRoman').valid_encoding?.should be_true - str.force_encoding('macRomania').valid_encoding?.should be_true - str.force_encoding('macThai').valid_encoding?.should be_true - str.force_encoding('macTurkish').valid_encoding?.should be_true - str.force_encoding('macUkraine').valid_encoding?.should be_true - str.force_encoding('stateless-ISO-2022-JP').valid_encoding?.should be_false - str.force_encoding('eucJP-ms').valid_encoding?.should be_false - str.force_encoding('CP51932').valid_encoding?.should be_false - str.force_encoding('GB2312').valid_encoding?.should be_false - str.force_encoding('GB12345').valid_encoding?.should be_false - str.force_encoding('ISO-2022-JP').valid_encoding?.should be_true - str.force_encoding('ISO-2022-JP-2').valid_encoding?.should be_true - str.force_encoding('CP50221').valid_encoding?.should be_true - str.force_encoding('Windows-1252').valid_encoding?.should be_true - str.force_encoding('Windows-1250').valid_encoding?.should be_true - str.force_encoding('Windows-1256').valid_encoding?.should be_true - str.force_encoding('Windows-1253').valid_encoding?.should be_true - str.force_encoding('Windows-1255').valid_encoding?.should be_true - str.force_encoding('Windows-1254').valid_encoding?.should be_true - str.force_encoding('TIS-620').valid_encoding?.should be_true - str.force_encoding('Windows-874').valid_encoding?.should be_true - str.force_encoding('Windows-1257').valid_encoding?.should be_true - str.force_encoding('Windows-31J').valid_encoding?.should be_false - str.force_encoding('MacJapanese').valid_encoding?.should be_false - str.force_encoding('UTF-7').valid_encoding?.should be_true - str.force_encoding('UTF8-MAC').valid_encoding?.should be_true - end + it "returns true for all encodings self is valid in" do + str = "\u{6754}" + str.force_encoding('ASCII-8BIT').valid_encoding?.should be_true + str.force_encoding('UTF-8').valid_encoding?.should be_true + str.force_encoding('US-ASCII').valid_encoding?.should be_false + str.force_encoding('Big5').valid_encoding?.should be_false + str.force_encoding('CP949').valid_encoding?.should be_false + str.force_encoding('Emacs-Mule').valid_encoding?.should be_false + str.force_encoding('EUC-JP').valid_encoding?.should be_false + str.force_encoding('EUC-KR').valid_encoding?.should be_false + str.force_encoding('EUC-TW').valid_encoding?.should be_false + str.force_encoding('GB18030').valid_encoding?.should be_false + str.force_encoding('GBK').valid_encoding?.should be_false + str.force_encoding('ISO-8859-1').valid_encoding?.should be_true + str.force_encoding('ISO-8859-2').valid_encoding?.should be_true + str.force_encoding('ISO-8859-3').valid_encoding?.should be_true + str.force_encoding('ISO-8859-4').valid_encoding?.should be_true + str.force_encoding('ISO-8859-5').valid_encoding?.should be_true + str.force_encoding('ISO-8859-6').valid_encoding?.should be_true + str.force_encoding('ISO-8859-7').valid_encoding?.should be_true + str.force_encoding('ISO-8859-8').valid_encoding?.should be_true + str.force_encoding('ISO-8859-9').valid_encoding?.should be_true + str.force_encoding('ISO-8859-10').valid_encoding?.should be_true + str.force_encoding('ISO-8859-11').valid_encoding?.should be_true + str.force_encoding('ISO-8859-13').valid_encoding?.should be_true + str.force_encoding('ISO-8859-14').valid_encoding?.should be_true + str.force_encoding('ISO-8859-15').valid_encoding?.should be_true + str.force_encoding('ISO-8859-16').valid_encoding?.should be_true + str.force_encoding('KOI8-R').valid_encoding?.should be_true + str.force_encoding('KOI8-U').valid_encoding?.should be_true + str.force_encoding('Shift_JIS').valid_encoding?.should be_false + str.force_encoding('UTF-16BE').valid_encoding?.should be_false + str.force_encoding('UTF-16LE').valid_encoding?.should be_false + str.force_encoding('UTF-32BE').valid_encoding?.should be_false + str.force_encoding('UTF-32LE').valid_encoding?.should be_false + str.force_encoding('Windows-1251').valid_encoding?.should be_true + str.force_encoding('IBM437').valid_encoding?.should be_true + str.force_encoding('IBM737').valid_encoding?.should be_true + str.force_encoding('IBM775').valid_encoding?.should be_true + str.force_encoding('CP850').valid_encoding?.should be_true + str.force_encoding('IBM852').valid_encoding?.should be_true + str.force_encoding('CP852').valid_encoding?.should be_true + str.force_encoding('IBM855').valid_encoding?.should be_true + str.force_encoding('CP855').valid_encoding?.should be_true + str.force_encoding('IBM857').valid_encoding?.should be_true + str.force_encoding('IBM860').valid_encoding?.should be_true + str.force_encoding('IBM861').valid_encoding?.should be_true + str.force_encoding('IBM862').valid_encoding?.should be_true + str.force_encoding('IBM863').valid_encoding?.should be_true + str.force_encoding('IBM864').valid_encoding?.should be_true + str.force_encoding('IBM865').valid_encoding?.should be_true + str.force_encoding('IBM866').valid_encoding?.should be_true + str.force_encoding('IBM869').valid_encoding?.should be_true + str.force_encoding('Windows-1258').valid_encoding?.should be_true + str.force_encoding('GB1988').valid_encoding?.should be_true + str.force_encoding('macCentEuro').valid_encoding?.should be_true + str.force_encoding('macCroatian').valid_encoding?.should be_true + str.force_encoding('macCyrillic').valid_encoding?.should be_true + str.force_encoding('macGreek').valid_encoding?.should be_true + str.force_encoding('macIceland').valid_encoding?.should be_true + str.force_encoding('macRoman').valid_encoding?.should be_true + str.force_encoding('macRomania').valid_encoding?.should be_true + str.force_encoding('macThai').valid_encoding?.should be_true + str.force_encoding('macTurkish').valid_encoding?.should be_true + str.force_encoding('macUkraine').valid_encoding?.should be_true + str.force_encoding('stateless-ISO-2022-JP').valid_encoding?.should be_false + str.force_encoding('eucJP-ms').valid_encoding?.should be_false + str.force_encoding('CP51932').valid_encoding?.should be_false + str.force_encoding('GB2312').valid_encoding?.should be_false + str.force_encoding('GB12345').valid_encoding?.should be_false + str.force_encoding('ISO-2022-JP').valid_encoding?.should be_true + str.force_encoding('ISO-2022-JP-2').valid_encoding?.should be_true + str.force_encoding('CP50221').valid_encoding?.should be_true + str.force_encoding('Windows-1252').valid_encoding?.should be_true + str.force_encoding('Windows-1250').valid_encoding?.should be_true + str.force_encoding('Windows-1256').valid_encoding?.should be_true + str.force_encoding('Windows-1253').valid_encoding?.should be_true + str.force_encoding('Windows-1255').valid_encoding?.should be_true + str.force_encoding('Windows-1254').valid_encoding?.should be_true + str.force_encoding('TIS-620').valid_encoding?.should be_true + str.force_encoding('Windows-874').valid_encoding?.should be_true + str.force_encoding('Windows-1257').valid_encoding?.should be_true + str.force_encoding('Windows-31J').valid_encoding?.should be_false + str.force_encoding('MacJapanese').valid_encoding?.should be_false + str.force_encoding('UTF-7').valid_encoding?.should be_true + str.force_encoding('UTF8-MAC').valid_encoding?.should be_true + end - it "returns false if self is valid in one encoding, but invalid in the one it's tagged with" do - str = "\u{8765}" - str.valid_encoding?.should be_true - str = str.force_encoding('ascii') - str.valid_encoding?.should be_false - end + it "returns false if self is valid in one encoding, but invalid in the one it's tagged with" do + str = "\u{8765}" + str.valid_encoding?.should be_true + str = str.force_encoding('ascii') + str.valid_encoding?.should be_false + end - it "returns false if self contains a character invalid in the associated encoding" do - "abc#{[0x80].pack('C')}".force_encoding('ascii').valid_encoding?.should be_false - end + it "returns false if self contains a character invalid in the associated encoding" do + "abc#{[0x80].pack('C')}".force_encoding('ascii').valid_encoding?.should be_false + end - it "returns false if a valid String had an invalid character appended to it" do - str = "a" - str.valid_encoding?.should be_true - str << [0xDD].pack('C').force_encoding('utf-8') - str.valid_encoding?.should be_false - end + it "returns false if a valid String had an invalid character appended to it" do + str = "a" + str.valid_encoding?.should be_true + str << [0xDD].pack('C').force_encoding('utf-8') + str.valid_encoding?.should be_false + end - it "returns true if an invalid string is appended another invalid one but both make a valid string" do - str = [0xD0].pack('C').force_encoding('utf-8') - str.valid_encoding?.should be_false - str << [0xBF].pack('C').force_encoding('utf-8') - str.valid_encoding?.should be_true - end + it "returns true if an invalid string is appended another invalid one but both make a valid string" do + str = [0xD0].pack('C').force_encoding('utf-8') + str.valid_encoding?.should be_false + str << [0xBF].pack('C').force_encoding('utf-8') + str.valid_encoding?.should be_true end end |