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
@@ -12,7 +12,11 @@ class Assert::Context
12
12
  @test = Factory.test
13
13
  @context_class = @test.context_class
14
14
  @callback_result = nil
15
- @result_callback = proc{ |result| @callback_result = result }
15
+ @test_results = []
16
+ @result_callback = proc do |result|
17
+ @callback_result = result
18
+ @test_results << result
19
+ end
16
20
  @context = @context_class.new(@test, @test.config, @result_callback)
17
21
  end
18
22
  subject{ @context }
@@ -32,9 +36,9 @@ class Assert::Context
32
36
  should have_imeths :with_backtrace, :subject
33
37
 
34
38
  def test_should_collect_context_info
35
- this = @__running_test__
36
- assert_match /test\/unit\/context_tests.rb$/, this.context_info.file
37
- assert_equal self.class, this.context_info.klass
39
+ test = @__assert_running_test__
40
+ assert_match /test\/unit\/context_tests.rb$/, test.context_info.file
41
+ assert_equal self.class, test.context_info.klass
38
42
  end
39
43
 
40
44
  end
@@ -275,7 +279,7 @@ class Assert::Context
275
279
  desc "with_backtrace method"
276
280
  setup do
277
281
  @from_bt = ['called_from_here']
278
- @from_block = proc { ignore; fail; pass; skip 'todo' }
282
+ @from_block = proc { ignore; fail; pass; skip 'todo'; }
279
283
  end
280
284
 
281
285
  should "replace the fail results from the block with the given backtrace" do
@@ -283,11 +287,11 @@ class Assert::Context
283
287
  begin
284
288
  @context.with_backtrace(@from_bt, &@from_block)
285
289
  rescue Assert::Result::TestSkipped => e
286
- @test.results << Assert::Result::Skip.for_test(@test, e)
290
+ @test_results << Assert::Result::Skip.for_test(@test, e)
287
291
  end
288
292
 
289
- assert_equal 5, @test.results.size
290
- norm_fail, with_ignore, with_fail, with_pass, with_skip = @test.results
293
+ assert_equal 5, @test_results.size
294
+ norm_fail, with_ignore, with_fail, with_pass, with_skip = @test_results
291
295
 
292
296
  assert_not_equal @from_bt, norm_fail.backtrace
293
297
  assert_equal @from_bt, with_ignore.backtrace
@@ -8,18 +8,11 @@ class Assert::DefaultSuite
8
8
  class UnitTests < Assert::Context
9
9
  desc "Assert::DefaultSuite"
10
10
  setup do
11
+ ci = Factory.context_info(Factory.modes_off_context_class)
12
+ @test = Factory.test(Factory.string, ci){ }
13
+
11
14
  @config = Factory.modes_off_config
12
15
  @suite = Assert::DefaultSuite.new(@config)
13
-
14
- ci = Factory.context_info(Factory.modes_off_context_class)
15
- [ Factory.test("should nothing", ci){ },
16
- Factory.test("should pass", ci){ assert(1==1); refute(1==0) },
17
- Factory.test("should fail", ci){ ignore; assert(1==0); refute(1==1) },
18
- Factory.test("should ignored", ci){ ignore },
19
- Factory.test("should skip", ci){ skip; ignore; assert(1==1) },
20
- Factory.test("should error", ci){ raise Exception; ignore; assert(1==1) }
21
- ].each{ |test| @suite.tests << test }
22
- @suite.tests.each(&:run)
23
16
  end
24
17
  subject{ @suite }
25
18
 
@@ -27,46 +20,76 @@ class Assert::DefaultSuite
27
20
  assert_kind_of Assert::Suite, subject
28
21
  end
29
22
 
