Skip to content
Browse files

move matcher to spec/support/matchers.rb

  • Loading branch information...
1 parent e4083fd commit d1c348914234cc80b8d00c8a21a103e071e1e632 @dchelimsky dchelimsky committed Mar 14, 2010
Showing with 47 additions and 82 deletions.
  1. +19 −79 spec/rspec/core/formatters/base_formatter_spec.rb
  2. +23 −0 spec/support/matchers.rb
  3. +5 −3 specs.watchr
View
98 spec/rspec/core/formatters/base_formatter_spec.rb
@@ -1,104 +1,44 @@
-require 'spec_helper'
+require "spec_helper"
describe Rspec::Core::Formatters::BaseFormatter do
- before do
- @formatter = Rspec::Core::Formatters::BaseFormatter.new
- end
-
- class HaveInterfaceMatcher
- def initialize(method)
- @method = method
- end
-
- attr_reader :object
- attr_reader :method
-
- def matches?(object)
- @object = object
- object.respond_to?(@method)
- end
-
- def with(arity)
- WithArity.new(self, @method, arity)
- end
-
- class WithArity
- def initialize(matcher, method, arity)
- @have_matcher = matcher
- @method = method
- @arity = arity
- end
-
- def matches?(an_object)
- @have_matcher.matches?(an_object) && real_arity == @arity
- end
-
- def failure_message
- "#{@have_matcher} should have method :#{@method} with #{argument_arity}, but it had #{real_arity}"
- end
-
- def arguments
- self
- end
-
- alias_method :argument, :arguments
-
- private
-
- def real_arity
- @have_matcher.object.method(@method).arity
- end
-
- def argument_arity
- if @arity == 1
- "1 argument"
- else
- "#{@arity} arguments"
- end
- end
- end
- end
-
- def have_interface_for(method)
- HaveInterfaceMatcher.new(method)
- end
+ let(:formatter) { Rspec::Core::Formatters::BaseFormatter.new }
- it "should have start as an interface with one argument" do
- @formatter.should have_interface_for(:start).with(1).argument
+ it "has start as an interface with one argument" do
+ formatter.should have_interface_for(:start).with(1).argument
end
- it "should have add_example_group as an interface with one argument" do
- @formatter.should have_interface_for(:add_example_group).with(1).argument
+ it "has add_example_group as an interface with one argument" do
+ formatter.should have_interface_for(:add_example_group).with(1).argument
end
- it "should have example_finished as an interface with one argument" do
- @formatter.should have_interface_for(:example_finished).with(1).arguments
+ it "has example_finished as an interface with one argument" do
+ formatter.should have_interface_for(:example_finished).with(1).arguments
end
- it "should have start_dump as an interface with 1 arguments" do
- @formatter.should have_interface_for(:start_dump).with(1).arguments
+ it "has start_dump as an interface with 1 arguments" do
+ formatter.should have_interface_for(:start_dump).with(1).arguments
end
- it "should have dump_failures as an interface with no arguments" do
- @formatter.should have_interface_for(:dump_failures).with(0).arguments
+ it "has dump_failures as an interface with no arguments" do
+ formatter.should have_interface_for(:dump_failures).with(0).arguments
end
- it "should have dump_summary as an interface with zero arguments" do
- @formatter.should have_interface_for(:dump_summary).with(0).arguments
+ it "has dump_summary as an interface with zero arguments" do
+ formatter.should have_interface_for(:dump_summary).with(0).arguments
end
- it "should have dump_pending as an interface with zero arguments" do
- @formatter.should have_interface_for(:dump_pending).with(0).arguments
+ it "has dump_pending as an interface with zero arguments" do
+ formatter.should have_interface_for(:dump_pending).with(0).arguments
end
- it "should have close as an interface with zero arguments" do
- @formatter.should have_interface_for(:close).with(0).arguments
+ it "has close as an interface with zero arguments" do
+ formatter.should have_interface_for(:close).with(0).arguments
end
describe '#format_backtrace' do
- it "should display the full backtrace when the example is given the :full_backtrace => true option", :full_backtrace => true
+ it "displays the full backtrace when the example is given the :full_backtrace => true option", :full_backtrace => true
end
View
23 spec/support/matchers.rb
@@ -19,3 +19,26 @@ def prepare(autotest)
autotest
end
end
+
+Rspec::Matchers.define :have_interface_for do |method|
+ match do |object|
+ @method = method
+ @object = object
+ object.respond_to?(method) && actual_arity == @expected_arity
+ end
+
+ chain :with do |arity|
+ @expected_arity = arity
+ end
+
+ chain(:argument) {}
+ chain(:arguments) {}
+
+ failure_message_for_should do
+ "#{@object} should have method :#{@method} with #{@expected_arity} argument(s), but it had #{actual_arity}"
+ end
+
+ def actual_arity
+ @object.method(@method).arity
+ end
+end
View
8 specs.watchr
@@ -31,9 +31,11 @@ end
# --------------------------------------------------
# Watchr Rules
# --------------------------------------------------
-watch('^spec/(.*)_spec\.rb' ) { |m| run_test_matching(m[1]) }
-watch('^lib/(.*)\.rb' ) { |m| run_test_matching(m[1]) }
-watch('^spec/spec_helper\.rb') { run_all_tests }
+watch('^spec/(.*)_spec\.rb') { |m| run_test_matching(m[1]) }
+watch('^lib/(.*)\.rb') { |m| run_test_matching(m[1]) }
+watch('^spec/spec_helper\.rb') { run_all_tests }
+watch('^spec/support/.*\.rb') { run_all_tests }
+
# --------------------------------------------------
# Signal Handling
# --------------------------------------------------

0 comments on commit d1c3489

Please sign in to comment.
Something went wrong with that request. Please try again.