summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/index_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/string/index_spec.rb')
-rw-r--r--spec/ruby/core/string/index_spec.rb101
1 files changed, 61 insertions, 40 deletions
diff --git a/spec/ruby/core/string/index_spec.rb b/spec/ruby/core/string/index_spec.rb
index 7889f69c5d..3f82181b98 100644
--- a/spec/ruby/core/string/index_spec.rb
+++ b/spec/ruby/core/string/index_spec.rb
@@ -1,18 +1,18 @@
# -*- encoding: utf-8 -*-
-require File.expand_path('../../../spec_helper', __FILE__)
-require File.expand_path('../fixtures/classes.rb', __FILE__)
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
describe "String#index" do
it "raises a TypeError if passed nil" do
- lambda { "abc".index nil }.should raise_error(TypeError)
+ -> { "abc".index nil }.should.raise(TypeError)
end
it "raises a TypeError if passed a boolean" do
- lambda { "abc".index true }.should raise_error(TypeError)
+ -> { "abc".index true }.should.raise(TypeError)
end
it "raises a TypeError if passed a Symbol" do
- lambda { "abc".index :a }.should raise_error(TypeError)
+ -> { "abc".index :a }.should.raise(TypeError)
end
it "calls #to_str to convert the first argument" do
@@ -27,8 +27,8 @@ describe "String#index" do
"abc".index("c", offset).should == 2
end
- it "raises a TypeError if passed a Fixnum" do
- lambda { "abc".index 97 }.should raise_error(TypeError)
+ it "raises a TypeError if passed an Integer" do
+ -> { "abc".index 97 }.should.raise(TypeError)
end
end
@@ -140,25 +140,39 @@ 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
+ "ありがとうありがとう".index("が", 3).should == 7
+ 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
+ -> do
+ "あれ".index char
+ end.should.raise(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(Encoding::CompatibilityError, "incompatible character encodings: ISO-2022-JP and EUC-JP")
end
end
@@ -217,6 +231,15 @@ describe "String#index with Regexp" do
$~.should == nil
end
+ it "always clear $~" do
+ "a".index(/a/)
+ $~.should_not == nil
+
+ string = "blablabla"
+ string.index(/bla/, string.length + 1)
+ $~.should == nil
+ end
+
it "starts the search at the given offset" do
"blablabla".index(/.{0}/, 5).should == 5
"blablabla".index(/.{1}/, 5).should == 5
@@ -264,7 +287,7 @@ describe "String#index with Regexp" do
end
it "returns nil if the Regexp matches the empty string and the offset is out of range" do
- "ruby".index(//,12).should be_nil
+ "ruby".index(//,12).should == nil
end
it "supports \\G which matches at the given start offset" do
@@ -293,24 +316,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)
+ -> do
+ "あれ".index re
+ end.should.raise(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)")
end
end