test-unit 3.4.3 → 3.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -3
  3. data/Rakefile +0 -9
  4. data/doc/text/getting-started.md +1 -1
  5. data/doc/text/news.md +53 -0
  6. data/lib/test/unit/assertion-failed-error.rb +35 -0
  7. data/lib/test/unit/assertions.rb +101 -20
  8. data/lib/test/unit/autorunner.rb +13 -1
  9. data/lib/test/unit/collector/descendant.rb +1 -0
  10. data/lib/test/unit/collector/dir.rb +4 -2
  11. data/lib/test/unit/collector/load.rb +2 -0
  12. data/lib/test/unit/collector/objectspace.rb +1 -0
  13. data/lib/test/unit/collector.rb +31 -0
  14. data/lib/test/unit/testcase.rb +31 -1
  15. data/lib/test/unit/testsuite.rb +1 -1
  16. data/lib/test/unit/util/memory-usage.rb +47 -0
  17. data/lib/test/unit/version.rb +1 -1
  18. data/lib/test/unit.rb +4 -4
  19. metadata +6 -83
  20. data/test/collector/test-descendant.rb +0 -182
  21. data/test/collector/test-load.rb +0 -475
  22. data/test/collector/test_dir.rb +0 -407
  23. data/test/collector/test_objectspace.rb +0 -102
  24. data/test/fixtures/header-label.csv +0 -3
  25. data/test/fixtures/header-label.tsv +0 -3
  26. data/test/fixtures/header.csv +0 -3
  27. data/test/fixtures/header.tsv +0 -3
  28. data/test/fixtures/no-header.csv +0 -2
  29. data/test/fixtures/no-header.tsv +0 -2
  30. data/test/fixtures/plus.csv +0 -3
  31. data/test/run-test.rb +0 -22
  32. data/test/test-assertions.rb +0 -2281
  33. data/test/test-attribute-matcher.rb +0 -38
  34. data/test/test-attribute.rb +0 -123
  35. data/test/test-code-snippet.rb +0 -79
  36. data/test/test-color-scheme.rb +0 -123
  37. data/test/test-color.rb +0 -47
  38. data/test/test-data.rb +0 -419
  39. data/test/test-diff.rb +0 -518
  40. data/test/test-emacs-runner.rb +0 -60
  41. data/test/test-error.rb +0 -26
  42. data/test/test-failure.rb +0 -33
  43. data/test/test-fault-location-detector.rb +0 -163
  44. data/test/test-fixture.rb +0 -713
  45. data/test/test-notification.rb +0 -33
  46. data/test/test-omission.rb +0 -81
  47. data/test/test-pending.rb +0 -70
  48. data/test/test-priority.rb +0 -184
  49. data/test/test-test-case.rb +0 -1284
  50. data/test/test-test-result.rb +0 -113
  51. data/test/test-test-suite-creator.rb +0 -97
  52. data/test/test-test-suite.rb +0 -151
  53. data/test/testunit-test-util.rb +0 -33
  54. data/test/ui/test_testrunmediator.rb +0 -20
  55. data/test/util/test-method-owner-finder.rb +0 -38
  56. data/test/util/test-output.rb +0 -11
  57. data/test/util/test_backtracefilter.rb +0 -52
  58. data/test/util/test_observable.rb +0 -102
  59. data/test/util/test_procwrapper.rb +0 -36
