assert 2.15.2 → 2.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/lib/assert/assertions.rb +6 -0
  3. data/lib/assert/config_helpers.rb +35 -14
  4. data/lib/assert/context.rb +36 -43
  5. data/lib/assert/context/test_dsl.rb +4 -4
  6. data/lib/assert/default_suite.rb +35 -40
  7. data/lib/assert/default_view.rb +109 -37
  8. data/lib/assert/file_line.rb +1 -1
  9. data/lib/assert/result.rb +67 -27
  10. data/lib/assert/runner.rb +14 -10
  11. data/lib/assert/suite.rb +41 -50
  12. data/lib/assert/test.rb +39 -81
  13. data/lib/assert/version.rb +1 -1
  14. data/lib/assert/view_helpers.rb +11 -21
  15. data/test/helper.rb +40 -0
  16. data/test/system/test_tests.rb +90 -88
  17. data/test/unit/assertions/assert_block_tests.rb +14 -10
  18. data/test/unit/assertions/assert_empty_tests.rb +14 -10
  19. data/test/unit/assertions/assert_equal_tests.rb +22 -14
  20. data/test/unit/assertions/assert_file_exists_tests.rb +14 -10
  21. data/test/unit/assertions/assert_includes_tests.rb +14 -10
  22. data/test/unit/assertions/assert_instance_of_tests.rb +14 -10
  23. data/test/unit/assertions/assert_kind_of_tests.rb +14 -10
  24. data/test/unit/assertions/assert_match_tests.rb +14 -10
  25. data/test/unit/assertions/assert_nil_tests.rb +14 -10
  26. data/test/unit/assertions/assert_raises_tests.rb +14 -10
  27. data/test/unit/assertions/assert_respond_to_tests.rb +14 -10
  28. data/test/unit/assertions/assert_same_tests.rb +20 -14
  29. data/test/unit/assertions/assert_true_false_tests.rb +28 -20
  30. data/test/unit/assertions_tests.rb +12 -9
  31. data/test/unit/config_helpers_tests.rb +72 -13
  32. data/test/unit/context/test_dsl_tests.rb +38 -45
  33. data/test/unit/context_tests.rb +12 -8
  34. data/test/unit/default_suite_tests.rb +66 -43
  35. data/test/unit/file_line_tests.rb +4 -1
  36. data/test/unit/result_tests.rb +71 -47
  37. data/test/unit/runner_tests.rb +34 -16
  38. data/test/unit/suite_tests.rb +61 -29
  39. data/test/unit/test_tests.rb +97 -134
  40. data/test/unit/view_helpers_tests.rb +17 -31
  41. metadata +2 -2
@@ -22,7 +22,7 @@ class Assert::Suite
22
22
 
23
23
  end
24
24
 
25
- class InitTests < Assert::Context
25
+ class InitTests < UnitTests
26
26
  desc "when init"
27
27
  setup do
28
28
  @config = Factory.modes_off_config
@@ -30,17 +30,16 @@ class Assert::Suite
30
30
  end
31
31
  subject{ @suite }
32
32
 
33
- should have_readers :config, :tests, :test_methods
33
+ should have_readers :config, :test_methods, :setups, :teardowns
34
34
  should have_accessors :start_time, :end_time
35
35
  should have_imeths :suite, :setup, :startup, :teardown, :shutdown
36
- should have_imeths :run_time, :test_rate, :result_rate, :count
37
- should have_imeths :ordered_tests, :reversed_tests
38
- should have_imeths :ordered_tests_by_run_time, :reversed_tests_by_run_time
39
- should have_imeths :test_count
40
- should have_imeths :ordered_results, :reversed_results
41
- should have_imeths :ordered_results_for_dump, :reversed_results_for_dump
42
- should have_imeths :result_count
43
- should have_imeths :before_load, :after_load
36
+ should have_imeths :tests_to_run?, :tests_to_run_count, :clear_tests_to_run
37
+ should have_imeths :find_test_to_run, :sorted_tests_to_run
38
+ should have_imeths :run_time, :test_rate, :result_rate
39
+ should have_imeths :test_count, :result_count, :pass_result_count
40
+ should have_imeths :fail_result_count, :error_result_count
41
+ should have_imeths :skip_result_count, :ignore_result_count
42
+ should have_imeths :before_load, :on_test, :after_load
44
43
  should have_imeths :on_start, :on_finish, :on_interrupt
