Class: Minicrest::IsLessThanOrEqualTo

Inherits:
Matcher
  • Object
show all
Defined in:
lib/minicrest/is_less_than_or_equal_to.rb

Overview

Matcher that checks if a value is less than or equal to an expected value.

Works with any objects that support the <= operator.

Examples:

Basic usage

is_less_than_or_equal_to(5).matches?(3)   # => true
is_less_than_or_equal_to(5).matches?(5)   # => true
is_less_than_or_equal_to(5).matches?(10)  # => false

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(expected) ⇒ IsLessThanOrEqualTo

Creates a new less-than-or-equal-to matcher.

Parameters:

  • expected (Comparable)

    the value to compare against



16
17
18
19
# File 'lib/minicrest/is_less_than_or_equal_to.rb', line 16

def initialize(expected)
  super()
  @expected = expected
end

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    description



32
33
34
# File 'lib/minicrest/is_less_than_or_equal_to.rb', line 32

def description
  "less than or equal to #{@expected.inspect}"
end

#failure_message(actual) ⇒ String

Returns the failure message when the match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    failure message



40
41
42
43
44
# File 'lib/minicrest/is_less_than_or_equal_to.rb', line 40

def failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect} to be less than or equal to #{@expected.inspect}
  MSG
end

#matches?(actual) ⇒ Boolean

Checks if actual is less than or equal to expected.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if actual <= expected



25
26
27
# File 'lib/minicrest/is_less_than_or_equal_to.rb', line 25

def matches?(actual)
  actual <= @expected
end

#negated_failure_message(actual) ⇒ String

Returns the failure message when a negated match fails.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    message indicating unexpected comparison



50
51
52
53
54
# File 'lib/minicrest/is_less_than_or_equal_to.rb', line 50

def negated_failure_message(actual)
  <<~MSG.chomp
    expected #{actual.inspect} not to be less than or equal to #{@expected.inspect}, but it was
  MSG
end