<commit_before>require 'spec_helper'


# Some of the Core::Behaviors::Macros methods tested in
# spec/lib/core/behaviors_spec.rb
describe Core::Behaviors::Macros do

  describe 'filters_input_on' do
    before do
      class SpecFilterByDefault < SpecModel(:name => :string)
        filters_input_on :name
      end

      class SpecFilterByMethod < SpecModel(:name => :string)
        filters_input_on :name, :filter => :strip
      end

      class SpecFilterByLambda < SpecModel(:name => :string)
        filters_input_on :name, :filter => lambda { |value| value.squish }
      end

      @initial_value = <<-eostring
        My uncle - high  ideals inspire him;\n\n
        but   when past joking he fell sick,
      eostring
      @expected_value =
        'My uncle - high ideals inspire him; but when past joking he fell sick,'
    end

    after do
      SpecModel.cleanup!
    end


    describe 'DefaultFilter' do
      let(:model) { SpecFilterByDefault.create }

      it 'should sanitize' do
        model.name = @initial_value
        model.name.should == @expected_value
      end
    end

    describe 'MethodFilter' do
      let(:model) { SpecFilterByMethod.create }

      it 'should sanitize' do
        model.name = "\t\n here  is sripped  \nvalue   "
        model.name.should == "here  is sripped  \nvalue"
      end

      it 'should not fail if method could not be called on given value' do
        expect { model.name = 5 }.
          to_not raise_error(NoMethodError, 'undefined method `squish` for 5:Fixnum')
      end
    end

    describe 'LambdaFilter' do
      let(:model) { SpecFilterByLambda.create }

      it 'should sanitize' do
        model.name = @initial_value
        model.name.should == @expected_value
      end

      it 'should raise exceptions from lambda' do
        expect { model.name = 5 }.
          to raise_error(NoMethodError, "undefined method `squish' for 5:Fixnum")
      end
    end

    describe 'InvalidFilter' do
      it 'should raise ArgumentError("Do not know how to handle filter `<invalid filter info>`")' do
        expect do
          class SpecFilterByInvalid < SpecModel(:name => :string)
            filters_input_on :name, :filter => 1..5
          end

        end.to raise_error(ArgumentError, 'Do not know how to handle filter `1..5`')
      end
    end
  end

end<commit_msg>Tests coverage for filters_input_on was improved.<commit_after>require 'spec_helper'


# Some of the Core::Behaviors::Macros methods tested in
# spec/lib/core/behaviors_spec.rb
describe Core::Behaviors::Macros do

  describe 'filters_input_on' do
    before do
      @initial_value = <<-eostring
        My uncle - high  ideals inspire him;\n\n
        but   when past joking he fell sick,
      eostring
      @expected_value =
        'My uncle - high ideals inspire him; but when past joking he fell sick,'
    end

    after do
      SpecModel.cleanup!
    end


    describe 'DefaultFilter' do
      before do
        class SpecFilterByDefault < SpecModel(:name => :string)
          filters_input_on :name
        end
      end

      let(:model) { SpecFilterByDefault.create }

      it 'should sanitize' do
        model.name = @initial_value
        model.name.should == @expected_value
      end
    end

    describe 'MethodFilter' do
      before do
        class SpecFilterByMethod < SpecModel(:name => :string)
          filters_input_on :name, :filter => :strip
        end
      end

      let(:model) { SpecFilterByMethod.create }

      it 'should sanitize' do
        model.name = "\t\n here  is sripped  \nvalue   "
        model.name.should == "here  is sripped  \nvalue"
      end

      it 'should not fail if method could not be called on given value' do
        expect { model.name = 5 }.
          to_not raise_error(NoMethodError, 'undefined method `squish` for 5:Fixnum')
      end
    end

    describe 'LambdaFilter' do
      before do
        class SpecFilterByLambda < SpecModel(:name => :string)
          filters_input_on :name, :filter => lambda { |value| value.squish }
        end
      end

      let(:model) { SpecFilterByLambda.create }

      it 'should sanitize' do
        model.name = @initial_value
        model.name.should == @expected_value
      end

      it 'should raise exceptions from lambda' do
        expect { model.name = 5 }.
          to raise_error(NoMethodError, "undefined method `squish' for 5:Fixnum")
      end
    end

    describe 'InvalidFilter' do
      it 'should raise ArgumentError("Do not know how to handle filter `<invalid filter info>`")' do
        expect do
          class SpecFilterByInvalid < SpecModel(:name => :string)
            filters_input_on :name, :filter => 1..5
          end

        end.to raise_error(ArgumentError, 'Do not know how to handle filter `1..5`')
      end
    end

    describe 'RegexpFilter' do
      before do
        class SpecFilterByRegexp < SpecModel(:name => :string)
          filters_input_on :name, :filter => /\d+/
        end
      end

      let(:model) { SpecFilterByRegexp.create }

      it 'should sanitize' do
        model.name = '0abc123def456'
        model.name.should == 'abcdef'
      end
    end

    describe 'AlphaFilter' do
      before do
        class SpecFilterByAlpha < SpecModel(:name => :string)
          filters_input_on :name, :filter => :alpha
        end
      end

      let(:model) { SpecFilterByAlpha.create }

      it 'should sanitize' do
        model.name = '0abc123def456'
        model.name.should == '0123456'
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.08617270596718947, 'fraction_numerical': 0.006670272219217595, 'mean_word_length': 2.234985422740525, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, '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': '3915245', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1592, 'n_words': 501}