class ArgumentSpecification::Matchers::BaseMatcher

Attributes

actual[R]
block[R]
metadata[R]

Public Class Methods

matcher_name(*names) click to toggle source

Add matcher names

Arguments:

names: (Splat)

Example:

>> class TestMatcher < BaseMatcher
>>   matcher_name :test_matcher
>> end
=> TestMatcher
# File lib/argspec/matchers/base_matcher.rb, line 16
def matcher_name(*names)
  names.each do |name|
    next unless name.is_a?(Symbol)

    DSL::Matchers.register(self, name)
  end
end

Public Instance Methods

failure_message() click to toggle source

The failure message when using ‘should’

Example:

>> matcher.failure_message
=> "'test' should be a 'Symbol'"
# File lib/argspec/matchers/base_matcher.rb, line 33
def failure_message
  actual = prettify_args(@actual)
  matcher = prettify_matcher(@metadata[:name])

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

    "'#{actual}' should #{matcher} '#{args}'"
  else
    "'#{actual}' should #{matcher}"
  end
end
failure_message_when_negated() click to toggle source

The failure message when using ‘should not’

Example:

>> matcher.failure_message_when_negated
=> "':test' should not be a 'Symbol'"
# File lib/argspec/matchers/base_matcher.rb, line 52
def failure_message_when_negated
  actual = prettify_args(@actual)
  matcher = prettify_matcher(@metadata[:name])

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

    "'#{actual}' should not #{matcher} '#{args}'"
  else
    "'#{actual}' should not #{matcher}"
  end
end
matches?() click to toggle source

Check if the actual object matches

You must override this method!

Example:

>> matcher.matches?
=> true
# File lib/argspec/matchers/base_matcher.rb, line 73
def matches?
  raise NotImplementedError, 'You must override the matches? method in your matcher.'
end

Private Instance Methods

actual=(actual) click to toggle source

Set the actual object

Arguments:

actual: (Object)

Example:

>> matcher.actual = :test
=> :test
# File lib/argspec/matchers/base_matcher.rb, line 101
def actual=(actual)
  @actual = actual
end
block=(block) click to toggle source

Set the block

Arguments:

block: (Proc)

Example:

>> matcher.block = Proc.new {}
=> #<Proc:0x00000000000000>
# File lib/argspec/matchers/base_matcher.rb, line 114
def block=(block)
  @block = block
end
prettify_args(*args) click to toggle source

Prettify arguments

Arguments:

args: (Splat)

Example:

>> prettify_args(:a)
=> ":a"
# File lib/argspec/matchers/base_matcher.rb, line 140
def prettify_args(*args)
  pretty_args = args.map do |argument|
    if argument == nil
      next 'nil'
    elsif argument.is_a?(Symbol)
      next ":#{argument}"
    else
      next argument
    end
  end

  return 'nil' if pretty_args.empty?
  return pretty_args.first if pretty_args.count == 1

  pretty_args
end
prettify_matcher(matcher) click to toggle source

Prettify a matcher

Arguments:

matcher: (String)

Example:

>> prettify_matcher(:be_a)
=> "be a"
# File lib/argspec/matchers/base_matcher.rb, line 127
def prettify_matcher(matcher)
  matcher.to_s.gsub('_', ' ')
end
setup(name, args) click to toggle source

Setup the matcher

Arguments:

name: (Symbol)
args: (Array)

Example:

>> matcher.setup(:test_matcher, [])
=> { name: :test_matcher, args: [] }
# File lib/argspec/matchers/base_matcher.rb, line 88
def setup(name, args)
  @metadata = { name: name, args: args }
end