assert 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. data/README.md +8 -8
  2. data/lib/assert.rb +9 -62
  3. data/lib/assert/assert_runner.rb +15 -30
  4. data/lib/assert/assertions.rb +105 -41
  5. data/lib/assert/cli.rb +1 -1
  6. data/lib/assert/config.rb +56 -0
  7. data/lib/assert/context.rb +44 -150
  8. data/lib/assert/context/setup_dsl.rb +70 -0
  9. data/lib/assert/context/subject_dsl.rb +39 -0
  10. data/lib/assert/context/suite_dsl.rb +20 -0
  11. data/lib/assert/context/test_dsl.rb +51 -0
  12. data/lib/assert/runner.rb +8 -2
  13. data/lib/assert/suite.rb +33 -12
  14. data/lib/assert/test.rb +11 -8
  15. data/lib/assert/utils.rb +23 -11
  16. data/lib/assert/version.rb +1 -1
  17. data/lib/assert/view.rb +9 -7
  18. data/lib/assert/view/base.rb +26 -4
  19. data/lib/assert/view/default_view.rb +1 -1
  20. data/lib/assert/view/helpers/ansi_styles.rb +1 -1
  21. data/lib/assert/view/helpers/common.rb +16 -6
  22. data/test/helper.rb +1 -94
  23. data/test/support/factory.rb +70 -0
  24. data/test/system/running_tests.rb +55 -28
  25. data/test/unit/assert_tests.rb +6 -33
  26. data/test/unit/assertions/assert_block_tests.rb +3 -3
  27. data/test/unit/assertions/assert_empty_tests.rb +6 -4
  28. data/test/unit/assertions/assert_equal_tests.rb +19 -22
  29. data/test/unit/assertions/assert_file_exists_tests.rb +6 -4
  30. data/test/unit/assertions/assert_includes_tests.rb +8 -4
  31. data/test/unit/assertions/assert_instance_of_tests.rb +8 -6
  32. data/test/unit/assertions/assert_kind_of_tests.rb +8 -5
  33. data/test/unit/assertions/assert_match_tests.rb +8 -4
  34. data/test/unit/assertions/assert_nil_tests.rb +6 -4
  35. data/test/unit/assertions/assert_raises_tests.rb +2 -2
  36. data/test/unit/assertions/assert_respond_to_tests.rb +7 -5
  37. data/test/unit/assertions/assert_same_tests.rb +75 -6
  38. data/test/unit/assertions/assert_true_false_tests.rb +116 -0
  39. data/test/unit/assertions_tests.rb +7 -5
  40. data/test/unit/config_tests.rb +58 -0
  41. data/test/unit/context/{setup_teardown_singleton_tests.rb → setup_dsl_tests.rb} +17 -19
  42. data/test/unit/context/subject_dsl_tests.rb +78 -0
  43. data/test/unit/context/suite_dsl_tests.rb +47 -0
  44. data/test/unit/context/{test_should_singleton_tests.rb → test_dsl_tests.rb} +33 -34
  45. data/test/unit/context_tests.rb +19 -15
  46. data/test/unit/runner_tests.rb +9 -3
  47. data/test/unit/suite_tests.rb +20 -23
  48. data/test/unit/test_tests.rb +22 -14
  49. data/test/unit/utils_tests.rb +15 -21
  50. data/test/unit/view_tests.rb +12 -5
  51. metadata +23 -10
  52. data/test/unit/context/basic_singleton_tests.rb +0 -86
