test-unit 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,6 @@
2
2
  # HACK: quick and dirty to get integrated into the new project - ryan
3
3
  module Test
4
4
  module Unit
5
- VERSION = '2.2.0'
5
+ VERSION = '2.3.0'
6
6
  end
7
7
  end
@@ -0,0 +1,3 @@
1
+ label,expected,augend,addend
2
+ positive positive,4,3,1
3
+ positive negative,-1,1,-2
@@ -0,0 +1,179 @@
1
+ class TestData < Test::Unit::TestCase
2
+ class Calc
3
+ def initialize
4
+ end
5
+
6
+ def plus(augend, addend)
7
+ augend + addend
8
+ end
9
+ end
10
+
11
+ class TestCalc < Test::Unit::TestCase
12
+ @@testing = false
13
+
14
+ class << self
15
+ def testing=(testing)
16
+ @@testing = testing
17
+ end
18
+ end
19
+
20
+ def valid?
21
+ @@testing
22
+ end
23
+
24
+ def setup
25
+ @calc = Calc.new
26
+ end
27
+
28
+ class TestDataSet < TestCalc
29
+ data("positive positive" => {:expected => 4, :augend => 3, :addend => 1},
30
+ "positive negative" => {:expected => -1, :augend => 1, :addend => -2})
31
+ def test_plus(data)
32
+ assert_equal(data[:expected],
33
+ @calc.plus(data[:augend], data[:addend]))
34
+ end
35
+ end
36
+
37
+ class TestNData < TestCalc
38
+ data("positive positive", {:expected => 4, :augend => 3, :addend => 1})
39
+ data("positive negative", {:expected => -1, :augend => 1, :addend => -2})
40
+ def test_plus(data)
41
+ assert_equal(data[:expected],
42
+ @calc.plus(data[:augend], data[:addend]))
43
+ end
44
+ end
45
+
46
+ class TestDynamicDataSet < TestCalc
47
+ data do
48
+ data_set = {}
49
+ data_set["positive positive"] = {
50
+ :expected => 3,
51
+ :augend => 1,
52
+ :addend => 2
53
+ }
54
+ data_set["positive negative"] = {
55
+ :expected => -1,
56
+ :augend => 1,
57
+ :addend => -2
58
+ }
59
+ data_set
60
+ end
61
+ DATA_PROC = current_attribute(:data)[:value].first
62
+ def test_plus(data)
63
+ assert_equal(data[:expected],
64
+ @calc.plus(data[:augend], data[:addend]))
65
+ end
66
+ end
67
+
68
+ class TestLoadDataSet < TestCalc
69
+ base_dir = File.dirname(__FILE__)
70
+ load_data("#{base_dir}/fixtures/plus.csv")
71
+ def test_plus(data)
72
+ assert_equal(data["expected"],
73
+ @calc.plus(data["augend"], data["addend"]))
74
+ end
75
+ end
76
+ end
77
+
78
+ def setup
79
+ TestCalc.testing = true
80
+ end
81
+
82
+ def teardown
83
+ TestCalc.testing = false
84
+ end
85
+
86
+ data("data set",
87
+ {
88
+ :test_case => TestCalc::TestDataSet,
89
+ :data_set => [{
90
+ "positive positive" => {
91
+ :expected => 4,
92
+ :augend => 3,
93
+ :addend => 1,
94
+ },
95
+ "positive negative" => {
96
+ :expected => -1,
97
+ :augend => 1,
98
+ :addend => -2,
99
+ },
100
+ }],
101
+ })
102
+ data("n-data",
103
+ {
104
+ :test_case => TestCalc::TestNData,
105
+ :data_set => [{
106
+ "positive positive" => {
107
+ :expected => 4,
108
+ :augend => 3,
109
+ :addend => 1,
110
+ },
111
+ },
112
+ {
113
+ "positive negative" => {
114
+ :expected => -1,
115
+ :augend => 1,
116
+ :addend => -2,
117
+ },
118
+ }],
119
+ })
120
+ data("dynamic-data-set",
121
+ {
122
+ :test_case => TestCalc::TestDynamicDataSet,
123
+ :data_set => [TestCalc::TestDynamicDataSet::DATA_PROC],
124
+ })
125
+ data("load-data-set",
126
+ {
127
+ :test_case => TestCalc::TestLoadDataSet,
128
+ :data_set => [{
129
+ "positive positive" => {
130
+ "expected" => 4,
131
+ "augend" => 3,
132
+ "addend" => 1,
133
+ },
134
+ },
135
+ {
136
+ "positive negative" => {
137
+ "expected" => -1,
138
+ "augend" => 1,
139
+ "addend" => -2,
140
+ },
141
+ }],
142
+ })
143
+ def test_data(data)
144
+ test_plus = data[:test_case].new("test_plus")
145
+ assert_equal(data[:data_set], test_plus[:data])
146
+ assert_not_nil(data[:data_set])
147
+ end
148
+
149
+ data("data set" => TestCalc::TestDataSet,
150
+ "n-data" => TestCalc::TestNData,
151
+ "dynamic-data-set" => TestCalc::TestDynamicDataSet,
152
+ "load-data-set" => TestCalc::TestLoadDataSet)
153
+
154
+ def test_suite(test_case)
155
+ suite = test_case.suite
156
+ assert_equal(["test_plus[positive positive](#{test_case.name})",
157
+ "test_plus[positive negative](#{test_case.name})"],
158
+ suite.tests.collect {|test| test.name})
159
+ end
160
+
161
+ data("data set" => TestCalc::TestDataSet,
162
+ "n-data" => TestCalc::TestNData,
163
+ "dynamic-data-set" => TestCalc::TestDynamicDataSet,
164
+ "load-data-set" => TestCalc::TestLoadDataSet)
165
+
166
+ def test_run(test_case)
167
+ result = _run_test(test_case)
168
+ assert_equal("2 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, " \
169
+ "0 omissions, 0 notifications", result.to_s)
170
+ end
171
+
172
+ def _run_test(test_case)
173
+ result = Test::Unit::TestResult.new
174
+ test = test_case.suite
175
+ yield(test) if block_given?
176
+ test.run(result) {}
177
+ result
178
+ end
179
+ end
@@ -66,6 +66,104 @@ class TestUnitFixture < Test::Unit::TestCase
66
66
  assert_called_fixtures(expected_setup_calls, test_case)
