Permalink
Browse files

support -e and --example options on CLI

  • Loading branch information...
1 parent 3388507 commit c6a5f1a5283c46ae728faa261b69119241707c7b @dchelimsky dchelimsky committed Jan 31, 2010
@@ -0,0 +1,44 @@
+Feature: example name option
+
+ Use the --example (or -e) option to filter the examples
+ to be run by name.
+
+ Background:
+ Given a file named "first_spec.rb" with:
+ """
+ describe "first group" do
+ it "first example in first group" do; end
+ it "second example in first group" do; end
+ end
+ """
+ Given a file named "second_spec.rb" with:
+ """
+ describe "second group" do
+ it "first example in second group" do; end
+ it "second example in second group" do; end
+ end
+ """
+
+ Scenario: no matches
+ When I run "spec . --example nothing_like_this"
+ Then the stdout should match "4 examples, 0 failures"
+
+ Scenario: all matches
+ When I run "spec . --example example"
+ Then the stdout should match "4 examples, 0 failures"
+
+ Scenario: one match in each file
+ When I run "spec . --example 'first example'"
+ Then the stdout should match "2 examples, 0 failures"
+
+ Scenario: one match in one file
+ When I run "spec . --example 'first example in first group'"
+ Then the stdout should match "1 example, 0 failures"
+
+ Scenario: one match in one file using regexp
+ When I run "spec . --example 'first .* first example'"
+ Then the stdout should match "1 example, 0 failures"
+
+ Scenario: one match in one file with group name
+ When I run "spec . --example 'second group first example'"
+ Then the stdout should match "1 example, 0 failures"
@@ -39,8 +39,8 @@ Feature: define matcher
And the stdout should match "should be a multiple of 3"
And the stdout should match "should not be a multiple of 4"
- And the stdout should match "should be a multiple of 4\n Failure"
- And the stdout should match "should not be a multiple of 3\n Failure"
+ And the stdout should match "Failure/Error: it {should be_a_multiple_of(4)}"
+ And the stdout should match "Failure/Error: it {should_not be_a_multiple_of(3)}"
And the stdout should match "4 examples, 2 failures"
And the stdout should match "expected 9 to be a multiple of 4"
@@ -33,6 +33,10 @@ def parse
options[:line_number] = o
end
+ opts.on('-e', '--example [FULL DESCRIPTION]', 'Run examples whose full descriptions match this pattern') do |o|
+ options[:full_description] = /#{o}/
+ end
+
opts.on('-p', '--profile', 'Enable profiling of examples with output of the top 10 slowest examples') do |o|
options[:profile_examples] = o
end
@@ -102,6 +102,10 @@ def color_enabled?
def line_number=(line_number)
filter_run :line_number => line_number.to_i
end
+
+ def full_description=(description)
+ filter_run :full_description => /#{description}/
+ end
# Enable profiling of example run - defaults to false
def profile_examples
@@ -162,7 +166,7 @@ def autorun!
end
def filter_run(options={})
- @filter = options unless @filter and @filter[:line_number]
+ @filter = options unless @filter and @filter[:line_number] || @filter[:full_description]
end
def run_all_when_everything_filtered?
@@ -125,7 +125,7 @@ def run(example_group_instance)
end
def inspect
- "#{@metadata[:behaviour][:name]} - #{@metadata[:description]}"
+ @metadata[:full_description]
end
def to_s
@@ -41,6 +41,7 @@ def for_example(description, options)
def configure_for_example(description, options)
store(:description, description.to_s)
+ store(:full_description, "#{self[:behaviour][:name]} #{self[:description]}")
store(:execution_result, {})
store(:caller, options.delete(:caller))
if self[:caller]
@@ -48,8 +48,15 @@ def options_from_args(*args)
options_from_args('-l','3').should include(:line_number => '3')
options_from_args('--line_number','3').should include(:line_number => '3')
end
+ end
+ describe "example" do
+ it "is parsed from --example or -e" do
+ options_from_args('--example','foo').should include(:full_description => /foo/)
+ options_from_args('-e','bar').should include(:full_description => /bar/)
+ end
end
+
describe "files_or_directories_to_run" do
it "parses files from '-c file.rb dir/file.rb'" do
@@ -111,6 +111,15 @@
end
+ context "with full_description" do
+
+ it "assigns the example name as the filter on description" do
+ @config.full_description = "foo"
+ @config.filter.should == {:full_description => /foo/}
+ end
+
+ end
+
end
describe "include" do
@@ -5,11 +5,11 @@
before do
behaviour = stub('behaviour',
:metadata => Rspec::Core::Metadata.new.process(
- 'behaviour_name',
+ 'group description',
:caller => ['foo_spec.rb:37']
)
).as_null_object
- @example = Rspec::Core::Example.new(behaviour, 'description', {}, (lambda {}))
+ @example = Rspec::Core::Example.new(behaviour, 'example description', {}, (lambda {}))
end
describe "attr readers" do
@@ -31,8 +31,8 @@
end
describe '#inspect' do
- it "should return 'behaviour_name - description'" do
- @example.inspect.should == 'behaviour_name - description'
+ it "should return 'group description - description'" do
+ @example.inspect.should == 'group description example description'
end
end
@@ -3,7 +3,7 @@
module Rspec
module Core
describe Metadata do
- describe "#generated_name" do
+ describe "[:behaviour][:name]" do
it "generates name for top level example group" do
m = Metadata.new
m.process("description", :caller => caller(0))
@@ -29,6 +29,16 @@ module Core
end
end
+ describe "[:full_description]" do
+ it "concats the behaviour name and description" do
+ m = Metadata.new
+ m[:behaviour][:name] = "group"
+
+ m = m.for_example("example", {})
+ m[:full_description].should == "group example"
+ end
+ end
+
describe "#determine_file_path" do
it "finds the first spec file in the caller array" do
m = Metadata.new
@@ -68,11 +78,15 @@ module Core
describe "#metadata_for_example" do
let(:caller_for_example) { caller(0) }
let(:line_number) { __LINE__ - 1 }
- let(:metadata) { Metadata.new.process(:caller => caller(0)) }
- let(:mfe) { metadata.for_example("this description", {:caller => caller_for_example, :arbitrary => :options}) }
+ let(:metadata) { Metadata.new.process("group description", :caller => caller(0)) }
+ let(:mfe) { metadata.for_example("example description", {:caller => caller_for_example, :arbitrary => :options}) }
it "stores the description" do
- mfe[:description].should == "this description"
+ mfe[:description].should == "example description"
+ end
+
+ it "stores the full_description (group description + example description)" do
+ mfe[:full_description].should == "group description example description"
end
it "creates an empty execution result" do

0 comments on commit c6a5f1a

Please sign in to comment.