matcher dsl

View: New views
3 Messages — Rating Filter:   Alert me  

matcher dsl

by David Chelimsky-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey all,

rspec-1.2.0 introduced a new matcher DSL, which lets you define a
matcher like this:

      Spec::Matchers.create :be_a_multiple_of do |expected|
        match do |actual|
          actual % expected == 0
        end
      end

This defines a matcher that you can use in an example like this:

  25.should be_a_multiple_of(5)

It occurs to me that "Spec::Matchers.create" is not the most intuitive
entry point. We're not defining Matchers (plural), nor are we really
creating anything at the point of declaration.

So I'm thinking of changing it (still supporting the original for now)
to "Spec.matcher" or "Spec.define_matcher", or something similar.
Anybody have any suggestions? My only restriction for the moment is
that it should not be top level (i.e. define_matcher with no scoping).

Thanks,
David
_______________________________________________
rspec-devel mailing list
rspec-devel@...
http://rubyforge.org/mailman/listinfo/rspec-devel

Re: matcher dsl

by Scott Taylor-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

David Chelimsky wrote:

> Hey all,
>
> rspec-1.2.0 introduced a new matcher DSL, which lets you define a
> matcher like this:
>
>       Spec::Matchers.create :be_a_multiple_of do |expected|
>         match do |actual|
>           actual % expected == 0
>         end
>       end
>  

Spec::Matcher.define or Spec::Matcher.create is fine with me.

It would only take "aliasing" the constant:

module Spec
  module Matchers
    ...
  end
 
  Matcher = Matchers
end

Scott

> This defines a matcher that you can use in an example like this:
>
>   25.should be_a_multiple_of(5)
>
> It occurs to me that "Spec::Matchers.create" is not the most intuitive
> entry point. We're not defining Matchers (plural), nor are we really
> creating anything at the point of declaration.
>
> So I'm thinking of changing it (still supporting the original for now)
> to "Spec.matcher" or "Spec.define_matcher", or something similar.
> Anybody have any suggestions? My only restriction for the moment is
> that it should not be top level (i.e. define_matcher with no scoping).
>
> Thanks,
> David
> _______________________________________________
> rspec-devel mailing list
> rspec-devel@...
> http://rubyforge.org/mailman/listinfo/rspec-devel
>  

_______________________________________________
rspec-devel mailing list
rspec-devel@...
http://rubyforge.org/mailman/listinfo/rspec-devel

Re: matcher dsl

by Zach Dennis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Mar 29, 2009 at 1:16 PM, David Chelimsky <dchelimsky@...> wrote:

> Hey all,
>
> rspec-1.2.0 introduced a new matcher DSL, which lets you define a
> matcher like this:
>
>      Spec::Matchers.create :be_a_multiple_of do |expected|
>        match do |actual|
>          actual % expected == 0
>        end
>      end
>
> This defines a matcher that you can use in an example like this:
>
>  25.should be_a_multiple_of(5)
>
> It occurs to me that "Spec::Matchers.create" is not the most intuitive
> entry point. We're not defining Matchers (plural), nor are we really
> creating anything at the point of declaration.
>
> So I'm thinking of changing it (still supporting the original for now)
> to "Spec.matcher" or "Spec.define_matcher", or something similar.
> Anybody have any suggestions? My only restriction for the moment is
> that it should not be top level (i.e. define_matcher with no scoping).

I don't like the idea of top-level either. Personally I prefer
Spec::Matcher.define over Spec.define_matcher, but Spec.define_matcher
is better than Spec.matcher or Spec::Matchers.create.

It would be nice if you could do multiple matchers in one-swipe, only
having to rely on the namespace once. For example, I have a
matchers.rb file which houses the misc simple matchers for a project.
It'd be nice to say something like:

Spec::Matchers.define do
  define :be_a_multiple_of do
     # ..
  end

  define :be_something_else do
    # ...
  end

  #...
end

I don't know how other folks organize their simple matchers though,

>
> Thanks,
> David
> _______________________________________________
> rspec-devel mailing list
> rspec-devel@...
> http://rubyforge.org/mailman/listinfo/rspec-devel
>



--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
_______________________________________________
rspec-devel mailing list
rspec-devel@...
http://rubyforge.org/mailman/listinfo/rspec-devel