module Matchy::MatcherBuilder

Public Class Methods

matcher_name() click to toggle source
# File lib/matchy/matcher_builder.rb, line 14
def self.matcher_name
  @matcher_name
end
new(match_block, test_case) click to toggle source
# File lib/matchy/matcher_builder.rb, line 21
def initialize(match_block, test_case)
  @match_block, @test_case = match_block, test_case
  @matcher_name = self.class.matcher_name
end

Public Instance Methods

build_matcher(matcher_name=nil, args=[], &block) click to toggle source
# 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
fail!(which) click to toggle source
# File lib/matchy/matcher_builder.rb, line 37
def fail!(which)
  @test_case.flunk(which ? failure_message : negative_failure_message)
end
matches?(given) click to toggle source
# File lib/matchy/matcher_builder.rb, line 31
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
method_missing(id, *args, &block) click to toggle source
# File lib/matchy/matcher_builder.rb, line 26
def method_missing(id, *args, &block)
  (self.chained_messages ||= []) << ChainedMessage.new(id, args, block)
  self
end
pass!(which) click to toggle source
# File lib/matchy/matcher_builder.rb, line 41
def pass!(which)
  @test_case.assert true
end