# -*- encoding: utf-8 -*- 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) end it "raises a TypeError if passed a boolean" do lambda { "abc".index true }.should raise_error(TypeError) end it "raises a TypeError if passed a Symbol" do lambda { "abc".index :a }.should raise_error(TypeError) end it "calls #to_str to convert the first argument" do char = mock("string index char") char.should_receive(:to_str).and_return("b") "abc".index(char).should == 1 end it "calls #to_int to convert the second argument" do offset = mock("string index offset") offset.should_receive(:to_int).and_return(1) "abc".index("c", offset).should == 2 end it "raises a TypeError if passed a Fixnum" do lambda { "abc".index 97 }.should raise_error(TypeError) end end describe "String#index with String" do it "behaves the same as String#index(char) for one-character strings" do "blablabla hello cruel world...!".split("").uniq.each do |str| chr = str[0] str.index(str).should == str.index(chr) 0.upto(str.size + 1) do |start| str.index(str, start).should == str.index(chr, start) end (-str.size - 1).upto(-1) do |start| str.index(str, start).should == str.index(chr, start) end end end it "returns the index of the first occurrence of the given substring" do "blablabla".index("").should == 0 "blablabla".index("b").should == 0 "blablabla".index("bla").should == 0 "blablabla".index("blabla").should == 0 "blablabla".index("blablabla").should == 0 "blablabla".index("l").should == 1 "blablabla".index("la").should == 1 "blablabla".index("labla").should == 1 "blablabla".index("lablabla").should == 1 "blablabla".index("a").should == 2 "blablabla".index("abla").should == 2 "blablabla".index("ablabla").should == 2 end it "doesn't set $~" do $~ = nil 'hello.'.index('ll') $~.should == nil end it "ignores string subclasses" do "blablabla".index(StringSpecs::MyString.new("bla")).should == 0 StringSpecs::MyString.new("blablabla").index("bla").should == 0 StringSpecs::MyString.new("blablabla").index(StringSpecs::MyString.new("bla")).should == 0 end it "starts the search at the given offset" do "blablabla".index("bl", 0).should == 0 "blablabla".index("bl", 1).should == 3 "blablabla".index("bl", 2).should == 3 "blablabla".index("bl", 3).should == 3 "blablabla".index("bla", 0).should == 0 "blablabla".index("bla", 1).should == 3 "blablabla".index("bla", 2).should == 3 "blablabla".index("bla", 3).should == 3 "blablabla".index("blab", 0).should == 0 "blablabla".index("blab", 1).should == 3 "blablabla".index("blab", 2).should == 3 "blablabla".index("blab", 3).should == 3 "blablabla".index("la", 1).should == 1 "blablabla".index("la", 2).should == 4 "blablabla".index("la", 3).should == 4 "blablabla".index("la", 4).should == 4 "blablabla".index("lab", 1).should == 1 "blablabla".index("lab", 2).should == 4 "blablabla".index("lab", 3).should == 4 "blablabla".index("lab", 4).should == 4 "blablabla".index("ab", 2).should == 2 "blablabla".index("ab", 3).should == 5 "blablabla".index("ab", 4).should == 5 "blablabla".index("ab", 5).should == 5 "blablabla".index("", 0).should == 0 "blablabla".index("", 1).should == 1 "blablabla".index("", 2).should == 2 "blablabla".index("", 7).should == 7 "blablabla".index("", 8).should == 8 "blablabla".index("", 9).should == 9 end it "starts the search at offset + self.length if offset is negative" do str = "blablabla" ["bl", "bla", "blab", "la", "lab", "ab", ""].each do |needle| (-str.length .. -1).each do |offset| str.index(needle, offset).should == str.index(needle, offset + str.length) end end end it "returns nil if the substring isn't found" do "blablabla".index("B").should == nil "blablabla".index("z").should == nil "blablabla".index("BLA").should == nil "blablabla".index("blablablabla").should == nil "blablabla".index("", 10).should == nil "hello".index("he", 1).should == nil "hello".index("he", 2).should == nil "I’ve got a multibyte character.\n".index("\n\n").should == nil 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 a partial first match" do "