Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added options hash to example group proxy.

  • Loading branch information...
commit a4b3aac62e940c50077bdeb7d9ad5ee11e02116d 1 parent 50ba934
@dchelimsky authored
View
31 examples/passing/options_example.rb
@@ -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
View
20 examples/passing/options_formatter.rb
@@ -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
View
6 lib/spec/example/example_group_methods.rb
@@ -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
View
8 lib/spec/example/example_group_proxy.rb
@@ -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
View
7 lib/spec/example/example_proxy.rb
@@ -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.
View
24 spec/spec/example/example_group_proxy_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.