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

load_extension('encoding')

describe :rb_enc_get_index, shared: true do
  it "returns the index of the encoding of a String" do
    @s.send(@method, "string").should >= 0
  end

  it "returns the index of the encoding of a Regexp" do
    @s.send(@method, /regexp/).should >= 0
  end
end

describe :rb_enc_set_index, shared: true do
  it "sets the object's encoding to the Encoding specified by the index" do
    obj = "abc"
    result = @s.send(@method, obj, 2)

    # This is used because indexes should be considered implementation
    # dependent. So a pair is returned:
    #   [rb_enc_find_index()->name, rb_enc_get(obj)->name]
    result.first.should == result.last
  end

  it "associates an encoding with a subclass of String" do
    str = CApiEncodingSpecs::S.new "abc"
    result = @s.send(@method, str, 1)
    result.first.should == result.last
  end

  ruby_version_is "2.6" do
    it "raises an ArgumentError for a non-encoding capable object" do
      obj = Object.new
      -> {
        result = @s.send(@method, obj, 1)
      }.should raise_error(ArgumentError, "cannot set encoding on non-encoding capable object")
    end
  end
end

describe "C-API Encoding function" do
  before :each do
    @s = CApiEncodingSpecs.new
  end

  ruby_version_is "2.6" do
    describe "rb_enc_alias" do
      it "creates an alias for an existing Encoding" do
        @s.rb_enc_alias("ZOMGWTFBBQ", "UTF-8").should >= 0
        Encoding.find("ZOMGWTFBBQ").name.should == "UTF-8"
      end
    end
  end

  describe "rb_enc_find" do
    it "returns the encoding of an Encoding" do
      @s.rb_enc_find("UTF-8").should == "UTF-8"
    end

    it "returns the encoding of an Encoding specified with lower case" do
      @s.rb_enc_find("utf-8").should == "UTF-8"
    end
  end

  describe "rb_enc_find_index" do
    it "returns the index of an Encoding" do
      @s.rb_enc_find_index("UTF-8").should >= 0
    end

    it "returns the index of an Encoding specified with lower case" do
      @s.rb_enc_find_index("utf-8").should >= 0
    end

    it "returns -1 for an non existing encoding" do
      @s.rb_enc_find_index("non-existent-encoding").should == -1
    end
  end

  describe "rb_enc_from_index" do
    it "returns an Encoding" do
      @s.rb_enc_from_index(0).should be_an_instance_of(String)
    end
  end

  describe "rb_usascii_encoding" do
    it "returns the encoding for Encoding::US_ASCII" do
      @s.rb_usascii_encoding.should == "US-ASCII"
    end
  end

  describe "rb_ascii8bit_encoding" do
    it "returns the encoding for Encoding::ASCII_8BIT" do
      @s.rb_ascii8bit_encoding.should == "ASCII-8BIT"
    end
  end

  describe "rb_utf8_encoding" do
    it "returns the encoding for Encoding::UTF_8" do
      @s.rb_utf8_encoding.should == "UTF-8"
    end
  end

  describe "rb_enc_from_encoding" do
    it "returns an Encoding instance from an encoding data structure" do
      @s.rb_enc_from_encoding("UTF-8").should == Encoding::UTF_8
    end
  end

  describe "rb_locale_encoding" do
    it "returns the encoding for the current locale" do
      @s.rb_locale_encoding.should == Encoding.find('locale').name
    end
  end

  describe "rb_filesystem_encoding" do
    it "returns the encoding for the current filesystem" do
      @s.rb_filesystem_encoding.should == Encoding.find('filesystem').name
    end
  end

  describe "rb_enc_get" do
    it "returns the encoding ossociated with an object" do
      str = "abc".encode Encoding::ASCII_8BIT
      @s.rb_enc_get(str).should == "ASCII-8BIT"
    end
  end

  describe "rb_obj_encoding" do
    it "returns the encoding ossociated with an object" do
      str = "abc".encode Encoding::ASCII_8BIT
      @s.rb_obj_encoding(str).should == Encoding::ASCII_8BIT
    end
  end

  describe "rb_enc_get_index" do
    it_behaves_like :rb_enc_get_index, :rb_enc_get_index

    it "returns the index of the encoding of a Symbol" do
      @s.send(@method, :symbol).should >= 0
    end

    it "returns -1 as the index of nil" do
      @s.send(@method, nil).should == -1
    end

    it "returns -1 as the index for immediates" do
      @s.send(@method, 1).should == -1
    end
  end

  describe "rb_enc_set_index" do
    it_behaves_like :rb_enc_set_index, :rb_enc_set_index
  end

  describe "rb_enc_str_new" do
    it "returns a String in US-ASCII encoding when high bits are set" do
      xEE = [0xEE].pack('C').force_encoding('utf-8')
      result = @s.rb_enc_str_new(xEE, 1, Encoding::US_ASCII)
      result.encoding.should equal(Encoding::US_ASCII)
    end
  end

  describe "rb_enc_str_coderange" do
    describe "when the encoding is ASCII-8BIT" do
      it "returns ENC_CODERANGE_7BIT if there are no high bits set" do
        result = @s.rb_enc_str_coderange("abc".force_encoding("ascii-8bit"))
        result.should == :coderange_7bit
      end

      it "returns ENC_CODERANGE_VALID if there are high bits set" do
        xEE = [0xEE].pack('C').force_encoding('utf-8')
        result = @s.rb_enc_str_coderange(xEE.force_encoding("ascii-8bit"))
        result.should == :coderange_valid<commit_msg>encoding.c (rb_enc_get_index): return -1 for non-encoding capable objects<commit_after># -*- encoding: utf-8 -*-
