<commit_before>require 'test_helper'

describe Lotus::Entity do
  before do
    class Car
      include Lotus::Entity
    end

    class Book
      include Lotus::Entity
      self.attributes = :title, :author
    end

    class NonFictionBook < Book
      self.attributes = :price
    end

    class CoolNonFictionBook < NonFictionBook
      self.attributes = :coolness
    end

    class Camera
      include Lotus::Entity
      attr_accessor :analog
    end
  end

  after do
    [:Car, :Book, :NonFictionBook, :CoolNonFictionBook, :Camera].each do |const|
      Object.send(:remove_const, const)
    end
  end

  describe 'attributes' do
    it 'defines attributes' do
      Car.attributes = :model
      Car.attributes.must_equal Set.new([:id, :model])
    end

    describe 'params is array' do
      it 'defines attributes' do
        Car.attributes = [:model]
        Car.attributes.must_equal Set.new([:id, :model])
      end
    end
  end

  describe '#initialize' do
    describe 'with defined attributes' do
      it 'accepts given attributes' do
        book = Book.new(title: "A Lover's Discourse: Fragments", author: '<NAME>')

        book.instance_variable_get(:@title).must_equal  "A Lover's Discourse: Fragments"
        book.instance_variable_get(:@author).must_equal '<NAME>'
      end

      it 'accepts given attributes as string keys' do
        book = Book.new('title' => "A Lover's Discourse: Fragments", 'author' => '<NAME>')

        book.instance_variable_get(:@title).must_equal  "A Lover's Discourse: Fragments"
        book.instance_variable_get(:@author).must_equal '<NAME>'
      end

      it 'ignores unknown attributes' do
        book = Book.new(unknown: 'x')

        book.instance_variable_get(:@unknown).must_be_nil
      end

      it 'accepts given attributes for subclasses' do
        book = NonFictionBook.new(title: 'Refactoring', author: '<NAME>', price: 50)

        book.instance_variable_get(:@title).must_equal  'Refactoring'
        book.instance_variable_get(:@author).must_equal '<NAME>'
        book.instance_variable_get(:@price).must_equal 50
      end

      it 'accepts given attributes for subclass of subclass' do
        book = CoolNonFictionBook.new(title: 'Refactoring', author: '<NAME>', price: 50, coolness: 'awesome')

        book.instance_variable_get(:@title).must_equal  'Refactoring'
        book.instance_variable_get(:@author).must_equal '<NAME>'
        book.instance_variable_get(:@price).must_equal 50
        book.instance_variable_get(:@coolness).must_equal 'awesome'
      end
    end

    describe 'with undefined attributes' do
      it 'has default accessor for id' do
        camera = Camera.new
        camera.must_respond_to :id
        camera.must_respond_to :id=
      end

      it 'is able to initialize an entity without given attributes' do
        camera = Camera.new
        camera.analog.must_be_nil
      end

      it 'is able to initialize an entity if it has the right accessors' do
        camera = Camera.new(analog: true)
        camera.analog.must_equal(true)
      end

      it "raises an error when the given attributes don't correspond to a known accessor" do
        -> { Camera.new(digital: true) }.must_raise(NoMethodError)
      end
    end
  end

  describe 'accessors' do
    it 'exposes getters for attributes' do
      book = Book.new(title: 'High Fidelity')<commit_msg>Added extra test case. Ref #105<commit_after>require 'test_helper'

describe Lotus::Entity do
  before do
    class Car
      include Lotus::Entity
    end

    class Book
      include Lotus::Entity
      self.attributes = :title, :author
    end

    class NonFictionBook < Book
      self.attributes = :price
    end

    class CoolNonFictionBook < NonFictionBook
      self.attributes = :coolness
    end

    class Camera
      include Lotus::Entity
      attr_accessor :analog
    end
  end

  after do
    [:Car, :Book, :NonFictionBook, :CoolNonFictionBook, :Camera].each do |const|
      Object.send(:remove_const, const)
    end
  end

  describe 'attributes' do
    it 'defines attributes' do
      Car.attributes = :model
      Car.attributes.must_equal Set.new([:id, :model])
    end

    describe 'params is array' do
      it 'defines attributes' do
        Car.attributes = [:model]
        Car.attributes.must_equal Set.new([:id, :model])
      end
    end
  end

  describe '#initialize' do
    describe 'with defined attributes' do
      it 'accepts given attributes' do
        book = Book.new(title: "A Lover's Discourse: Fragments", author: '<NAME>')

        book.instance_variable_get(:@title).must_equal  "A Lover's Discourse: Fragments"
        book.instance_variable_get(:@author).must_equal '<NAME>'
      end

      it 'accepts given attributes as string keys' do
        book = Book.new('title' => "A Lover's Discourse: Fragments", 'author' => '<NAME>')

        book.instance_variable_get(:@title).must_equal  "A Lover's Discourse: Fragments"
        book.instance_variable_get(:@author).must_equal '<NAME>'
      end

      it 'ignores unknown attributes' do
        book = Book.new(unknown: 'x')

        book.instance_variable_get(:@unknown).must_be_nil
      end

      it 'accepts given attributes for subclasses' do
        book = NonFictionBook.new(title: 'Refactoring', author: '<NAME>', price: 50)

        book.instance_variable_get(:@title).must_equal  'Refactoring'
        book.instance_variable_get(:@author).must_equal '<NAME>'
        book.instance_variable_get(:@price).must_equal 50
      end

      it 'accepts given attributes for subclass of subclass' do
        book = CoolNonFictionBook.new(title: 'Refactoring', author: '<NAME>', price: 50, coolness: 'awesome')

        book.instance_variable_get(:@title).must_equal  'Refactoring'
        book.instance_variable_get(:@author).must_equal '<NAME>'
        book.instance_variable_get(:@price).must_equal 50
        book.instance_variable_get(:@coolness).must_equal 'awesome'
      end

      it "doesn't interfer with superclass attributes" do
        book = Book.new(title: "Good Math", author: "<NAME>", price: 34, coolness: true)

        book.instance_variable_get(:@title).must_equal  'Good Math'
        book.instance_variable_get(:@author).must_equal '<NAME>'
        book.instance_variable_get(:@price).must_be_nil
        book.instance_variable_get(:@coolness).must_be_nil
      end
    end

    describe 'with undefined attributes' do
      it 'has default accessor for id' do
        camera = Camera.new
        camera.must_respond_to :id
        camera.must_respond_to :id=
      end

      it 'is able to initialize an entity without given attributes' do
        camera = Camera.new
        camera.analog.must_be_nil
      end

      it 'is able to initialize an entity if it has the right accessors' do
        camera = Camera.new(analog: true)
        camera.analog.must_equal(true)
      end

      it "raises an error when the given attributes don't correspond to a known accessor" do
        -> { Camera.new(digital: true) }.must_raise(NoMethodError)
      end
    end
  end

  describe 'accessors' do
    it 'exposes getters for attributes' do
      book = Book.new(title: 'High Fidelity')
{'fraction_non_alphanumeric': 0.09865221620119494, 'fraction_numerical': 0.0029178824510212586, 'mean_word_length': 2.847140566541956, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, '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': '2599498', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2122, 'n_words': 593}