Browse files

Split up docstrings method

  • Loading branch information...
1 parent a422545 commit 5ecb796bd3fb53e29e25be06ddf905f715d5563f @rentalcustard committed May 10, 2011
View
8 features/custom_matchers/define_matcher.feature
@@ -103,9 +103,11 @@ Feature: define matcher
match do |actual|
actual % expected == 0
end
- docstrings do
- {:positive => "is multiple of #{expected}",
- :negative => "is not multiple of #{expected}"}
+ docstring_for_should do
+ "is multiple of #{expected}"
+ end
+ docstring_for_should_not do
+ "is not multiple of #{expected}"
end
end
View
8 lib/rspec/matchers.rb
@@ -94,9 +94,11 @@ module RSpec
# description do
# # generate and return the appropriate string.
# end
- # docstrings do
- # # generate and return appropriate positive and negative
- # # strings as a hash
+ # docstring_for_should do
+ # # generate and return the appropriate string.
+ # end
+ # docstring_for_should_not do
+ # # generate and return the appropriate string.
# end
# end
#
View
27 lib/rspec/matchers/be.rb
@@ -53,9 +53,12 @@ def description
"be"
end
- def docstrings
- {:positive => "is",
- :negative => "is not"}
+ def docstring_for_should
+ "is"
+ end
+
+ def docstring_for_should_not
+ "is not"
end
[:==, :<, :<=, :>=, :>, :===].each do |operator|
@@ -119,9 +122,12 @@ def description
"be #{@operator} #{expected_to_sentence}#{args_to_sentence}"
end
- def docstrings
- {:positive => "is #{@operator} #{expected_to_sentence}#{args_to_sentence}",
- :negative => "is not #{@operator} #{expected_to_sentence}#{args_to_sentence}"}
+ def docstring_for_should
+ "is #{@operator} #{expected_to_sentence}#{args_to_sentence}"
+ end
+
+ def docstring_for_should_not
+ "is not #{@operator} #{expected_to_sentence}#{args_to_sentence}"
end
end
@@ -161,9 +167,12 @@ def description
"#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}"
end
- def docstrings
- {:positive => "is #{expected_to_sentence}#{args_to_sentence}",
- :negative => "is not #{expected_to_sentence}#{args_to_sentence}"}
+ def docstring_for_should
+ "is #{expected_to_sentence}#{args_to_sentence}"
+ end
+
+ def docstring_for_should_not
+ "is not #{expected_to_sentence}#{args_to_sentence}"
end
private
View
4 lib/rspec/matchers/eq.rb
@@ -44,8 +44,8 @@ def eq(expected)
"== #{_expected_}"
end
- docstrings do
- {:positive => "== #{_expected_}"}
+ docstring_for_should do
+ "== #{_expected_}"
end
end
end
View
9 lib/rspec/matchers/equal.rb
@@ -48,9 +48,12 @@ def inspect_object(o)
MESSAGE
end
- docstrings do
- {:positive => "equals #{_expected_.inspect}",
- :negative => "does not equal #{_expected_.inspect}"}
+ docstring_for_should do
+ "equals #{_expected_.inspect}"
+ end
+
+ docstring_for_should_not do
+ "does not equal #{_expected_.inspect}"
end
end
end
View
20 lib/rspec/matchers/generated_descriptions.rb
@@ -16,14 +16,20 @@ def self.generated_description
end
private
-
+
+ def self.docstring(negative=false)
+ if negative && last_matcher.respond_to?(:docstring_for_should_not)
+ last_matcher.docstring_for_should_not
+ elsif last_matcher.respond_to?(:docstring_for_should)
+ last_matcher.docstring_for_should
+ else
+ nil
+ end
+ end
+
def self.last_description(negative=false)
- if last_matcher.respond_to?(:docstrings)
- if negative
- last_matcher.docstrings[:negative]
- else
- last_matcher.docstrings[:positive]
- end
+ if doc = docstring(negative)
+ doc
else
last_matcher.respond_to?(:description) ? "#{last_should.to_s.sub("_", " ")} #{last_matcher.description}" : <<-MESSAGE
When you call a matcher in an example without a String, like this:
View
9 lib/rspec/matchers/has.rb
@@ -21,9 +21,12 @@ def description
[method_description(@expected), args_description(@args)].compact.join(' ')
end
- def docstrings
- {:positive => description.sub(/have/, "has"),
- :negative => "not #{description}"}
+ def docstring_for_should
+ description.sub(/have/, "has")
+ end
+
+ def docstring_for_should_not
+ "not #{description}"
end
private
View
9 lib/rspec/matchers/have.rb
@@ -67,9 +67,12 @@ def description
"have #{relative_expectation} #{@collection_name}"
end
- def docstrings
- {:positive => "has #{relative_expectation} #{@collection_name}",
- :negative => "does not have #{relative_expectation} #{@collection_name}"}
+ def docstring_for_should
+ "has #{relative_expectation} #{@collection_name}"
+ end
+
+ def docstring_for_should_not
+ "does not have #{relative_expectation} #{@collection_name}"
end
def respond_to?(sym)
View
4 lib/rspec/matchers/match_array.rb
@@ -31,8 +31,8 @@ def description
"contain exactly #{_pretty_print(@expected)}"
end
- def docstrings
- {:positive => "contains exactly #{_pretty_print(@expected)}"}
+ def docstring_for_should
+ "contains exactly #{_pretty_print(@expected)}"
end
private
View
13 lib/rspec/matchers/matcher.rb
@@ -15,8 +15,8 @@ def initialize(name, *expected, &declarations)
@match_for_should_not_block = nil
@messages = {
- :docstrings => lambda { {:positive => "#{name_to_indicative_sentence}#{expected_to_sentence}",
- :negative => "#{name_to_indicative_sentence(true)}#{expected_to_sentence}"} },
+ :docstring_for_should => lambda { "#{name_to_indicative_sentence}#{expected_to_sentence}" },
+ :docstring_for_should_not => lambda { "#{name_to_indicative_sentence(true)}#{expected_to_sentence}" },
:description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
:failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
:failure_message_for_should_not => lambda {|actual| "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"}
@@ -94,8 +94,13 @@ def description(&block)
end
# See RSpec::Matchers
- def docstrings(&block)
- cache_or_call_cached(:docstrings, &block)
+ def docstring_for_should(&block)
+ cache_or_call_cached(:docstring_for_should, &block)
+ end
+
+ # See RSpec::Matchers
+ def docstring_for_should_not(&block)
+ cache_or_call_cached(:docstring_for_should_not, &block)
end
#Used internally by objects returns by +should+ and +should_not+.
View
9 lib/rspec/matchers/operator_matcher.rb
@@ -52,9 +52,12 @@ def description
"#{@operator} #{@expected.inspect}"
end
- def docstrings
- {:positive => description,
- :negative => "does not #{description}"}
+ def docstring_for_should
+ description
+ end
+
+ def docstring_for_should_not
+ "does not #{description}"
end
private
View
9 lib/rspec/matchers/raise_error.rb
@@ -68,9 +68,12 @@ def description
"raise #{expected_error}"
end
- def docstrings
- {:positive => "raises #{expected_error}",
- :negative => "does not raise #{expected_error}"}
+ def docstring_for_should
+ "raises #{expected_error}"
+ end
+
+ def docstring_for_should_not
+ "does not raise #{expected_error}"
end
private
View
9 lib/rspec/matchers/respond_to.rb
@@ -27,9 +27,12 @@ def description
"respond to #{pp_names}#{with_arity}"
end
- def docstrings
- {:positive => "responds to #{pp_names}#{with_arity}",
- :negative => "does not respond to #{pp_names}#{with_arity}"}
+ def docstring_for_should
+ "responds to #{pp_names}#{with_arity}"
+ end
+
+ def docstring_for_should_not
+ "does not respond to #{pp_names}#{with_arity}"
end
def with(n)
View
9 lib/rspec/matchers/throw_symbol.rb
@@ -64,9 +64,12 @@ def description
"throw #{expected}"
end
- def docstrings
- {:positive => "throws #{expected}",
- :negative => "does not throw #{expected}"}
+ def docstring_for_should
+ "throws #{expected}"
+ end
+
+ def docstring_for_should_not
+ "does not throw #{expected}"
end
private

0 comments on commit 5ecb796

Please sign in to comment.