Permalink
Browse files

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.
  • Loading branch information...
lailsonbm authored and dchelimsky committed Oct 30, 2010
1 parent e0c18c3 commit aa6f7e8744abb4bdc2060fb32a58bd2aeea04e14
Showing with 87 additions and 0 deletions.
  1. +74 −0 features/command_line/tag.feature
  2. +13 −0 lib/rspec/core/option_parser.rb
@@ -0,0 +1,74 @@
+Feature: tag option
+
+ Use the --tag (or -t) option to filter the examples to be run by tag.
+
+ The tag can be a simple name or a name:value pair. In the first case,
+ examples with :name => true will be filtered. In the second case, examples
+ with :name => value will be filtered, where value is always a string.
+ In both cases, name is converted to a symbol.
+
+ Tags can also be used to exclude examples by adding a ~ before the tag.
+ For example ~tag will exclude all examples marked with :tag => true and
+ ~tag:value will exclude all examples marked with :tag => value.
+
+ To be compatible with the Cucumber syntax, tags can optionally start with
+ a @, that will be ignored.
+
+ Background:
+ Given a file named "tagged_spec.rb" with:
+ """
+ describe "group with tagged specs" do
+ it "example I'm working now", :focus => true do; end
+ it "special example", :type => 'special' do; end
+ it "slow example", :skip => true do; end
+ it "ordinary example", :speed => 'slow' do; end
+ it "untagged example" do; end
+ end
+ """
+
+ Scenario: filter examples with non-existent tag
+ When I run "rspec . --tag mytag"
+ And the output should contain "0 examples, 0 failures"
+
+ Scenario: filter examples with a simple tag
+ When I run "rspec . --tag focus"
+ Then the output should contain "Run filtered using {:focus=>true}"
+ And the output should contain "1 example, 0 failures"
+
+ Scenario: filter examples with a simple tag and @
+ When I run "rspec . --tag @focus"
+ Then the output should contain "Run filtered using {:focus=>true}"
+ Then the output should contain "1 example, 0 failures"
+
+ Scenario: filter examples with a name:value tag
+ When I run "rspec . --tag type:special"
+ Then the output should contain:
+ """
+ Run filtered using {:type=>"special"}
+ """
+ And the output should contain "1 example, 0 failures"
+
+ Scenario: filter examples with a name:value tag and @
+ When I run "rspec . --tag @type:special"
+ Then the output should contain:
+ """
+ Run filtered using {:type=>"special"}
+ """
+ And the output should contain "1 example, 0 failures"
+
+ Scenario: exclude examples with a simple tag
+ When I run "rspec . --tag ~skip"
+ Then the output should contain "4 examples, 0 failures"
+
+ Scenario: exclude examples with a simple tag and @
+ When I run "rspec . --tag ~@skip"
+ Then the output should contain "4 examples, 0 failures"
+
+ Scenario: exclude examples with a name:value tag
+ When I run "rspec . --tag ~speed:slow"
+ Then the output should contain "4 examples, 0 failures"
+
+ Scenario: exclude examples with a name:value tag and @
+ When I run "rspec . --tag ~@speed:slow"
+ Then the output should contain "4 examples, 0 failures"
+
@@ -99,6 +99,19 @@ def parser(options)
parser.on('--autotest') do |o|
options[:autotest] = true
end
+
+ parser.on('-t', '--tag TAG[:VALUE]', 'Run examples with the specified tag',
+ 'To exclude examples, add ~ before the tag (e.g. ~slow)',
+ '(TAG is always converted to a symbol)') do |tag|
+ filter_type = tag.start_with?('~') ? :exclusion_filter : :filter
+
+ name,value = tag.gsub(/^(~@|~|@)/, '').split(':')
+ name = name.to_sym
+ value = true if value.nil?
+
+ options[filter_type] ||= {}
+ options[filter_type][name] = value
+ end
end
end
end

0 comments on commit aa6f7e8

Please sign in to comment.