45
44
  should have_imeths :before_test, :after_test, :on_result
46
45
 
@@ -48,12 +47,7 @@ class Assert::Suite
48
47
  assert_equal @config, subject.config
49
48
  end
50
49
 
51
- should "override the config helper's suite value with itself" do
52
- assert_equal subject, subject.suite
53
- end
54
-
55
50
  should "default its attrs" do
56
- assert_equal [], subject.tests
57
51
  assert_equal [], subject.test_methods
58
52
  assert_equal [], subject.setups
59
53
  assert_equal [], subject.teardowns
@@ -61,6 +55,19 @@ class Assert::Suite
61
55
  assert_equal subject.start_time, subject.end_time
62
56
  end
63
57
 
58
+ should "override the config helper's suite value with itself" do
59
+ assert_equal subject, subject.suite
60
+ end
61
+
62
+ should "not provide any test/result count implementations" do
63
+ assert_nil subject.test_count
64
+ assert_nil subject.pass_result_count
65
+ assert_nil subject.fail_result_count
66
+ assert_nil subject.error_result_count
67
+ assert_nil subject.skip_result_count
68
+ assert_nil subject.ignore_result_count
69
+ end
70
+
64
71
  should "know its run time and rates" do
65
72
  assert_equal 0, subject.run_time
66
73
  assert_equal 0, subject.test_rate
@@ -77,20 +84,6 @@ class Assert::Suite
77
84
  assert_equal (subject.result_count / subject.run_time), subject.result_rate
78
85
  end
79
86
 
80
- should "not provide any test or result attrs" do
81
- assert_nil subject.ordered_tests
82
- assert_nil subject.reversed_tests
83
- assert_nil subject.ordered_tests_by_run_time
84
- assert_nil subject.reversed_tests_by_run_time
85
- assert_nil subject.test_count
86
-
87
- assert_nil subject.ordered_results
88
- assert_nil subject.reversed_results
89
- assert_nil subject.ordered_results_for_dump
90
- assert_nil subject.reversed_results_for_dump
91
- assert_nil subject.result_count
92
- end
93
-
94
87
  should "add setup procs" do
95
88
  status = nil
96
89
  @suite.setup{ status = "setups" }
@@ -113,4 +106,43 @@ class Assert::Suite
113
106
 
114
107
  end
115
108
 
109
+ class WithTestsLoadedTests < InitTests
110
+ desc "with tests loaded"
111
+ setup do
112
+ ci = proc{ Factory.context_info(Factory.modes_off_context_class) }
113
+ @tests = [
114
+ Factory.test("should nothing", ci.call){ },
115
+ Factory.test("should pass", ci.call){ assert(1==1); refute(1==0) },
116
+ Factory.test("should fail", ci.call){ ignore; assert(1==0); refute(1==1) },
117
+ Factory.test("should ignore", ci.call){ ignore },
118
+ Factory.test("should skip", ci.call){ skip; ignore; assert(1==1) },
119
+ Factory.test("should error", ci.call){ raise Exception; ignore; assert(1==1) }
120
+ ]
121
+ @tests.each{ |test| @suite.on_test(test) }
122
+ end
123
+
124
+ should "know its tests-to-run attrs" do
125
+ assert_equal @tests.size, subject.tests_to_run_count
126
+ assert_true subject.tests_to_run?
127
+
128
+ subject.clear_tests_to_run
129
+
130
+ assert_equal 0, subject.tests_to_run_count
131
+ assert_false subject.tests_to_run?
132
+ end
133
+
134
+ should "find a test to run given a file line" do
135
+ test = @tests.sample
136
+ assert_same test, subject.find_test_to_run(test.file_line)
137
+ end
138
+
139
+ should "know its sorted tests to run" do
140
+ sorted_tests = subject.sorted_tests_to_run{ 1 }
141
+ assert_equal @tests.size, sorted_tests.size
142
+ assert_kind_of Assert::Test, sorted_tests.first
143
+ assert_same sorted_tests.first, subject.sorted_tests_to_run{ 1 }.first
144
+ end
145
+
146
+ end
147
+
116
148
  end