@@ -0,0 +1,70 @@
1
+ require 'assert/config'
2
+ require 'assert/result'
3
+ require 'assert/suite'
4
+ require 'assert/test'
5
+
6
+ module Factory
7
+
8
+ def self.context_info_called_from
9
+ "/path/to_file.rb:1234"
10
+ end
11
+
12
+ def self.context_info(context_klass = nil)
13
+ Assert::Suite::ContextInfo.new(context_klass || self.context_class, context_info_called_from)
14
+ end
15
+
16
+ # Generate an anonymous `Context` inherited from `Assert::Context` by default.
17
+ # This provides a common interface for all contexts used in testing.
18
+
19
+ def self.context_class(inherit_from = nil, &block)
20
+ klass = Class.new(inherit_from || Assert::Context, &block)
21
+ default = const_name = "FactoryAssertContext"
22
+
23
+ while(Object.const_defined?(const_name)) do
24
+ const_name = "#{default}#{rand(Time.now.to_i)}"
25
+ end
26
+ Object.const_set(const_name, klass)
27
+ klass
28
+ end
29
+
30
+ # Generate a no-op test for use in testing.
31
+
32
+ def self.test(*args, &block)
33
+ opts, config, context_info, name = [
34
+ args.last.kind_of?(::Hash) ? args.pop.dup : {},
35
+ args.last.kind_of?(Assert::Config) ? args.pop : self.modes_off_config,
36
+ args.last.kind_of?(Assert::Suite::ContextInfo) ? args.pop : self.context_info,
37
+ args.last.kind_of?(::String) ? args.pop : 'a test'
38
+ ]
39
+ Assert::Test.new(name, context_info, config, opts, &block)
40
+ end
41
+
42
+ # Generate a skip result for use in testing.
43
+
44
+ def self.skip_result(name, exception)
45
+ Assert::Result::Skip.new(Factory.test(name), exception)
46
+ end
47
+
48
+ def self.modes_off_config
49
+ Assert::Config.new({
50
+ :capture_output => false,
51
+ :halt_on_fail => false,
52
+ :changed_only => false,
53
+ :pp_objects => false,
54
+ :debug => false
55
+ })
56
+ end
57
+
58
+ def self.modes_off_suite
59
+ Assert::Suite.new(self.modes_off_config)
60
+ end
61
+
62
+ def self.modes_off_context_class(*args, &block)
63
+ suite_obj = self.modes_off_suite
64
+ self.context_class(*args) do
65
+ suite(suite_obj)
66
+ instance_eval(&block) if !block.nil?
67
+ end
68
+ end
69
+
70
+ end
@@ -1,11 +1,11 @@
1
1
  require 'assert'
2
2
 
3
- class RunningTheTestsTests < Assert::Context
4
- desc "Assert tests that are run"
3
+ class RunningSystemTests < Assert::Context
4
+ desc "Running a test (with no halt-on-fail) that"
5
5
  subject{ @test }
6
6
 
7
- class NothingTests < RunningTheTestsTests
8
- desc "and does nothing"
7
+ class NothingTests < RunningSystemTests
8
+ desc "does nothing"
9
9
  setup do
10
10
  @test = Factory.test
11
11
  @test.run
@@ -17,8 +17,8 @@ class RunningTheTestsTests < Assert::Context
17
17
 
18
18
  end
19
19
 
20
- class PassTests < RunningTheTestsTests
21
- desc "and passes a single assertion"
20
+ class PassTests < RunningSystemTests
21
+ desc "passes a single assertion"
22
22
  setup do
23
23
  @test = Factory.test{ assert(1 == 1) }
24
24
  @test.run
@@ -27,14 +27,15 @@ class RunningTheTestsTests < Assert::Context
27
27
  should "have 1 result" do
28
28
  assert_equal 1, subject.result_count
29
29
  end
30
+
30
31
  should "have 1 pass result" do
31
32
  assert_equal 1, subject.result_count(:pass)
32
33
  end
33
34
 
34
35
  end
35
36
 
36
- class FailTests < RunningTheTestsTests
37
- desc "and fails a single assertion"
37
+ class FailTests < RunningSystemTests
38
+ desc "fails a single assertion"
38
39
  setup do
39
40
  @test = Factory.test{ assert(1 == 0) }
40
41
  @test.run
@@ -43,14 +44,15 @@ class RunningTheTestsTests < Assert::Context
43
44
  should "have 1 result" do
44
45
  assert_equal 1, subject.result_count
45
46
  end
47
+
46
48
  should "have 1 fail result" do
47
49
  assert_equal 1, subject.result_count(:fail)
48
50
  end
49
51
 
50
52
  end
51
53
 
52
- class SkipTests < RunningTheTestsTests
53
- desc "and skips"
54
+ class SkipTests < RunningSystemTests
55
+ desc "skips once"
54
56
  setup do
55
57
  @test = Factory.test{ skip }
56
58
  @test.run
@@ -59,14 +61,15 @@ class RunningTheTestsTests < Assert::Context
59
61
  should "have 1 result" do
60
62
  assert_equal 1, subject.result_count
61
63
  end
64
+
62
65
  should "have 1 skip result" do
63
66
  assert_equal 1, subject.result_count(:skip)
64
67
  end
65
68
 
66
69
  end
67
70
 
68
- class ErrorTests < RunningTheTestsTests
69
- desc "and errors"
71
+ class ErrorTests < RunningSystemTests
72
+ desc "errors once"
70
73
  setup do
71
74
  @test = Factory.test{ raise("WHAT") }
72
75
  @test.run
@@ -75,14 +78,15 @@ class RunningTheTestsTests < Assert::Context
75
78
  should "have 1 result" do
76
79
  assert_equal 1, subject.result_count
77
80
  end
81
+
78
82
  should "have 1 error result" do
