<commit_before>require File.dirname(__FILE__) + '/../spec_helper'

describe Constraints do
  before(:each) do
    @form = SampleForm.new
  end
  describe 'regex' do
    it "should not trigger when value is nil" do
      Constraints.verify({'regex' => 'a.c'}, nil, @form).should == []
    end
    it "should not trigger when value matches regex " do
      Constraints.verify({'regex' =>'a.c'}, 'abc', @form).should == []
    end
    it "should not trigger when value is not nil or ''" do
      Constraints.verify({'regex' =>'a.c'}, 'abd', @form).should == ["Answer must match regular expression a.c"]
    end
    it "should accept regex objects as the value" do
      Constraints.verify({'regex' =>/a.c/}, 'abc', @form).should == []
    end
  end

  describe 'range' do
    it "should not trigger when value is nil" do
      Constraints.verify({'range' => '1-5'}, nil, @form).should == []
    end
    it "should not trigger when value is empty string" do
      Constraints.verify({'range' => '1-5'}, '', @form).should == []
    end
    it "should trigger when value is out of range" do
      Constraints.verify({'range' => '1-5'}, '9', @form).should == ["Answer must be between 1 and 5"]
    end
    it "should not trigger when value is in range" do
      Constraints.verify({'range' => '1-3'}, '1', @form).should == []
      Constraints.verify({'range' => '1-3'}, '2', @form).should == []
      Constraints.verify({'range' => '1-3'}, '3', @form).should == []
    end
  end

  describe 'date' do
    it "should not trigger when date is nil" do
      Constraints.verify({'date' => :in_past}, nil, @form).should == []
    end
    it ":in_past should trigger when date is in the future" do
      Constraints.verify({'date' => :in_past}, (Time.now+100).to_s, @form).should == ["Date cannot be in the future."]
    end
    it ":in_past should trigger when date is in the past" do
      Constraints.verify({'date' => :in_past}, (Time.now-100).to_s, @form).should == []
    end
    it ":in_future should trigger when date is in the future" do
      Constraints.verify({'date' => :in_future}, (Time.now-100).to_s, @form).should == ["Date cannot be in the past."]
    end
    it ":in_future should trigger when date is in the past" do
      Constraints.verify({'date' => :in_future}, (Time.now+100).to_s, @form).should == []
    end
  end

  describe 'max_length' do
    it "should trigger when value has more characters than the given length" do
      Constraints.verify({'max_length' => 2}, 'abc', @form).should == ["Answer must not be more than 2 characters long"]
    end
    it "should not trigger when value has fewer characters than the given length" do
      Constraints.verify({'max_length' => 2}, 'ab', @form).should == []
      Constraints.verify({'max_length' => 2}, 'a', @form).should == []
    end
  end

  describe 'unique' do
    before(:each) do
      @record = Record.make(@form,'new_entry',{:name =>'<NAME>'})
      @record.save('new_entry')
      @record = Record.make(@form,'new_entry',{:name =>'<NAME>'})
      @record.save('new_entry')
    end
    it "should trigger when a field value is not unique" do
      @form.with_record(@record) do
        Constraints.verify({'unique' => 'name'}, '<NAME>', @form).should == ['Answer must be unique']
      end
    end
    it "should not trigger when a field value not unique but is the value of the current record" do
      @form.with_record(@record) do
        Constraints.verify({'unique' => 'name'}, '<NAME>', @form).should == []
      end
    end
    it "should not trigger when a field value is unique" do
      @form.with_record(@record) do
        Constraints.verify({'unique' => 'name'}, 'Fred', @form).should == []
      end
    end
  end

  describe '-- using a Proc' do
    before(:each) do
      @theProc = Proc.new {|value,form| value == 'squidness' ? 'no sqiddyiness' : nil}
    end
    it "should trigger when the given proc returns an error message" do
      Constraints.verify({'proc' =>@theProc}, 'squidness', @form).should == ['no sqiddyiness']
    end
    it "should trigger not when the given proc returns no error message" do
      Constraints.verify({'proc' =>@theProc}, 'cow', @form).should == []
    end
  end
  describe '-- using a Proc to test against other form values' do
    before(:each) do
      @record = Record.make(@form,'new_entry',{:occupation =>'cowherd'})
      @theProc = Proc.new {|value,form| form.field_value('name') =~ /Smith$/ && form.field_value('occupation') == 'cowherd' ? 'no Smith cowherds when value of the field is boink' : nil}
    end
    it "should trigger when the given proc returns an error message" do
      @record.name = "<NAME>"
      @form.with_record(@record) do
        Constraints.verify({'proc' =>@theProc}, 'boink', @form).should == ['no Smith cowherds when value of the field is boink']
      end
    end
    it "should trigger when the given proc returns an error message" do
      @record.name = "<NAME>"
      @form.with_record(@record) do
        Constraints.verify({'proc' =>@theProc}, 'squidness', @form).should == ['no Smith cowherds when value of the field is boink']
      end
    end
    it "should trigger not when the given proc returns no error message" do
      @record.name = "<NAME>"
      @form.with_record(@record) do
        Constraints.verify({'proc' =>@theProc}, 'cow', @form).should == []
      end
    end
    it "should trigger not when the given proc returns no error message" do
      @record.name = "<NAME>"
      @form.with_record(@record) do
        Constraints.verify({'proc' =>@theProc}, 'voink', @form).should == []
      end
    end
  end

  describe 'required' do
    it "should trigger when value is nil" do
      Constraints.verify({'required' =>true}, nil, @form).should == [Constraints::RequiredErrMessage]
    end
    it "should trigger when value is ''" do
      Constraints.verify({'required' =>true}, '', @form).should == [Constraints::RequiredErrMessage]
    end
    it "should give a different error message if also constrianed as set" do
      Constraints.verify({'required' =>true,'set' => [{'apple' => 'Apple'},{'banana' => 'Banana'}]}, '', @form).should == [Constraints::RequiredMultiErrMessage]
    end
    it "should not trigger when value is not nil or ''" do
      Constraints.verify({'required' =>true}, 'fish', @form).should == []
    end
  end
  describe 'required conditional' do
    before(:each) do
      @record = Record.make(@form,'new_entry',{:name =>'<NAME>'})
    end
    describe '-- using related field and = operator' do
      it "should trigger when related field has stated value" do
        @form.with_record(@record) do
          Constraints.verify({'required' =>'name=<NAME>'}, nil, @form).should == ["#{Constraints::RequiredErrMessage} when Name is <NAME>"]
        end
      end
      it "should not trigger when related field has different value from stated value" do
        @form.with_record(@record) do
          Constraints.verify({'required' =>'name=<NAME>'}, nil, @form).should == []
        end
      end
    end
    describe '-- using related field and =~ as regex operator' do
      it "should trigger when related field has stated regex" do
        @form.with_record(@record) do
          Constraints.verify({'required' =>'name=~S.*h'}, nil, @form).should == ["#{Constraints::RequiredErrMessage} when Name matches regex S.*h"]
        end
      end
      it "should not trigger when related field has different value from stated regex" do
        @form.with_record(@record) do
          Constraints.verify({'required' =>'name=~^x$'}, nil, @form).should == []
        end
      end
    end
  end
  describe 'set' do
    it "should trigger when value is not in set" do
      Constraints.verify({'set' => [{'apple' => 'Apple'},{'banana' => 'Banana'}]}, 'kiwi,apple', @form).should == ["Answer must be one of Apple, Banana"]
    end

    it "should not trigger when value is not in set" do
      Constraints.verify({'set' => [{'apple' => 'Apple'},{'banana' => 'Banana'}]}, 'banana', @form).should == []
    end<commit_msg>fixed constraints spec for hide_required_extra_errors configuration option<commit_after>require File.dirname(__FILE__) + '/../spec_helper'

