Fix compiler warning due to missing function prototype.
[svn.git] / subversion / bindings / swig / ruby / test / test-unit-ext / priority.rb
blob5c9a2082b36edaad8566f74a7bcbeb447c9d797c
1 require "test/unit"
3 require "fileutils"
5 module Test
6   module Unit
7     class TestCase
8       class << self
9         def inherited(sub)
10           super
11           sub.instance_variable_set("@priority_initialized", true)
12           sub.instance_variable_set("@priority_table", {})
13           sub.priority :normal
14         end
16         def include(*args)
17           args.reverse_each do |mod|
18             super(mod)
19             next unless defined?(@priority_initialized)
20             mod.instance_methods(false).each do |name|
21               set_priority(name)
22             end
23           end
24         end
26         def method_added(name)
27           set_priority(name) if defined?(@priority_initialized)
28         end
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}"
35           end
36           if tests.empty?
37             @current_priority = name
38           else
39             tests.each do |test|
40               set_priority(test, name)
41             end
42           end
43         end
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)
50         end
52         private
53         def priority_check_method_name(priority_name)
54           "run_priority_#{priority_name}?"
55         end
57         def normalize_test_name(test_name)
58           "test_#{test_name.to_s.sub(/^test_/, '')}"
59         end
61         def set_priority(name, priority=@current_priority)
62           @priority_table[normalize_test_name(name)] = priority
63         end
65         def run_priority_must?(test_name)
66           true
67         end
69         def run_priority_important?(test_name)
70           rand > 0.1
71         end
73         def run_priority_high?(test_name)
74           rand > 0.3
75         end
77         def run_priority_normal?(test_name)
78           rand > 0.5
79         end
81         def run_priority_low?(test_name)
82           rand > 0.75
83         end
85         def run_priority_never?(test_name)
86           false
87         end
88       end
90       def need_to_run?
91         !previous_test_success? or self.class.need_to_run?(@method_name)
92       end
94       alias_method :original_run, :run
95       def run(result, &block)
96         original_run(result, &block)
97       ensure
98         if passed?
99           FileUtils.touch(passed_file)
100         else
101           FileUtils.rm_f(passed_file)
102         end
103       end
105       private
106       def previous_test_success?
107         File.exist?(passed_file)
108       end
110       def result_dir
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)
115         dir
116       end
118       def passed_file
119         File.join(result_dir, "passed")
120       end
122       def escaped_method_name
123         @method_name.to_s.gsub(/[!?]$/) do |matched|
124           case matched
125           when "!"
126             ".destructive"
127           when "?"
128             ".predicate"
129           end
130         end
131       end
132     end
134     class TestSuite
135       @@priority_mode = false
137       class << self
138         def priority_mode=(bool)
139           @@priority_mode = bool
140         end
141       end
143       alias_method :original_run, :run
144       def run(*args, &block)
145         priority_mode = @@priority_mode
146         if priority_mode
147           @original_tests = @tests
148           apply_priority
149         end
150         original_run(*args, &block)
151       ensure
152         @tests = @original_tests if priority_mode
153       end
155       def apply_priority
156         @tests = @tests.reject {|test| !test.need_to_run?}
157       end
159       def need_to_run?
160         apply_priority
161         !@tests.empty?
162       end
163     end
165     class AutoRunner
166       alias_method :original_options, :options
167       def options
168         opts = original_options
169         opts.on("--[no-]priority", "use priority mode") do |bool|
170           TestSuite.priority_mode = bool
171         end
172         opts
173       end
174     end
175   end