67
67
  end
68
68
 
69
+ def test_cleanup_without_option
70
+ expected_cleanup_calls = [:custom_cleanup_method3,
71
+ :custom_cleanup_method1,
72
+ :custom_cleanup_method0,
73
+ :cleanup]
74
+ test_case = assert_cleanup(expected_cleanup_calls, [])
75
+ assert_inherited_cleanup(expected_cleanup_calls, test_case)
76
+
77
+ assert_inherited_cleanup([:cleanup], nil)
78
+ assert_called_fixtures(expected_cleanup_calls, test_case)
79
+ end
80
+
81
+ def test_cleanup_with_before_option
82
+ expected_cleanup_calls = [:custom_cleanup_method3,
83
+ :custom_cleanup_method0,
84
+ :custom_cleanup_method1,
85
+ :cleanup]
86
+ test_case = assert_cleanup(expected_cleanup_calls,
87
+ [[{:before => :append}],
88
+ [{:before => :append}],
89
+ [{:before => :prepend}],
90
+ [{:before => :prepend}]])
91
+ assert_inherited_cleanup(expected_cleanup_calls, test_case)
92
+
93
+ assert_inherited_cleanup([:cleanup], nil)
94
+ assert_called_fixtures(expected_cleanup_calls, test_case)
95
+ end
96
+
97
+ def test_cleanup_with_after_option
98
+ expected_cleanup_calls = [:cleanup,
99
+ :custom_cleanup_method3,
100
+ :custom_cleanup_method0,
101
+ :custom_cleanup_method1]
102
+ test_case = assert_cleanup(expected_cleanup_calls,
103
+ [[{:after => :append}],
104
+ [{:after => :append}],
105
+ [{:after => :prepend}],
106
+ [{:after => :prepend}]])
107
+ assert_inherited_cleanup(expected_cleanup_calls, test_case)
108
+
109
+ assert_inherited_cleanup([:cleanup], nil)
110
+ assert_called_fixtures(expected_cleanup_calls, test_case)
111
+ end
112
+
113
+ def test_cleanup_with_invalid_option
114
+ assert_invalid_cleanup_option(:unknown => true)
115
+ assert_invalid_cleanup_option(:before => :unknown)
116
+ assert_invalid_cleanup_option(:after => :unknown)
117
+ end
118
+
119
+ def test_cleanup_with_option_to_inherited
120
+ expected_cleanup_calls = [:cleanup]
121
+ test_case = assert_cleanup(expected_cleanup_calls, nil)
122
+ assert_inherited_cleanup([:custom_cleanup_method3,
123
+ :custom_cleanup_method1,
124
+ :custom_cleanup_method0,
125
+ :cleanup],
126
+ test_case, [])
127
+
128
+ assert_inherited_cleanup([:cleanup], nil)
129
+ assert_called_fixtures(expected_cleanup_calls, test_case)
130
+ end
131
+
132
+ def test_cleanup_with_exception
133
+ test_case = Class.new(Test::Unit::TestCase) do
134
+ def called_ids
135
+ @called_ids ||= []
136
+ end
137
+
138
+ def called(id)
139
+ called_ids << id
140
+ end
141
+
142
+ def cleanup
143
+ called(:cleanup)
144
+ raise "cleanup"
145
+ end
146
+
147
+ cleanup
148
+ def custom_cleanup_method0
149
+ called(:custom_cleanup_method0)
150
+ raise "custom_cleanup_method0"
151
+ end
152
+
153
+ cleanup
154
+ def custom_cleanup_method1
155
+ called(:custom_cleanup_method1)
156
+ raise "custom_cleanup_method1"
157
+ end
158
+
159
+ def test_nothing
160
+ end
161
+ end
162
+
163
+ assert_called_fixtures([:custom_cleanup_method1],
164
+ test_case)
165
+ end
166
+
69
167
  def test_teardown_without_option