@@ -17,14 +17,7 @@ class Assert::Test
17
17
  end
18
18
  subject{ Assert::Test }
19
19
 
20
- should have_imeths :result_count_meth, :name_file_line_context_data
21
- should have_imeths :for_block, :for_method
22
-
23
- should "know the result count method name for a given type" do
24
- type = Factory.string
25
- exp = "#{type}_result_count".to_sym
26
- assert_equal exp, subject.result_count_meth(type)
27
- end
20
+ should have_imeths :name_file_line_context_data, :for_block, :for_method
28
21
 
29
22
  should "know how to build the name and file line given context" do
30
23
  test_name = Factory.string
@@ -86,10 +79,7 @@ class Assert::Test
86
79
  :output => Factory.string,
87
80
  :run_time => Factory.float(1.0),
88
81
  }
89
- @meta_data[:total_result_count] = Factory.integer(100)
90
- Assert::Result.types.keys.each do |type|
91
- @meta_data[Assert::Test.result_count_meth(type)] = Factory.integer(100)
92
- end
82
+
93
83
  @run_data = {
94
84
  :context_info => @context_info,
95
85
  :config => @config,
@@ -100,12 +90,9 @@ class Assert::Test
100
90
  end
101
91
  subject{ @test }
102
92
 
103
- should have_readers :file_line, :name, :output, :run_time, :total_result_count
104
- should have_imeths *Assert::Result.types.keys.map{ |k| Assert::Test.result_count_meth(k) }
105
- should have_readers :context_info, :config, :code, :results
106
- should have_imeths :data, :context_class, :file, :line_number
107
- should have_imeths :result_rate, :result_count, :capture_result, :run
108
- should have_imeths *Assert::Result.types.keys.map{ |k| "#{k}_results" }
93
+ should have_imeths :file_line, :file_name, :line_num
94
+ should have_imeths :name, :output, :run_time
95
+ should have_imeths :context_info, :context_class, :config, :code, :run
109
96
 
110
97
  should "use any given attrs" do
111
98
  assert_equal @file_line, subject.file_line
@@ -113,13 +100,6 @@ class Assert::Test
113
100
  assert_equal @meta_data[:output], subject.output
114
101
  assert_equal @meta_data[:run_time], subject.run_time
115
102
 
116
- assert_equal @meta_data[:total_result_count], subject.total_result_count
117
-
118
- Assert::Result.types.keys.each do |type|
119
- n = Assert::Test.result_count_meth(type)
120
- assert_equal @meta_data[n], subject.send(n)
121
- end
122
-
123
103
  assert_equal @context_info, subject.context_info
124
104
  assert_equal @config, subject.config
125
105
  assert_equal @test_code, subject.code
@@ -132,85 +112,34 @@ class Assert::Test
132
112
  assert_equal '', test.name
133
113
  assert_equal '', test.output
134
114
  assert_equal 0, test.run_time
135
- assert_equal 0, test.total_result_count
136
-
137
- Assert::Result.types.keys.each do |type|
138
- assert_equal 0, test.send(Assert::Test.result_count_meth(type))
139
- end
140
115
 
141
116
  assert_nil test.context_info
142
117
  assert_nil test.config
143
118
  assert_nil test.code
144
119
  end
145
120
 
146
- should "have no results before running" do
147
- assert_empty subject.results
148
- end
149
-
150
- should "know its data hash" do
151
- assert_equal @meta_data, subject.data
152
- end
153
-
154
121
  should "know its context class" do
155
122
  assert_equal @context_class, subject.context_class
156
123
  end
157
124
 
158
- should "file line and number" do
159
- assert_equal subject.file_line.file, subject.file
160
- assert_equal subject.file_line.line, subject.line_number
161
- end
162
-
163
- should "know its result rate" do
164
- count = Factory.integer(100)
165
- time = Factory.float(1.0) + 1.0
166
-
167
- Assert.stub(subject, :result_count){ count }
168
- Assert.stub(subject, :run_time){ time }
169
- exp = count / time
170
- assert_equal exp, subject.result_rate
171
-
172
- Assert.stub(subject, :run_time){ 0 }
173
- assert_equal 0.0, subject.result_rate
174
-
175
- Assert.stub(subject, :run_time){ 0.0 }
176
- assert_equal 0.0, subject.result_rate
177
- end
178
-
179
- should "know its result counts" do
180
- assert_equal subject.total_result_count, subject.result_count
181
-
182
- Assert::Result.types.keys.each do |type|
183
- exp = subject.send(Assert::Test.result_count_meth(type))
184
- assert_equal exp, subject.result_count(type)
185
- end
186
- end
187
-
188
- should "capture results" do
189
- result = Factory.pass_result
190
- prev_total_count = subject.total_result_count
191
- prev_pass_count = subject.pass_result_count
192
- callback_result = nil
193
- callback = proc{ |r| callback_result = r}
194
-
195
- subject.capture_result(result, callback)
196
-
197
- assert_equal result, subject.results.last
198
- assert_equal prev_total_count + 1, subject.total_result_count
199
- assert_equal prev_pass_count + 1, subject.pass_result_count
200
- assert_equal result, callback_result
125
+ should "know its file line attrs" do
126
+ assert_equal subject.file_line.file, subject.file_name
127
+ assert_equal subject.file_line.line.to_i, subject.line_num
201
128
  end
202
129
 
203
130
  should "have a custom inspect that only shows limited attributes" do
204
- attrs_string = [:name, :context_info, :results].collect do |method|
131
+ attrs = [:name, :context_info].collect do |method|
205
132
  "@#{method}=#{subject.send(method).inspect}"
206
133
  end.join(" ")
207
- expected = "#<#{subject.class}:#{'0x0%x' % (subject.object_id << 1)} #{attrs_string}>"
208
- assert_equal expected, subject.inspect
134
+ exp = "#<#{subject.class}:#{'0x0%x' % (subject.object_id << 1)} #{attrs}>"
135
+ assert_equal exp, subject.inspect
209
136
  end
210
137
 
211
138
  end
212
139
 
213
- class PassFailIgnoreTotalTests < UnitTests
140
+ class PassFailIgnoreHandlingTests < UnitTests
141
+ include Assert::Test::TestHelpers
142
+
214
143
  setup do
215
144
  @test = Factory.test("pass fail ignore test", @context_info) do
216
145
  ignore("something")
@@ -227,58 +156,97 @@ class Assert::Test
227
156
  assert(true)
228
157
  assert(false)
229
158
  end
230
- @test.run
159
+ @test.run(&test_run_callback)
231
160
  end
232
161
  subject{ @test }
233
162
 
234
- should "know its pass results" do
235
- assert_kind_of Array, subject.pass_results
236
- assert_equal 3, subject.pass_results.size
237
- subject.pass_results.each do |result|
163
+ should "capture results in the test and any setups/teardowns" do
164
+ assert_equal 9, test_run_results.size
165
+ test_run_results.each do |result|
166
+ assert_kind_of Assert::Result::Base, result
167
+ end
168
+ end
169
+
170
+ should "capture pass results in the test and any setups/teardowns" do
171
+ assert_equal 3, test_run_results(:pass).size
172
+ test_run_results(:pass).each do |result|
238
173
  assert_kind_of Assert::Result::Pass, result
239
174
  end
240
- assert_equal subject.pass_results.size, subject.result_count(:pass)
241
175
  end
242
176
 
243
- should "know its fail results" do
244
- assert_kind_of Array, subject.fail_results
245
- assert_equal 3, subject.fail_results.size
246
- subject.fail_results.each do |result|
177
+ should "capture fail results in the test and any setups/teardowns" do
178
+ assert_equal 3, test_run_results(:fail).size
179
+ test_run_results(:fail).each do |result|
247
180
  assert_kind_of Assert::Result::Fail, result
248
181
  end
249
- assert_equal subject.fail_results.size, subject.result_count(:fail)
250
182
  end
251
183
 
252
- should "know its ignore results" do
253
- assert_kind_of Array, subject.ignore_results
254
- assert_equal 3, subject.ignore_results.size
255
- subject.ignore_results.each do |result|
184
+ should "capture ignore results in the test and any setups/teardowns" do
185
+ assert_equal 3, test_run_results(:ignore).size
186
+ test_run_results(:ignore).each do |result|
256
187
  assert_kind_of Assert::Result::Ignore, result
257
188
  end
258
- assert_equal subject.ignore_results.size, subject.result_count(:ignore)
259
189
  end
260
190
 
261
- should "know the total number of results" do
262
- assert_equal(9, subject.result_count)
191
+ end
192
+
193
+ class FailHandlingTests < UnitTests
194
+ include Assert::Test::TestHelpers
195
+
196
+ desc "when in halt-on-fail mode"
197
+
198
+ should "capture fail results" do
199
+ test = Factory.test("halt-on-fail test", @context_info) do
200
+ raise Assert::Result::TestFailure
201
+ end
202
+ test.run(&test_run_callback)
203
+
204
+ assert_failed(test)
205
+ end
206
+
207
+ should "capture fails in the context setup" do
208
+ test = Factory.test("setup halt-on-fail test", @context_info){ }
209
+ test.context_class.setup{ raise Assert::Result::TestFailure }
210
+ test.run(&test_run_callback)
211
+
212
+ assert_failed(test)
213
+ end
214
+
215
+ should "capture fails in the context teardown" do
216
+ test = Factory.test("teardown halt-on-fail test", @context_info){ }
217
+ test.context_class.teardown{ raise Assert::Result::TestFailure }
218
+ test.run(&test_run_callback)
219
+
220
+ assert_failed(test)
221
+ end
222
+
223
+ private
224
+
225
+ def assert_failed(test)
226
+ with_backtrace(caller) do
227
+ assert_equal 1, test_run_result_count, 'too many/few fail results'
228
+ test_run_results.each do |result|
229
+ assert_kind_of Assert::Result::Fail, result, 'not a fail result'
230
+ end
231
+ end
263
232
  end
264
233
 
265
234
  end
266
235
 
267
236
  class SkipHandlingTests < UnitTests
268
- setup do
269
- @test = Factory.test("skip test", @context_info){ skip }
270
- @test.run
271
- end
272
- subject{ @test }
237
+ include Assert::Test::TestHelpers
273
238
 
274
239
  should "capture skip results" do
275
- assert_skipped(subject)
240
+ test = Factory.test("skip test", @context_info){ skip }
241
+ test.run(&test_run_callback)
242
+
243
+ assert_skipped(test)
276
244
  end
277
245
 
278
246
  should "capture skips in the context setup" do
279
247
  test = Factory.test("setup skip test", @context_info){ }
280
248
  test.context_class.setup{ skip }
281
- test.run
249
+ test.run(&test_run_callback)
282
250
 
283
251
  assert_skipped(test)
284
252
  end
@@ -286,7 +254,7 @@ class Assert::Test
286
254
  should "capture skips in the context teardown" do
287
255
  test = Factory.test("teardown skip test", @context_info){ }
288
256
  test.context_class.teardown{ skip }
289
- test.run
257
+ test.run(&test_run_callback)
290
258
 
291
259
  assert_skipped(test)
292
260
  end
@@ -295,33 +263,31 @@ class Assert::Test
295
263
 
296
264
  def assert_skipped(test)
297
265
  with_backtrace(caller) do
298
- assert_equal 1, test.skip_results.size, 'too many/few skip results'
299
- test.skip_results.each do |result|
300
- assert_kind_of Assert::Result::Skip, result, 'result is not a skip result'
266
+ assert_equal 1, test_run_result_count, 'too many/few skip results'
267
+ test_run_results.each do |result|
268
+ assert_kind_of Assert::Result::Skip, result, 'not a skip result'
301
269
  end
302
- assert_equal test.skip_results.size, test.result_count(:skip), 'skip result not counted'
303
270
  end
304
271
  end
305
272
 
306
273
  end
307
274
 
308
275
  class ErrorHandlingTests < UnitTests
309
- setup do
310
- @test = Factory.test("error test", @context_info) do
276
+ include Assert::Test::TestHelpers
277
+
278
+ should "capture error results" do
279
+ test = Factory.test("error test", @context_info) do
311
280
  raise StandardError, "WHAT"
312
281
  end
313
- @test.run
314
- end
315
- subject{ @test }
282
+ test.run(&test_run_callback)
316
283
 
317
- should "capture error results" do
318
- assert_errored(subject)
284
+ assert_errored(test)
319
285
  end
320
286
 
321
287
  should "capture errors in the context setup" do
322
288
  test = Factory.test("setup error test", @context_info){ }
323
289
  test.context_class.setup{ raise 'an error' }
324
- test.run
290
+ test.run(&test_run_callback)
325
291
 
326
292
  assert_errored(test)
327
293
  end
@@ -329,7 +295,7 @@ class Assert::Test
329
295
  should "capture errors in the context teardown" do
330
296
  test = Factory.test("teardown error test", @context_info){ }
331
297
  test.context_class.teardown{ raise 'an error' }
332
- test.run
298
+ test.run(&test_run_callback)
333
299
 
334
300
  assert_errored(test)
335
301
  end
@@ -338,26 +304,23 @@ class Assert::Test
338
304
 
339
305
  def assert_errored(test)
340
306
  with_backtrace(caller) do
341
- assert_equal 1, subject.error_results.size, 'too many/few error results'
342
- test.error_results.each do |result|
343
- assert_kind_of Assert::Result::Error, result, 'result is not an error result'
307
+ assert_equal 1, test_run_result_count, 'too many/few error results'
308
+ test_run_results.each do |result|
309
+ assert_kind_of Assert::Result::Error, result, 'not an error result'
344
310
  end
345
- assert_equal test.error_results.size, test.result_count(:error), 'error result not counted'
346
311
  end
347
312
  end
348
313
 
349
314
  end
350
315
 
351
316
  class SignalExceptionHandlingTests < UnitTests
352
- setup do
353
- @test = Factory.test("signal test", @context_info) do
317
+
318
+ should "raise any signal exceptions and not capture as an error" do
319
+ test = Factory.test("signal test", @context_info) do
354
320
  raise SignalException, "USR1"
355
321
  end
356
- end
357
- subject{ @test }
358
322
 
359
- should "raise any signal exceptions and not capture as an error" do
360
- assert_raises(SignalException){ subject.run }
323
+ assert_raises(SignalException){ test.run }
361
324
  end
362
325
 
363
326
  should "raises signal exceptions in the context setup" do