<commit_before>require 'pathname'
__dir__ = Pathname(__FILE__).dirname.expand_path

# global first, then local to length validators
require __dir__.parent.parent + "spec_helper"
require __dir__ + 'spec_helper'

describe DataMapper::Validate::LengthValidator do
  it "lets user specify custom error message" do
    class Jabberwock
      include DataMapper::Resource
      property :id, Integer, :key => true
      property :snickersnack, String
      validates_length :snickersnack, :within => 3..40, :message => "worble warble"
    end
    wock = Jabberwock.new
    wock.valid?.should == false
    wock.errors.on(:snickersnack).should include('worble warble')
    wock.snickersnack = "hello"
    wock.id = 1
    wock.valid?.should == true
  end

  it "lets user specify a minimum length of a string field" do
    class MotorLaunch
      validates_length :name, :min => 3
    end

    launch = MotorLaunch.new
    launch.name = 'Ab'
    launch.valid?.should == false
    launch.errors.on(:name).should include('Name must be more than 3 characters long')
  end

  it "aliases :minimum for :min" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :minimum => 3
    end

    launch = MotorLaunch.new
    launch.name = 'Ab'
    launch.valid?.should == false
    launch.errors.on(:name).should include('Name must be more than 3 characters long')
  end

  it "lets user specify a maximum length of a string field" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :max => 5
    end

    launch = MotorLaunch.new
    launch.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
    launch.valid?.should == false
    launch.errors.on(:name).should include('Name must be less than 5 characters long')
  end

  it "aliases :maximum for :max" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :maximum => 5
    end
    launch = MotorLaunch.new
    launch.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
    launch.valid?.should == false
    launch.errors.on(:name).should include('Name must be less than 5 characters long')
  end

  it "lets user specify a length as range" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :in => (3..5)
    end

    launch = MotorLaunch.new
    launch.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
    launch.valid?.should == false
    launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')

    launch.name = 'A'
    launch.valid?.should == false
    launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')

    launch.name = 'Ride'
    launch.valid?.should == true
  end

  it "aliases :within for :in" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :within => (3..5)
    end

    launch = MotorLaunch.new<commit_msg>obj.valid?.should == true => obj.should be_valid<commit_after>require 'pathname'
__dir__ = Pathname(__FILE__).dirname.expand_path

# global first, then local to length validators
require __dir__.parent.parent + "spec_helper"
require __dir__ + 'spec_helper'

describe DataMapper::Validate::LengthValidator do
  it "lets user specify custom error message" do
    class Jabberwock
      include DataMapper::Resource
      property :id, Integer, :key => true
      property :snickersnack, String
      validates_length :snickersnack, :within => 3..40, :message => "worble warble"
    end
    wock = Jabberwock.new
    wock.should_not be_valid
    wock.errors.on(:snickersnack).should include('worble warble')
    wock.snickersnack = "hello"
    wock.id = 1
    wock.should be_valid
  end

  it "lets user specify a minimum length of a string field" do
    class MotorLaunch
      validates_length :name, :min => 3
    end

    launch = MotorLaunch.new
    launch.name = 'Ab'
    launch.should_not be_valid
    launch.errors.on(:name).should include('Name must be more than 3 characters long')
  end

  it "aliases :minimum for :min" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :minimum => 3
    end

    launch = MotorLaunch.new
    launch.name = 'Ab'
    launch.should_not be_valid
    launch.errors.on(:name).should include('Name must be more than 3 characters long')
  end

  it "lets user specify a maximum length of a string field" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :max => 5
    end

    launch = MotorLaunch.new
    launch.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
    launch.should_not be_valid
    launch.errors.on(:name).should include('Name must be less than 5 characters long')
  end

  it "aliases :maximum for :max" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :maximum => 5
    end
    launch = MotorLaunch.new
    launch.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
    launch.should_not be_valid
    launch.errors.on(:name).should include('Name must be less than 5 characters long')
  end

  it "lets user specify a length as range" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :in => (3..5)
    end

    launch = MotorLaunch.new
    launch.name = 'Lip­smackin­thirst­quenchin­acetastin­motivatin­good­buzzin­cool­talkin­high­walkin­fast­livin­ever­givin­cool­fizzin'
    launch.should_not be_valid
    launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')

    launch.name = 'A'
    launch.should_not be_valid
    launch.errors.on(:name).should include('Name must be between 3 and 5 characters long')

    launch.name = 'Ride'
    launch.should be_valid
  end

  it "aliases :within for :in" do
    class MotorLaunch
      validators.clear!
      validates_length :name, :within => (3..5)
    end

    launch = MotorLaunch.new
{'fraction_non_alphanumeric': 0.10204081632653061, 'fraction_numerical': 0.00647878198898607, 'mean_word_length': 3.550478997789241, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '6034280', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1956, 'n_words': 524}