<commit_before>require 'test_helper'

describe Enumerize::Base do
  let(:klass) do
    Class.new do
      include Enumerize
    end
  end

  let(:object) { klass.new }

  it 'returns nil when not set' do
    klass.enumerize(:foo, :in => [:a, :b])
    object.foo.must_equal nil
  end

  it 'returns value that was set' do
    klass.enumerize(:foo, :in => [:a, :b])
    object.foo = :a
    object.foo.must_equal 'a'
  end

  it 'returns translation' do
    store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
      klass.enumerize(:foo, :in => [:a, :b])
      object.foo = :a
      object.foo.text.must_equal 'a text'
      object.foo_text.must_equal 'a text'
      object.foo_text.must_equal 'a text'
    end
  end

  it 'returns nil as translation when value is nil' do
    store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
      klass.enumerize(:foo, :in => [:a, :b])
      object.foo_text.must_equal nil
    end
  end

  it 'scopes translation by i18 key' do
    def klass.model_name
      name = "ExampleClass"
      def name.i18n_key
        'example_class'
      end

      name
    end

    store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}}) do
      klass.enumerize(:foo, :in => [:a, :b])
      object.foo = :a
      object.foo.text.must_equal 'a text scoped'
      object.foo_text.must_equal 'a text scoped'
    end
  end

  it 'returns humanized value if there are no translations' do
    store_translations(:en, :enumerize => {}) do
      klass.enumerize(:foo, :in => [:a, :b])
      object.foo = :a
      object.foo_text.must_equal 'A'
    end
  end

  it 'stores value as string' do
    klass.enumerize(:foo, :in => [:a, :b])
    object.foo = :a
    object.instance_variable_get(:@foo).must_be_instance_of String
  end

  it 'handles default value' do
    klass.enumerize(:foo, :in => [:a, :b], :default => :b)
    object.foo.must_equal 'b'
  end

  it 'raises exception on invalid default value' do
    proc {
      klass.enumerize(:foo, :in => [:a, :b], :default => :c)
    }.must_raise ArgumentError
  end

  it 'has enumerized attributes' do
    klass.enumerized_attributes.must_equal({})
    klass.enumerize(:foo, :in => %w[a b])
    klass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
  end

  it "doesn't override existing method" do
    method = klass.method(:name)
    klass.enumerize(:name, :in => %w[a b], :default => 'a')
    klass.method(:name).must_equal method
  end
end<commit_msg>add failing test for inheriting enumerized attributes<commit_after>require 'test_helper'

describe Enumerize::Base do
  let(:klass) do
    Class.new do
      include Enumerize
    end
  end
  let(:subklass) do
    Class.new(klass)
  end

  let(:object) { klass.new }

  it 'returns nil when not set' do
    klass.enumerize(:foo, :in => [:a, :b])
    object.foo.must_equal nil
  end

  it 'returns value that was set' do
    klass.enumerize(:foo, :in => [:a, :b])
    object.foo = :a
    object.foo.must_equal 'a'
  end

  it 'returns translation' do
    store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
      klass.enumerize(:foo, :in => [:a, :b])
      object.foo = :a
      object.foo.text.must_equal 'a text'
      object.foo_text.must_equal 'a text'
      object.foo_text.must_equal 'a text'
    end
  end

  it 'returns nil as translation when value is nil' do
    store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
      klass.enumerize(:foo, :in => [:a, :b])
      object.foo_text.must_equal nil
    end
  end

  it 'scopes translation by i18 key' do
    def klass.model_name
      name = "ExampleClass"
      def name.i18n_key
        'example_class'
      end

      name
    end

    store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}}) do
      klass.enumerize(:foo, :in => [:a, :b])
      object.foo = :a
      object.foo.text.must_equal 'a text scoped'
      object.foo_text.must_equal 'a text scoped'
    end
  end

  it 'returns humanized value if there are no translations' do
    store_translations(:en, :enumerize => {}) do
      klass.enumerize(:foo, :in => [:a, :b])
      object.foo = :a
      object.foo_text.must_equal 'A'
    end
  end

  it 'stores value as string' do
    klass.enumerize(:foo, :in => [:a, :b])
    object.foo = :a
    object.instance_variable_get(:@foo).must_be_instance_of String
  end

  it 'handles default value' do
    klass.enumerize(:foo, :in => [:a, :b], :default => :b)
    object.foo.must_equal 'b'
  end

  it 'raises exception on invalid default value' do
    proc {
      klass.enumerize(:foo, :in => [:a, :b], :default => :c)
    }.must_raise ArgumentError
  end

  it 'has enumerized attributes' do
    klass.enumerized_attributes.must_equal({})
    klass.enumerize(:foo, :in => %w[a b])
    klass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
  end

  it "doesn't override existing method" do
    method = klass.method(:name)
    klass.enumerize(:name, :in => %w[a b], :default => 'a')
    klass.method(:name).must_equal method
  end

  it "inherits enumerized attributes from a parent class" do
    klass.enumerize(:foo, :in => %w[a b])
    subklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
  end
end
{'fraction_non_alphanumeric': 0.14002642007926025, 'fraction_numerical': 0.0015097188148707303, 'mean_word_length': 2.9700374531835205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 54, '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': '1309218', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1901, 'n_words': 507}