30
- should "know its test and result attrs" do
31
- assert_equal 6, subject.tests.size
32
- assert_kind_of Assert::Test, subject.tests.first
33
-
34
- assert_equal subject.tests.size, subject.test_count
35
- assert_equal subject.tests, subject.ordered_tests
36
-
37
- exp = subject.ordered_tests.sort{ |a, b| a.run_time <=> b.run_time }
38
- assert_equal exp, subject.ordered_tests_by_run_time
39
-
40
- assert_equal 8, subject.result_count
41
-
42
- exp = subject.ordered_tests.inject([]){ |results, t| results += t.results }
43
- assert_equal exp, subject.ordered_results
44
-
45
- assert_equal 2, subject.result_count(:pass)
46
- assert_equal 2, subject.result_count(:fail)
47
- assert_equal 2, subject.result_count(:ignore)
48
- assert_equal 1, subject.result_count(:skip)
49
- assert_equal 1, subject.result_count(:error)
23
+ should "default its test/result counts" do
24
+ assert_equal 0, subject.test_count
25
+ assert_equal 0, subject.result_count
26
+ assert_equal 0, subject.pass_result_count
27
+ assert_equal 0, subject.fail_result_count
28
+ assert_equal 0, subject.error_result_count
29
+ assert_equal 0, subject.skip_result_count
30
+ assert_equal 0, subject.ignore_result_count
50
31
  end
51
32
 
52
- should "count its tests and results" do
53
- assert_equal subject.test_count, subject.count(:tests)
54
- assert_equal subject.result_count, subject.count(:results)
33
+ should "increment its test count on `before_test`" do
34
+ subject.before_test(@test)
35
+ assert_equal 1, subject.test_count
36
+ end
55
37
 
56
- assert_equal subject.result_count(:pass), subject.count(:passed)
57
- assert_equal subject.result_count(:pass), subject.count(:pass)
38
+ should "increment its result counts on `on_result`" do
39
+ subject.on_result(Factory.pass_result)
40
+ assert_equal 1, subject.result_count
41
+ assert_equal 1, subject.pass_result_count
42
+ assert_equal 0, subject.fail_result_count
43
+ assert_equal 0, subject.error_result_count
44
+ assert_equal 0, subject.skip_result_count
45
+ assert_equal 0, subject.ignore_result_count
46
+
47
+ subject.on_result(Factory.fail_result)
48
+ assert_equal 2, subject.result_count
49
+ assert_equal 1, subject.pass_result_count
50
+ assert_equal 1, subject.fail_result_count
51
+ assert_equal 0, subject.error_result_count
52
+ assert_equal 0, subject.skip_result_count
53
+ assert_equal 0, subject.ignore_result_count
54
+
55
+ subject.on_result(Factory.error_result)
56
+ assert_equal 3, subject.result_count
57
+ assert_equal 1, subject.pass_result_count
58
+ assert_equal 1, subject.fail_result_count
59
+ assert_equal 1, subject.error_result_count
60
+ assert_equal 0, subject.skip_result_count
61
+ assert_equal 0, subject.ignore_result_count
62
+
63
+ subject.on_result(Factory.skip_result)
64
+ assert_equal 4, subject.result_count
65
+ assert_equal 1, subject.pass_result_count
66
+ assert_equal 1, subject.fail_result_count
67
+ assert_equal 1, subject.error_result_count
68
+ assert_equal 1, subject.skip_result_count
69
+ assert_equal 0, subject.ignore_result_count
70
+
71
+ subject.on_result(Factory.ignore_result)
72
+ assert_equal 5, subject.result_count
73
+ assert_equal 1, subject.pass_result_count
74
+ assert_equal 1, subject.fail_result_count
75
+ assert_equal 1, subject.error_result_count
76
+ assert_equal 1, subject.skip_result_count
77
+ assert_equal 1, subject.ignore_result_count
78
+ end
58
79
 
59
- assert_equal subject.result_count(:fail), subject.count(:failed)
60
- assert_equal subject.result_count(:fail), subject.count(:fail)
80
+ should "clear the run data on `on_start`" do
81
+ subject.before_test(@test)
82
+ subject.on_result(Factory.pass_result)
61
83
 
62
- assert_equal subject.result_count(:ignore), subject.count(:ignored)
63
- assert_equal subject.result_count(:ignore), subject.count(:ignore)
84
+ assert_equal 1, subject.test_count
85
+ assert_equal 1, subject.result_count
86
+ assert_equal 1, subject.pass_result_count
64
87
 
65
- assert_equal subject.result_count(:skip), subject.count(:skipped)
66
- assert_equal subject.result_count(:skip), subject.count(:skip)
88
+ subject.on_start
67
89
 
68
- assert_equal subject.result_count(:error), subject.count(:errored)
69
- assert_equal subject.result_count(:error), subject.count(:error)
90
+ assert_equal 0, subject.test_count
91
+ assert_equal 0, subject.result_count
92
+ assert_equal 0, subject.pass_result_count
70
93
  end
