Class: Minicrest::Between
Overview
Matcher that checks if a value is between a minimum and maximum.
Instance Method Summary collapse
-
#description ⇒ String
Returns a description of what this matcher expects.
-
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
-
#initialize(min, max, exclusive: false) ⇒ Between
constructor
Creates a new between matcher.
-
#matches?(actual) ⇒ Boolean
Checks if actual is between min and max.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
Methods inherited from Matcher
Constructor Details
#initialize(min, max, exclusive: false) ⇒ Between
Creates a new between matcher.
13 14 15 16 17 18 |
# File 'lib/minicrest/between.rb', line 13 def initialize(min, max, exclusive: false) super() @min = min @max = max @exclusive = exclusive end |
Instance Method Details
#description ⇒ String
Returns a description of what this matcher expects.
39 40 41 42 |
# File 'lib/minicrest/between.rb', line 39 def description range_type = @exclusive ? 'exclusively' : 'inclusively' "between #{@min.inspect} and #{@max.inspect} (#{range_type})" end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
48 49 50 51 |
# File 'lib/minicrest/between.rb', line 48 def (actual) range_type = @exclusive ? 'exclusively' : 'inclusively' "expected #{actual.inspect} to be between #{@min.inspect} and #{@max.inspect} (#{range_type})" end |
#matches?(actual) ⇒ Boolean
Checks if actual is between min and max.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/minicrest/between.rb', line 24 def matches?(actual) return false unless actual.respond_to?(:>=) && actual.respond_to?(:<=) if @exclusive actual > @min && actual < @max else actual >= @min && actual <= @max end rescue ArgumentError, NoMethodError false end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
57 58 59 60 |
# File 'lib/minicrest/between.rb', line 57 def (actual) range_type = @exclusive ? 'exclusively' : 'inclusively' "expected #{actual.inspect} not to be between #{@min.inspect} and #{@max.inspect} (#{range_type}), but it was" end |