Skip to content

Instantly share code, notes, and snippets.

@rintaun
Last active December 16, 2015 08:49
Show Gist options
  • Save rintaun/5408685 to your computer and use it in GitHub Desktop.
Save rintaun/5408685 to your computer and use it in GitHub Desktop.
require 'rspec/core/example_group'
require 'rspec/core/metadata'
require 'rspec/core/formatters/progress_formatter'
class RealEnglishFormatter < RSpec::Core::Formatters::ProgressFormatter
module ::RSpec
module Core
class ExampleGroup
def self.context(*args, &example_group_block)
o = self.describe *args, &example_group_block
o.metadata[:example_group][:type] = :context
o
end
end
class Metadata < Hash
module GroupMetadataHash
def full_description
desc_parts = container_stack.reverse.collect { |a|
this_part = a[:description_args]
this_part = this_part.join(' ')
if a[:type] == :context
def this_part.after?; end
end
this_part
}
end
end
module MetadataHash
def build_description_from(context, *parts)
front = []
if context.is_a?(Array)
context.each do |this_part|
if this_part.respond_to? :after?
parts.push this_part
else
front.push this_part
end
end
end
front.reverse.each do |part|
parts.unshift part
end
return parts.join(' ')
end
end
end
end
end
end
describe SomeClass do
describe '.some_method' do
context 'when passed a string' do
it 'returns the string verbatim' do
expect(SomeClass.some_method 'blah').to eq('blah')
end
end
context 'when passed an array' do
it 'joins the things together' do
expect(SomeClass.some_method ['blah','blah']).to eq('blah blah')
end
end
end
end
__END__
(extra spaces below added for easier reading)
The descriptions for these end up being stringified as:
- SomeClass.some_method when passed a string returns the string verbatim
- SomeClass.some_method when passed an array joins the things together
But it would be much more natural for them to read:
- SomeClass.some_method returns the string verbatim when passed a string
- SomeClass.some_method joins the things together when passed an array
@rintaun
Copy link
Author

rintaun commented Apr 17, 2013

My problem is that I would like to package these as a text formatter, with the modifications only in place when it is set specifically as the active formatter. It's possible that I can do it all without modifying the RSpec core classes, but for now this seems to be the easiest way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment