<commit_before>require_relative 'spec_helper'

class Class
  def description value
    the_class = class << self; self; end
    the_value = value
    the_class.send(:define_method, :the_description) do
      the_value
    end
  end
end

class ::Agent
end

class FirstTest < HuginnAgent
  def self.description; 'a'; end

  def default_options
    @default_options ||= Object.new
  end
end

class SecondTest < HuginnAgent
  def self.description; 'b'; end

  def default_options
    @default_options ||= Object.new
  end
end

describe HuginnAgent do

  it "should be a class" do
    HuginnAgent.new.is_a?(HuginnAgent).must_equal true
  end

  describe "emit" do

    after do
      Object.send(:remove_const, :FirstTestAgent) if Object.constants.include?(:FirstTestAgent)
      Object.send(:remove_const, :SecondTestAgent) if Object.constants.include?(:FirstTestAgent)
    end

    [
      { type: FirstTest,  agent: :FirstTestAgent  },
      { type: SecondTest, agent: :SecondTestAgent },
    ].each do |test|

      describe "creating an agent" do

        it "should create an agent class" do
          test[:type].emit
          Object.constants.include?(test[:agent]).must_equal true
        end

        it "should create each agent with an Agent base class" do
          test[:type].emit
          eval(test[:agent].to_s).new.is_a?(Agent).must_equal true
        end

        it "should set the agent description" do
          test[:type].emit
          eval(test[:agent].to_s)
            .the_description.must_equal test[:type].description
        end

      end

    end

  end

  describe "description" do

    it "should default to nil" do
      HuginnAgent.description.nil?.must_equal true
    end

    it "should have each agent create their own description" do
      FirstTest.description.must_equal 'a'
      SecondTest.description.must_equal 'b'
    end

  end

  describe "default_options" do
    it "should default to an empty hash" do
      HuginnAgent.new.default_options.count.must_equal 0
      HuginnAgent.new.default_options.is_a?(Hash).must_equal true
    end

    it "should default the agents to return their specific default options" do
      expected_result = Object.new
      first_test      = Struct.new(:default_options, :parent_agent).new expected_result, nil

      FirstTest.stubs(:new).returns first_test
      FirstTest.emit
      FirstTestAgent.new.default_options.must_be_same_as expected_result
    end
  end

  describe "validate_options "do

    it "should have a base validate_options method" do
      HuginnAgent.new.validate_options
    end

    it "should pass the errors from the base class" do
      expected_result = Object.new
      first_test      = Struct.new(:validate_options, :parent_agent).new expected_result, nil

      FirstTest.stubs(:new).returns first_test
      FirstTest.emit

      result = FirstTestAgent.new.validate_options
      result.must_be_same_as expected_result
    end

  end

  describe "options" do

    it "should be bound to the actual agent in use" do
      FirstTest.emit
      options = Object.new
      first_test = FirstTestAgent.new
      first_test.stubs(:options).returns options

      first_test.base_agent.options.must_be_same_as options
    end
  end

end<commit_msg>Cleanup.<commit_after>require_relative 'spec_helper'

class Class
  def description value
    the_class = class << self; self; end
    the_value = value
    the_class.send(:define_method, :the_description) do
      the_value
    end
  end
end

class ::Agent
end

class FirstTest < HuginnAgent
  def self.description; 'a'; end

  def default_options
    @default_options ||= Object.new
  end
end

class SecondTest < HuginnAgent
  def self.description; 'b'; end

  def default_options
    @default_options ||= Object.new
  end
end

describe HuginnAgent do

  it "should be a class" do
    HuginnAgent.new.is_a?(HuginnAgent).must_equal true
  end

  describe "emit" do

    after do
      Object.send(:remove_const, :FirstTestAgent) if Object.constants.include?(:FirstTestAgent)
      Object.send(:remove_const, :SecondTestAgent) if Object.constants.include?(:FirstTestAgent)
    end

    [
      { type: FirstTest,  agent: :FirstTestAgent  },
      { type: SecondTest, agent: :SecondTestAgent },
    ].each do |test|

      describe "creating an agent" do

        it "should create an agent class" do
          test[:type].emit
          Object.constants.include?(test[:agent]).must_equal true
        end

        it "should create each agent with an Agent base class" do
          test[:type].emit
          eval(test[:agent].to_s).new.is_a?(Agent).must_equal true
        end

        it "should set the agent description" do
          test[:type].emit
          eval(test[:agent].to_s)
            .the_description.must_equal test[:type].description
        end

      end

    end

  end

  describe "description" do

    it "should default to nil" do
      HuginnAgent.description.nil?.must_equal true
    end

    it "should have each agent create their own description" do
      FirstTest.description.must_equal 'a'
      SecondTest.description.must_equal 'b'
    end

  end

  describe "default_options" do
    it "should default to an empty hash" do
      HuginnAgent.new.default_options.count.must_equal 0
      HuginnAgent.new.default_options.is_a?(Hash).must_equal true
    end

    it "should default the agents to return their specific default options" do
      expected_result = Object.new
      first_test      = Struct.new(:default_options, :parent_agent).new expected_result, nil

      FirstTest.stubs(:new).returns first_test
      FirstTest.emit
      FirstTestAgent.new.default_options.must_be_same_as expected_result
    end
  end

  describe "validate_options "do

    it "should have a base validate_options method" do
      HuginnAgent.new.validate_options
    end

    it "should pass the errors from the base class" do
      expected_result = Object.new
      first_test      = Struct.new(:validate_options, :parent_agent).new expected_result, nil

      FirstTest.stubs(:new).returns first_test
      FirstTest.emit

      result = FirstTestAgent.new.validate_options
      result.must_be_same_as expected_result
    end

  end

  describe "options" do

    let(:options)    { Object.new }
    let(:first_test) { FirstTestAgent.new }

    before do
      FirstTest.emit
      first_test.stubs(:options).returns options
    end

    it "should be bound to the actual agent in use" do
      first_test.base_agent.options.must_be_same_as options
    end

  end

end
{'fraction_non_alphanumeric': 0.07712278558338424, 'fraction_numerical': 0.00030543677458766036, 'mean_word_length': 2.847826086956522, 'pattern_counts': {'":': 0, '<': 11, '<?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': '5970499', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1913, 'n_words': 538}