@@ -1,33 +0,0 @@
1
- require 'test/unit'
2
- require 'testunit-test-util'
3
-
4
- class TestUnitNotification < Test::Unit::TestCase
5
- include TestUnitTestUtil
6
-
7
- class TestCase < Test::Unit::TestCase
8
- class << self
9
- def suite
10
- Test::Unit::TestSuite.new(name)
11
- end
12
- end
13
-
14
- def test_notify
15
- notify("1st notify")
16
- notify("2nd notify. Reach here.")
17
- end
18
- end
19
-
20
- def test_notify
21
- result = _run_test("test_notify")
22
- assert_equal("1 tests, 0 assertions, 0 failures, 0 errors, 0 pendings, " \
23
- "0 omissions, 2 notifications",
24
- result.to_s)
25
- assert_fault_messages(["1st notify", "2nd notify. Reach here."],
26
- result.notifications)
27
- end
28
-
29
- private
30
- def _run_test(name)
31
- super(TestCase, name)
32
- end
33
- end
@@ -1,81 +0,0 @@
1
- require 'test/unit'
2
- require 'testunit-test-util'
3
-
4
- class TestUnitOmission < Test::Unit::TestCase
5
- include TestUnitTestUtil
6
-
7
- class TestCase < Test::Unit::TestCase
8
- class << self
9
- def suite
10
- Test::Unit::TestSuite.new(name)
11
- end
12
- end
13
-
14
- def test_omit
15
- omit("1st omit")
16
- omit("2nd omit. Should not be reached here.")
17
- assert(true, "Should not be reached here too.")
18
- end
19
-
20
- def test_omit_with_condition
21
- omit_if(false, "Never omit.")
22
- omit_unless(true, "Never omit too.")
23
- omit_if(true, "Should omit.")
24
- omit("The last omit. Should not be reached here.")
25
- end
26
-
27
- def test_omit_with_block
28
- omit("Omit block") do
29
- flunk("Should not be reached here.")
30
- end
31
- assert(true, "Should be reached here.")
32
- end
33
-
34
- def test_omit_with_block_and_condition
35
- omit_if(false, "Never omit.") do
36
- assert(true, "Should be reached here.")
37
- end
38
- omit_if(true, "Should omit.") do
39
- flunk("Never reached here.")
40
- end
41
- assert(true, "Should be reached here too.")
42
- end
43
- end
44
-
45
- def test_omit
46
- result = _run_test("test_omit")
47
- assert_equal("1 tests, 0 assertions, 0 failures, 0 errors, 0 pendings, " \
48
- "1 omissions, 0 notifications",
49
- result.to_s)
50
- assert_fault_messages(["1st omit"], result.omissions)
51
- end
52
-
53
- def test_omit_with_condition
54
- result = _run_test("test_omit_with_condition")
55
- assert_equal("1 tests, 0 assertions, 0 failures, 0 errors, 0 pendings, " \
56
- "1 omissions, 0 notifications",
57
- result.to_s)
58
- assert_fault_messages(["Should omit."], result.omissions)
59
- end
60
-
61
- def test_omit_with_block
62
- result = _run_test("test_omit_with_block")
63
- assert_equal("1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, " \
64
- "1 omissions, 0 notifications",
65
- result.to_s)
66
- assert_fault_messages(["Omit block"], result.omissions)
67
- end
68
-
69
- def test_omit_with_condition_and_block
70
- result = _run_test("test_omit_with_block_and_condition")
71
- assert_equal("1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, " \
72
- "1 omissions, 0 notifications",
73
- result.to_s)
74
- assert_fault_messages(["Should omit."], result.omissions)
75
- end
76
-
77
- private
78
- def _run_test(name)
79
- super(TestCase, name)
80
- end
81
- end
data/test/test-pending.rb DELETED
@@ -1,70 +0,0 @@
1
- require 'test/unit'
2
- require 'testunit-test-util'
3
-
4
- class TestUnitPending < Test::Unit::TestCase
5
- include TestUnitTestUtil
6
-
7
- class TestCase < Test::Unit::TestCase
8
- class << self
9
- def suite
10
- Test::Unit::TestSuite.new(name)
11
- end
12
- end
13
-
14
- def test_pend
15
- pend("1st pend")
16
- pend("2nd pend. Should not be reached here.")
17
- assert(true, "Should not be reached here too.")
18
- end
19
-
20
- def test_pend_with_failure_in_block
21
- pend("Wait a minute") do
22
- raise "Not implemented yet"
23
- end
24
- assert(true, "Reached here.")
25
- end
26
-
27
- def test_pend_with_no_failure_in_block
28
- pend("Wait a minute") do
29
- "Nothing raised"
30
- end
31
- assert(true, "Not reached here.")
32
- end
33
- end
34
-
35
- def test_pend
36
- test = nil
37
- result = _run_test("test_pend") {|t| test = t}
38
- assert_equal("1 tests, 0 assertions, 0 failures, 0 errors, 1 pendings, " \
39
- "0 omissions, 0 notifications",
40
- result.to_s)
41
- assert_fault_messages(["1st pend"], result.pendings)
42
- assert_true(test.interrupted?)
43
- end
44
-
45
- def test_pend_with_failure_in_block
46
- test = nil
47
- result = _run_test("test_pend_with_failure_in_block") {|t| test = t}
48
- assert_equal("1 tests, 1 assertions, 0 failures, 0 errors, 1 pendings, " \
49
- "0 omissions, 0 notifications",
50
- result.to_s)
51
- assert_fault_messages(["Wait a minute"], result.pendings)
52
- assert_false(test.interrupted?)
53
- end
54
-
55
- def test_pend_with_no_failure_in_block
56
- test = nil
57
- result = _run_test("test_pend_with_no_failure_in_block") {|t| test = t}
58
- assert_equal("1 tests, 1 assertions, 1 failures, 0 errors, 0 pendings, " \
59
- "0 omissions, 0 notifications",
60
- result.to_s)
61
- assert_fault_messages(["Pending block should not be passed: Wait a minute."],
62
- result.failures)
63
- assert_true(test.interrupted?)
64
- end
65
-
66
- private
67
- def _run_test(name, &block)
68
- super(TestCase, name, &block)
69
- end
70
- end
@@ -1,184 +0,0 @@
1
- require 'test/unit'
2
-
3
- class TestUnitPriority < Test::Unit::TestCase
4
- class TestCase < Test::Unit::TestCase
5
- class << self
6
- def suite
7
- Test::Unit::TestSuite.new(name)
8
- end
9
- end
10
-
11
- priority :must
12
- def test_must
13
- assert(true)
14
- end
15
-
16
- def test_must_inherited
17
- assert(true)
18
- end
19
-
20
- priority :important
21
- def test_important
22
- assert(true)
23
- end
24
-
25
- def test_important_inherited
26
- assert(true)
27
- end
28
-
29
- priority :high
30
- def test_high
31
- assert(true)
32
- end
33
-
34
- def test_high_inherited
35
- assert(true)
36
- end
37
-
38
- priority :normal
39
- def test_normal
40
- assert(true)
41
- end
42
-
43
- def test_normal_inherited
44
- assert(true)
45
- end
46
-
47
- priority :low
48
- def test_low
49
- assert(true)
50
- end
51
-
52
- def test_low_inherited
53
- assert(true)
54
- end
55
-
56
- priority :never
57
- def test_never
58
- assert(true)
59
- end
60
-
61
- def test_never_inherited
62
- assert(true)
63
- end
64
- end
65
-
66
- def test_priority_must
67
- assert_priority("must", 1.0, 0.0001)
68
- end
69
-
70
- def test_priority_important
71
- assert_priority("important", 0.9, 0.09)
72
- end
73
-
74
- def test_priority_high
75
- assert_priority("high", 0.70, 0.1)
76
- end
77
-
78
- def test_priority_normal
79
- assert_priority("normal", 0.5, 0.1)
80
- end
81
-
82
- def test_priority_low
83
- assert_priority("low", 0.25, 0.1)
84
- end
85
-
86
- def test_priority_never
87
- assert_priority("never", 0.0, 0.0001)
88
- end
89
-
90
- def assert_priority(priority, expected, delta)
91
- assert_need_to_run("test_#{priority}", expected, delta)
92
- assert_need_to_run("test_#{priority}_inherited", expected, delta)
93
- end
94
-
95
- def assert_need_to_run(test_name, expected, delta)
96
- test = TestCase.new(test_name)
97
- n = 1000
98
- n_need_to_run = 0
99
- n.times do |i|
100
- n_need_to_run += 1 if Test::Unit::Priority::Checker.need_to_run?(test)
101
- end
102
- assert_in_delta(expected, n_need_to_run.to_f / n, delta)
103
- end
104
-
105
- class TestClassName < self
106
- def test_colon
107
- assert_escaped_name("Test_colon__colon_Priority", "Test::Priority")
108
- end
109
-
110
- def test_space
111
- assert_escaped_name("test_priority", "test priority")
112
- end
113
-
114
- def test_slash
115
- assert_escaped_name("test_priority", "test/priority")
116
- end
117
-
118
- def test_back_slash
119
- assert_escaped_name("test_priority", "test\/priority")
120
- end
121
-
122
- def test_question
123
- assert_escaped_name("#question.predicate", "#question?")
124
- end
125
-
126
- def test_exclamation
127
- assert_escaped_name("#exclamation.destructive", "#exclamation!")
128
- end
129
-
130
- def test_equal
131
- assert_escaped_name("#equal.equal", "#equal=")
132
- end
133
-
134
- def assert_escaped_name(expected, class_name)
135
- checker = Checker.new(nil)
136
- escaped_class_name = checker.send(:escape_class_name, class_name)
137
- assert_equal(expected, escaped_class_name)
138
- end
139
- end
140
-
141
- class TestFileName < self
142
- class SpecialNameTestCase < Test::Unit::TestCase
143
- class << self
144
- def suite
145
- Test::Unit::TestSuite.new(name)
146
- end
147
- end
148
-
149
- def test_question?
150
- end
151
-
152
- def test_exclamation!
153
- end
154
-
155
- def test_equal=
156
- end
157
-
158
- test "have space" do
159
- end
160
- end
161
-
162
- def test_question
163
- assert_escaped_name("test_colon__#question.predicate_case",
164
- "test: #question? case")
165
- end
166
-
167
- def test_exclamation
168
- assert_escaped_name("test_colon__#exclamation.destructive_case",
169
- "test: #exclamation! case")
170
- end
171
-
172
- def test_equal
173
- assert_escaped_name("test_colon__#equal.equal_case",
174
- "test: #equal= case")
175
- end
176
-
177
- def assert_escaped_name(expected, test_method_name)
178
- checker = Checker.new(SpecialNameTestCase.new(test_method_name))
179
- passed_file = checker.send(:passed_file)
180
- method_name_component = File.basename(File.dirname(passed_file))
181
- assert_equal(expected, method_name_component)
182
- end
183
- end
184
- end
@@ -1,1284 +0,0 @@
1
- # Author:: Nathaniel Talbott.
2
- # Copyright:: Copyright (c) 2008-2014 Kouhei Sutou <kou@clear-code.com>
3
- # Copyright:: Copyright (c) 2011 Haruka Yoshihara <yoshihara@clear-code.com>
4
- # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott
5
- # License:: Ruby license.
6
-
7
- require 'test/unit'
8
-
9
- module Test
10
- module Unit
11
- class TestTestCase < TestCase
12
- self.test_order = :random
13
- def test_creation
14
- test_case = Class.new(TestCase) do
15
- def test_with_arguments(arg1, arg2)
16
- end
17
- end
18
-
19
- test = test_case.new(:test_with_arguments)
20
- check("Should have caught an invalid test when there are arguments",
21
- !test.valid?)
22
-
23
- test = test_case.new(:non_existent_test)
24
- check("Should have caught an invalid test when the method does not exist",
25
- !test.valid?)
26
- end
27
-
28
- def setup
29
- @tc_failure_error = Class.new(TestCase) do
30
- def test_failure
31
- assert_block("failure") do
32
- false
33
- end
34
- end
35
- def test_error
36
- 1 / 0
37
- end
38
- def test_nested_failure
39
- nested
40
- end
41
- def nested
42
- assert_block("nested") do
43
- false
44
- end
45
- end
46
- def return_passed?
47
- return passed?
48
- end
49
- end
50
-
51
- def @tc_failure_error.name
52
- "TC_FailureError"
53
- end
54
- end
55
-
56
- def jruby_backtrace_entry?(entry)
57
- entry.start_with?("org/jruby/")
58
- end
59
-
60
- def rubinius_backtrace_entry?(entry)
61
- entry.start_with?("kernel/")
62
- end
63
-
64
- def internal_backtrace_entry?(entry)
65
- entry.start_with?("<internal:")
66
- end
67
-
68
- def normalize_location(location)
69
- filtered_location = location.reject do |entry|
70
- jruby_backtrace_entry?(entry) or
71
- rubinius_backtrace_entry?(entry) or
72
- internal_backtrace_entry?(entry)
73
- end
74
- filtered_location.collect do |entry|
75
- entry.sub(/:\d+:/, ":0:")
76
- end
77
- end
78
-
79
- def test_add_failed_assertion
80
- test_case = @tc_failure_error.new(:test_failure)
81
- assert do
82
- test_case.passed?
83
- end
84
-
85
- result = TestResult.new
86
- faults = []
87
- result.add_listener(TestResult::FAULT) do |fault|
88
- faults << fault
89
- end
90
- progress = []
91
- test_case.run(result) do |*arguments|
92
- progress << arguments
93
- end
94
- fault_details = faults.collect do |fault|
95
- {
96
- :class => fault.class,
97
- :message => fault.message,
98
- :test_name => fault.test_name,
99
- :location => normalize_location(fault.location),
100
- }
101
- end
102
- assert_equal([
103
- {
104
- :class => Failure,
105
- :message => "failure",
106
- :test_name => "test_failure(TC_FailureError)",
107
- :location => [
108
- "#{__FILE__}:0:in `test_failure'",
109
- "#{__FILE__}:0:in `#{__method__}'",
110
- ],
111
- },
112
- ],
113
- fault_details)
114
-
115
- assert do
116
- not test_case.passed?
117
- end
118
- assert_equal([
119
- [TestCase::STARTED, test_case.name],
120
- [TestCase::STARTED_OBJECT, test_case],
121
- [TestCase::FINISHED, test_case.name],
122
- [TestCase::FINISHED_OBJECT, test_case],
123
- ],
124
- progress)
125
- end
126
-
127
- def test_add_failure_nested
128
- test_case = @tc_failure_error.new(:test_nested_failure)
129
- assert do
130
- test_case.passed?
131
- end
132
-
133
- result = TestResult.new
134
- faults = []
135
- result.add_listener(TestResult::FAULT) do |fault|
136
- faults << fault
137
- end
138
- test_case.run(result) do
139
- end
140
- fault_details = faults.collect do |fault|
141
- {
142
- :class => fault.class,
143
- :message => fault.message,
144
- :test_name => fault.test_name,
145
- :location => normalize_location(fault.location),
146
- }
147
- end
148
- assert_equal([
149
- {
150
- :class => Failure,
151
- :message => "nested",
152
- :test_name => "test_nested_failure(TC_FailureError)",
153
- :location => [
154
- "#{__FILE__}:0:in `nested'",
155
- "#{__FILE__}:0:in `test_nested_failure'",
156
- "#{__FILE__}:0:in `#{__method__}'",
157
- ],
158
- },
159
- ],
160
- fault_details)
161
-
162
- assert do
163
- not test_case.passed?
164
- end
165
- end
166
-
167
- def jruby?
168
- defined?(JRUBY_VERSION)
169
- end
170
-
171
- def rubinius?
172
- false # TODO
173
- end
174
-
175
- def cruby?
176
- (not jruby?) and (not rubinius?)
177
- end
178
-
179
- def test_add_error
180
- test_case = @tc_failure_error.new(:test_error)
181
- assert do
182
- test_case.passed?
183
- end
184
-
185
- result = TestResult.new
186
- faults = []
187
- result.add_listener(TestResult::FAULT) do |fault|
188
- faults << fault
189
- end
190
- test_case.run(result) do
191
- end
192
- fault_details = faults.collect do |fault|
193
- {
194
- :class => fault.class,
195
- :message => fault.message,
196
- :test_name => fault.test_name,
197
- :location => normalize_location(fault.location),
198
- }
199
- end
200
- location = []
201
- location << "#{__FILE__}:0:in `/'" if cruby?
202
- location << "#{__FILE__}:0:in `test_error'"
203
- location << "#{__FILE__}:0:in `#{__method__}'"
204
- assert_equal([
205
- {
206
- :class => Error,
207
- :message => "ZeroDivisionError: divided by 0",
208
- :test_name => "test_error(TC_FailureError)",
209
- :location => location,
210
- },
211
- ],
212
- fault_details)
213
-
214
- assert do
215
- not test_case.passed?
216
- end
217
- end
218
-
219
- def test_no_tests
220
- suite = TestCase.suite
221
- check("Should have a test suite", suite.instance_of?(TestSuite))
222
- check("Should have one test", suite.size == 1)
223
- check("Should have the default test", suite.tests.first.name == "default_test(Test::Unit::TestCase)")
224
-
225
- result = TestResult.new
226
- suite.run(result) {}
227
- check("Should have had one test run", result.run_count == 1)
228
- check("Should have had one test failure", result.failure_count == 1)
229
- check("Should have had no errors", result.error_count == 0)
230
- end
231
-
232
- def test_suite
233
- tc = Class.new(TestCase) do
234
- def test_succeed
235
- assert_block {true}
236
- end
237
- def test_fail
238
- assert_block {false}
239
- end
240
- def test_error
241
- 1/0
242
- end
243
- def dont_run
244
- assert_block {true}
245
- end
246
- def test_dont_run(argument)
247
- assert_block {true}
248
- end
249
- def test
250
- assert_block {true}
251
- end
252
- end
253
-
254
- suite = tc.suite
255
- check("Should have a test suite", suite.instance_of?(TestSuite))
256
- check("Should have three tests", suite.size == 3)
257
-
258
- result = TestResult.new
259
- suite.run(result) {}
260
- check("Should have had three test runs", result.run_count == 3)
261
- check("Should have had one test failure", result.failure_count == 1)
262
- check("Should have had one test error", result.error_count == 1)
263
- end
264
-
265
- def test_setup_teardown
266
- tc = Class.new(TestCase) do
267
- attr_reader(:setup_called, :teardown_called)
268
- def initialize(test)
269
- super(test)
270
- @setup_called = false
271
- @teardown_called = false
272
- end
273
- def setup
274
- @setup_called = true
275
- end
276
- def teardown
277
- @teardown_called = true
278
- end
279
- def test_succeed
280
- assert_block {true}
281
- end
282
- def test_fail
283
- assert_block {false}
284
- end
285
- def test_error
286
- raise "Error!"
287
- end
288
- end
289
- result = TestResult.new
290
-
291
- test = tc.new(:test_succeed)
292
- test.run(result) {}
293
- check("Should have called setup the correct number of times", test.setup_called)
294
- check("Should have called teardown the correct number of times", test.teardown_called)
295
-
296
- test = tc.new(:test_fail)
297
- test.run(result) {}
298
- check("Should have called setup the correct number of times", test.setup_called)
299
- check("Should have called teardown the correct number of times", test.teardown_called)
300
-
301
- test = tc.new(:test_error)
302
- test.run(result) {}
303
- check("Should have called setup the correct number of times", test.setup_called)
304
- check("Should have called teardown the correct number of times", test.teardown_called)
305
-
306
- check("Should have had two test runs", result.run_count == 3)
307
- check("Should have had a test failure", result.failure_count == 1)
308
- check("Should have had a test error", result.error_count == 1)
309
- end
310
-
311
- def test_assertion_failed_not_called
312
- tc = Class.new(TestCase) do
313
- def test_thing
314
- raise AssertionFailedError.new
315
- end
316
- end
317
-
318
- suite = tc.suite
319
- check("Should have one test", suite.size == 1)
320
- result = TestResult.new
321
- suite.run(result) {}
322
- check("Should have had one test run", result.run_count == 1)
323
- check("Should have had one assertion failure", result.failure_count == 1)
324
- check("Should not have any assertion errors but had #{result.error_count}", result.error_count == 0)
325
- end
326
-
327
- def test_equality
328
- tc1 = Class.new(TestCase) do
329
- def test_1
330
- end
331
- def test_2
332
- end
333
- end
334
-
335
- tc2 = Class.new(TestCase) do
336
- def test_1
337
- end
338
- end
339
-
340
- test1 = tc1.new('test_1')
341
- test2 = tc1.new('test_1')
342
- check("Should be equal", test1 == test2)
343
- check("Should be equal", test2 == test1)
344
-
345
- test1 = tc1.new('test_2')
346
- check("Should not be equal", test1 != test2)
347
- check("Should not be equal", test2 != test1)
348
-
349
- test2 = tc1.new('test_2')
350
- check("Should be equal", test1 == test2)
351
- check("Should be equal", test2 == test1)
352
-
353
- test1 = tc1.new('test_1')
354
- test2 = tc2.new('test_1')
355
- check("Should not be equal", test1 != test2)
356
- check("Should not be equal", test2 != test1)
357
-
358
- check("Should not be equal", test1 != Object.new)
359
- check("Should not be equal", Object.new != test1)
360
- end
361
-
362
- def test_re_raise_exception
363
- test_case = Class.new(TestCase) do
364
- def test_raise_interrupt
365
- raise Interrupt, "from test"
366
- end
367
- end
368
-
369
- test = test_case.new("test_raise_interrupt")
370
- begin
371
- test.run(TestResult.new) {}
372
- check("Should not be reached", false)
373
- rescue Exception
374
- check("Interrupt exception should be re-raised", $!.class == Interrupt)
375
- end
376
- end
377
-
378
- def test_timeout_error
379
- test_case = Class.new(TestCase) do
380
- def test_raise_timeout_error
381
- require "timeout"
382
- raise Timeout::Error
383
- end
384
- end
385
-
386
- test_suite = test_case.suite
387
- result = TestResult.new
388
- begin
389
- test_suite.run(result) {}
390
- check("Timeout::Error should be handled as error",
391
- result.error_count == 1)
392
- rescue Exception
393
- check("Timeout::Error should not be passed through: #{$!}", false)
394
- end
395
- end
396
-
397
- def test_interrupted
398
- test_case = Class.new(TestCase) do
399
- def test_fail
400
- flunk
401
- end
402
-
403
- def test_nothing
404
- end
405
- end
406
-
407
- failed_test = test_case.new(:test_fail)
408
- failed_test.run(TestResult.new) {}
409
- check("Should be interrupted", failed_test.interrupted?)
410
-
411
- success_test = test_case.new(:test_nothing)
412
- success_test.run(TestResult.new) {}
413
- check("Should not be interrupted", !success_test.interrupted?)
414
- end
415
-
416
- def test_inherited_test_should_be_ignored
417
- test_case = Class.new(TestCase) do
418
- def test_nothing
419
- end
420
- end
421
-
422
- sub_test_case = Class.new(test_case) do
423
- def test_fail
424
- flunk
425
- end
426
- end
427
-
428
- test = test_case.new("test_nothing")
429
- assert_predicate(test, :valid?)
430
-
431
- test = sub_test_case.new("test_fail")
432
- assert_predicate(test, :valid?)
433
-
434
- test = sub_test_case.new("test_nothing")
435
- assert_not_predicate(test, :valid?)
436
- end
437
-
438
- def test_mixin_test_should_not_be_ignored
439
- test_module = Module.new do
440
- def test_nothing
441
- end
442
- end
443
-
444
- test_case = Class.new(Test::Unit::TestCase) do
445
- include test_module
446
-
447
- def test_fail
448
- flunk
449
- end
450
- end
451
-
452
- assert_nothing_thrown do
453
- test_case.new("test_nothing")
454
- end
455
-
456
- assert_nothing_thrown do
457
- test_case.new("test_fail")
458
- end
459
- end
460
-
461
- def test_defined_order
462
- test_case = Class.new(Test::Unit::TestCase) do
463
- def test_z
464
- end
465
-
466
- def test_1
467
- end
468
-
469
- def test_a
470
- end
471
- end
472
-
473
- test_case.test_order = :defined
474
- assert_equal(["test_z", "test_1", "test_a"],
475
- test_case.suite.tests.collect {|test| test.method_name})
476
- end
477
-
478
- def test_declarative_style
479
- test_case = Class.new(Test::Unit::TestCase) do
480
- test "declarative style test definition" do
481
- end
482
-
483
- test "include parenthesis" do
484
- end
485
-
486
- test "1 + 2 = 3" do
487
- end
488
- end
489
-
490
- test_case.test_order = :defined
491
-
492
- assert_equal(["test: declarative style test definition",
493
- "test: include parenthesis",
494
- "test: 1 + 2 = 3"],
495
- test_case.suite.tests.collect {|test| test.method_name})
496
-
497
- assert_equal(["declarative style test definition",
498
- "include parenthesis",
499
- "1 + 2 = 3"],
500
- test_case.suite.tests.collect {|test| test.description})
501
- end
502
-
503
- def test_test_mark
504
- test_case = Class.new(Test::Unit::TestCase) do
505
- test
506
- def my_test_method
507
- end
508
- end
509
-
510
- test_case.test_order = :defined
511
-
512
- assert_equal(["my_test_method"],
513
- test_case.suite.tests.collect {|test| test.method_name})
514
- end
515
-
516
- def test_redefine_method
517
- test_case = Class.new(Test::Unit::TestCase) do
518
- self.test_order = :alphabetic
519
-
520
- def test_name
521
- end
522
- alias_method :test_name2, :test_name
523
-
524
- def test_name
525
- end
526
- end
527
-
528
- suite = test_case.suite
529
- assert_equal(["test_name", "test_name2"],
530
- suite.tests.collect {|test| test.method_name})
531
- result = TestResult.new
532
- suite.run(result) {}
533
- assert_equal("2 tests, 0 assertions, 0 failures, " +
534
- "0 errors, 0 pendings, 0 omissions, 1 notifications",
535
- result.summary)
536
- end
537
-
538
- def test_data_driven_test
539
- test_case = Class.new(TestCase) do
540
- def test_with_data(data)
541
- end
542
- end
543
-
544
- test = test_case.new("test_with_data")
545
- assert_not_predicate(test, :valid?)
546
- test.assign_test_data("label1", :test_data1)
547
- assert_predicate(test, :valid?)
548
- end
549
-
550
- def test_data_driven_test_without_parameter
551
- test_case = Class.new(TestCase) do
552
- data("label" => "value")
553
- def test_without_parameter
554
- assert_equal("value", data)
555
- end
556
- end
557
-
558
- suite = test_case.suite
559
- assert_equal(["test_without_parameter"],
560
- suite.tests.collect {|test| test.method_name})
561
- result = TestResult.new
562
- suite.run(result) {}
563
- assert_equal("1 tests, 1 assertions, 0 failures, " +
564
- "0 errors, 0 pendings, 0 omissions, 0 notifications",
565
- result.summary)
566
- end
567
-
568
- private
569
- def check(message, passed)
570
- add_assertion
571
- raise AssertionFailedError.new(message) unless passed
572
- end
573
-
574
- class TestTestDefined < self
575
- class TestNoQuery < self
576
- def test_no_test
577
- test_case = Class.new(TestCase) do
578
- end
579
- assert_false(test_case.test_defined?({}))
580
- end
581
-
582
- def test_have_def_style_test
583
- test_case = Class.new(TestCase) do
584
- def test_nothing
585
- end
586
- end
587
- assert_true(test_case.test_defined?({}))
588
- end
589
-
590
- def test_have_method_style_test
591
- test_case = Class.new(TestCase) do
592
- test "nothing" do
593
- end
594
- end
595
- assert_true(test_case.test_defined?({}))
596
- end
597
- end
598
-
599
- class TestPath < self
600
- class TestDefStyle < self
601
- def test_base_name
602
- test_case = Class.new(TestCase) do
603
- def test_nothing
604
- end
605
- end
606
- base_name = File.basename(__FILE__)
607
- assert_true(test_case.test_defined?(:path => base_name))
608
- end
609
-
610
- def test_absolute_path
611
- test_case = Class.new(TestCase) do
612
- def test_nothing
613
- end
614
- end
615
- assert_true(test_case.test_defined?(:path => __FILE__))
616
- end
617
-
618
- def test_not_match
619
- test_case = Class.new(TestCase) do
620
- def test_nothing
621
- end
622
- end
623
- assert_false(test_case.test_defined?(:path => "nonexistent.rb"))
624
- end
625
- end
626
-
627
- class TestMethodStyle < self
628
- def test_base_name
629
- test_case = Class.new(TestCase) do
630
- test "nothing" do
631
- end
632
- end
633
- base_name = File.basename(__FILE__)
634
- assert_true(test_case.test_defined?(:path => base_name))
635
- end
636
-
637
- def test_absolute_path
638
- test_case = Class.new(TestCase) do
639
- test "nothing" do
640
- end
641
- end
642
- assert_true(test_case.test_defined?(:path => __FILE__))
643
- end
644
-
645
- def test_not_match
646
- test_case = Class.new(TestCase) do
647
- test "nothing" do
648
- end
649
- end
650
- assert_false(test_case.test_defined?(:path => "nonexistent.rb"))
651
- end
652
- end
653
- end
654
-
655
- class TestLine < self
656
- class TestDefStyle < self
657
- def test_before
658
- line_before = nil
659
- test_case = Class.new(TestCase) do
660
- line_before = __LINE__
661
- def test_nothing
662
- end
663
- end
664
- assert_false(test_case.test_defined?(:line => line_before))
665
- end
666
-
667
- def test_def
668
- line_def = nil
669
- test_case = Class.new(TestCase) do
670
- line_def = __LINE__; def test_nothing
671
- end
672
- end
673
- assert_true(test_case.test_defined?(:line => line_def))
674
- end
675
-
676
- def test_after
677
- line_after = nil
678
- test_case = Class.new(TestCase) do
679
- def test_nothing
680
- end
681
- line_after = __LINE__
682
- end
683
- assert_true(test_case.test_defined?(:line => line_after))
684
- end
685
-
686
- def test_child
687
- child_test_case = nil
688
- line_child = nil
689
- parent_test_case = Class.new(TestCase) do
690
- test "parent" do
691
- end
692
-
693
- child_test_case = Class.new(self) do
694
- line_child = __LINE__; test "child" do
695
- end
696
- end
697
- end
698
- assert_equal([
699
- false,
700
- true,
701
- ],
702
- [
703
- parent_test_case.test_defined?(:line => line_child),
704
- child_test_case.test_defined?(:line => line_child),
705
- ])
706
- end
707
- end
708
-
709
- class TestMethodStyle < self
710
- def test_before
711
- line_before = nil
712
- test_case = Class.new(TestCase) do
713
- line_before = __LINE__
714
- test "nothing" do
715
- end
716
- end
717
- assert_false(test_case.test_defined?(:line => line_before))
718
- end
719
-
720
- def test_method
721
- line_method = nil
722
- test_case = Class.new(TestCase) do
723
- line_method = __LINE__; test "nothing" do
724
- end
725
- end
726
- assert_true(test_case.test_defined?(:line => line_method))
727
- end
728
-
729
- def test_after
730
- line_after = nil
731
- test_case = Class.new(TestCase) do
732
- test "nothing" do
733
- end
734
- line_after = __LINE__
735
- end
736
- assert_true(test_case.test_defined?(:line => line_after))
737
- end
738
-
739
- def test_child
740
- child_test_case = nil
741
- line_child = nil
742
- parent_test_case = Class.new(TestCase) do
743
- test "parent" do
744
- end
745
-
746
- child_test_case = Class.new(self) do
747
- line_child = __LINE__; test "child" do
748
- end
749
- end
750
- end
751
- assert_equal([
752
- false,
753
- true,
754
- ],
755
- [
756
- parent_test_case.test_defined?(:line => line_child),
757
- child_test_case.test_defined?(:line => line_child),
758
- ])
759
- end
760
-
761
- def test_with_setup
762
- line = nil
763
- test_case = Class.new(TestCase) do
764
- setup do
765
- end
766
-
767
- line = __LINE__; test "with setup" do
768
- end
769
- end
770
- assert do
771
- test_case.test_defined?(:line => line,
772
- :method_name => "test: with setup")
773
- end
774
- end
775
- end
776
- end
777
-
778
- class TestMethodName < self
779
- class TestDefStyle < self
780
- def test_match
781
- test_case = Class.new(TestCase) do
782
- def test_nothing
783
- end
784
- end
785
- query = {:method_name => "test_nothing"}
786
- assert_true(test_case.test_defined?(query))
787
- end
788
-
789
- def test_not_match
790
- test_case = Class.new(TestCase) do
791
- def test_nothing
792
- end
793
- end
794
- query = {:method_name => "test_nonexistent"}
795
- assert_false(test_case.test_defined?(query))
796
- end
797
- end
798
-
799
- class TestMethodStyle < self
800
- def test_match
801
- test_case = Class.new(TestCase) do
802
- test "nothing" do
803
- end
804
- end
805
- query = {:method_name => "test: nothing"}
806
- assert_true(test_case.test_defined?(query))
807
- end
808
-
809
- def test_not_match
810
- test_case = Class.new(TestCase) do
811
- test "nothing" do
812
- end
813
- end
814
- query = {:method_name => "test: nonexistent"}
815
- assert_false(test_case.test_defined?(query))
816
- end
817
- end
818
- end
819
-
820
- class TestCombine < self
821
- class TestDefStyle < self
822
- def test_line_middle
823
- line_middle = nil
824
- test_case = Class.new(TestCase) do
825
- def test_before
826
- end
827
- line_middle = __LINE__
828
- def test_after
829
- end
830
- end
831
- query = {
832
- :path => __FILE__,
833
- :line => line_middle,
834
- :method_name => "test_before",
835
- }
836
- assert_true(test_case.test_defined?(query))
837
- end
838
-
839
- def test_line_after_def
840
- line_after_def = nil
841
- test_case = Class.new(TestCase) do
842
- def test_before
843
- end
844
-
845
- line_after_def = __LINE__; def test_after
846
- end
847
- end
848
- query = {
849
- :path => __FILE__,
850
- :line => line_after_def,
851
- :method_name => "test_before",
852
- }
853
- assert_false(test_case.test_defined?(query))
854
- end
855
- end
856
-
857
- class TestMethodStyle < self
858
- def test_line_middle
859
- line_middle = nil
860
- test_case = Class.new(TestCase) do
861
- test "before" do
862
- end
863
- line_middle = __LINE__
864
- test "after" do
865
- end
866
- end
867
- query = {
868
- :path => __FILE__,
869
- :line => line_middle,
870
- :method_name => "test: before",
871
- }
872
- assert_true(test_case.test_defined?(query))
873
- end
874
-
875
- def test_line_after_method
876
- line_after_method = nil
877
- test_case = Class.new(TestCase) do
878
- test "before" do
879
- end
880
-
881
- line_after_method = __LINE__; test "after" do
882
- end
883
- end
884
- query = {
885
- :path => __FILE__,
886
- :line => line_after_method,
887
- :method_name => "test: before",
888
- }
889
- assert_false(test_case.test_defined?(query))
890
- end
891
- end
892
- end
893
- end
894
-
895
- class TestSubTestCase < self
896
- class TestName < self
897
- def test_anonymous
898
- test_case = Class.new(TestCase)
899
- sub_test_case = test_case.sub_test_case("sub test case") do
900
- end
901
- assert_equal("sub test case", sub_test_case.name)
902
- end
903
-
904
- def test_named
905
- test_case = Class.new(TestCase)
906
- def test_case.name
907
- "ParentTestCase"
908
- end
909
- sub_test_case = test_case.sub_test_case("sub test case") do
910
- end
911
- assert_equal("ParentTestCase::sub test case", sub_test_case.name)
912
- end
913
- end
914
-
915
- def test_suite
916
- test_case = Class.new(TestCase)
917
- sub_test_case = test_case.sub_test_case("sub test case") do
918
- def test_nothing
919
- end
920
- end
921
- test_method_names = sub_test_case.suite.tests.collect do |test|
922
- test.method_name
923
- end
924
- assert_equal(["test_nothing"], test_method_names)
925
- end
926
-
927
- def test_duplicated_name
928
- test_case = Class.new(TestCase) do
929
- def test_nothing
930
- end
931
- end
932
- sub_test_case = test_case.sub_test_case("sub test case") do
933
- def test_nothing
934
- end
935
- end
936
-
937
- test_method_names = test_case.suite.tests.collect do |test|
938
- test.method_name
939
- end
940
- sub_test_method_names = sub_test_case.suite.tests.collect do |test|
941
- test.method_name
942
- end
943
-
944
- assert_equal([
945
- ["test_nothing"],
946
- ["test_nothing"],
947
- ],
948
- [
949
- test_method_names,
950
- sub_test_method_names,
951
- ])
952
- end
953
- end
954
-
955
- class TestStartupShutdown < self
956
- class TestOrder < self
957
- module CallLogger
958
- def called
959
- @@called ||= []
960
- end
961
- end
962
-
963
- def call_order(test_case)
964
- test_case.called.clear
965
- test_suite = test_case.suite
966
- test_suite.run(TestResult.new) {}
967
- test_case.called
968
- end
969
-
970
- class TestNoInheritance < self
971
- def setup
972
- @test_case = Class.new(TestCase) do
973
- extend CallLogger
974
-
975
- class << self
976
- def startup
977
- called << :startup
978
- end
979
-
980
- def shutdown
981
- called << :shutdown
982
- end
983
- end
984
-
985
- def setup
986
- self.class.called << :setup
987
- end
988
-
989
- def teardown
990
- self.class.called << :teardown
991
- end
992
-
993
- def test1
994
- end
995
-
996
- def test2
997
- end
998
- end
999
- end
1000
-
1001
- def test_call_order
1002
- assert_equal([
1003
- :startup,
1004
- :setup, :teardown,
1005
- :setup, :teardown,
1006
- :shutdown,
1007
- ],
1008
- call_order(@test_case))
1009
- end
1010
- end
1011
-
1012
- class TestInheritance < self
1013
- def setup
1014
- @original_descendants = TestCase::DESCENDANTS.dup
1015
- TestCase::DESCENDANTS.clear
1016
-
1017
- @parent_test_case = Class.new(TestCase) do
1018
- extend CallLogger
1019
-
1020
- self.test_order = :alphabetic
1021
-
1022
- class << self
1023
- def startup
1024
- called << :startup_parent
1025
- end
1026
-
1027
- def shutdown
1028
- called << :shutdown_parent
1029
- end
1030
- end
1031
-
1032
- def setup
1033
- self.class.called << :setup_parent
1034
- end
1035
-
1036
- def teardown
1037
- self.class.called << :teardown_parent
1038
- end
1039
-
1040
- def test1_parent
1041
- self.class.called << :test1_parent
1042
- end
1043
-
1044
- def test2_parent
1045
- self.class.called << :test2_parent
1046
- end
1047
- end
1048
-
1049
- @child_test_case = Class.new(@parent_test_case) do
1050
- class << self
1051
- def startup
1052
- called << :startup_child
1053
- end
1054
-
1055
- def shutdown
1056
- called << :shutdown_child
1057
- end
1058
- end
1059
-
1060
- def setup
1061
- self.class.called << :setup_child
1062
- end
1063
-
1064
- def teardown
1065
- self.class.called << :teardown_child
1066
- end
1067
-
1068
- def test1_child
1069
- self.class.called << :test1_child
1070
- end
1071
-
1072
- def test2_child
1073
- self.class.called << :test2_child
1074
- end
1075
- end
1076
- end
1077
-
1078
- def teardown
1079
- TestCase::DESCENDANTS.replace(@original_descendants)
1080
- end
1081
-
1082
- def test_call_order
1083
- collector = Collector::Descendant.new
1084
- test_suite = collector.collect
1085
- test_suite.run(TestResult.new) {}
1086
- called = @parent_test_case.called
1087
- assert_equal([
1088
- :startup_parent,
1089
- :setup_parent, :test1_parent, :teardown_parent,
1090
- :setup_parent, :test2_parent, :teardown_parent,
1091
- :startup_child,
1092
- :setup_child, :test1_child, :teardown_child,
1093
- :setup_child, :test2_child, :teardown_child,
1094
- :shutdown_child,
1095
- :shutdown_parent,
1096
- ],
1097
- called)
1098
- end
1099
- end
1100
- end
1101
-
1102
- class TestError < self
1103
- def run_test_case(test_case)
1104
- test_suite = test_case.suite
1105
- result = TestResult.new
1106
- test_suite.run(result) {}
1107
- result
1108
- end
1109
-
1110
- def error_count(test_case)
1111
- run_test_case(test_case).error_count
1112
- end
1113
-
1114
- def test_on_startup
1115
- test_case = Class.new(TestCase) do
1116
- class << self
1117
- def startup
1118
- raise "from startup"
1119
- end
1120
- end
1121
-
1122
- def test_nothing
1123
- end
1124
- end
1125
-
1126
- assert_equal(1, error_count(test_case))
1127
- end
1128
-
1129
- def test_pass_through_on_startup
1130
- test_case = Class.new(TestCase) do
1131
- class << self
1132
- def startup
1133
- raise Interrupt, "from startup"
1134
- end
1135
- end
1136
-
1137
- def test_nothing
1138
- end
1139
- end
1140
-
1141
- assert_raise(Interrupt) do
1142
- run_test_case(test_case)
1143
- end
1144
- end
1145
-
1146
- def test_on_shutdown
1147
- test_case = Class.new(TestCase) do
1148
- class << self
1149
- def shutdown
1150
- raise "from shutdown"
1151
- end
1152
- end
1153
-
1154
- def test_nothing
1155
- end
1156
- end
1157
-
1158
- assert_equal(1, error_count(test_case))
1159
- end
1160
-
1161
- def test_pass_through_on_shutdown
1162
- test_case = Class.new(TestCase) do
1163
- class << self
1164
- def shutdown
1165
- raise Interrupt, "from shutdown"
1166
- end
1167
- end
1168
-
1169
- def test_nothing
1170
- end
1171
- end
1172
-
1173
- assert_raise(Interrupt) do
1174
- run_test_case(test_case)
1175
- end
1176
- end
1177
-
1178
- def test_pass_through_in_test
1179
- test_case = Class.new(TestCase) do
1180
- @called = []
1181
- class << self
1182
- def called
1183
- @called
1184
- end
1185
-
1186
- def startup
1187
- @called << :startup
1188
- end
1189
-
1190
- def shutdown
1191
- @called << :shutdown
1192
- end
1193
- end
1194
-
1195
- def test_error
1196
- raise Interrupt, "from test"
1197
- end
1198
- end
1199
-
1200
- assert_raise(Interrupt) do
1201
- run_test_case(test_case)
1202
- end
1203
- assert_equal([:startup, :shutdown],
1204
- test_case.called)
1205
- end
1206
-
1207
- class TestName < self
1208
- def test_no_data
1209
- test_case = Class.new(TestCase) do
1210
- class << self
1211
- def name
1212
- "TestCase"
1213
- end
1214
- end
1215
-
1216
- def test_nothing
1217
- end
1218
- end
1219
-
1220
- test = test_case.new("test_nothing")
1221
- assert_equal("test_nothing(TestCase)",
1222
- test.name)
1223
- end
1224
-
1225
- def test_data
1226
- test_case = Class.new(TestCase) do
1227
- class << self
1228
- def name
1229
- "TestCase"
1230
- end
1231
- end
1232
-
1233
- def test_nothing
1234
- end
1235
- end
1236
-
1237
- test = test_case.new("test_nothing")
1238
- test.assign_test_data("(nil)", nil)
1239
- assert_equal("test_nothing[(nil)](TestCase)",
1240
- test.name)
1241
- end
1242
- end
1243
-
1244
- class TestLocalName < self
1245
- def test_no_data
1246
- test_case = Class.new(TestCase) do
1247
- class << self
1248
- def name
1249
- "TestCase"
1250
- end
1251
- end
1252
-
1253
- def test_nothing
1254
- end
1255
- end
1256
-
1257
- test = test_case.new("test_nothing")
1258
- assert_equal("test_nothing",
1259
- test.local_name)
1260
- end
1261
-
1262
- def test_data
1263
- test_case = Class.new(TestCase) do
1264
- class << self
1265
- def name
1266
- "TestCase"
1267
- end
1268
- end
1269
-
1270
- def test_nothing
1271
- end
1272
- end
1273
-
1274
- test = test_case.new("test_nothing")
1275
- test.assign_test_data("(nil)", nil)
1276
- assert_equal("test_nothing[(nil)]",
1277
- test.local_name)
1278
- end
1279
- end
1280
- end
1281
- end
1282
- end
1283
- end
1284
- end