Skip to content

Instantly share code, notes, and snippets.

@expectedbehavior
Created January 31, 2010 20:56
Show Gist options
  • Save expectedbehavior/291239 to your computer and use it in GitHub Desktop.
Save expectedbehavior/291239 to your computer and use it in GitHub Desktop.
A patch to allow tag-based testing
module ActiveSupport
module Testing
module Declarative
# test "verify something" do
# ...
# end
alias :tu_test_original :test
def test(name, tags = nil, &block)
if tag_option_specified?
raise "missing argument: -t [@tag1 [@tag2 ...]]" if parse_cli_tags_list.blank?
return if tags.blank?
cli_tags = parse_cli_tags_list
matching_tags = tag_intersection(cli_tags, tags)
return if matching_tags.blank?
end
tu_test_original(name, &block)
end
private
def tag_intersection(tags1, tags2)
cleaned_tags1 = tags1.map{|x| clean_tag x }
cleaned_tags2 = tags2.map{|x| clean_tag x }
cleaned_tags1 & cleaned_tags2
end
def parse_cli_tags_list
tag_cli_switch_used = determine_tag_cli_switch
unless tag_cli_switch_used.blank?
tag_start_index = ARGV.find_index(tag_cli_switch_used) + 1
next_option_index = ARGV[tag_start_index..-1].find_index{|x| x =~ /-/} || -1
tag_list = ARGV[tag_start_index..next_option_index]
else
[]
end
end
def tag_option_specified?
ARGV.include?('--tags') || ARGV.include?('-t')
end
private #helpers
def clean_tag(tag)
tag.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase.gsub(/^[:@]/,"")
end
def determine_tag_cli_switch
ARGV.include?('--tags') ? "--tags" : "-t" if tag_option_specified?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment