11 sub.instance_variable_set("@priority_initialized", true)
12 sub.instance_variable_set("@priority_table", {})
17 args.reverse_each do |mod|
19 next unless defined?(@priority_initialized)
20 mod.instance_methods(false).each do |name|
26 def method_added(name)
27 set_priority(name) if defined?(@priority_initialized)
30 def priority(name, *tests)
31 singleton_class = (class << self; self; end)
32 priority_check_method = priority_check_method_name(name)
33 unless singleton_class.private_method_defined?(priority_check_method)
34 raise ArgumentError, "unknown priority: #{name}"
37 @current_priority = name
40 set_priority(test, name)
45 def need_to_run?(test_name)
46 normalized_test_name = normalize_test_name(test_name)
47 priority = @priority_table[normalized_test_name]
48 return true unless priority
49 __send__(priority_check_method_name(priority), test_name)
53 def priority_check_method_name(priority_name)
54 "run_priority_#{priority_name}?"
57 def normalize_test_name(test_name)
58 "test_#{test_name.to_s.sub(/^test_/, '')}"
61 def set_priority(name, priority=@current_priority)
62 @priority_table[normalize_test_name(name)] = priority
65 def run_priority_must?(test_name)
69 def run_priority_important?(test_name)
73 def run_priority_high?(test_name)
77 def run_priority_normal?(test_name)
81 def run_priority_low?(test_name)
85 def run_priority_never?(test_name)
91 !previous_test_success? or self.class.need_to_run?(@method_name)
94 alias_method :original_run, :run
95 def run(result, &block)
96 original_run(result, &block)
99 FileUtils.touch(passed_file)
101 FileUtils.rm_f(passed_file)
106 def previous_test_success?
107 File.exist?(passed_file)
111 dir = File.join(File.dirname($0), ".test-result",
112 self.class.name, escaped_method_name)
113 dir = File.expand_path(dir)
114 FileUtils.mkdir_p(dir)
119 File.join(result_dir, "passed")
122 def escaped_method_name
123 @method_name.to_s.gsub(/[!?]$/) do |matched|
135 @@priority_mode = false
138 def priority_mode=(bool)
139 @@priority_mode = bool
143 alias_method :original_run, :run
144 def run(*args, &block)
145 priority_mode = @@priority_mode
147 @original_tests = @tests
150 original_run(*args, &block)
152 @tests = @original_tests if priority_mode
156 @tests = @tests.reject {|test| !test.need_to_run?}
166 alias_method :original_options, :options
168 opts = original_options
169 opts.on("--[no-]priority", "use priority mode") do |bool|
170 TestSuite.priority_mode = bool