waterlink / spec2.cr

Enhanced `spec` testing library for [Crystal](http://crystal-lang.org/).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Registering custom matchers

mosop opened this issue · comments

commented

Hi, I wrote a custom matcher for spec2. I tried to use the register_matcher macro, but, unfortunately, my matcher is not suited to that. Probably, that's because my matcher's initializer has named arguments and a block argument as:

def initialize(foo, bar = "default", baz = true, &block)
  # ...
end

So, I directly define my matcher into the Spec2::Matchers namespace for now.

Here is my proposal for the additional protocol to register custom matchers.

module Spec2::Matcher
  macro register(matcher)
    ::Spec2::Matchers << ::{{matcher.id}}
    module ::Spec2::Matchers
      include ::{{matcher.id}}
    end
  end
end
module DslNameMatcher
  def self.matcher_name
    "dsl_name"
  end

  def dsl_name(foo, bar = "bar", baz = true, &block)
    Matcher.new(foo, bar, baz, block)
  end

  class Matcher
    include Spec2::Matcher

    def initialize(foo, bar, baz, block)
      # ...
    end
  end
end

Spec2::Matcher.register DslNameMatcher

It's simple and accepts all argument patterns.

commented

let me close this issue because there is no reply. thanks.