71
94
 
72
95
  end
@@ -14,7 +14,10 @@ class Assert::FileLine
14
14
  should have_imeths :parse
15
15
 
16
16
  should "know how to parse and init from a file line path string" do
17
- file_line_path = "#{@file}:#{@line}"
17
+ file_line_path = [
18
+ "#{@file}:#{@line}",
19
+ "#{@file}:#{@line} #{Factory.string}"
20
+ ].sample
18
21
  file_line = subject.parse(file_line_path)
19
22
 
20
23
  assert_equal @file, file_line.file
@@ -1,6 +1,8 @@
1
1
  require 'assert'
2
2
  require 'assert/result'
3
3
 
4
+ require 'assert/file_line'
5
+
4
6
  module Assert::Result
5
7
 
6
8
  class UnitTests < Assert::Context
@@ -38,24 +40,26 @@ module Assert::Result
38
40
  desc "Base"
39
41
  setup do
40
42
  @given_data = {
41
- :type => Factory.string,
42
- :name => Factory.string,
43
- :test_name => Factory.string,
44
- :test_id => Factory.string,
45
- :message => Factory.string,
46
- :output => Factory.text,
47
- :backtrace => Backtrace.new(caller),
48
- :trace => Factory.string
43
+ :type => Factory.string,
44
+ :name => Factory.string,
45
+ :test_name => Factory.string,
46
+ :test_file_line => Assert::FileLine.new(Factory.string, Factory.integer),
47
+ :message => Factory.string,
48
+ :output => Factory.text,
49
+ :backtrace => Backtrace.new(caller),
50
+ :trace => Factory.string
49
51
  }
50
52
  @result = Base.new(@given_data)
51
53
  end
52
54
  subject{ @result }
53
55
 
54
56
  should have_cmeths :type, :name, :for_test
55
- should have_imeths :type, :name, :test_name, :test_id
57
+ should have_imeths :type, :name, :test_name, :test_file_line
58
+ should have_imeths :test_file_name, :test_line_num, :test_id
56
59
  should have_imeths :message, :output, :backtrace, :trace
60
+ should have_imeths :file_line, :file_name, :line_num
57
61
  should have_imeths *Assert::Result.types.keys.map{ |k| "#{k}?" }
58
- should have_imeths :set_backtrace, :data, :to_sym, :to_s
62
+ should have_imeths :set_backtrace, :to_sym, :to_s
59
63
 
60
64
  should "know its class-level type/name" do
61
65
  assert_equal :unknown, subject.class.type
@@ -79,60 +83,78 @@ module Assert::Result
79
83
  end
80
84
 
81
85
  should "use any given attrs" do
82
- assert_equal @given_data[:type].to_sym, subject.type
83
- assert_equal @given_data[:name], subject.name
84
- assert_equal @given_data[:test_name], subject.test_name
85
- assert_equal @given_data[:test_id], subject.test_id
86
- assert_equal @given_data[:message], subject.message
87
- assert_equal @given_data[:output], subject.output
88
- assert_equal @given_data[:backtrace], subject.backtrace
89
- assert_equal @given_data[:trace], subject.trace
86
+ assert_equal @given_data[:type].to_sym, subject.type
87
+ assert_equal @given_data[:name], subject.name
88
+ assert_equal @given_data[:test_name], subject.test_name
89
+ assert_equal @given_data[:test_file_line], subject.test_file_line
90
+ assert_equal @given_data[:message], subject.message
91
+ assert_equal @given_data[:output], subject.output
92
+ assert_equal @given_data[:backtrace], subject.backtrace
93
+ assert_equal @given_data[:trace], subject.trace
90
94
  end
91
95
 
92
96
  should "default its attrs" do
93
97
  result = Base.new({})
94
98
 
95
- assert_equal :unknown, result.type
96
- assert_equal '', result.name
97
- assert_equal '', result.test_name
98
- assert_equal '', result.test_id
99
- assert_equal '', result.message
100
- assert_equal '', result.output
101
- assert_equal Backtrace.new([]), result.backtrace
102
- assert_equal '', result.trace
99
+ assert_equal :unknown, result.type
100
+ assert_equal '', result.name
101
+ assert_equal '', result.test_name
102
+ assert_equal Assert::FileLine.parse(''), result.test_file_line
103
+ assert_equal '', result.message
104
+ assert_equal '', result.output
105
+ assert_equal Backtrace.new([]), result.backtrace
106
+ assert_equal '', result.trace
103
107
  end
