<commit_before># -*- encoding: utf-8 -*-
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'fixtures/utf-8-encoding'

describe "String#rindex with object" do
  it "raises a TypeError if obj isn't a String, Integer or Regexp" do
    not_supported_on :opal do
      -> { "hello".rindex(:sym) }.should raise_error(TypeError)
    end
    -> { "hello".rindex(mock('x')) }.should raise_error(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)
    -> { "hello".rindex(obj) }.should raise_error(TypeError)
  end

  it "tries to convert obj to a string via to_str" do
    obj = mock('lo')
    def obj.to_str() "lo" end
    "hello".rindex(obj).should == "hello".rindex("lo")

    obj = mock('o')
    def obj.respond_to?(arg, *) true end
    def obj.method_missing(*args) "o" end
    "hello".rindex(obj).should == "hello".rindex("o")
  end
end

describe "String#rindex with String" do
  it "behaves the same as String#rindex(char) for one-character strings" do
    "blablabla hello cruel world...!".split("").uniq.each do |str|
      chr = str[0]
      str.rindex(str).should == str.rindex(chr)

      0.upto(str.size + 1) do |start|
        str.rindex(str, start).should == str.rindex(chr, start)
      end

      (-str.size - 1).upto(-1) do |start|<commit_msg>Don't accept integer as first parameter to String#rindex<commit_after># -*- encoding: utf-8 -*-
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'fixtures/utf-8-encoding'

describe "String#rindex with object" do
  it "raises a TypeError if obj isn't a String or Regexp" do
    not_supported_on :opal do
      -> { "hello".rindex(:sym) }.should raise_error(TypeError)
    end
    -> { "hello".rindex(mock('x')) }.should raise_error(TypeError)
  end

  it "raises a TypeError if obj is an Integer" do
    -> { "hello".rindex(42) }.should raise_error(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)
    -> { "hello".rindex(obj) }.should raise_error(TypeError)
  end

  it "tries to convert obj to a string via to_str" do
    obj = mock('lo')
    def obj.to_str() "lo" end
    "hello".rindex(obj).should == "hello".rindex("lo")

    obj = mock('o')
    def obj.respond_to?(arg, *) true end
    def obj.method_missing(*args) "o" end
    "hello".rindex(obj).should == "hello".rindex("o")
  end
end

describe "String#rindex with String" do
  it "behaves the same as String#rindex(char) for one-character strings" do
    "blablabla hello cruel world...!".split("").uniq.each do |str|
      chr = str[0]
      str.rindex(str).should == str.rindex(chr)

      0.upto(str.size + 1) do |start|
        str.rindex(str, start).should == str.rindex(chr, start)
      end

      (-str.size - 1).upto(-1) do |start|
{'fraction_non_alphanumeric': 0.15191181536341716, 'fraction_numerical': 0.005511539786427834, 'mean_word_length': 3.2895125553914326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '486196', 'n_tokens_mistral': 1042, 'n_tokens_neox': 1007, 'n_words': 275}