Skip to content

Commit

Permalink
Added options hash to example group proxy.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Apr 29, 2009
1 parent 50ba934 commit a4b3aac
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
31 changes: 31 additions & 0 deletions examples/passing/options_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This demonstrates the use of the options hash to support custom reporting.
# To see the result, run this command from the project root:
#
# bin/spec --require examples/passing/options_formatter.rb examples/passing/options_example.rb \
# --format OptionsFormatter

require File.dirname(__FILE__) + '/spec_helper'

describe "this group will be reported", :report => true do
it "this example will be reported", :report => true do
# no-op
end

it "this example will not be reported", :report => false do
# no-op
end

it "this example will also not be reported", :foo => 'bar' do
# no-op
end

it "this example will also also not be reported" do
# no-op
end
end

describe "this group will not be reported", :report => false do
it "though this example will", :report => true do
# no-op
end
end
20 changes: 20 additions & 0 deletions examples/passing/options_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This is an example of how you can use a custom formatter to do custom
# reporting. This formatter will only report example groups and examples that
# have :report => true (or anything truthy) in the declaration. See
# options_example.rb in this directory.

require 'spec/runner/formatter/base_text_formatter'

class OptionsFormatter < Spec::Runner::Formatter::BaseTextFormatter
def example_started(proxy)
if proxy.options[:report]
puts proxy.description
end
end

def example_group_started(proxy)
if proxy.options[:report]
puts proxy.description
end
end
end
6 changes: 5 additions & 1 deletion lib/spec/example/example_group_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ def build_description_from(*args)
include Spec::Example::PredicateMatchers
include Spec::Example::ArgsAndOptions

attr_reader :options, :location
attr_reader :location

def options # :nodoc:
@options ||= {}
end

def inherited(klass) # :nodoc:
super
Expand Down
8 changes: 8 additions & 0 deletions lib/spec/example/example_group_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ def initialize(example_group) # :nodoc:
@examples = example_group.example_proxies
@location = example_group.location
@backtrace = example_group.location # deprecated - see the backtrace method below
@options = example_group.options.dup
@options.delete(:location)
@options.delete(:scope)
end

# Optional hash passed to the example group declaration. Note that RSpec uses
# this hash internally and reserves the keys :location and :scope for its own
# use (and removes them from this hash)
attr_reader :options

# This is the description passed to the <tt>describe()</tt> method or any
# of its aliases
attr_reader :description
Expand Down
7 changes: 3 additions & 4 deletions lib/spec/example/example_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ def initialize(description=nil, options={}, location=nil) # :nodoc:
@description, @options, @location = description, options, location
end

# Optional hash passed to the example declaration
attr_reader :options

# This is the docstring passed to the <tt>it()</tt> method or any
# of its aliases
attr_reader :description

# Internal use only - used to store options to pass to example
# when it is initialized
attr_reader :options # :nodoc:

# The file and line number at which the represented example
# was declared. This is extracted from <tt>caller</tt>, and is therefore
# formatted as an individual line in a backtrace.
Expand Down
24 changes: 24 additions & 0 deletions spec/spec/example/example_group_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ def proxy
end
end

describe "#options" do
it "provides the options passed to the example group declaration" do
group.stub!(:options => {:a => 'b'})
proxy.options.should == {:a => 'b'}
end

it "excludes :location" do
group.stub!(:options => {:location => 'b'})
proxy.options.should == {}
end

it "excludes :scope" do
group.stub!(:options => {:scope => 'b'})
proxy.options.should == {}
end

it "preserves the original hash" do
hash = {:a => 'b', :location => 'here', :scope => 'tiny'}
group.stub!(:options => hash)
proxy.options.should == {:a => 'b'}
hash.should == {:a => 'b', :location => 'here', :scope => 'tiny'}
end
end

end
end
end

0 comments on commit a4b3aac

Please sign in to comment.