summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/rindex_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/string/rindex_spec.rb')
-rw-r--r--spec/ruby/core/string/rindex_spec.rb64
1 files changed, 40 insertions, 24 deletions
diff --git a/spec/ruby/core/string/rindex_spec.rb b/spec/ruby/core/string/rindex_spec.rb
index 7085914189..acecec224f 100644
--- a/spec/ruby/core/string/rindex_spec.rb
+++ b/spec/ruby/core/string/rindex_spec.rb
@@ -1,20 +1,23 @@
# -*- encoding: utf-8 -*-
-require File.expand_path('../../../spec_helper', __FILE__)
-require File.expand_path('../fixtures/classes.rb', __FILE__)
-require File.expand_path('../fixtures/utf-8-encoding.rb', __FILE__)
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
describe "String#rindex with object" do
- it "raises a TypeError if obj isn't a String, Fixnum or Regexp" do
+ it "raises a TypeError if obj isn't a String or Regexp" do
not_supported_on :opal do
- lambda { "hello".rindex(:sym) }.should raise_error(TypeError)
+ -> { "hello".rindex(:sym) }.should.raise(TypeError)
end
- lambda { "hello".rindex(mock('x')) }.should raise_error(TypeError)
+ -> { "hello".rindex(mock('x')) }.should.raise(TypeError)
+ end
+
+ it "raises a TypeError if obj is an Integer" do
+ -> { "hello".rindex(42) }.should.raise(TypeError)
end
it "doesn't try to convert obj to an integer via to_int" do
obj = mock('x')
obj.should_not_receive(:to_int)
- lambda { "hello".rindex(obj) }.should raise_error(TypeError)
+ -> { "hello".rindex(obj) }.should.raise(TypeError)
end
it "tries to convert obj to a string via to_str" do
@@ -190,7 +193,22 @@ describe "String#rindex with String" do
end
it "raises a TypeError when given offset is nil" do
- lambda { "str".rindex("st", nil) }.should raise_error(TypeError)
+ -> { "str".rindex("st", nil) }.should.raise(TypeError)
+ end
+
+ it "handles a substring in a superset encoding" do
+ 'abc'.dup.force_encoding(Encoding::US_ASCII).rindex('é').should == nil
+ end
+
+ it "handles a substring in a subset encoding" do
+ 'été'.rindex('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.rindex(pattern) }.should.raise(Encoding::CompatibilityError, "incompatible character encodings: ISO-2022-JP and EUC-JP")
end
end
@@ -344,25 +362,23 @@ describe "String#rindex with Regexp" do
end
it "raises a TypeError when given offset is nil" do
- lambda { "str".rindex(/../, nil) }.should raise_error(TypeError)
+ -> { "str".rindex(/../, nil) }.should.raise(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)
+ -> do
+ "あれ".rindex re
+ end.should.raise(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)")
end
end