Permalink
Browse files

Only correctly tagged features run when running with that tag is spec…

…ified
  • Loading branch information...
1 parent b295232 commit fc8db0bdc2b79163dd27146be8fdc4e3eabfb995 @elight committed Dec 5, 2010
View
@@ -8,8 +8,6 @@ require 'shoulda'
# Test::Unit::UI::VERBOSE
Rake::TestTask.new('test') do |t|
t.libs << 'test'
- src_files = Dir.glob('src/**/*.rb')
- src_files.each { |f| puts f; require f[0...-3] }
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
View
@@ -1,30 +1,47 @@
+require 'test/unit'
+
+require File.join(File.dirname(__FILE__), 'coulda', 'world')
+require File.join(File.dirname(__FILE__), 'coulda', 'feature')
+require File.join(File.dirname(__FILE__), 'coulda', 'scenario')
+require File.join(File.dirname(__FILE__), 'coulda', 'pending')
+require File.join(File.dirname(__FILE__), 'coulda', 'vendor', 'constantize')
+require File.join(File.dirname(__FILE__), 'coulda', 'vendor', 'underscore')
+require File.join(File.dirname(__FILE__), 'coulda', 'tasks')
+
module Coulda
SyntaxError = Class.new(StandardError)
-end
-
-require 'test/unit'
-require 'coulda/world'
-require 'coulda/feature'
-require 'coulda/scenario'
-require 'coulda/pending'
-require 'coulda/vendor/constantize'
-require 'coulda/vendor/underscore'
-require 'coulda/tasks'
+ def Tag(name)
+ @feature_tags ||= []
+ @feature_tags << name.to_s
+ end
-module Kernel
# Factory method for Test::Unit::TestCase subclasses
def Feature(name, opts = {}, &block)
+ unless @processed_cmd_line_args
+ @processed_cmd_line_args = true
+ process_command_line_tags
+ end
+
+ if @requested_tags && !@requested_tags.empty?
+ if @feature_tags.nil? || !@feature_tags.any? { |f_tag| @requested_tags.include? f_tag}
+ @feature_tags = nil
+ return
+ end
+ end
+ @feature_tags = nil
+
test_class = Class.new(opts[:testcase_class] || Coulda.default_testcase_class || Test::Unit::TestCase)
+ World.register_feature(test_class, name)
+
Coulda::assign_class_to_const test_class, name
test_class.class_eval &block if block_given?
test_class.assert_presence_of_intent
- World.register_feature(test_class, name)
+
+
test_class
end
-end
-module Coulda
def self.default_testcase_class=(klass)
unless klass.is_a?(Class) && klass.ancestors.include?(Test::Unit::TestCase)
raise Exception, "Can only provide a Test::Unit::TestCase"
@@ -44,6 +61,13 @@ def assign_class_to_const(test_class, potential_const)
titleized_underscored_name = base_name.super_custom_underscore.gsub(/\b('?[a-z])/) { $1.upcase }
Object.const_set(titleized_underscored_name, test_class)
end
+
+ def process_command_line_tags
+ tags = ARGV.inject([]) { |m, a| m << a if a =~ /^tags=/; m }
+ @requested_tags = tags.map { |t| t.split("=")[1].split(",") }.flatten
+ end
end
include ::Coulda
+
+
@@ -0,0 +1,17 @@
+require 'rake'
+
+namespace :coulda do
+ desc "Execute tagged tests only"
+ task :tagged_tests, :tag do |task, tag|
+ ARGV << "tags=#{tag["tag"]}"
+
+ $LOAD_PATH.unshift("test")
+
+ require 'test/unit'
+
+ test_files = Dir.glob(File.join('test', '**', '*_test.rb'))
+ test_files.each do |file|
+ require file
+ end
+ end
+end
View
@@ -0,0 +1,18 @@
+require 'rubygems'
+require 'rake'
+require 'rake/testtask'
+
+gem 'shoulda'
+require 'shoulda'
+
+require '../lib/coulda/tasks'
+
+# Test::Unit::UI::VERBOSE
+Rake::TestTask.new('test') do |t|
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Default: run tests.'
+task :default => 'test'
@@ -0,0 +1,39 @@
+require 'test_helper'
+
+Tag :tagged_feature
+Feature "Test tagged feature" do
+ Scenario "tagged_feature scenario" do
+ When "I raise" do
+ raise
+ end
+ end
+end
+
+Feature "Test tagged scenarios" do
+ 2.times do |i|
+ Tag :tagged_scenario
+ Scenario "tagged_scenario #{i}" do
+ When "I raise" do
+ raise
+ end
+ end
+ end
+
+ Scenario "untagged" do
+ When "I raise" do
+ raise
+ end
+ end
+end
+
+Tag :tagged_feature_with_tagged_scenarios
+Feature "Test tagged feature with differently tagged scenarios" do
+ 3.times do |i|
+ Tag :junk_tag
+ Scenario "tagged_feature_with_tagged_scenarios #{i}" do
+ When "I raise" do
+ raise
+ end
+ end
+ end
+end
@@ -0,0 +1,7 @@
+require 'test/unit'
+require 'rubygems' if RUBY_VERSION != "1.9.1"
+require 'shoulda'
+
+require File.join(File.dirname(__FILE__), "..", "..", "lib", "coulda")
+
+include Coulda
@@ -0,0 +1,42 @@
+require "test_helper"
+require "open3"
+
+Feature "Running only Features and Scenarios with a specific tag" do
+ in_order_to "focus on a subset of a problem"
+ as_a "developer using coulda"
+ i_want_to "run only the Scenarios that are relevant to the problem at hand"
+
+ Scenario "Tagged Feature" do
+ When "I execute Coulda requesting only a tagged feature run" do
+ @error = `cd tag_test; rake 'coulda:tagged_tests[tagged_feature]'`
+ end
+
+ Then "I should only execute the Scenarios in that tagged Feature" do
+ # I'm forcing all of the executed scenarios to fail so I can just check the output
+ assert_equal 1, @error.scan(/RuntimeError/).count
+ assert_equal 1, @error.scan(/Test_tagged_feature/).count
+ end
+ end
+
+ Scenario "Tagged Scenarios" do
+ When "I execute Coulda requesting only a tagged feature run" do
+ @error = `cd tag_test; rake 'coulda:tagged_tests[tagged_scenario]'`
+ end
+
+ Then "I should only execute the Scenarios matching that tag" do
+ assert_equal 2, @error.scan(/RuntimeError/).count
+ assert_equal 2, @error.scan(/Test_tagged_scenario/).count
+ end
+ end
+
+ Scenario "Tagged Scenarios should inherit tags from their container tagged Feature" do
+ When "I execute Coulda requesting only a tagged feature run" do
+ @error = `cd tag_test; rake 'coulda:tagged_tests[tagged_feature_with_tagged_scenarios]'`
+ end
+
+ Then "I should only execute the Scenarios matching that Feature's tag" do
+ assert_equal 3, @error.scan(/RuntimeError/).count
+ assert_equal 3, @error.scan(/Test_tagged_feature_with_tagged_scenarios/).count
+ end
+ end
+end

0 comments on commit fc8db0b

Please sign in to comment.