79
83
  assert_equal 1, subject.result_count(:error)
80
84
  end
81
85
 
82
86
  end
83
87
 
84
- class MixedTests < RunningTheTestsTests
85
- desc "and has 1 pass and 1 fail assertion"
88
+ class MixedTests < RunningSystemTests
89
+ desc "has 1 pass and 1 fail assertion"
86
90
  setup do
87
91
  @test = Factory.test do
88
92
  assert(1 == 1)
@@ -94,17 +98,19 @@ class RunningTheTestsTests < Assert::Context
94
98
  should "have 2 total results" do
95
99
  assert_equal 2, subject.result_count
96
100
  end
101
+
97
102
  should "have 1 pass result" do
98
103
  assert_equal 1, subject.result_count(:pass)
99
104
  end
105
+
100
106
  should "have 1 fail result" do
101
107
  assert_equal 1, subject.result_count(:fail)
102
108
  end
103
109
 
104
110
  end
105
111
 
106
- class MixedSkipTests < RunningTheTestsTests
107
- desc "and has 1 pass and 1 fail assertion with a skip call in between"
112
+ class MixedSkipTests < RunningSystemTests
113
+ desc "has 1 pass and 1 fail assertion with a skip call in between"
108
114
  setup do
109
115
  @test = Factory.test do
110
116
  assert(1 == 1)
@@ -117,23 +123,27 @@ class RunningTheTestsTests < Assert::Context
117
123
  should "have 2 total results" do
118
124
  assert_equal 2, subject.result_count
119
125
  end
126
+
120
127
  should "have a skip for its last result" do
121
128
  assert_kind_of Assert::Result::Skip, subject.results.last
122
129
  end
130
+
123
131
  should "have 1 pass result" do
124
132
  assert_equal 1, subject.result_count(:pass)
125
133
  end
134
+
126
135
  should "have 1 skip result" do
127
136
  assert_equal 1, subject.result_count(:skip)
128
137
  end
138
+
129
139
  should "have 0 fail results" do
130
140
  assert_equal 0, subject.result_count(:fail)
131
141
  end
132
142
 
133
143
  end
134
144
 
135
- class MixedErrorTests < RunningTheTestsTests
136
- desc "and has 1 pass and 1 fail assertion with an exception raised in between"
145
+ class MixedErrorTests < RunningSystemTests
146
+ desc "has 1 pass and 1 fail assertion with an exception raised in between"
137
147
  setup do
138
148
  @test = Factory.test do
139
149
  assert(1 == 1)
@@ -146,23 +156,27 @@ class RunningTheTestsTests < Assert::Context
146
156
  should "have an error for its last result" do
147
157
  assert_kind_of Assert::Result::Error, subject.results.last
148
158
  end
159
+
149
160
  should "have 2 total results" do
150
161
  assert_equal 2, subject.result_count
151
162
  end
163
+
152
164
  should "have 1 pass result" do
153
165
  assert_equal 1, subject.result_count(:pass)
154
166
  end
167
+
155
168
  should "have 1 error result" do
156
169
  assert_equal 1, subject.result_count(:error)
157
170
  end
171
+
158
172
  should "have 0 fail results" do
159
173
  assert_equal 0, subject.result_count(:fail)
160
174
  end
161
175
 
162
176
  end
163
177
 
164
- class MixedPassTests < RunningTheTestsTests
165
- desc "and has 1 pass and 1 fail assertion with a pass call in between"
178
+ class MixedPassTests < RunningSystemTests
179
+ desc "has 1 pass and 1 fail assertion with a pass call in between"
166
180
  setup do
167
181
  @test = Factory.test do
168
182
  assert(1 == 1)
@@ -175,20 +189,23 @@ class RunningTheTestsTests < Assert::Context
175
189
  should "have a pass for its last result" do
176
190
  assert_kind_of Assert::Result::Fail, subject.results.last
177
191
  end
192
+
178
193
  should "have 3 total results" do
179
194
  assert_equal 3, subject.result_count
180
195
  end
196
+
181
197
  should "have 2 pass results" do
182
198
  assert_equal 2, subject.result_count(:pass)
183
199
  end
200
+
184
201
  should "have 1 fail results" do
185
202
  assert_equal 1, subject.result_count(:fail)
186
203
  end
187
204
 
188
205
  end
189
206
 
190
- class MixedFailTests < RunningTheTestsTests
191
- desc "and has 1 pass and 1 fail assertion with a fail call in between"
207
+ class MixedFailTests < RunningSystemTests
208
+ desc "has 1 pass and 1 fail assertion with a fail call in between"
192
209
  setup do
