<commit_before>require 'spec_helper'

describe Hashie::Extensions::Coercion do
  class NotInitializable
    private_class_method :new
  end

  class Initializable
    attr_reader :coerced, :value

    def initialize(obj, coerced = nil)
      @coerced = coerced
      @value = obj.class.to_s
    end

    def coerced?
      !@coerced.nil?
    end
  end

  class Coercable < Initializable
    def self.coerce(obj)
      new(obj, true)
    end
  end

  before(:each) do
    class ExampleCoercableHash < Hash
      include Hashie::Extensions::Coercion
      include Hashie::Extensions::MergeInitializer
    end
  end

  subject { ExampleCoercableHash }

  let(:instance) { subject.new }

  describe '#coerce_key' do
    context 'nesting' do
      class BaseCoercableHash < Hash
        include Hashie::Extensions::Coercion
        include Hashie::Extensions::MergeInitializer
      end

      class NestedCoercableHash < BaseCoercableHash
        coerce_key :foo, String
        coerce_key :bar, Integer
      end

      class RootCoercableHash < BaseCoercableHash
        coerce_key :nested, NestedCoercableHash
        coerce_key :nested_list, Array[NestedCoercableHash]
        coerce_key :nested_hash, Hash[String => NestedCoercableHash]
      end

      def test_nested_object(obj)
        expect(obj).to be_a(NestedCoercableHash)
        expect(obj[:foo]).to be_a(String)
        expect(obj[:bar]).to be_an(Integer)
      end

      subject { RootCoercableHash }
      let(:instance) { subject.new }

      it 'coerces nested objects' do
        instance[:nested] = { foo: 123, bar: '456' }
        test_nested_object(instance[:nested])
      end

      it 'coerces nested arrays' do
        instance[:nested_list] = [
          { foo: 123, bar: '456' },<commit_msg>Another spec for #288.<commit_after>require 'spec_helper'

describe Hashie::Extensions::Coercion do
  class NotInitializable
    private_class_method :new
  end

  class Initializable
    attr_reader :coerced, :value

    def initialize(obj, coerced = nil)
      @coerced = coerced
      @value = obj.class.to_s
    end

    def coerced?
      !@coerced.nil?
    end
  end

  class Coercable < Initializable
    def self.coerce(obj)
      new(obj, true)
    end
  end

  before(:each) do
    class ExampleCoercableHash < Hash
      include Hashie::Extensions::Coercion
      include Hashie::Extensions::MergeInitializer
    end
  end

  subject { ExampleCoercableHash }

  let(:instance) { subject.new }

  describe '#coerce_key' do
    context 'nesting' do
      class BaseCoercableHash < Hash
        include Hashie::Extensions::Coercion
        include Hashie::Extensions::MergeInitializer
      end

      class NestedCoercableHash < BaseCoercableHash
        coerce_key :foo, String
        coerce_key :bar, Integer
      end

      class OtherNestedCoercableHash < BaseCoercableHash
        coerce_key :foo, Symbol
      end

      class RootCoercableHash < BaseCoercableHash
        coerce_key :nested, NestedCoercableHash
        coerce_key :other, OtherNestedCoercableHash
        coerce_key :nested_list, Array[NestedCoercableHash]
        coerce_key :nested_hash, Hash[String => NestedCoercableHash]
      end

      def test_nested_object(obj)
        expect(obj).to be_a(NestedCoercableHash)
        expect(obj[:foo]).to be_a(String)
        expect(obj[:bar]).to be_an(Integer)
      end

      subject { RootCoercableHash }
      let(:instance) { subject.new }

      it 'does not add coercions to superclass' do
        instance[:nested] = { foo: 'bar' }
        instance[:other]  = { foo: 'bar' }
        expect(instance[:nested][:foo]).to be_a String
        expect(instance[:other][:foo]).to be_a Symbol
      end

      it 'coerces nested objects' do
        instance[:nested] = { foo: 123, bar: '456' }
        test_nested_object(instance[:nested])
      end

      it 'coerces nested arrays' do
        instance[:nested_list] = [
          { foo: 123, bar: '456' },
{'fraction_non_alphanumeric': 0.08775923115832068, 'fraction_numerical': 0.006828528072837633, 'mean_word_length': 2.752371916508539, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 5, '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': '6801578', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1221, 'n_words': 286}