<commit_before>require 'spec_helper'

describe ExampleModel do
  before do
    BallGag.engine = nil
    ExampleModel.clear_gagged_attributes
  end

  describe '#gag' do
    it 'should add #{attribute}_gagged? method' do
      ExampleModel.gag :words
      ExampleModel.new.should respond_to :words_gagged?
    end

    it 'should add #{attribute}_not_gagged? method' do
      ExampleModel.gag :words
      ExampleModel.new.should respond_to :words_not_gagged?
    end
  end

  describe '#gagged_attributes' do
    it 'should have no gagged attributes' do
      ExampleModel.gagged_attributes.should be_empty
    end

    it 'should gag attribute' do
      ExampleModel.gag :words
      ExampleModel.gagged_attributes.should include :words
    end

    it 'should gag multiple attributes' do
      ExampleModel.gag :words, :email
      ExampleModel.gagged_attributes.should include :words
      ExampleModel.gagged_attributes.should include :email
    end
  end

  describe '#clear_gagged_attributes' do
    it 'should clear gagged attributes' do
      ExampleModel.gag :words
      ExampleModel.clear_gagged_attributes
      ExampleModel.gagged_attributes.should be_empty
    end

    it 'should remove #{attribute}_not_gagged? method' do
      ExampleModel.gag :words
      ExampleModel.new.should respond_to(:words_not_gagged?)
      ExampleModel.clear_gagged_attributes
      ExampleModel.new.should_not respond_to(:words_not_gagged?)
    end

    it 'should remove #{attribute}_gagged? method' do
      ExampleModel.gag :words
      ExampleModel.new.should respond_to(:words_gagged?)
      ExampleModel.clear_gagged_attributes
      ExampleModel.new.should_not respond_to(:words_gagged?)
    end
  end

  describe 'single attribute gagged' do
    describe 'return values' do
      context 'when callable returns' do
        context 'true' do
          before do
            ExampleModel.gag :words, lambda { |*| true }
          end

          it '#{attribute}_gagged? should be false' do
            ExampleModel.new.words_gagged?.should be_false
          end
          it '#{attribute}_not_gagged? should be true' do
            ExampleModel.new.words_not_gagged?.should be_true
          end
        end

        context 'false' do
          before do
            ExampleModel.gag :words, lambda { |*| false }
          end

          it '#{attribute}_gagged? should be true' do
            ExampleModel.new.words_gagged?.should be_true
          end
          it '#{attribute}_not_gagged? should be false' do
            ExampleModel.new.words_not_gagged?.should be_false
          end
        end
      end
    end

    specify 'callable is called with attribute value' do
      callable = lambda { |words| }
      ExampleModel.gag :words, callable

      mock_words = mock('words')

      callable.should_receive(:call).
        with(mock_words)

      instance = ExampleModel.new
      instance.stub!(words: mock_words)

      instance.words_gagged?
    end
  end

  describe 'multiple attributes gagged' do
    describe 'return values' do
      context 'when results map returns true for attribute' do
        before { ExampleModel.gag :words, :email do |*| { words: true } end }

        it 'should return false for #{attribute}_gagged?' do
          ExampleModel.new.words_gagged?.should be_false
        end

        it 'should return true for #{attribute}_not_gagged?' do
          ExampleModel.new.words_not_gagged?.should be_true
        end
      end
    end

    specify 'callable is called with map of attributes' do
      callable = lambda { |map, options| }
      mock_words = mock('words')
      mock_email = mock('email')

      ExampleModel.gag :words, :email, callable
      callable.should_receive(:call).
        with(
          hash_including(words: mock_words, email: mock_email), anything).
        and_return({})

      instance = ExampleModel.new
      instance.stub!(words: mock_words, email: mock_email)
      instance.words_gagged?
    end

    specify 'callable is called with attr in options' do
      callable = lambda { |map, options| }
      ExampleModel.gag :words, :email, callable
      callable.should_receive(:call).
        with(anything, hash_including(attr: :words)).
        and_return({})


      ExampleModel.new.words_gagged?
    end
  end

  describe 'callable options' do
    specify 'instance is in options' do
      callable = lambda { |words, options| }
      ExampleModel.gag :words, callable

      instance = ExampleModel.new
      callable.should_receive(:call).
        with(anything, hash_including(instance: instance))

      instance.words_gagged?
    end

    specify 'attr is in options' do
      callable = lambda { |words, options| }
      ExampleModel.gag :words, callable
      callable.should_receive(:call).
        with(anything, hash_including(attr: :words))

      ExampleModel.new.words_gagged?
    end

    specify 'gag options are in options' do
      callable = lambda { |words, options| }
      mock_options = mock('options')<commit_msg>Demonstrate attrs passed in callable options<commit_after>require 'spec_helper'

