Permalink
Browse files

more simple matching refactoring

  • Loading branch information...
1 parent 8ec1fa1 commit b18a89629a069d3cf7b19442e07b22ae35a57318 @dchelimsky committed Jun 19, 2008
Showing with 36 additions and 30 deletions.
  1. +27 −29 lib/spec/matchers/simple_matcher.rb
  2. +9 −1 spec/spec/matchers/simple_matcher_spec.rb
@@ -1,12 +1,9 @@
module Spec
module Matchers
class SimpleMatcher
- attr_reader :description
-
def initialize(description, &match_block)
- @description = description
+ @messenger = Messenger.new(description)
@match_block = match_block
- @messenger = Messenger.new
end
def matches?(actual)
@@ -16,37 +13,38 @@ def matches?(actual)
@match_block.call(@actual)
end
- def failure_message()
- return @messenger.failure_message(@description, @actual)
+ def failure_message
+ @messenger.failure_message(@actual)
end
- def negative_failure_message()
- return @messenger.negative_failure_message(@description, @actual)
- end
- end
-
- def simple_matcher(message, &match_block)
- SimpleMatcher.new(message, &match_block)
- end
-
- class Messenger
- def failure_message=(message)
- @custom_message = message
- end
-
- def negative_failure_message=(message)
- @custom_negative_message = message
+ def negative_failure_message
+ @messenger.negative_failure_message(@actual)
end
- def failure_message(description, actual)
- @custom_message ? @custom_message :
- %[expected #{description.inspect} but got #{actual.inspect}]
+ def description
+ @messenger.description
end
-
- def negative_failure_message(description, actual)
- @custom_negative_message ? @custom_negative_message :
- %[expected not to get #{description.inspect}, but got #{actual.inspect}]
+
+ class Messenger
+ attr_accessor :description
+ attr_writer :failure_message, :negative_failure_message
+
+ def initialize(description)
+ @description = description
+ end
+
+ def failure_message(actual)
+ @failure_message || %[expected #{@description.inspect} but got #{actual.inspect}]
+ end
+
+ def negative_failure_message(actual)
+ @negative_failure_message || %[expected not to get #{@description.inspect}, but got #{actual.inspect}]
+ end
end
end
+
+ def simple_matcher(message, &match_block)
+ SimpleMatcher.new(message, &match_block)
+ end
end
end
@@ -28,7 +28,7 @@ module Matchers
end
end
- context "with arity of 2" do
+ describe "with arity of 2" do
it "should support a custom failure message" do
matcher = simple_matcher("thing") do |given, messenger|
messenger.failure_message = "custom message"
@@ -44,6 +44,14 @@ module Matchers
matcher.matches?("other")
matcher.negative_failure_message.should == "custom message"
end
+
+ it "should support a custom description" do
+ matcher = simple_matcher("thing") do |given, messenger|
+ messenger.description = "custom message"
+ end
+ matcher.matches?("description")
+ matcher.description.should == "custom message"
+ end
end
end

0 comments on commit b18a896

Please sign in to comment.