require_relative 'spec_helper'
require_relative 'fixtures/encoding'

load_extension('encoding')

describe :rb_enc_get_index, shared: true do
  it "returns the index of the encoding of a String" do
    @s.send(@method, "string").should >= 0
  end

  it "returns the index of the encoding of a Regexp" do
    @s.send(@method, /regexp/).should >= 0
  end
end

describe :rb_enc_set_index, shared: true do
  it "sets the object's encoding to the Encoding specified by the index" do
    obj = "abc"
    result = @s.send(@method, obj, 2)

    # This is used because indexes should be considered implementation
    # dependent. So a pair is returned:
    #   [rb_enc_find_index()->name, rb_enc_get(obj)->name]
    result.first.should == result.last
  end

  it "associates an encoding with a subclass of String" do
    str = CApiEncodingSpecs::S.new "abc"
    result = @s.send(@method, str, 1)
    result.first.should == result.last
  end

  ruby_version_is "2.6" do
    it "raises an ArgumentError for a non-encoding capable object" do
      obj = Object.new
      -> {
        result = @s.send(@method, obj, 1)
      }.should raise_error(ArgumentError, "cannot set encoding on non-encoding capable object")
    end
  end
end

describe "C-API Encoding function" do
  before :each do
    @s = CApiEncodingSpecs.new
  end

  ruby_version_is "2.6" do
    describe "rb_enc_alias" do
      it "creates an alias for an existing Encoding" do
        @s.rb_enc_alias("ZOMGWTFBBQ", "UTF-8").should >= 0
        Encoding.find("ZOMGWTFBBQ").name.should == "UTF-8"
      end
    end
  end

  describe "rb_enc_find" do
    it "returns the encoding of an Encoding" do
      @s.rb_enc_find("UTF-8").should == "UTF-8"
    end

    it "returns the encoding of an Encoding specified with lower case" do
      @s.rb_enc_find("utf-8").should == "UTF-8"
    end
  end

  describe "rb_enc_find_index" do
    it "returns the index of an Encoding" do
      @s.rb_enc_find_index("UTF-8").should >= 0
    end

    it "returns the index of an Encoding specified with lower case" do
      @s.rb_enc_find_index("utf-8").should >= 0
    end

    it "returns -1 for an non existing encoding" do
      @s.rb_enc_find_index("non-existent-encoding").should == -1
    end
  end

  describe "rb_enc_from_index" do
    it "returns an Encoding" do
      @s.rb_enc_from_index(0).should be_an_instance_of(String)
    end
  end

  describe "rb_usascii_encoding" do
    it "returns the encoding for Encoding::US_ASCII" do
      @s.rb_usascii_encoding.should == "US-ASCII"
    end
  end

  describe "rb_ascii8bit_encoding" do
    it "returns the encoding for Encoding::ASCII_8BIT" do
      @s.rb_ascii8bit_encoding.should == "ASCII-8BIT"
    end
  end

  describe "rb_utf8_encoding" do
    it "returns the encoding for Encoding::UTF_8" do
      @s.rb_utf8_encoding.should == "UTF-8"
    end
  end

  describe "rb_enc_from_encoding" do
    it "returns an Encoding instance from an encoding data structure" do
      @s.rb_enc_from_encoding("UTF-8").should == Encoding::UTF_8
    end
  end

  describe "rb_locale_encoding" do
    it "returns the encoding for the current locale" do
      @s.rb_locale_encoding.should == Encoding.find('locale').name
    end
  end

  describe "rb_filesystem_encoding" do
    it "returns the encoding for the current filesystem" do
      @s.rb_filesystem_encoding.should == Encoding.find('filesystem').name
    end
  end

  describe "rb_enc_get" do
    it "returns the encoding ossociated with an object" do
      str = "abc".encode Encoding::ASCII_8BIT
      @s.rb_enc_get(str).should == "ASCII-8BIT"
    end
  end

  describe "rb_obj_encoding" do
    it "returns the encoding ossociated with an object" do
      str = "abc".encode Encoding::ASCII_8BIT
      @s.rb_obj_encoding(str).should == Encoding::ASCII_8BIT
    end
  end

  describe "rb_enc_get_index" do
    it_behaves_like :rb_enc_get_index, :rb_enc_get_index

    it "returns the index of the encoding of a Symbol" do
      @s.send(@method, :symbol).should >= 0
    end

    it "returns -1 as the index of nil" do
      @s.send(@method, nil).should == -1
    end

    it "returns -1 as the index for immediates" do
      @s.send(@method, 1).should == -1
    end

    it "returns -1 for an object without an encoding" do
      obj = Object.new
      @s.send(@method, obj).should == -1
    end
  end

  describe "rb_enc_set_index" do
    it_behaves_like :rb_enc_set_index, :rb_enc_set_index
  end

  describe "rb_enc_str_new" do
    it "returns a String in US-ASCII encoding when high bits are set" do
      xEE = [0xEE].pack('C').force_encoding('utf-8')
      result = @s.rb_enc_str_new(xEE, 1, Encoding::US_ASCII)
      result.encoding.should equal(Encoding::US_ASCII)
    end
  end

  describe "rb_enc_str_coderange" do
    describe "when the encoding is ASCII-8BIT" do
      it "returns ENC_CODERANGE_7BIT if there are no high bits set" do
        result = @s.rb_enc_str_coderange("abc".force_encoding("ascii-8bit"))
        result.should == :coderange_7bit
      end

      it "returns ENC_CODERANGE_VALID if there are high bits set" do
        xEE = [0xEE].pack('C').force_encoding('utf-8')
        result = @s.rb_enc_str_coderange(xEE.force_encoding("ascii-8bit"))
        result.should == :coderange_valid
{'fraction_non_alphanumeric': 0.10127182991647685, 'fraction_numerical': 0.010535307517084282, 'mean_word_length': 3.1047915855083756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '946426', 'n_tokens_mistral': 3686, 'n_tokens_neox': 3554, 'n_words': 1044}