Class: Mustard::DefaultMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mustard/matchers/default_matcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, *args) ⇒ DefaultMatcher

Returns a new instance of DefaultMatcher.



16
17
18
19
# File 'lib/mustard/matchers/default_matcher.rb', line 16

def initialize(subject, *args)
  @subject = subject
  @args = *args
end

Class Method Details

.subclass_for(name, behavior) ⇒ Object

This will genereate a subclass of DefaultMatcher to act as the matcher class. This way we can persist the name of the matcher and the block of behavior.



6
7
8
9
10
11
12
13
14
# File 'lib/mustard/matchers/default_matcher.rb', line 6

def self.subclass_for(name, behavior)
  klass = Class.new(self)
  class << klass
    attr_accessor :name, :behavior
  end
  klass.name = name
  klass.behavior = behavior
  klass
end

Instance Method Details

#failure_messageObject



25
26
27
# File 'lib/mustard/matchers/default_matcher.rb', line 25

def failure_message
  "expected #{@subject.inspect} to #{message_action}"
end

#match?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/mustard/matchers/default_matcher.rb', line 21

def match?
  @subject.instance_exec(*@args, &self.class.behavior)
end

#message_actionObject



33
34
35
36
37
# File 'lib/mustard/matchers/default_matcher.rb', line 33

def message_action
  message_action = self.class.name.to_s.tr("_", " ")
  message_action += " " + @args.map(&:inspect).join(", ") unless @args.empty?
  message_action
end

#negative_failure_messageObject



29
30
31
# File 'lib/mustard/matchers/default_matcher.rb', line 29

def negative_failure_message
  "expected #{@subject.inspect} to not #{message_action}"
end