<commit_before>require 'spec_helper'

describe Sequent::Core::Helpers::AttributeSupport do

  class NestedTestClass
    include Sequent::Core::Helpers::AttributeSupport, ActiveModel::Validations
    attrs message: String
    validates_presence_of :message
  end

  class AttributeSupportTestClass
    include Sequent::Core::Helpers::AttributeSupport, ActiveModel::Validations
    attrs message: String, nested_test_class: NestedTestClass
    validates_presence_of :message
    validates_with Sequent::Core::Helpers::AssociationValidator, associations: :nested_test_class
  end

  class SubTestClass < NestedTestClass
    attrs sub_message: String
    validates_presence_of :sub_message
  end

  module TestModule
    include Sequent::Core::Helpers::AttributeSupport

    attrs some_included_attribute: String
  end

  class TestClassWithIncludedModule
    include Sequent::Core::Helpers::AttributeSupport, TestModule

    attrs some_attribute: String
  end

  it "returns validation errors as hash" do
    subject = NestedTestClass.new
    expect(subject.valid?).to be_falsey
    expect(subject.validation_errors).to have(1).items
    expect(subject.validation_errors[:message]).to_not be_nil
  end
  
  it "returns all validations of nested classes in same hash" do
    subject = AttributeSupportTestClass.new
    subject.nested_test_class = NestedTestClass.new
    expect(subject.valid?).to be_falsey
    expect(subject.validation_errors).to have(3).items
    expect(subject.validation_errors[:message]).to_not be_nil
    expect(subject.validation_errors[:nested_test_class]).to_not be_nil
    expect(subject.validation_errors[:nested_test_class_message]).to_not be_nil
  end

  it "returns all validation errors including from superclass" do
    subject = SubTestClass.new
    expect(subject.valid?).to be_falsey
    expect(subject.validation_errors).to have(2).items
    expect(subject.validation_errors[:message]).to_not be_nil
    expect(subject.validation_errors[:sub_message]).to_not be_nil
  end

  it "does not fail when it does not have errors" do
    subject = NestedTestClass.new
    subject.message = "foo"
    expect(subject.valid?).to be_truthy
    expect(subject.validation_errors).to be_empty
  end

  it "supports including modules" do
    expect(TestClassWithIncludedModule.types).to eq({some_attribute: String, some_included_attribute: String})
  end

  it "should support subclasses" do
    expect(SubTestClass.types).to eq({sub_message: String, message: String})
  end

end<commit_msg>More readable spec<commit_after>require 'spec_helper'

describe Sequent::Core::Helpers::AttributeSupport do
  context ".validation_errors" do
    class NestedTestClass
      include Sequent::Core::Helpers::AttributeSupport, ActiveModel::Validations
      attrs message: String
      validates_presence_of :message
    end

    class AttributeSupportTestClass
      include Sequent::Core::Helpers::AttributeSupport, ActiveModel::Validations
      attrs message: String, nested_test_class: NestedTestClass
      validates_presence_of :message
      validates_with Sequent::Core::Helpers::AssociationValidator, associations: :nested_test_class
    end

    class SubTestClass < NestedTestClass
      attrs sub_message: String
      validates_presence_of :sub_message
    end

    it "returns validation errors as hash" do
      subject = NestedTestClass.new
      expect(subject.valid?).to be_falsey
      expect(subject.validation_errors).to have(1).items
      expect(subject.validation_errors[:message]).to_not be_nil
    end

    it "returns all validations of nested classes in same hash" do
      subject = AttributeSupportTestClass.new
      subject.nested_test_class = NestedTestClass.new
      expect(subject.valid?).to be_falsey
      expect(subject.validation_errors).to have(3).items
      expect(subject.validation_errors[:message]).to_not be_nil
      expect(subject.validation_errors[:nested_test_class]).to_not be_nil
      expect(subject.validation_errors[:nested_test_class_message]).to_not be_nil
    end

    it "returns all validation errors including from superclass" do
      subject = SubTestClass.new
      expect(subject.valid?).to be_falsey
      expect(subject.validation_errors).to have(2).items
      expect(subject.validation_errors[:message]).to_not be_nil
      expect(subject.validation_errors[:sub_message]).to_not be_nil
    end

    it "does not fail when it does not have errors" do
      subject = NestedTestClass.new
      subject.message = "foo"
      expect(subject.valid?).to be_truthy
      expect(subject.validation_errors).to be_empty
    end

  end

  context "including and inheritance" do
    module TestModule
      include Sequent::Core::Helpers::AttributeSupport

      attrs some_included_attribute: String
    end

    class TestClassWithIncludedModule
      include Sequent::Core::Helpers::AttributeSupport, TestModule

      attrs some_attribute: String
    end

    it "supports including modules" do
      expect(TestClassWithIncludedModule.types).to eq({some_attribute: String, some_included_attribute: String})
    end

    it "should support subclasses" do
      expect(SubTestClass.types).to eq({sub_message: String, message: String})
    end
  end

end
{'fraction_non_alphanumeric': 0.0768199233716475, 'fraction_numerical': 0.0011494252873563218, 'mean_word_length': 4.438541666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2356579', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1442, 'n_words': 339}