Class: ArgumentSpecification::Matchers::All

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/argspec/matchers/all.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(expected) ⇒ All

Create a new matcher instance

Arguments:

expected: (BaseMatcher)

Example:

>> symbol_matcher = ArgumentSpecification::Matchers::BeA.new(Symbol)
>> ArgumentSpecification::Matchers::All.new(symbol_matcher)
=> #<ArgumentSpecification::Matchers::All:0x00000000000000 @expected=#<ArgumentSpecification::Matchers::BeA:0x00000000000000 @expected=Symbol>>


18
19
20
21
22
23
24
# File 'lib/argspec/matchers/all.rb', line 18

def initialize(expected)
  if expected.is_a?(BaseMatcher) == false || expected.is_a?(All)
    raise ArgumentError, 'You must provide a matcher as an argument (excluding the all matcher).'
  end

  @expected = expected
end

Instance Attribute Details

#expectedObject (readonly)

Returns the value of attribute expected.



6
7
8
# File 'lib/argspec/matchers/all.rb', line 6

def expected
  @expected
end

Instance Method Details

#failure_messageObject

The failure message when using ‘should’

Example:

>> matcher.failure_message
=> "'test' should be a 'Symbol'"


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/argspec/matchers/all.rb', line 32

def failure_message
  actual = prettify_args(@actual)
  matcher = prettify_matcher(@expected.[:name])

  if @expected.[:args].count > 0
    args = prettify_args(*@expected.[:args])

    "'#{actual}' should all #{matcher} '#{args}'"
  else
    "'#{actual}' should all #{matcher}"
  end
end

#failure_message_when_negatedObject

The failure message when using ‘should not’

Example:

>> matcher.failure_message_when_negated
=> "':test' should not be a 'Symbol'"


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/argspec/matchers/all.rb', line 51

def failure_message_when_negated
  actual = prettify_args(@actual)
  matcher = prettify_matcher(@expected.[:name])

  if @expected.[:args].count > 0
    args = prettify_args(*@expected.[:args])

    "'#{actual}' should not all #{matcher} '#{args}'"
  else
    "'#{actual}' should not all #{matcher}"
  end
end

#matches?Boolean

Check if the actual object matches

Example:

>> matcher.matches?
=> true

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/argspec/matchers/all.rb', line 70

def matches?
  actual = @actual.is_a?(Array) ? @actual : [@actual]
  actual.each do |value|
    @expected.send(:actual=, value)
    @expected.send(:block=, @block) if @block

    return false unless @expected.matches?
  end

  true
end