Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit aa6f7e8

Browse files
Lailson Bandeiradchelimsky
authored andcommitted
Added support for filtering with tags on CLI.
Simple support for tags, as described in http://github.com/rspec/rspec-core/issues/37. - Closes #37.
1 parent e0c18c3 commit aa6f7e8

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

features/command_line/tag.feature

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Feature: tag option
2+
3+
Use the --tag (or -t) option to filter the examples to be run by tag.
4+
5+
The tag can be a simple name or a name:value pair. In the first case,
6+
examples with :name => true will be filtered. In the second case, examples
7+
with :name => value will be filtered, where value is always a string.
8+
In both cases, name is converted to a symbol.
9+
10+
Tags can also be used to exclude examples by adding a ~ before the tag.
11+
For example ~tag will exclude all examples marked with :tag => true and
12+
~tag:value will exclude all examples marked with :tag => value.
13+
14+
To be compatible with the Cucumber syntax, tags can optionally start with
15+
a @, that will be ignored.
16+
17+
Background:
18+
Given a file named "tagged_spec.rb" with:
19+
"""
20+
describe "group with tagged specs" do
21+
it "example I'm working now", :focus => true do; end
22+
it "special example", :type => 'special' do; end
23+
it "slow example", :skip => true do; end
24+
it "ordinary example", :speed => 'slow' do; end
25+
it "untagged example" do; end
26+
end
27+
"""
28+
29+
Scenario: filter examples with non-existent tag
30+
When I run "rspec . --tag mytag"
31+
And the output should contain "0 examples, 0 failures"
32+
33+
Scenario: filter examples with a simple tag
34+
When I run "rspec . --tag focus"
35+
Then the output should contain "Run filtered using {:focus=>true}"
36+
And the output should contain "1 example, 0 failures"
37+
38+
Scenario: filter examples with a simple tag and @
39+
When I run "rspec . --tag @focus"
40+
Then the output should contain "Run filtered using {:focus=>true}"
41+
Then the output should contain "1 example, 0 failures"
42+
43+
Scenario: filter examples with a name:value tag
44+
When I run "rspec . --tag type:special"
45+
Then the output should contain:
46+
"""
47+
Run filtered using {:type=>"special"}
48+
"""
49+
And the output should contain "1 example, 0 failures"
50+
51+
Scenario: filter examples with a name:value tag and @
52+
When I run "rspec . --tag @type:special"
53+
Then the output should contain:
54+
"""
55+
Run filtered using {:type=>"special"}
56+
"""
57+
And the output should contain "1 example, 0 failures"
58+
59+
Scenario: exclude examples with a simple tag
60+
When I run "rspec . --tag ~skip"
61+
Then the output should contain "4 examples, 0 failures"
62+
63+
Scenario: exclude examples with a simple tag and @
64+
When I run "rspec . --tag ~@skip"
65+
Then the output should contain "4 examples, 0 failures"
66+
67+
Scenario: exclude examples with a name:value tag
68+
When I run "rspec . --tag ~speed:slow"
69+
Then the output should contain "4 examples, 0 failures"
70+
71+
Scenario: exclude examples with a name:value tag and @
72+
When I run "rspec . --tag ~@speed:slow"
73+
Then the output should contain "4 examples, 0 failures"
74+

lib/rspec/core/option_parser.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ def parser(options)
9999
parser.on('--autotest') do |o|
100100
options[:autotest] = true
101101
end
102+
103+
parser.on('-t', '--tag TAG[:VALUE]', 'Run examples with the specified tag',
104+
'To exclude examples, add ~ before the tag (e.g. ~slow)',
105+
'(TAG is always converted to a symbol)') do |tag|
106+
filter_type = tag.start_with?('~') ? :exclusion_filter : :filter
107+
108+
name,value = tag.gsub(/^(~@|~|@)/, '').split(':')
109+
name = name.to_sym
110+
value = true if value.nil?
111+
112+
options[filter_type] ||= {}
113+
options[filter_type][name] = value
114+
end
102115
end
103116
end
104117
end

0 commit comments

Comments
 (0)