104
108
 
105
- should "know if it is a certain type of result" do
106
- Assert::Result.types.keys.each do |type|
107
- assert_false subject.send("#{type}?")
108
- Assert.stub(subject, :type){ type }
109
- assert_true subject.send("#{type}?")
110
- end
109
+ should "know its test file line attrs" do
110
+ exp = @given_data[:test_file_line]
111
+ assert_equal exp.file, subject.test_file_name
112
+ assert_equal exp.line.to_i, subject.test_line_num
113
+ assert_equal exp.to_s, subject.test_id
111
114
  end
112
115
 
113
116
  should "allow setting a new backtrace" do
114
117
  new_bt = Factory.integer(3).times.map{ Factory.string }
115
118
  exp_backtrace = Backtrace.new(new_bt)
116
119
  exp_trace = exp_backtrace.filtered.first.to_s
117
-
118
120
  subject.set_backtrace(new_bt)
121
+ assert_equal exp_backtrace, subject.backtrace
122
+ assert_equal exp_trace, subject.trace
119
123
 
124
+ # test that the first bt line is used if filtered is empty
125
+ assert_lib_path = File.join(ROOT_PATH, "lib/#{Factory.string}:#{Factory.integer}")
126
+ new_bt = Factory.integer(3).times.map{ assert_lib_path }
127
+ exp_backtrace = Backtrace.new(new_bt)
128
+ exp_trace = exp_backtrace.first.to_s
129
+ subject.set_backtrace(new_bt)
120
130
  assert_equal exp_backtrace, subject.backtrace
121
131
  assert_equal exp_trace, subject.trace
122
132
  end
123
133
 
124
- should "know its data" do
125
- exp = {
126
- :type => subject.type,
127
- :name => subject.name,
128
- :test_name => subject.test_name,
129
- :test_id => subject.test_id,
130
- :message => subject.message,
131
- :output => subject.output,
132
- :backtrace => subject.backtrace,
133
- :trace => subject.trace,
134
- }
135
- assert_equal exp, subject.data
134
+ should "know its file line attrs" do
135
+ new_bt = Factory.integer(3).times.map{ Factory.string }
136
+ subject.set_backtrace(new_bt)
137
+ exp = Assert::FileLine.parse(subject.backtrace.filtered.first.to_s)
138
+ assert_equal exp, subject.file_line
139
+ assert_equal exp.file, subject.file_name
140
+ assert_equal exp.line.to_i, subject.line_num
141
+
142
+ # test that the first bt line is used if filtered is empty
143
+ assert_lib_path = File.join(ROOT_PATH, "lib/#{Factory.string}:#{Factory.integer}")
144
+ new_bt = Factory.integer(3).times.map{ assert_lib_path }
145
+ subject.set_backtrace(new_bt)
146
+ exp = Assert::FileLine.parse(subject.backtrace.first.to_s)
147
+ assert_equal exp, subject.file_line
148
+ assert_equal exp.file, subject.file_name
149
+ assert_equal exp.line.to_i, subject.line_num
150
+ end
151
+
152
+ should "know if it is a certain type of result" do
153
+ Assert::Result.types.keys.each do |type|
154
+ assert_false subject.send("#{type}?")
155
+ Assert.stub(subject, :type){ type }
156
+ assert_true subject.send("#{type}?")
157
+ end
136
158
  end
137
159
 
138
160
  should "know its symbol representation" do
@@ -164,8 +186,10 @@ module Assert::Result
164
186
  end
165
187
 
166
188
  should "show only its class and message when inspected" do
167
- exp = "#<#{subject.class}:#{'0x0%x' % (subject.object_id << 1)}"\
168
- " @message=#{subject.message.inspect}>"
189
+ exp = "#<#{subject.class}:#{'0x0%x' % (subject.object_id << 1)} "\
190
+ "@message=#{subject.message.inspect} "\
191
+ "@file_line=#{subject.file_line.to_s.inspect} "\
192
+ "@test_file_line=#{subject.test_file_line.to_s.inspect}>"
169
193
  assert_equal exp, subject.inspect
