Sha256: 56b211e54f7602fcbf0f9a94bf90689cd149c55e3a9f0588847391fbe4171c40

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module Matchy
  module MatcherBuilder
    class ChainedMessage < Struct.new(:name, :args, :block); end
    
    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 Matchy.assertions_module
        @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
        
        alias_method :failure_message, :positive_failure_message
      end
      
      Class.new(&body).new(match_block, self)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mcmire-matchy-0.5.2 lib/matchy/matcher_builder.rb
mcmire-matchy-0.5.1 lib/matchy/matcher_builder.rb