Permalink
Browse files

Add format_docstrings option to configuration.

  • Loading branch information...
1 parent 45f23ab commit 4fd3882d7e667a0c876bc6a412fa85fc70a6d314 @alex-tan alex-tan committed with myronmarston Sep 30, 2012
@@ -858,6 +858,25 @@ def group_ordering_block
@group_ordering_block ||= DEFAULT_ORDERING
end
+ # @private
+ DEFAULT_FORMATTER = lambda { |string| string }
+
+ # Formats the docstring output using the block provided.
+ #
+ # @example
+ # # This will strip the descriptions of both examples and example groups.
+ # RSpec.configure do |config|
+ # config.format_docstrings { |s| s.strip }
+ # end
+ def format_docstrings(&block)
+ @format_docstrings_block = block_given? ? block : DEFAULT_FORMATTER
+ end
+
+ # @private
+ def format_docstrings_block
+ @format_docstrings_block ||= DEFAULT_FORMATTER
+ end
+
# Sets a strategy by which to order groups and examples.
#
# @example
@@ -49,7 +49,8 @@ def self.delegate_to_metadata(*keys)
# there is one, otherwise returns a message including the location of the
# example.
def description
- metadata[:description].to_s.empty? ? "example at #{location}" : metadata[:description]
+ description = metadata[:description].to_s.empty? ? "example at #{location}" : metadata[:description]
+ RSpec.configuration.format_docstrings_block.call(description)
end
# @attr_reader
@@ -44,7 +44,12 @@ def self.delegate_to_metadata(*names)
end
end
- delegate_to_metadata :description, :described_class, :file_path
+ def description
+ description = metadata[:example_group][:description]
+ RSpec.configuration.format_docstrings_block.call(description)
+ end
+
+ delegate_to_metadata :described_class, :file_path
alias_method :display_name, :description
# @private
alias_method :describes, :described_class
@@ -19,6 +19,14 @@ def metadata_hash(*args)
end
end
+ context "when RSpec.configuration.format_docstrings is set to a block" do
+ it "formats the description with that block" do
+ RSpec.configuration.format_docstrings { |s| s.upcase }
+ group = ExampleGroup.describe(' an example ')
+ group.description.should eq(' AN EXAMPLE ')
+ end
+ end
+
context 'when RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values is set to false' do
before(:each) do
RSpec.configure { |c| c.treat_symbols_as_metadata_keys_with_true_values = false }
@@ -45,6 +45,17 @@ def metadata_hash(*args)
end
end
+ describe "when there is an explicit description" do
+ context "when RSpec.configuration.format_docstrings is set to a block" do
+ it "formats the description using the block" do
+ RSpec.configuration.format_docstrings { |s| s.strip }
+ example = example_group.example(' an example with whitespace ') {}
+ example_group.run
+ example.description.should eql('an example with whitespace')
+ end
+ end
+ end
+
describe "when there is no explicit description" do
def expect_with(*frameworks)
RSpec.configuration.stub(:expecting_with_rspec?).and_return(frameworks.include?(:rspec))
@@ -58,6 +69,16 @@ def assert(val)
end
end
+ context "when RSpec.configuration.format_docstrings is set to a block" do
+ it "formats the description using the block" do
+ RSpec.configuration.format_docstrings { |s| s.upcase }
+ example_group.example { 5.should eq(5) }
+ example_group.run
+ pattern = /EXAMPLE AT #{relative_path(__FILE__).upcase}:#{__LINE__ - 2}/
+ example_group.examples.first.description.should match(pattern)
+ end
+ end
+
context "when `expect_with :rspec` is configured" do
before(:each) { expect_with :rspec }

0 comments on commit 4fd3882

Please sign in to comment.