170
194
  end
171
195
 
@@ -52,10 +52,6 @@ class Assert::Runner
52
52
  callback_mixin = Module.new
53
53
  @runner_class = Class.new(Assert::Runner) do
54
54
  include CallbackMixin
55
-
56
- def run!(&block)
57
- self.suite.tests.each(&block)
58
- end
59
55
  end
60
56
  suite_class = Class.new(Assert::DefaultSuite){ include CallbackMixin }
61
57
  view_class = Class.new(Assert::View){ include CallbackMixin }
@@ -67,14 +63,24 @@ class Assert::Runner
67
63
 
68
64
  @ci = Factory.context_info(Factory.modes_off_context_class)
69
65
  @test = Factory.test("should pass", @ci){ assert(1==1) }
70
- @config.suite.tests << @test
66
+ @config.suite.on_test(@test)
71
67
 
72
68
  @runner = @runner_class.new(@config)
73
69
  @result = @runner.run
74
70
  end
75
71
 
76
- should "return an integer exit code" do
72
+ should "return the fail+error result count as an integer exit code" do
77
73
  assert_equal 0, @result
74
+
75
+ fail_count = Factory.integer
76
+ error_count = Factory.integer
77
+ Assert.stub(subject, :fail_result_count){ fail_count }
78
+ Assert.stub(subject, :error_result_count){ error_count }
79
+ Assert.stub(@test, :run){ } # no-op
80
+ result = @runner.run
81
+
82
+ exp = fail_count + error_count
83
+ assert_equal exp, result
78
84
  end
79
85
 
80
86
  should "run all callbacks on itself, the suite and the view" do
@@ -102,34 +108,46 @@ class Assert::Runner
102
108
  assert_true view.on_finish_called
103
109
  end
104
110
 
105
- should "descibe running the tests in random order if there are tests" do
111
+ should "describe running the tests in random order if there are tests" do
106
112
  exp = "Running tests in random order, " \
107
113
  "seeded with \"#{subject.runner_seed}\"\n"
108
114
  assert_includes exp, @view_output
109
115
 
110
116
  @view_output.gsub!(/./, '')
111
- @config.suite.tests.clear
117
+ @config.suite.clear_tests_to_run
112
118
  subject.run
113
119
  assert_not_includes exp, @view_output
114
120
  end
115
121
 
116
122
  should "run only a single test if a single test is configured" do
117
- other_test = Factory.test("should also pass", @ci){ assert(1==1) }
118
- @config.suite.tests << other_test
123
+ test = Factory.test("should pass", @ci){ assert(1==1) }
124
+ @config.suite.clear_tests_to_run
125
+ @config.suite.on_test(test)
126
+ @config.single_test test.file_line.to_s
119
127
 
120
- @config.single_test @test.file_line.to_s
128
+ runner = @runner_class.new(@config).tap(&:run)
129
+ assert_equal [test], runner.before_test_called
130
+ end
131
+
132
+ should "not run any tests if a single test is configured but can't be found" do
133
+ test = Factory.test("should pass", @ci){ assert(1==1) }
134
+ @config.suite.clear_tests_to_run
135
+ @config.suite.on_test(test)
136
+ @config.single_test Factory.string
121
137
 
122
- runner = @runner_class.new(@config)
123
- runner.run
124
- assert_equal [@test], runner.before_test_called
138
+ runner = @runner_class.new(@config).tap(&:run)
139
+ assert_nil runner.before_test_called
125
140
  end
126
141
 
127
- should "descibe running only a single test if a single test is configured" do
142
+ should "describe running only a single test if a single test is configured" do
143
+ @config.suite.clear_tests_to_run
144
+ @config.suite.on_test(@test)
128
145
  @config.single_test @test.file_line.to_s
129
146
  @view_output.gsub!(/./, '')
130
147
  subject.run
131
148
 
132
- exp = "Running test: #{subject.single_test_file_line}\n"
149
+ exp = "Running test: #{subject.single_test_file_line}, " \
150
+ "seeded with \"#{subject.runner_seed}\"\n"
133
151
  assert_includes exp, @view_output
134
152
  end
135
153