70
168
  expected_teardown_calls = [:custom_teardown_method3,
71
169
  :custom_teardown_method1,
@@ -236,6 +334,67 @@ class TestUnitFixture < Test::Unit::TestCase
236
334
  _test_case
237
335
  end
238
336
 
337
+ def assert_cleanup_customizable(expected, parent, options)
338
+ test_case = Class.new(parent || Test::Unit::TestCase) do
339
+ yield(self, :before) if block_given?
340
+
341
+ def called_ids
342
+ @called_ids ||= []
343
+ end
344
+
345
+ def called(id)
346
+ called_ids << id
347
+ end
348
+
349
+ def cleanup
350
+ called(:cleanup)
351
+ end
352
+
353
+ cleanup(*(options[0] || [])) if options
354
+ def custom_cleanup_method0
355
+ called(:custom_cleanup_method0)
356
+ end
357
+
358
+ def custom_cleanup_method1
359
+ called(:custom_cleanup_method1)
360
+ end
361
+ cleanup(*[:custom_cleanup_method1, *(options[1] || [])]) if options
362
+
363
+ cleanup(*(options[2] || [])) if options
364
+ def custom_cleanup_method2
365
+ called(:custom_cleanup_method2)
366
+ end
367
+ unregister_cleanup(:custom_cleanup_method2) if options
368
+
369
+ cleanup(*(options[3] || [])) if options
370
+ def custom_cleanup_method3
371
+ called(:custom_cleanup_method3)
372
+ end
373
+
374
+ def test_nothing
375
+ end
376
+
377
+ yield(self, :after) if block_given?
378
+ end
379
+
380
+ assert_called_fixtures(expected, test_case)
381
+ test_case
382
+ end
383
+
384
+ def assert_cleanup(expected, options)
385
+ assert_cleanup_customizable(expected, nil, options)
386
+ assert_cleanup_customizable(expected, nil, options) do |test_case, tag|
387
+ test_case.send(:include, EmptyModule) if tag == :before
388
+ end
389
+ end
390
+
391
+ def assert_inherited_cleanup(expected, parent, options=nil)
392
+ assert_cleanup_customizable(expected, parent, options)
393
+ assert_cleanup_customizable(expected, parent, options) do |test_case, tag|
394
+ test_case.send(:include, EmptyModule) if tag == :before
395
+ end
396
+ end
397
+
239
398
  def assert_teardown_customizable(expected, parent, options)
240
399
  test_case = Class.new(parent || Test::Unit::TestCase) do
241
400
  yield(self, :before) if block_given?
@@ -318,6 +477,10 @@ class TestUnitFixture < Test::Unit::TestCase
318
477
  assert_invalid_option(:setup, option)
319
478
  end
320
479
 
480
+ def assert_invalid_cleanup_option(option)
481
+ assert_invalid_option(:cleanup, option)
482
+ end
483
+
321
484
  def assert_invalid_teardown_option(option)
322
485
  assert_invalid_option(:teardown, option)
323
486
  end
@@ -9,26 +9,20 @@ module Test
9
9
  class TestTestCase < TestCase
10
10
  self.test_order = :random
11
11
  def test_creation
12
- tc = Class.new(TestCase) do
12
+ test_case = Class.new(TestCase) do
13
13
  def test_with_arguments(arg1, arg2)
14
14
  end
15
15
  end
16
-
17
- caught = true
18
- catch(:invalid_test) do
19
- tc.new(:test_with_arguments)
20
- caught = false
21
- end
22
- check("Should have caught an invalid test when there are arguments", caught)
23
-
24
- caught = true
25
- catch(:invalid_test) do
26
- tc.new(:non_existent_test)
27
- caught = false
28
- end
29
- check("Should have caught an invalid test when the method does not exist", caught)
16
+
17
+ test = test_case.new(:test_with_arguments)
18
+ check("Should have caught an invalid test when there are arguments",
19
+ !test.valid?)
20
+
21
+ test = test_case.new(:non_existent_test)
22
+ check("Should have caught an invalid test when the method does not exist",
23
+ !test.valid?)
30
24
  end
31
-
25
+
32
26
  def setup
33
27
  @tc_failure_error = Class.new(TestCase) do
34
28
  def test_failure
@@ -75,7 +69,11 @@ module Test
75
69
  test_case.run(result) { |*arguments| progress << arguments }
76
70
  check("The failure should have triggered the listener", called)
77
71
  check("The failure should have set passed?", !test_case.return_passed?)
78
- check("The progress block should have been updated correctly", [[TestCase::STARTED, test_case.name], [TestCase::FINISHED, test_case.name]] == progress)
72
+ check("The progress block should have been updated correctly",
73
+ [[TestCase::STARTED, test_case.name],
74
+ [TestCase::STARTED_OBJECT, test_case],
75
+ [TestCase::FINISHED, test_case.name],
76
+ [TestCase::FINISHED_OBJECT, test_case]] == progress)
79
77
  end
80
78
 
81
79
  def test_add_failure_nested
@@ -441,17 +439,14 @@ module Test
441
439
  end
442
440
  end
443
441
 
444
- assert_nothing_thrown do
445
- test_case.new("test_nothing")
446
- end
442
+ test = test_case.new("test_nothing")
443
+ assert_predicate(test, :valid?)
447
444
 
448
- assert_nothing_thrown do
449
- sub_test_case.new("test_fail")
450
- end
445
+ test = sub_test_case.new("test_fail")
446
+ assert_predicate(test, :valid?)
451
447
 
452
- assert_throw(:invalid_test) do
453
- sub_test_case.new("test_nothing")
454
- end
448
+ test = sub_test_case.new("test_nothing")
449
+ assert_not_predicate(test, :valid?)
455
450
  end
456
451
 
457
452
  def test_mixin_test_should_not_be_ignored
@@ -511,9 +506,9 @@ module Test
511
506
 
512
507
  test_case.test_order = :defined
513
508
 
514
- assert_equal(["test_declarative_style_test_definition",
515
- "test_include_parenthesis",
516
- "test_1_2_3"],
509
+ assert_equal(["declarative style test definition",
510
+ "include parenthesis",
511
+ "1 + 2 = 3"],
517
512
  test_case.suite.tests.collect {|test| test.method_name})
518
513
 
519
514
  assert_equal(["declarative style test definition",
@@ -522,6 +517,19 @@ module Test
522
517
  test_case.suite.tests.collect {|test| test.description})
523
518
  end
524
519
 
520
+ def test_test_mark
521
+ test_case = Class.new(Test::Unit::TestCase) do
522
+ test
523
+ def my_test_method
524
+ end
525
+ end
526
+
527
+ test_case.test_order = :defined
528
+
529
+ assert_equal(["my_test_method"],
530
+ test_case.suite.tests.collect {|test| test.method_name})
531
+ end
532
+
525
533
  def test_redefine_method
526
534
  test_case = Class.new(Test::Unit::TestCase) do
527
535
  def test_name
@@ -542,6 +550,18 @@ module Test
542
550
  result.summary)
543
551
  end
544
552
 
553
+ def test_data_driven_test
554
+ test_case = Class.new(TestCase) do
555
+ def test_with_data(data)
556
+ end
557
+ end
558
+
559
+ test = test_case.new("test_with_data")
560
+ assert_not_predicate(test, :valid?)
561
+ test.assign_test_data("label1", :test_data1)
562
+ assert_predicate(test, :valid?)
563
+ end
564
+
545
565
  private
546
566
  def check(message, passed)
547
567
  add_assertion