193
210
  @test = Factory.test do
194
211
  assert(1 == 0)
@@ -201,20 +218,23 @@ class RunningTheTestsTests < Assert::Context
201
218
  should "have a fail for its last result" do
202
219
  assert_kind_of Assert::Result::Pass, subject.results.last
203
220
  end
221
+
204
222
  should "have 3 total results" do
205
223
  assert_equal 3, subject.result_count
206
224
  end
225
+
207
226
  should "have 1 pass results" do
208
227
  assert_equal 1, subject.result_count(:pass)
209
228
  end
229
+
210
230
  should "have 2 fail results" do
211
231
  assert_equal 2, subject.result_count(:fail)
212
232
  end
213
233
 
214
234
  end
215
235
 
216
- class MixedFlunkTests < RunningTheTestsTests
217
- desc "and has 1 pass and 1 fail assertion with a flunk call in between"
236
+ class MixedFlunkTests < RunningSystemTests
237
+ desc "has 1 pass and 1 fail assertion with a flunk call in between"
218
238
  setup do
219
239
  @test = Factory.test do
220
240
  assert(1 == 0)
@@ -227,20 +247,23 @@ class RunningTheTestsTests < Assert::Context
227
247
  should "have a fail for its last result" do
228
248
  assert_kind_of Assert::Result::Pass, subject.results.last
229
249
  end
250
+
230
251
  should "have 3 total results" do
231
252
  assert_equal 3, subject.result_count
232
253
  end
254
+
233
255
  should "have 1 pass results" do
234
256
  assert_equal 1, subject.result_count(:pass)
235
257
  end
258
+
236
259
  should "have 2 fail results" do
237
260
  assert_equal 2, subject.result_count(:fail)
238
261
  end
239
262
 
240
263
  end
241
264
 
242
- class WithSetupTests < RunningTheTestsTests
243
- desc "a Test that runs and has assertions that depend on setups"
265
+ class WithSetupTests < RunningSystemTests
266
+ desc "has assertions that depend on setups"
244
267
  setup do
245
268
  assert_style_msg = @asm = "set by assert style setup"
246
269
  testunit_style_msg = @tusm = "set by test/unit style setup"
@@ -273,17 +296,19 @@ class RunningTheTestsTests < Assert::Context
273
296
  assert_equal 2, subject.result_count
274
297
  assert_equal 2, subject.result_count(:pass)
275
298
  end
299
+
276
300
  should "have run the assert style setup" do
277
301
  assert_equal @asm, subject.pass_results.first.message
278
302
  end
303
+
279
304
  should "have run the test/unit style setup" do
280
305
  assert_equal @tusm, subject.pass_results.last.message
281
306
  end
282
307
 
283
308
  end
284
309
 
285
- class WithTeardownTests < RunningTheTestsTests
286
- desc "a Test that runs and has assertions with teardowns"
310
+ class WithTeardownTests < RunningSystemTests
311
+ desc "has assertions with teardowns"
287
312
  setup do
288
313
  assert_style_msg = @asm = "set by assert style teardown"
289
314
  testunit_style_msg = @tusm = "set by test/unit style teardown"
@@ -315,9 +340,11 @@ class RunningTheTestsTests < Assert::Context
315
340
  assert_equal 2, subject.result_count
316
341
  assert_equal 2, subject.result_count(:pass)
317
342
  end
343
+
318
344
  should "have run the assert style teardown" do
319
345
  assert_equal @asm, subject.pass_results.first.message
320
346
  end
347
+
321
348
  should "have run test/unit style teardown" do
322
349
  assert_equal @tusm, subject.pass_results.last.message
323
350
  end
@@ -1,9 +1,6 @@
1
1
  require 'assert'
2
2
 
3
- require 'assert/view/default_view'
4
- require 'assert/runner'
5
- require 'assert/suite'
6
- require 'assert/utils'
3
+ require 'assert/config'
7
4
 
8
5
  module Assert
9
6
 
@@ -11,10 +8,10 @@ module Assert
11
8
  desc "the Assert module"
12
9
  subject { Assert }
13
10
 
14
- should have_imeths :view, :suite, :runner, :config, :configure
11
+ should have_imeths :config, :configure, :view, :suite, :runner
15
12
 
16
- should "know its config singleton" do
17
- assert_same Config, subject.config
13
+ should "know its config instance" do
14
+ assert_kind_of Assert::Config, subject.config
18
15
  end
19
16
 
20
17
  should "map its view, suite and runner to its config" do
@@ -23,32 +20,8 @@ module Assert
23
20
  assert_same subject.config.runner, subject.runner
24
21
  end
25
22
 
