assert 2.1.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -223,6 +223,20 @@ module Assert
223
223
  raise(Result::TestSkipped, skip_msg || "")
224
224
  end
225
225
 
226
+ # alter the backtraces of fail results generated in the given block
227
+ def with_backtrace(bt, &block)
228
+ bt ||= []
229
+ current_results.count.tap do |count|
230
+ begin
231
+ instance_eval(&block)
232
+ rescue Result::TestSkipped, Result::TestFailure => e
233
+ e.set_backtrace(bt); raise(e)
234
+ ensure
235
+ current_results[count..-1].each{ |r| r.set_backtrace(bt) }
236
+ end
237
+ end
238
+ end
239
+
226
240
  def subject
227
241
  if subj = self.class.subject
228
242
  instance_eval(&subj)
@@ -252,5 +266,9 @@ module Assert
252
266
  end
253
267
  end
254
268
 
269
+ def current_results
270
+ @__running_test__.results
271
+ end
272
+
255
273
  end
256
274
  end
@@ -52,6 +52,12 @@ module Assert::Result
52
52
  (self.backtrace.filtered.first || self.test.context_info.called_from).to_s
53
53
  end
54
54
 
55
+ # chose to implement this way instead of using an `attr_writer` to be
56
+ # consistant with how you override exception backtraces.
57
+ def set_backtrace(bt)
58
+ @backtrace = Backtrace.new(bt || [])
59
+ end
60
+
55
61
  def ==(other)
56
62
  self.class == other.class && self.message == other.message
57
63
  end
@@ -110,7 +116,7 @@ module Assert::Result
110
116
  end
111
117
 
112
118
  # raised by the 'skip' context helper to break test execution
113
- class TestSkipped < RuntimeError; end
119
+ TestSkipped = Class.new(RuntimeError)
114
120
 
115
121
  class Skip < Base
116
122
 
@@ -1,3 +1,3 @@
1
1
  module Assert
2
- VERSION = "2.1.2"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -17,7 +17,7 @@ class Assert::Context
17
17
 
18
18
  should have_imeths :assert, :assert_not, :refute
19
19
  should have_imeths :skip, :pass, :fail, :flunk, :ignore
20
- should have_imeths :subject
20
+ should have_imeths :with_backtrace, :subject
21
21
 
22
22
  def test_should_collect_context_info
23
23
  this = @__running_test__
@@ -219,6 +219,33 @@ class Assert::Context
219
219
 
220
220
  end
221
221
 
222
+ class WithBacktraceTests < BasicTests
223
+ desc "with_backtrace method"
224
+ setup do
225
+ @from_bt = ['called_from_here']
226
+ @from_block = proc { ignore; fail; pass; skip 'todo' }
227
+ end
228
+
229
+ should "replace the fail results from the block with the given backtrace" do
230
+ @context.fail 'not affected'
231
+ begin
232
+ @context.with_backtrace(@from_bt, &@from_block)
233
+ rescue Assert::Result::TestSkipped => e
234
+ @test.results << Assert::Result::Skip.new(@test, e)
235
+ end
236
+
237
+ assert_equal 5, @test.results.size
238
+ norm_fail, with_ignore, with_fail, with_pass, with_skip = @test.results
239
+
240
+ assert_not_equal @from_bt, norm_fail.backtrace
241
+ assert_equal @from_bt, with_ignore.backtrace
242
+ assert_equal @from_bt, with_fail.backtrace
243
+ assert_equal @from_bt, with_pass.backtrace
244
+ assert_equal @from_bt, with_skip.backtrace
245
+ end
246
+
247
+ end
248
+
222
249
  class InspectTests < BasicTests
223
250
  desc "inspect method"
224
251
  setup do
@@ -40,6 +40,7 @@ module Assert::Result
40
40
 
41
41
  should have_readers :test, :message, :backtrace
42
42
  should have_imeths :test_name, :name, :to_sym, :to_s, :trace
43
+ should have_imeth :set_backtrace
43
44
 
44
45
  Assert::Result.types.keys.each do |type|
45
46
  should "respond to the instance method ##{type}?" do
@@ -64,6 +65,13 @@ module Assert::Result
64
65
  assert_equal exp, subject.inspect
65
66
  end
66
67
 
68
+ should "allow overriding the result backtrace with `set_backtrace`" do
69
+ subject.set_backtrace(['bt'])
70
+
71
+ assert_kind_of Assert::Result::Backtrace, subject.backtrace
72
+ assert_equal ['bt'], subject.backtrace
73
+ end
74
+
67
75
  end
68
76
 
69
77
  class ToStringTests < BaseTests
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 1
9
8
  - 2
10
- version: 2.1.2
9
+ - 0
10
+ version: 2.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-05-02 00:00:00 Z
19
+ date: 2013-06-03 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: ansi