Module: Matchy::MatcherBuilder

Included in:
CustomMatcher
Defined in:
lib/matchy/matcher_builder.rb

Defined Under Namespace

Classes: ChainedMessage

Instance Method Summary collapse

Instance Method Details

#build_matcher(matcher_name = nil, args = [], &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/matchy/matcher_builder.rb', line 5

def build_matcher(matcher_name=nil, args=[], &block)
  match_block = lambda do |actual, matcher|
    block.call(actual, matcher, args)
  end
  
  body = lambda do |klass|
    include Test::Unit::Assertions
    @matcher_name = matcher_name.to_s
    
    def self.matcher_name
      @matcher_name
    end

    attr_reader :matcher_name
    attr_accessor :positive_failure_message, :negative_failure_message, :chained_messages
    
    def initialize(match_block, test_case)
      @match_block, @test_case = match_block, test_case
      @matcher_name = self.class.matcher_name
    end

    def method_missing(id, *args, &block)
      (self.chained_messages ||= []) << ChainedMessage.new(id, args, block)
      self
    end

    def matches?(given)
      @positive_failure_message ||= "Matching with '#{matcher_name}' failed, although it should match."
      @negative_failure_message ||= "Matching with '#{matcher_name}' passed, although it should_not match."
      @match_block.call(given, self)
    end
    
    def fail!(which)
      @test_case.flunk(which ? failure_message : negative_failure_message)
    end

    def pass!(which)
      @test_case.assert true
    end
    
    alias_method :failure_message, :positive_failure_message
  end
  
  Class.new(&body).new(match_block, self)
end