diff options
Diffstat (limited to 'spec/ruby/core/string/index_spec.rb')
-rw-r--r-- | spec/ruby/core/string/index_spec.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/spec/ruby/core/string/index_spec.rb b/spec/ruby/core/string/index_spec.rb index 8d2c8af193..be79708045 100644 --- a/spec/ruby/core/string/index_spec.rb +++ b/spec/ruby/core/string/index_spec.rb @@ -146,6 +146,7 @@ describe "String#index with String" do it "returns the character index after offset" do "われわれ".index("わ", 1).should == 2 + "ありがとうありがとう".index("が", 3).should == 7 end it "returns the character index after a partial first match" do @@ -158,6 +159,21 @@ describe "String#index with String" do "あれ".index char end.should raise_error(Encoding::CompatibilityError) end + + it "handles a substring in a superset encoding" do + 'abc'.dup.force_encoding(Encoding::US_ASCII).index('é').should == nil + end + + it "handles a substring in a subset encoding" do + 'été'.index('t'.dup.force_encoding(Encoding::US_ASCII)).should == 1 + end + + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + str = 'abc'.dup.force_encoding("ISO-2022-JP") + pattern = 'b'.dup.force_encoding("EUC-JP") + + -> { str.index(pattern) }.should raise_error(Encoding::CompatibilityError, "incompatible character encodings: ISO-2022-JP and EUC-JP") + end end describe "String#index with Regexp" do @@ -303,6 +319,17 @@ describe "String#index with Regexp" do "われわわれ".index(/わ/, 3).should == 3 end + ruby_bug "#19763", ""..."3.3.0" do + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + re = Regexp.new "れ".encode(Encoding::EUC_JP) + -> do + "あれ".index re + end.should raise_error(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)") + end + end + + # The exception message was incorrectly "incompatible character encodings: UTF-8 and EUC-JP" before 3.3.0 + # Still test that the right exception class is used before that. it "raises an Encoding::CompatibilityError if the encodings are incompatible" do re = Regexp.new "れ".encode(Encoding::EUC_JP) -> do |