test-unit 1.2.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/History.txt +27 -0
  2. data/Manifest.txt +30 -8
  3. data/README.txt +9 -4
  4. data/Rakefile +16 -1
  5. data/bin/testrb +0 -0
  6. data/lib/test/unit/assertions.rb +148 -48
  7. data/lib/test/unit/attribute.rb +125 -0
  8. data/lib/test/unit/autorunner.rb +101 -71
  9. data/lib/test/unit/collector/descendant.rb +23 -0
  10. data/lib/test/unit/collector/dir.rb +1 -1
  11. data/lib/test/unit/collector/load.rb +135 -0
  12. data/lib/test/unit/color.rb +61 -0
  13. data/lib/test/unit/diff.rb +524 -0
  14. data/lib/test/unit/error.rb +70 -2
  15. data/lib/test/unit/exceptionhandler.rb +39 -0
  16. data/lib/test/unit/failure.rb +63 -4
  17. data/lib/test/unit/fixture.rb +185 -0
  18. data/lib/test/unit/notification.rb +125 -0
  19. data/lib/test/unit/omission.rb +143 -0
  20. data/lib/test/unit/pending.rb +146 -0
  21. data/lib/test/unit/priority.rb +146 -0
  22. data/lib/test/unit/runner/console.rb +46 -0
  23. data/lib/test/unit/runner/emacs.rb +8 -0
  24. data/lib/test/unit/testcase.rb +193 -76
  25. data/lib/test/unit/testresult.rb +37 -28
  26. data/lib/test/unit/testsuite.rb +35 -1
  27. data/lib/test/unit/ui/console/outputlevel.rb +14 -0
  28. data/lib/test/unit/ui/console/testrunner.rb +96 -28
  29. data/lib/test/unit/ui/emacs/testrunner.rb +49 -0
  30. data/lib/test/unit/ui/testrunner.rb +20 -0
  31. data/lib/test/unit/ui/testrunnermediator.rb +28 -19
  32. data/lib/test/unit/ui/testrunnerutilities.rb +2 -7
  33. data/lib/test/unit/util/backtracefilter.rb +2 -1
  34. data/lib/test/unit/version.rb +1 -1
  35. data/test/collector/test_descendant.rb +135 -0
  36. data/test/collector/test_load.rb +333 -0
  37. data/test/run-test.rb +13 -0
  38. data/test/test_assertions.rb +221 -56
  39. data/test/test_attribute.rb +86 -0
  40. data/test/test_color.rb +37 -0
  41. data/test/test_diff.rb +477 -0
  42. data/test/test_emacs_runner.rb +60 -0
  43. data/test/test_fixture.rb +275 -0
  44. data/test/test_notification.rb +33 -0
  45. data/test/test_omission.rb +81 -0
  46. data/test/test_pending.rb +70 -0
  47. data/test/test_priority.rb +89 -0
  48. data/test/test_testcase.rb +160 -5
  49. data/test/test_testresult.rb +61 -52
  50. data/test/testunit_test_util.rb +14 -0
  51. data/test/ui/test_testrunmediator.rb +20 -0
  52. metadata +53 -23
  53. data/lib/test/unit/ui/fox/testrunner.rb +0 -268
  54. data/lib/test/unit/ui/gtk/testrunner.rb +0 -416
  55. data/lib/test/unit/ui/gtk2/testrunner.rb +0 -465
  56. data/lib/test/unit/ui/tk/testrunner.rb +0 -260
  57. data/test/runit/test_assert.rb +0 -402
  58. data/test/runit/test_testcase.rb +0 -91
  59. data/test/runit/test_testresult.rb +0 -144
  60. data/test/runit/test_testsuite.rb +0 -49
