Move ValidateNumericalityOf submatchers to a subfolder
This commit is contained in:
parent
d5e1a94aa2
commit
27c065ba55
|
@ -4,9 +4,6 @@ require 'shoulda/matchers/active_model/validation_message_finder'
|
|||
require 'shoulda/matchers/active_model/exception_message_finder'
|
||||
require 'shoulda/matchers/active_model/allow_value_matcher'
|
||||
require 'shoulda/matchers/active_model/disallow_value_matcher'
|
||||
require 'shoulda/matchers/active_model/only_integer_matcher'
|
||||
require 'shoulda/matchers/active_model/odd_even_number_matcher'
|
||||
require 'shoulda/matchers/active_model/comparison_matcher'
|
||||
require 'shoulda/matchers/active_model/ensure_length_of_matcher'
|
||||
require 'shoulda/matchers/active_model/ensure_inclusion_of_matcher'
|
||||
require 'shoulda/matchers/active_model/ensure_exclusion_of_matcher'
|
||||
|
@ -15,6 +12,9 @@ require 'shoulda/matchers/active_model/validate_uniqueness_of_matcher'
|
|||
require 'shoulda/matchers/active_model/validate_acceptance_of_matcher'
|
||||
require 'shoulda/matchers/active_model/validate_confirmation_of_matcher'
|
||||
require 'shoulda/matchers/active_model/validate_numericality_of_matcher'
|
||||
require 'shoulda/matchers/active_model/numericality_matchers/comparison_matcher'
|
||||
require 'shoulda/matchers/active_model/numericality_matchers/odd_even_number_matcher'
|
||||
require 'shoulda/matchers/active_model/numericality_matchers/only_integer_matcher'
|
||||
require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher'
|
||||
require 'shoulda/matchers/active_model/errors'
|
||||
require 'shoulda/matchers/active_model/have_secure_password_matcher'
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
module Shoulda # :nodoc:
|
||||
module Matchers
|
||||
module ActiveModel # :nodoc:
|
||||
# Examples:
|
||||
# it { should validate_numericality_of(:attr).
|
||||
# is_greater_than(6).
|
||||
# less_than(20)...(and so on) }
|
||||
class ComparisonMatcher < ValidationMatcher
|
||||
def initialize(value, operator)
|
||||
@value = value
|
||||
@operator = operator
|
||||
@message = nil
|
||||
end
|
||||
|
||||
def for(attribute)
|
||||
@attribute = attribute
|
||||
self
|
||||
end
|
||||
|
||||
def matches?(subject)
|
||||
@subject = subject
|
||||
disallows_value_of(value_to_compare, @message)
|
||||
end
|
||||
|
||||
def allowed_types
|
||||
'integer'
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
@message = message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def value_to_compare
|
||||
case @operator
|
||||
when :> then [@value, @value - 1].sample
|
||||
when :>= then @value - 1
|
||||
when :== then @value + 1
|
||||
when :< then [@value, @value + 1].sample
|
||||
when :<= then @value + 1
|
||||
end
|
||||
end
|
||||
|
||||
def expectation
|
||||
case @operator
|
||||
when :> then "greater than"
|
||||
when :>= then "greater than or equal to"
|
||||
when :== then "equal to"
|
||||
when :< then "less than"
|
||||
when :<= then "less than or equal to"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,59 @@
|
|||
module Shoulda # :nodoc:
|
||||
module Matchers
|
||||
module ActiveModel # :nodoc:
|
||||
module NumericalityMatchers
|
||||
# Examples:
|
||||
# it { should validate_numericality_of(:attr).
|
||||
# is_greater_than(6).
|
||||
# less_than(20)...(and so on) }
|
||||
class ComparisonMatcher < ValidationMatcher
|
||||
def initialize(value, operator)
|
||||
@value = value
|
||||
@operator = operator
|
||||
@message = nil
|
||||
end
|
||||
|
||||
def for(attribute)
|
||||
@attribute = attribute
|
||||
self
|
||||
end
|
||||
|
||||
def matches?(subject)
|
||||
@subject = subject
|
||||
disallows_value_of(value_to_compare, @message)
|
||||
end
|
||||
|
||||
def allowed_types
|
||||
'integer'
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
@message = message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def value_to_compare
|
||||
case @operator
|
||||
when :> then [@value, @value - 1].sample
|
||||
when :>= then @value - 1
|
||||
when :== then @value + 1
|
||||
when :< then [@value, @value + 1].sample
|
||||
when :<= then @value + 1
|
||||
end
|
||||
end
|
||||
|
||||
def expectation
|
||||
case @operator
|
||||
when :> then "greater than"
|
||||
when :>= then "greater than or equal to"
|
||||
when :== then "equal to"
|
||||
when :< then "less than"
|
||||
when :<= then "less than or equal to"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,49 @@
|
|||
module Shoulda # :nodoc:
|
||||
module Matchers
|
||||
module ActiveModel # :nodoc:
|
||||
module NumericalityMatchers
|
||||
class OddEvenNumberMatcher # :nodoc:
|
||||
NON_EVEN_NUMBER_VALUE = 1
|
||||
NON_ODD_NUMBER_VALUE = 2
|
||||
|
||||
def initialize(attribute, options = {})
|
||||
@attribute = attribute
|
||||
options[:odd] ||= true
|
||||
options[:even] ||= false
|
||||
|
||||
if options[:odd] && !options[:even]
|
||||
@disallow_value_matcher = DisallowValueMatcher.new(NON_ODD_NUMBER_VALUE).
|
||||
for(@attribute).
|
||||
with_message(:odd)
|
||||
else
|
||||
@disallow_value_matcher = DisallowValueMatcher.new(NON_EVEN_NUMBER_VALUE).
|
||||
for(@attribute).
|
||||
with_message(:even)
|
||||
end
|
||||
end
|
||||
|
||||
def matches?(subject)
|
||||
@disallow_value_matcher.matches?(subject)
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
@disallow_value_matcher.with_message(message)
|
||||
self
|
||||
end
|
||||
|
||||
def allowed_types
|
||||
'integer'
|
||||
end
|
||||
|
||||
def failure_message_for_should
|
||||
@disallow_value_matcher.failure_message_for_should
|
||||
end
|
||||
|
||||
def failure_message_for_should_not
|
||||
@disallow_value_matcher.failure_message_for_should_not
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,39 @@
|
|||
module Shoulda # :nodoc:
|
||||
module Matchers
|
||||
module ActiveModel # :nodoc:
|
||||
module NumericalityMatchers
|
||||
class OnlyIntegerMatcher # :nodoc:
|
||||
NON_INTEGER_VALUE = 0.1
|
||||
|
||||
def initialize(attribute)
|
||||
@attribute = attribute
|
||||
@disallow_value_matcher = DisallowValueMatcher.new(NON_INTEGER_VALUE).
|
||||
for(attribute).
|
||||
with_message(:not_an_integer)
|
||||
end
|
||||
|
||||
def matches?(subject)
|
||||
@disallow_value_matcher.matches?(subject)
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
@disallow_value_matcher.with_message(message)
|
||||
self
|
||||
end
|
||||
|
||||
def allowed_types
|
||||
'integer'
|
||||
end
|
||||
|
||||
def failure_message_for_should
|
||||
@disallow_value_matcher.failure_message_for_should
|
||||
end
|
||||
|
||||
def failure_message_for_should_not
|
||||
@disallow_value_matcher.failure_message_for_should_not
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,47 +0,0 @@
|
|||
module Shoulda # :nodoc:
|
||||
module Matchers
|
||||
module ActiveModel # :nodoc:
|
||||
class OddEvenNumberMatcher # :nodoc:
|
||||
NON_EVEN_NUMBER_VALUE = 1
|
||||
NON_ODD_NUMBER_VALUE = 2
|
||||
|
||||
def initialize(attribute, options = {})
|
||||
@attribute = attribute
|
||||
options[:odd] ||= true
|
||||
options[:even] ||= false
|
||||
|
||||
if options[:odd] && !options[:even]
|
||||
@disallow_value_matcher = DisallowValueMatcher.new(NON_ODD_NUMBER_VALUE).
|
||||
for(@attribute).
|
||||
with_message(:odd)
|
||||
else
|
||||
@disallow_value_matcher = DisallowValueMatcher.new(NON_EVEN_NUMBER_VALUE).
|
||||
for(@attribute).
|
||||
with_message(:even)
|
||||
end
|
||||
end
|
||||
|
||||
def matches?(subject)
|
||||
@disallow_value_matcher.matches?(subject)
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
@disallow_value_matcher.with_message(message)
|
||||
self
|
||||
end
|
||||
|
||||
def allowed_types
|
||||
'integer'
|
||||
end
|
||||
|
||||
def failure_message_for_should
|
||||
@disallow_value_matcher.failure_message_for_should
|
||||
end
|
||||
|
||||
def failure_message_for_should_not
|
||||
@disallow_value_matcher.failure_message_for_should_not
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,37 +0,0 @@
|
|||
module Shoulda # :nodoc:
|
||||
module Matchers
|
||||
module ActiveModel # :nodoc:
|
||||
class OnlyIntegerMatcher # :nodoc:
|
||||
NON_INTEGER_VALUE = 0.1
|
||||
|
||||
def initialize(attribute)
|
||||
@attribute = attribute
|
||||
@disallow_value_matcher = DisallowValueMatcher.new(NON_INTEGER_VALUE).
|
||||
for(attribute).
|
||||
with_message(:not_an_integer)
|
||||
end
|
||||
|
||||
def matches?(subject)
|
||||
@disallow_value_matcher.matches?(subject)
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
@disallow_value_matcher.with_message(message)
|
||||
self
|
||||
end
|
||||
|
||||
def allowed_types
|
||||
'integer'
|
||||
end
|
||||
|
||||
def failure_message_for_should
|
||||
@disallow_value_matcher.failure_message_for_should
|
||||
end
|
||||
|
||||
def failure_message_for_should_not
|
||||
@disallow_value_matcher.failure_message_for_should_not
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -32,43 +32,43 @@ module Shoulda # :nodoc:
|
|||
end
|
||||
|
||||
def only_integer
|
||||
add_submatcher(OnlyIntegerMatcher.new(@attribute))
|
||||
add_submatcher(NumericalityMatchers::OnlyIntegerMatcher.new(@attribute))
|
||||
self
|
||||
end
|
||||
|
||||
def is_greater_than(value)
|
||||
add_submatcher(ComparisonMatcher.new(value, :>).for(@attribute))
|
||||
add_submatcher(NumericalityMatchers::ComparisonMatcher.new(value, :>).for(@attribute))
|
||||
self
|
||||
end
|
||||
|
||||
def is_greater_than_or_equal_to(value)
|
||||
add_submatcher(ComparisonMatcher.new(value, :>=).for(@attribute))
|
||||
add_submatcher(NumericalityMatchers::ComparisonMatcher.new(value, :>=).for(@attribute))
|
||||
self
|
||||
end
|
||||
|
||||
def is_equal_to(value)
|
||||
add_submatcher(ComparisonMatcher.new(value, :==).for(@attribute))
|
||||
add_submatcher(NumericalityMatchers::ComparisonMatcher.new(value, :==).for(@attribute))
|
||||
self
|
||||
end
|
||||
|
||||
def is_less_than(value)
|
||||
add_submatcher(ComparisonMatcher.new(value, :<).for(@attribute))
|
||||
add_submatcher(NumericalityMatchers::ComparisonMatcher.new(value, :<).for(@attribute))
|
||||
self
|
||||
end
|
||||
|
||||
def is_less_than_or_equal_to(value)
|
||||
add_submatcher(ComparisonMatcher.new(value, :<=).for(@attribute))
|
||||
add_submatcher(NumericalityMatchers::ComparisonMatcher.new(value, :<=).for(@attribute))
|
||||
self
|
||||
end
|
||||
|
||||
def odd
|
||||
odd_number_matcher = OddEvenNumberMatcher.new(@attribute, :odd => true)
|
||||
odd_number_matcher = NumericalityMatchers::OddEvenNumberMatcher.new(@attribute, :odd => true)
|
||||
add_submatcher(odd_number_matcher)
|
||||
self
|
||||
end
|
||||
|
||||
def even
|
||||
even_number_matcher = OddEvenNumberMatcher.new(@attribute, :even => true)
|
||||
even_number_matcher = NumericalityMatchers::OddEvenNumberMatcher.new(@attribute, :even => true)
|
||||
add_submatcher(even_number_matcher)
|
||||
self
|
||||
end
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Shoulda::Matchers::ActiveModel::ComparisonMatcher do
|
||||
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::ComparisonMatcher do
|
||||
it_behaves_like 'a numerical submatcher' do
|
||||
subject { Shoulda::Matchers::ActiveModel::ComparisonMatcher.new(0, :>) }
|
||||
subject { described_class.new(0, :>) }
|
||||
end
|
||||
|
||||
context 'is_greater_than' do
|
|
@ -1,8 +1,8 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Shoulda::Matchers::ActiveModel::OddEvenNumberMatcher do
|
||||
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::OddEvenNumberMatcher do
|
||||
it_behaves_like 'a numerical submatcher' do
|
||||
subject { Shoulda::Matchers::ActiveModel::OddEvenNumberMatcher.new(:attr) }
|
||||
subject { described_class.new(:attr) }
|
||||
end
|
||||
|
||||
context 'given an attribute that only allows odd number values' do
|
|
@ -1,8 +1,8 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Shoulda::Matchers::ActiveModel::OnlyIntegerMatcher do
|
||||
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::OnlyIntegerMatcher do
|
||||
it_behaves_like 'a numerical submatcher' do
|
||||
subject { Shoulda::Matchers::ActiveModel::OnlyIntegerMatcher.new(:attr) }
|
||||
subject { described_class.new(:attr) }
|
||||
end
|
||||
|
||||
context 'given an attribute that only allows integer values' do
|
Loading…
Reference in New Issue