describe Constraints do
  before(:each) do
    @form = SampleForm.new
  end
  describe 'regex' do
    it "should not trigger when value is nil" do
      Constraints.verify({'regex' => 'a.c'}, nil, @form).should == []
    end
    it "should not trigger when value matches regex " do
      Constraints.verify({'regex' =>'a.c'}, 'abc', @form).should == []
    end
    it "should not trigger when value is not nil or ''" do
      Constraints.verify({'regex' =>'a.c'}, 'abd', @form).should == ["Answer must match regular expression a.c"]
    end
    it "should accept regex objects as the value" do
      Constraints.verify({'regex' =>/a.c/}, 'abc', @form).should == []
    end
  end

  describe 'range' do
    it "should not trigger when value is nil" do
      Constraints.verify({'range' => '1-5'}, nil, @form).should == []
    end
    it "should not trigger when value is empty string" do
      Constraints.verify({'range' => '1-5'}, '', @form).should == []
    end
    it "should trigger when value is out of range" do
      Constraints.verify({'range' => '1-5'}, '9', @form).should == ["Answer must be between 1 and 5"]
    end
    it "should not trigger when value is in range" do
      Constraints.verify({'range' => '1-3'}, '1', @form).should == []
      Constraints.verify({'range' => '1-3'}, '2', @form).should == []
      Constraints.verify({'range' => '1-3'}, '3', @form).should == []
    end
  end

  describe 'date' do
    it "should not trigger when date is nil" do
      Constraints.verify({'date' => :in_past}, nil, @form).should == []
    end
    it ":in_past should trigger when date is in the future" do
      Constraints.verify({'date' => :in_past}, (Time.now+100).to_s, @form).should == ["Date cannot be in the future."]
    end
    it ":in_past should trigger when date is in the past" do
      Constraints.verify({'date' => :in_past}, (Time.now-100).to_s, @form).should == []
    end
    it ":in_future should trigger when date is in the future" do
      Constraints.verify({'date' => :in_future}, (Time.now-100).to_s, @form).should == ["Date cannot be in the past."]
    end
    it ":in_future should trigger when date is in the past" do
      Constraints.verify({'date' => :in_future}, (Time.now+100).to_s, @form).should == []
    end
  end

  describe 'max_length' do
    it "should trigger when value has more characters than the given length" do
      Constraints.verify({'max_length' => 2}, 'abc', @form).should == ["Answer must not be more than 2 characters long"]
    end
    it "should not trigger when value has fewer characters than the given length" do
      Constraints.verify({'max_length' => 2}, 'ab', @form).should == []
      Constraints.verify({'max_length' => 2}, 'a', @form).should == []
    end
  end

  describe 'unique' do
    before(:each) do
      @record = Record.make(@form,'new_entry',{:name =>'<NAME>'})
      @record.save('new_entry')
      @record = Record.make(@form,'new_entry',{:name =>'<NAME>'})
      @record.save('new_entry')
    end
    it "should trigger when a field value is not unique" do
      @form.with_record(@record) do
        Constraints.verify({'unique' => 'name'}, '<NAME>', @form).should == ['Answer must be unique']
      end
    end
    it "should not trigger when a field value not unique but is the value of the current record" do
      @form.with_record(@record) do
        Constraints.verify({'unique' => 'name'}, '<NAME>', @form).should == []
      end
    end
    it "should not trigger when a field value is unique" do
      @form.with_record(@record) do
        Constraints.verify({'unique' => 'name'}, 'Fred', @form).should == []
      end
    end
  end

  describe '-- using a Proc' do
    before(:each) do
      @theProc = Proc.new {|value,form| value == 'squidness' ? 'no sqiddyiness' : nil}
    end
    it "should trigger when the given proc returns an error message" do
      Constraints.verify({'proc' =>@theProc}, 'squidness', @form).should == ['no sqiddyiness']
    end
    it "should trigger not when the given proc returns no error message" do
      Constraints.verify({'proc' =>@theProc}, 'cow', @form).should == []
    end
  end
  describe '-- using a Proc to test against other form values' do
    before(:each) do
      @record = Record.make(@form,'new_entry',{:occupation =>'cowherd'})
      @theProc = Proc.new {|value,form| form.field_value('name') =~ /Smith$/ && form.field_value('occupation') == 'cowherd' ? 'no Smith cowherds when value of the field is boink' : nil}
    end
    it "should trigger when the given proc returns an error message" do
      @record.name = "<NAME>"
      @form.with_record(@record) do
        Constraints.verify({'proc' =>@theProc}, 'boink', @form).should == ['no Smith cowherds when value of the field is boink']
      end
    end
    it "should trigger when the given proc returns an error message" do
      @record.name = "<NAME>"
      @form.with_record(@record) do
        Constraints.verify({'proc' =>@theProc}, 'squidness', @form).should == ['no Smith cowherds when value of the field is boink']
      end
    end
    it "should trigger not when the given proc returns no error message" do
      @record.name = "<NAME>"
      @form.with_record(@record) do
        Constraints.verify({'proc' =>@theProc}, 'cow', @form).should == []
      end
    end
    it "should trigger not when the given proc returns no error message" do
      @record.name = "<NAME>"
      @form.with_record(@record) do
        Constraints.verify({'proc' =>@theProc}, 'voink', @form).should == []
      end
    end
  end

  describe 'required' do
    it "should trigger when value is nil" do
      Constraints.verify({'required' =>true}, nil, @form).should == [Constraints::RequiredErrMessage]
    end
    it "should trigger when value is ''" do
      Constraints.verify({'required' =>true}, '', @form).should == [Constraints::RequiredErrMessage]
    end
    it "should give a different error message if also constrianed as set" do
      Constraints.verify({'required' =>true,'set' => [{'apple' => 'Apple'},{'banana' => 'Banana'}]}, '', @form).should == [Constraints::RequiredMultiErrMessage]
    end
    it "should not trigger when value is not nil or ''" do
      Constraints.verify({'required' =>true}, 'fish', @form).should == []
    end
  end
  describe 'required conditional' do
    before(:each) do
      Form.config[:hide_required_extra_errors] = false
      @record = Record.make(@form,'new_entry',{:name =>'<NAME>'})
    end
    describe '-- using related field and = operator' do
      it "should trigger when related field has stated value" do
        @form.with_record(@record) do
          Constraints.verify({'required' =>'name=<NAME>'}, nil, @form).should == ["#{Constraints::RequiredErrMessage} when Name is <NAME>"]
        end
      end
      it "should not show the when part of the error message if options switched off" do
        @form.with_record(@record) do
          Form.config[:hide_required_extra_errors] = true
          Constraints.verify({'required' =>'name=<NAME>'}, nil, @form).should == ["#{Constraints::RequiredErrMessage}"]
        end
      end
      it "should not trigger when related field has different value from stated value" do
        @form.with_record(@record) do
          Constraints.verify({'required' =>'name=<NAME>'}, nil, @form).should == []
        end
      end
    end
    describe '-- using related field and =~ as regex operator' do
      it "should trigger when related field has stated regex" do
        @form.with_record(@record) do
          Constraints.verify({'required' =>'name=~S.*h'}, nil, @form).should == ["#{Constraints::RequiredErrMessage} when Name matches regex S.*h"]
        end
      end
      it "should not trigger when related field has different value from stated regex" do
        @form.with_record(@record) do
          Constraints.verify({'required' =>'name=~^x$'}, nil, @form).should == []
        end
      end
    end
  end
  describe 'set' do
    it "should trigger when value is not in set" do
      Constraints.verify({'set' => [{'apple' => 'Apple'},{'banana' => 'Banana'}]}, 'kiwi,apple', @form).should == ["Answer must be one of Apple, Banana"]
    end

    it "should not trigger when value is not in set" do
      Constraints.verify({'set' => [{'apple' => 'Apple'},{'banana' => 'Banana'}]}, 'banana', @form).should == []
    end
{'fraction_non_alphanumeric': 0.1537435586541376, 'fraction_numerical': 0.004122461351924826, 'mean_word_length': 3.142641888498242, 'pattern_counts': {'":': 8, '<': 28, '<?xml version=': 0, '>': 125, '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': '4598655', 'n_tokens_mistral': 5041, 'n_tokens_neox': 4888, 'n_words': 1678}