@@ -0,0 +1,143 @@
1
+ require 'test/unit/util/backtracefilter'
2
+
3
+ module Test
4
+ module Unit
5
+ class Omission
6
+ include Util::BacktraceFilter
7
+ attr_reader :test_name, :location, :message
8
+
9
+ SINGLE_CHARACTER = 'O'
10
+ LABEL = "Omission"
11
+
12
+ # Creates a new Omission with the given location and
13
+ # message.
14
+ def initialize(test_name, location, message)
15
+ @test_name = test_name
16
+ @location = location
17
+ @message = message
18
+ end
19
+
20
+ # Returns a single character representation of a omission.
21
+ def single_character_display
22
+ SINGLE_CHARACTER
23
+ end
24
+
25
+ def label
26
+ LABEL
27
+ end
28
+
29
+ # Returns a brief version of the error description.
30
+ def short_display
31
+ "#{@test_name}: #{@message.split("\n")[0]}"
32
+ end
33
+
34
+ # Returns a verbose version of the error description.
35
+ def long_display
36
+ backtrace = filter_backtrace(location).join("\n")
37
+ "#{label}: #{@message}\n#{@test_name}\n#{backtrace}"
38
+ end
39
+
40
+ # Overridden to return long_display.
41
+ def to_s
42
+ long_display
43
+ end
44
+ end
45
+
46
+ class OmittedError < StandardError
47
+ end
48
+
49
+
50
+ module TestCaseOmissionSupport
51
+ class << self
52
+ def included(base)
53
+ base.class_eval do
54
+ include OmissionHandler
55
+ end
56
+ end
57
+ end
58
+
59
+ # Omit the test of part of the test.
60
+ #
61
+ # Example:
62
+ # def test_omission
63
+ # omit
64
+ # # Not reached here
65
+ # end
66
+ #
67
+ # def test_omission_with_here
68
+ # omit do
69
+ # # Not ran here
70
+ # end
71
+ # # Reached here
72
+ # end
73
+ def omit(message=nil, &block)
74
+ message ||= "omitted."
75
+ if block_given?
76
+ omission = Omission.new(name, filter_backtrace(caller), message)
77
+ add_omission(omission)
78
+ else
79
+ raise OmittedError.new(message)
80
+ end
81
+ end
82
+
83
+ def omit_if(condition, *args, &block)
84
+ omit(*args, &block) if condition
85
+ end
86
+
87
+ def omit_unless(condition, *args, &block)
88
+ omit(*args, &block) unless condition
89
+ end
90
+
91
+ private
92
+ def add_omission(omission)
93
+ current_result.add_omission(omission)
94
+ end
95
+ end
96
+
97
+ module OmissionHandler
98
+ class << self
99
+ def included(base)
100
+ base.exception_handler(:handle_omitted_error)
101
+ end
102
+ end
103
+
104
+ private
105
+ def handle_omitted_error(exception)
106
+ return false unless exception.is_a?(OmittedError)
107
+ omission = Omission.new(name,
108
+ filter_backtrace(exception.backtrace),
109
+ exception.message)
110
+ add_omission(omission)
111
+ true
112
+ end
113
+ end
114
+
115
+ module TestResultOmissionSupport
116
+ attr_reader :omissions
117
+
118
+ # Records a Test::Unit::Omission.
119
+ def add_omission(omission)
120
+ @omissions << omission
121
+ notify_fault(omission)
122
+ notify_changed
123
+ end
124
+
125
+ # Returns the number of omissions this TestResult has
126
+ # recorded.
127
+ def omission_count
128
+ @omissions.size
129
+ end
130
+
131
+ private
132
+ def initialize_containers
133
+ super
134
+ @omissions = []
135
+ @summary_generators << :omission_summary
136
+ end
137
+
138
+ def omission_summary
139
+ "#{omission_count} omissions"
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,146 @@
1
+ require 'test/unit/util/backtracefilter'
2
+
3
+ module Test
4
+ module Unit
5
+ class Pending
6
+ include Util::BacktraceFilter
7
+ attr_reader :test_name, :location, :message
8
+
9
+ SINGLE_CHARACTER = 'P'
10
+ LABEL = "Pending"
11
+
12
+ # Creates a new Pending with the given location and
13
+ # message.
14
+ def initialize(test_name, location, message)
15
+ @test_name = test_name
16
+ @location = location
17
+ @message = message
18
+ end
19
+
20
+ # Returns a single character representation of a pending.
21
+ def single_character_display
22
+ SINGLE_CHARACTER
23
+ end
24
+
25
+ def label
26
+ LABEL
27
+ end
28
+
29
+ # Returns a brief version of the error description.
30
+ def short_display
31
+ "#{@test_name}: #{@message.split("\n")[0]}"
32
+ end
33
+
34
+ # Returns a verbose version of the error description.
35
+ def long_display
36
+ backtrace = filter_backtrace(location).join("\n")
37
+ "#{label}: #{@message}\n#{@test_name}\n#{backtrace}"
38
+ end
39
+
40
+ # Overridden to return long_display.
41
+ def to_s
42
+ long_display
43
+ end
44
+ end
45
+
46
+ class PendedError < StandardError
47
+ end
48
+
49
+
50
+ module TestCasePendingSupport
51
+ class << self
52
+ def included(base)
53
+ base.class_eval do
54
+ include PendingHandler
55
+ end
56
+ end
57
+ end
58
+
59
+ # Marks the test or part of the test is pending.
60
+ #
61
+ # Example:
62
+ # def test_pending
63
+ # pend
64
+ # # Not reached here
65
+ # end
66
+ #
67
+ # def test_pending_with_here
68
+ # pend do
69
+ # # Ran here
70
+ # # Fails if the block doesn't raise any error.
71
+ # # Because it means the block is passed unexpectedly.
72
+ # end
73
+ # # Reached here
74
+ # end
75
+ def pend(message=nil, &block)
76
+ message ||= "pended."
77
+ if block_given?
78
+ pending = nil
79
+ begin
80
+ yield
81
+ rescue Exception
82
+ pending = Pending.new(name, filter_backtrace(caller), message)
83
+ add_pending(pending)
84
+ end
85
+ unless pending
86
+ flunk("Pending block should not be passed: #{message}")
87
+ end
88
+ else
89
+ raise PendedError.new(message)
90
+ end
91
+ end
92
+
93
+ private
94
+ def add_pending(pending)
95
+ problem_occurred
96
+ current_result.add_pending(pending)
97
+ end
98
+ end
99
+
100
+ module PendingHandler
101
+ class << self
102
+ def included(base)
103
+ base.exception_handler(:handle_pended_error)
104
+ end
105
+ end
106
+
107
+ private
108
+ def handle_pended_error(exception)
109
+ return false unless exception.is_a?(PendedError)
110
+ pending = Pending.new(name,
111
+ filter_backtrace(exception.backtrace),
112
+ exception.message)
113
+ add_pending(pending)
114
+ true
115
+ end
116
+ end
117
+
118
+ module TestResultPendingSupport
119
+ attr_reader :pendings
120
+
121
+ # Records a Test::Unit::Pending.
122
+ def add_pending(pending)
123
+ @pendings << pending
124
+ notify_fault(pending)
125
+ notify_changed
126
+ end
127
+
128
+ # Returns the number of pendings this TestResult has
129
+ # recorded.
130
+ def pending_count
131
+ @pendings.size
132
+ end
133
+
134
+ private
135
+ def initialize_containers
136
+ super
137
+ @pendings = []
138
+ @summary_generators << :pending_summary
139
+ end
140
+
141
+ def pending_summary
142
+ "#{pending_count} pendings"
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,146 @@
1
+ require "fileutils"
2
+ require "tmpdir"
3
+
4
+ module Test
5
+ module Unit
6
+ module Priority
7
+ class << self
8
+ def included(base)
9
+ base.extend(ClassMethods)
10
+
11
+ base.class_eval do
12
+ setup :priority_setup, :before => :prepend
13
+ teardown :priority_teardown, :after => :append
14
+ end
15
+ end
16
+ end
17
+
18
+ class Checker
19
+ class << self
20
+ def have_priority?(name)
21
+ singleton_class = (class << self; self; end)
22
+ singleton_class.method_defined?(priority_check_method_name(name))
23
+ end
24
+
25
+ def need_to_run?(test)
26
+ priority = test[:priority] || :normal
27
+ if have_priority?(priority)
28
+ send(priority_check_method_name(priority), test)
29
+ else
30
+ true
31
+ end
32
+ end
33
+
34
+ def run_priority_must?(test)
35
+ true
36
+ end
37
+
38
+ def run_priority_important?(test)
39
+ rand > 0.1
40
+ end
41
+
42
+ def run_priority_high?(test)
43
+ rand > 0.3
44
+ end
45
+
46
+ def run_priority_normal?(test)
47
+ rand > 0.5
48
+ end
49
+
50
+ def run_priority_low?(test)
51
+ rand > 0.75
52
+ end
53
+
54
+ def run_priority_never?(test)
55
+ false
56
+ end
57
+
58
+ private
59
+ def priority_check_method_name(priority_name)
60
+ "run_priority_#{priority_name}?"
61
+ end
62
+ end
63
+
64
+ attr_reader :test
65
+ def initialize(test)
66
+ @test = test
67
+ end
68
+
69
+ def setup
70
+ FileUtils.rm_f(passed_file)
71
+ end
72
+
73
+ def teardown
74
+ if @test.send(:passed?)
75
+ FileUtils.touch(passed_file)
76
+ else
77
+ FileUtils.rm_f(passed_file)
78
+ end
79
+ end
80
+
81
+ def need_to_run?
82
+ !previous_test_success? or self.class.need_to_run?(@test)
83
+ end
84
+
85
+ private
86
+ def previous_test_success?
87
+ File.exist?(passed_file)
88
+ end
89
+
90
+ def result_dir
91
+ components = [".test-result",
92
+ @test.class.name || "AnonymousTestCase",
93
+ @test.method_name.to_s]
94
+ parent_directories = [File.dirname($0), Dir.pwd]
95
+ if Process.respond_to?(:uid)
96
+ parent_directories << File.join(Dir.tmpdir, Process.uid.to_s)
97
+ end
98
+ parent_directories.each do |parent_directory|
99
+ dir = File.expand_path(File.join(parent_directory, *components))
100
+ begin
101
+ FileUtils.mkdir_p(dir)
102
+ return dir
103
+ rescue Errno::EACCES
104
+ end
105
+ end
106
+
107
+ raise Errno::EACCES, parent_directories.join(", ")
108
+ end
109
+
110
+ def passed_file
111
+ File.join(result_dir, "passed")
112
+ end
113
+
114
+ def escaped_method_name
115
+ @method_name.to_s.gsub(/[!?=]$/) do |matched|
116
+ case matched
117
+ when "!"
118
+ ".destructive"
119
+ when "?"
120
+ ".predicate"
121
+ when "="
122
+ ".equal"
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ module ClassMethods
129
+ def priority(name, *tests)
130
+ unless Checker.have_priority?(name)
131
+ raise ArgumentError, "unknown priority: #{name}"
132
+ end
133
+ attribute(:priority, name, {:keep => true}, *tests)
134
+ end
135
+ end
136
+
137
+ def priority_setup
138
+ Checker.new(self).setup
139
+ end
140
+
141
+ def priority_teardown
142
+ Checker.new(self).teardown
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,46 @@
1
+ module Test
2
+ module Unit
3
+ AutoRunner.register_runner(:console) do |auto_runner|
4
+ require 'test/unit/ui/console/testrunner'
5
+ Test::Unit::UI::Console::TestRunner
6
+ end
7
+
8
+ AutoRunner.setup_option do |auto_runner, opts|
9
+ require 'test/unit/ui/console/outputlevel'
10
+
11
+ output_levels = [
12
+ [:silent, UI::Console::OutputLevel::SILENT],
13
+ [:progress, UI::Console::OutputLevel::PROGRESS_ONLY],
14
+ [:normal, UI::Console::OutputLevel::NORMAL],
15
+ [:verbose, UI::Console::OutputLevel::VERBOSE],
16
+ ]
17
+ opts.on('-v', '--verbose=[LEVEL]', output_levels,
18
+ "Set the output level (default is verbose).",
19
+ "(#{auto_runner.keyword_display(output_levels)})") do |level|
20
+ level ||= output_levels.assoc(:verbose)[1]
21
+ auto_runner.runner_options[:output_level] = level
22
+ end
23
+
24
+ use_color_options = [
25
+ [:auto, :auto],
26
+ ["-", false],
27
+ ["no", false],
28
+ ["false", false],
29
+ ["+", true],
30
+ ["yes", true],
31
+ ["true", true],
32
+ ]
33
+ opts.on("--[no-]use-color=[auto]", use_color_options,
34
+ "Use color output",
35
+ "(default is auto") do |use_color|
36
+ case use_color
37
+ when nil
38
+ use_color = true
39
+ when :auto
40
+ use_color = nil
41
+ end
42
+ auto_runner.runner_options[:use_color] = use_color
43
+ end
44
+ end
45
+ end
46
+ end