Class: ArgumentSpecification::Matchers::BeComparedTo

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/argspec/matchers/be_compared_to.rb

Instance Attribute Summary collapse

Attributes inherited from BaseMatcher

#actual, #block, #metadata

Instance Method Summary collapse

Methods inherited from BaseMatcher

matcher_name

Constructor Details

#initialize(operator, expected) ⇒ BeComparedTo

Create a new matcher instance

Arguments:

operator: (Symbol)
expected: (Object)

Example:

>> ArgumentSpecification::Matchers::BeComparedTo.new(:==, 10)
=> #<ArgumentSpecification::Matchers::BeComparedTo:0x00000000000000 @operator=:==, @expected=10>


16
17
18
19
# File 'lib/argspec/matchers/be_compared_to.rb', line 16

def initialize(operator, expected)
  @operator = operator
  @expected = expected
end

Instance Attribute Details

#expectedObject (readonly)

Returns the value of attribute expected.



4
5
6
# File 'lib/argspec/matchers/be_compared_to.rb', line 4

def expected
  @expected
end

#operatorObject (readonly)

Returns the value of attribute operator.



4
5
6
# File 'lib/argspec/matchers/be_compared_to.rb', line 4

def operator
  @operator
end

Instance Method Details

#failure_messageObject

The failure message when using ‘should’

Example:

>> matcher.failure_message
=> "':foo' should be equal to ':test'"


27
28
29
30
31
# File 'lib/argspec/matchers/be_compared_to.rb', line 27

def failure_message
  actual = prettify_args(@actual)

  "'#{actual}' should #{pretty_matcher} '#{@expected}'"
end

#failure_message_when_negatedObject

The failure message when using ‘should not’

Example:

>> matcher.failure_message_when_negated
=> "':test' should not be equal to ':test'"


39
40
41
42
43
# File 'lib/argspec/matchers/be_compared_to.rb', line 39

def failure_message_when_negated
  actual = prettify_args(@actual)

  "'#{actual}' should not #{pretty_matcher} '#{@expected}'"
end

#matches?Boolean

Check if the actual object matches

Example:

>> matcher.matches?
=> true

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/argspec/matchers/be_compared_to.rb', line 51

def matches?
  begin
    @actual.send(@operator, @expected)
  rescue ArgumentError
    false
  end
end