describe ExampleModel do
  before do
    BallGag.engine = nil
    ExampleModel.clear_gagged_attributes
  end

  describe '#gag' do
    it 'should add #{attribute}_gagged? method' do
      ExampleModel.gag :words
      ExampleModel.new.should respond_to :words_gagged?
    end

    it 'should add #{attribute}_not_gagged? method' do
      ExampleModel.gag :words
      ExampleModel.new.should respond_to :words_not_gagged?
    end
  end

  describe '#gagged_attributes' do
    it 'should have no gagged attributes' do
      ExampleModel.gagged_attributes.should be_empty
    end

    it 'should gag attribute' do
      ExampleModel.gag :words
      ExampleModel.gagged_attributes.should include :words
    end

    it 'should gag multiple attributes' do
      ExampleModel.gag :words, :email
      ExampleModel.gagged_attributes.should include :words
      ExampleModel.gagged_attributes.should include :email
    end
  end

  describe '#clear_gagged_attributes' do
    it 'should clear gagged attributes' do
      ExampleModel.gag :words
      ExampleModel.clear_gagged_attributes
      ExampleModel.gagged_attributes.should be_empty
    end

    it 'should remove #{attribute}_not_gagged? method' do
      ExampleModel.gag :words
      ExampleModel.new.should respond_to(:words_not_gagged?)
      ExampleModel.clear_gagged_attributes
      ExampleModel.new.should_not respond_to(:words_not_gagged?)
    end

    it 'should remove #{attribute}_gagged? method' do
      ExampleModel.gag :words
      ExampleModel.new.should respond_to(:words_gagged?)
      ExampleModel.clear_gagged_attributes
      ExampleModel.new.should_not respond_to(:words_gagged?)
    end
  end

  describe 'single attribute gagged' do
    describe 'return values' do
      context 'when callable returns' do
        context 'true' do
          before do
            ExampleModel.gag :words, lambda { |*| true }
          end

          it '#{attribute}_gagged? should be false' do
            ExampleModel.new.words_gagged?.should be_false
          end
          it '#{attribute}_not_gagged? should be true' do
            ExampleModel.new.words_not_gagged?.should be_true
          end
        end

        context 'false' do
          before do
            ExampleModel.gag :words, lambda { |*| false }
          end

          it '#{attribute}_gagged? should be true' do
            ExampleModel.new.words_gagged?.should be_true
          end
          it '#{attribute}_not_gagged? should be false' do
            ExampleModel.new.words_not_gagged?.should be_false
          end
        end
      end
    end

    specify 'callable is called with attribute value' do
      callable = lambda { |words| }
      ExampleModel.gag :words, callable

      mock_words = mock('words')

      callable.should_receive(:call).
        with(mock_words)

      instance = ExampleModel.new
      instance.stub!(words: mock_words)

      instance.words_gagged?
    end
  end

  describe 'multiple attributes gagged' do
    describe 'return values' do
      context 'when results map returns true for attribute' do
        before { ExampleModel.gag :words, :email do |*| { words: true } end }

        it 'should return false for #{attribute}_gagged?' do
          ExampleModel.new.words_gagged?.should be_false
        end

        it 'should return true for #{attribute}_not_gagged?' do
          ExampleModel.new.words_not_gagged?.should be_true
        end
      end
    end

    specify 'callable is called with map of attributes' do
      callable = lambda { |map, options| }
      mock_words = mock('words')
      mock_email = mock('email')

      ExampleModel.gag :words, :email, callable
      callable.should_receive(:call).
        with(
          hash_including(words: mock_words, email: mock_email), anything).
        and_return({})

      instance = ExampleModel.new
      instance.stub!(words: mock_words, email: mock_email)
      instance.words_gagged?
    end

    specify 'callable is called with attr in options' do
      callable = lambda { |map, options| }
      ExampleModel.gag :words, :email, callable

      callable.should_receive(:call).
        with(anything, hash_including(attr: :words)).
        and_return({})
      ExampleModel.new.words_gagged?

      callable.should_receive(:call).
        with(anything, hash_including(attr: :email)).
        and_return({})
      ExampleModel.new.email_gagged?
    end

      ExampleModel.new.words_gagged?
    end
  end

  describe 'callable options' do
    specify 'instance is in options' do
      callable = lambda { |words, options| }
      ExampleModel.gag :words, callable

      instance = ExampleModel.new
      callable.should_receive(:call).
        with(anything, hash_including(instance: instance))

      instance.words_gagged?
    end

    specify 'attr is in options' do
      callable = lambda { |words, options| }
      ExampleModel.gag :words, callable
      callable.should_receive(:call).
        with(anything, hash_including(attr: :words))

      ExampleModel.new.words_gagged?
    end

    specify 'gag options are in options' do
      callable = lambda { |words, options| }
      mock_options = mock('options')
{'fraction_non_alphanumeric': 0.08012851718430533, 'fraction_numerical': 0.0, 'mean_word_length': 2.639971651311127, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7612372', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2909, 'n_words': 792}