26
- # Note: don't really need to explicitly test the configure/init meths
27
- # nothing runs as expected if they aren't working
28
-
29
- end
30
-
31
- class ConfigTests < Assert::Context
32
- desc "the Assert Config singleton"
33
- subject { Config }
34
-
35
- should have_imeths :suite, :view, :runner, :test_dir, :test_helper, :changed_files
36
- should have_imeths :runner_seed, :pp_proc, :use_diff_proc, :run_diff_proc
37
- should have_imeths :capture_output, :halt_on_fail, :changed_only, :pp_objects
38
- should have_imeths :debug, :apply
39
-
40
- should "default the view, suite, and runner" do
41
- assert_kind_of Assert::View::DefaultView, subject.view
42
- assert_kind_of Assert::Suite, subject.suite
43
- assert_kind_of Assert::Runner, subject.runner
44
- end
45
-
46
- should "default the optional values" do
47
- assert_not_nil subject.runner_seed
48
- assert_not_nil subject.pp_proc
49
- assert_not_nil subject.use_diff_proc
50
- assert_not_nil subject.run_diff_proc
51
- end
23
+ # Note: don't really need to explicitly test the configure method as
24
+ # nothing runs if it isn't working
52
25
 
53
26
  end
54
27
 
@@ -4,7 +4,7 @@ require 'assert/assertions'
4
4
  module Assert::Assertions
5
5
 
6
6
  class AssertBlockTests < Assert::Context
7
- desc "the `assert_block` helper"
7
+ desc "`assert_block`"
8
8
  setup do
9
9
  desc = @desc = "assert block fail desc"
10
10
  @test = Factory.test do
@@ -29,7 +29,7 @@ module Assert::Assertions
29
29
  end
30
30
 
31
31
  class AssertNotBlockTests < Assert::Context
32
- desc "the assert_not_block helper"
32
+ desc "`assert_not_block`"
33
33
  setup do
34
34
  desc = @desc = "assert not block fail desc"
35
35
  @test = Factory.test do
@@ -47,7 +47,7 @@ module Assert::Assertions
47
47
  end
48
48
 
49
49
  should "have a fail message with custom and generic explanations" do
50
- exp = "#{@desc}\nExpected block to return a false value."
50
+ exp = "#{@desc}\nExpected block to not return a true value."
51
51
  assert_equal exp, subject.fail_results.first.message
52
52
  end
53
53
 
@@ -6,7 +6,7 @@ require 'assert/utils'
6
6
  module Assert::Assertions
7
7
 
8
8
  class AssertEmptyTests < Assert::Context
9
- desc "the assert_empty helper"
9
+ desc "`assert_empty`"
10
10
  setup do
11
11
  desc = @desc = "assert empty fail desc"
12
12
  args = @args = [ [ 1 ], desc ]
@@ -14,6 +14,7 @@ module Assert::Assertions
14
14
  assert_empty([]) # pass
15
15
  assert_empty(*args) # fail
16
16
  end
17
+ @c = @test.config
17
18
  @test.run
18
19
  end
19
20
  subject{ @test }
@@ -25,14 +26,14 @@ module Assert::Assertions
25
26
  end
26
27
 
27
28
  should "have a fail message with custom and generic explanations" do
28
- exp = "#{@args[1]}\nExpected #{Assert::U.show(@args[0])} to be empty."
29
+ exp = "#{@args[1]}\nExpected #{Assert::U.show(@args[0], @c)} to be empty."
29
30
  assert_equal exp, subject.fail_results.first.message
30
31
  end
31
32
 
32
33
  end
33
34
 
34
35
  class AssertNotEmptyTests < Assert::Context
35
- desc "the assert_not_empty helper"
36
+ desc "`assert_not_empty`"
36
37
  setup do
37
38
  desc = @desc = "assert not empty fail desc"
38
39
  args = @args = [ [], desc ]
@@ -40,6 +41,7 @@ module Assert::Assertions
40
41
  assert_not_empty([ 1 ]) # pass
41
42
  assert_not_empty(*args) # fail
42
43
  end
44
+ @c = @test.config
43
45
  @test.run
44
46
  end
45
47
  subject{ @test }
@@ -51,7 +53,7 @@ module Assert::Assertions
51
53
  end
52
54
 
53
55
  should "have a fail message with custom and generic explanations" do
54
- exp = "#{@args[1]}\nExpected #{Assert::U.show(@args[0])} to not be empty."
56
+ exp = "#{@args[1]}\nExpected #{Assert::U.show(@args[0], @c)} to not be empty."
55
57
  assert_equal exp, subject.fail_results.first.message
56
58
  end
57
59