assert 2.15.1 → 2.15.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- data.tar.gz: 224cee40b0eced9582504764379f3f5dafae637c
4
- metadata.gz: 5858e3f1715252601de72d47678dc9f215267254
5
2
  SHA512:
6
- data.tar.gz: 6b3d06c534eb907ae92c281b36de62967138bbc3788d536c92ad03b8d28d637dcf0ad0526fb66610dcc9b6cbc894a44e786c312bcfd6f93ae1ea29319f8aaf6f
7
- metadata.gz: ed18f6eaea92fcec29d9b6b2200cb23704ad8b4090372493a81b67067b251176d71408231f8bc7e7c9de1a88c55cd854857d6fa153c67d79bf3a752b5fdb1df1
3
+ data.tar.gz: 87349e6848472856f049017257486230a2b69f52bcf4d5ebdf4d4a23f88dd5b479d1499890ea1c63e890186c4c86d665c982d162ebd86a51e07762b809029e26
4
+ metadata.gz: 539e868afc215555b585a85dd4caf52e9fbc7082e2eada2c33aea41900e22bb3a547c193b40f5433192bcd27571efd79254d93f54b7a973eab8fef0489398a55
5
+ SHA1:
6
+ data.tar.gz: fd4a2030e5b99394b4461d7673324e00fad58647
7
+ metadata.gz: bb7bf7dd4fb2b739e897932d9499487ac730e1fc
data/README.md CHANGED
@@ -89,6 +89,8 @@ myobj.mymeth(123)
89
89
  # => StubError: arity mismatch
90
90
  Assert.stub(myobj, :mymeth).with(123){ 'stub-meth' }
91
91
  # => StubError: arity mismatch
92
+ Assert.stub_send(myobj, :mymeth) # call to the original method post-stub
93
+ # => 'meth'
92
94
 
93
95
  Assert.stub(myobj, :myval){ 'stub-meth' }
94
96
  # => StubError: arity mismatch
@@ -99,6 +101,10 @@ myobj.myval(123)
99
101
  # => '123'
100
102
  myobj.myval(456)
101
103
  # => StubError: `myval(456)` not stubbed.
104
+ Assert.stub_send(myobj, :myval, 123) # call to the original method post-stub
105
+ # => 123
106
+ Assert.stub_send(myobj, :myval, 456)
107
+ # => 456
102
108
 
103
109
  Assert.unstub(myobj, :mymeth)
104
110
  Assert.unstub(myobj, :myval)
@@ -10,7 +10,7 @@ module Assert
10
10
 
11
11
  def initialize(config, test_paths, test_options)
12
12
  @config = config
13
- Assert::CLI.bench('Apply settings') do
13
+ Assert::CLI.bench('Applying settings') do
14
14
  apply_user_settings
15
15
  apply_local_settings
16
16
  apply_env_settings
@@ -25,7 +25,7 @@ module Assert
25
25
  def init(test_files, test_dir)
26
26
  # load any test helper file
27
27
  if test_dir && (h = File.join(test_dir, self.config.test_helper)) && File.exists?(h)
28
- Assert::CLI.bench('Require test helper'){ require h }
28
+ Assert::CLI.bench('Requiring test helper'){ require h }
29
29
  end
30
30
 
31
31
  if self.config.list
@@ -38,7 +38,7 @@ module Assert
38
38
  runner.before_load(test_files)
39
39
  suite.before_load(test_files)
40
40
  view.before_load(test_files)
41
- Assert::CLI.bench("Require #{test_files.size} test files") do
41
+ Assert::CLI.bench("Requiring #{test_files.size} test files") do
42
42
  test_files.each{ |p| require p }
43
43
  end
44
44
  if self.config.debug
@@ -11,16 +11,25 @@ module Assert
11
11
  args.include?('-d') || args.include?('--debug')
12
12
  end
13
13
 
14
- def self.debug_msg(msg, time_in_ms = nil)
15
- "[DEBUG] #{msg}#{" (#{time_in_ms} ms)" if time_in_ms}"
14
+ def self.debug_msg(msg)
15
+ "[DEBUG] #{msg}"
16
16
  end
17
17
 
18
- def self.bench(msg, &block)
18
+ def self.debug_start_msg(msg)
19
+ debug_msg("#{msg}...".ljust(30))
20
+ end
21
+
22
+ def self.debug_finish_msg(time_in_ms)
23
+ " (#{time_in_ms} ms)"
24
+ end
25
+
26
+ def self.bench(start_msg, &block)
19
27
  if !Assert.config.debug
20
28
  block.call; return
21
29
  end
30
+ print debug_start_msg(start_msg)
22
31
  RoundedMillisecondTime.new(Benchmark.measure(&block).real).tap do |time_in_ms|
23
- puts debug_msg(msg, time_in_ms)
32
+ puts debug_finish_msg(time_in_ms)
24
33
  end
25
34
  end
26
35
 
@@ -4,20 +4,30 @@ module Assert
4
4
  @stubs ||= {}
5
5
  end
6
6
 
7
- def self.stub(*args, &block)
8
- (self.stubs[Assert::Stub.key(*args)] ||= Assert::Stub.new(*args)).tap do |s|
9
- s.do = block
10
- end
7
+ def self.stub(obj, meth, &block)
8
+ (self.stubs[Assert::Stub.key(obj, meth)] ||= begin
9
+ orig_caller = caller
10
+ Assert::Stub.new(obj, meth, orig_caller)
11
+ end).tap{ |s| s.do = block }
11
12
  end
12
13
 
13
- def self.unstub(*args)
14
- (self.stubs.delete(Assert::Stub.key(*args)) || Assert::Stub::NullStub.new).teardown
14
+ def self.unstub(obj, meth)
15
+ (self.stubs.delete(Assert::Stub.key(obj, meth)) || Assert::Stub::NullStub.new).teardown
15
16
  end
16
17
 
17
18
  def self.unstub!
18
19
  self.stubs.keys.each{ |key| self.stubs.delete(key).teardown }
19
20
  end
20
21
 
22
+ def self.stub_send(obj, meth, *args, &block)
23
+ orig_caller = caller
24
+ stub = self.stubs.fetch(Assert::Stub.key(obj, meth)) do
25
+ msg = "`#{meth}` not stubbed"
26
+ raise NotStubbedError.new(msg).tap{ |e| e.set_backtrace(orig_caller) }
27
+ end
28
+ stub.call_method(args, &block)
29
+ end
30
+
21
31
  StubError = Class.new(ArgumentError)
22
32
  NotStubbedError = Class.new(StubError)
23
33
  StubArityError = Class.new(StubError)
@@ -34,52 +44,53 @@ module Assert
34
44
 
35
45
  attr_reader :method_name, :name, :ivar_name, :do
36
46
 
37
- def initialize(object, method_name, &block)
47
+ def initialize(object, method_name, orig_caller = nil, &block)
48
+ orig_caller ||= caller
38
49
  @metaclass = class << object; self; end
39
50
  @method_name = method_name.to_s
40
51
  @name = "__assert_stub__#{object.object_id}_#{@method_name}"
41
52
  @ivar_name = "@__assert_stub_#{object.object_id}_" \
42
53
  "#{@method_name.to_sym.object_id}"
43
54
 
44
- setup(object)
55
+ setup(object, orig_caller)
45
56
 
46
- @do = block || Proc.new do |*args, &block|
47
- err_msg = "#{inspect_call(args)} not stubbed."
48
- inspect_lookup_stubs.tap do |stubs|
49
- err_msg += "\nStubs:\n#{stubs}" if !stubs.empty?
50
- end
51
- raise NotStubbedError, err_msg
52
- end
53
- @lookup = Hash.new{ |hash, key| self.do }
57
+ @do = block
58
+ @lookup = {}
59
+ end
60
+
61
+ def do=(block)
62
+ @do = block || @do
54
63
  end
55
64
 
56
- def call(*args, &block)
65
+ def call_method(args, &block)
66
+ @method.call(*args, &block)
67
+ end
68
+
69
+ def call(args, orig_caller = nil, &block)
70
+ orig_caller ||= caller
57
71
  unless arity_matches?(args)
58
- message = "arity mismatch on `#{@method_name}`: " \
59
- "expected #{number_of_args(@method.arity)}, " \
60
- "called with #{args.size}"
61
- raise StubArityError, message
72
+ msg = "arity mismatch on `#{@method_name}`: " \
73
+ "expected #{number_of_args(@method.arity)}, " \
74
+ "called with #{args.size}"
75
+ raise StubArityError.new(msg).tap{ |e| e.set_backtrace(orig_caller) }
62
76
  end
63
- @lookup[args].call(*args, &block)
77
+ lookup(args, orig_caller).call(*args, &block)
64
78
  rescue NotStubbedError => exception
65
79
  @lookup.rehash
66
- @lookup[args].call(*args, &block)
80
+ lookup(args, orig_caller).call(*args, &block)
67
81
  end
68
82
 
69
83
  def with(*args, &block)
84
+ orig_caller = caller
70
85
  unless arity_matches?(args)
71
- message = "arity mismatch on `#{@method_name}`: " \
72
- "expected #{number_of_args(@method.arity)}, " \
73
- "stubbed with #{args.size}"
74
- raise StubArityError, message
86
+ msg = "arity mismatch on `#{@method_name}`: " \
87
+ "expected #{number_of_args(@method.arity)}, " \
88
+ "stubbed with #{args.size}"
89
+ raise StubArityError.new(msg).tap{ |e| e.set_backtrace(orig_caller) }
75
90
  end
76
91
  @lookup[args] = block
77
92
  end
78
93
 
79
- def do=(block)
80
- @do = block || @do
81
- end
82
-
83
94
  def teardown
84
95
  @metaclass.send(:undef_method, @method_name)
85
96
  Assert.send(:remove_instance_variable, @ivar_name)
@@ -95,9 +106,10 @@ module Assert
95
106
 
96
107
  protected
97
108
 
98
- def setup(object)
109
+ def setup(object, orig_caller)
99
110
  unless object.respond_to?(@method_name)
100
- raise StubError, "#{object.inspect} does not respond to `#{@method_name}`"
111
+ msg = "#{object.inspect} does not respond to `#{@method_name}`"
112
+ raise StubError.new(msg).tap{ |e| e.set_backtrace(orig_caller) }
101
113
  end
102
114
  is_constant = object.kind_of?(Module)
103
115
  local_object_methods = object.methods(false).map(&:to_s)
@@ -118,13 +130,25 @@ module Assert
118
130
  Assert.instance_variable_set(@ivar_name, self)
119
131
  @metaclass.class_eval <<-stub_method
120
132
  def #{@method_name}(*args, &block)
121
- Assert.instance_variable_get("#{@ivar_name}").call(*args, &block)
133
+ Assert.instance_variable_get("#{@ivar_name}").call(args, caller, &block)
122
134
  end
123
135
  stub_method
124
136
  end
125
137
 
126
138
  private
127
139
 
140
+ def lookup(args, orig_caller)
141
+ @lookup.fetch(args) do
142
+ self.do || begin
143
+ msg = "#{inspect_call(args)} not stubbed."
144
+ inspect_lookup_stubs.tap do |stubs|
145
+ msg += "\nStubs:\n#{stubs}" if !stubs.empty?
146
+ end
147
+ raise NotStubbedError.new(msg).tap{ |e| e.set_backtrace(orig_caller) }
148
+ end
149
+ end
150
+ end
151
+
128
152
  def arity_matches?(args)
129
153
  return true if @method.arity == args.size # mandatory args
130
154
  return true if @method.arity < 0 && args.size >= (@method.arity+1).abs # variable args
@@ -1,3 +1,3 @@
1
1
  module Assert
2
- VERSION = "2.15.1"
2
+ VERSION = "2.15.2"
3
3
  end
@@ -10,7 +10,7 @@ module Assert
10
10
  subject { Assert }
11
11
 
12
12
  should have_imeths :config, :configure, :view, :suite, :runner
13
- should have_imeths :stubs, :stub, :unstub, :unstub!
13
+ should have_imeths :stubs, :stub, :unstub, :unstub!, :stub_send
14
14
 
15
15
  should "know its config instance" do
16
16
  assert_kind_of Assert::Config, subject.config
@@ -29,10 +29,14 @@ module Assert
29
29
 
30
30
  class StubTests < UnitTests
31
31
  setup do
32
+ @orig_value = Factory.string
33
+ @stub_value = Factory.string
34
+
32
35
  @myclass = Class.new do
33
- def mymeth; 'meth'; end
36
+ def initialize(value); @value = value; end
37
+ def mymeth; @value; end
34
38
  end
35
- @myobj = @myclass.new
39
+ @myobj = @myclass.new(@orig_value)
36
40
  end
37
41
 
38
42
  should "build a stub" do
@@ -49,31 +53,31 @@ module Assert
49
53
  should "set the stub's do block if given a block" do
50
54
  Assert.stub(@myobj, :mymeth)
51
55
  assert_raises(NotStubbedError){ @myobj.mymeth }
52
- Assert.stub(@myobj, :mymeth){ 'mymeth' }
53
- assert_equal 'mymeth', @myobj.mymeth
56
+ Assert.stub(@myobj, :mymeth){ @stub_value }
57
+ assert_equal @stub_value, @myobj.mymeth
54
58
  end
55
59
 
56
60
  should "teardown stubs" do
57
- assert_equal 'meth', @myobj.mymeth
61
+ assert_equal @orig_value, @myobj.mymeth
58
62
  Assert.unstub(@myobj, :mymeth)
59
- assert_equal 'meth', @myobj.mymeth
63
+ assert_equal @orig_value, @myobj.mymeth
60
64
 
61
- assert_equal 'meth', @myobj.mymeth
62
- Assert.stub(@myobj, :mymeth){ 'mymeth' }
63
- assert_equal 'mymeth', @myobj.mymeth
65
+ assert_equal @orig_value, @myobj.mymeth
66
+ Assert.stub(@myobj, :mymeth){ @stub_value }
67
+ assert_equal @stub_value, @myobj.mymeth
64
68
  Assert.unstub(@myobj, :mymeth)
65
- assert_equal 'meth', @myobj.mymeth
69
+ assert_equal @orig_value, @myobj.mymeth
66
70
  end
67
71
 
68
72
  should "know and teardown all stubs" do
69
- assert_equal 'meth', @myobj.mymeth
73
+ assert_equal @orig_value, @myobj.mymeth
70
74
 
71
- Assert.stub(@myobj, :mymeth){ 'mymeth' }
72
- assert_equal 'mymeth', @myobj.mymeth
75
+ Assert.stub(@myobj, :mymeth){ @stub_value }
76
+ assert_equal @stub_value, @myobj.mymeth
73
77
  assert_equal 1, Assert.stubs.size
74
78
 
75
79
  Assert.unstub!
76
- assert_equal 'meth', @myobj.mymeth
80
+ assert_equal @orig_value, @myobj.mymeth
77
81
  assert_empty Assert.stubs
78
82
  end
79
83
 
@@ -91,6 +95,15 @@ module Assert
91
95
  assert_empty Assert.stubs
92
96
  end
93
97
 
98
+ should "be able to call a stub's original method" do
99
+ assert_raises(NotStubbedError){ Assert.stub_send(@myobj, :mymeth) }
100
+
101
+ Assert.stub(@myobj, :mymeth){ @stub_value }
102
+
103
+ assert_equal @stub_value, @myobj.mymeth
104
+ assert_equal @orig_value, Assert.stub_send(@myobj, :mymeth)
105
+ end
106
+
94
107
  end
95
108
 
96
109
  end
@@ -24,6 +24,7 @@ class Assert::Stub
24
24
  should have_readers :method_name, :name, :ivar_name, :do
25
25
  should have_writers :do
26
26
  should have_cmeths :key
27
+ should have_imeths :call_method, :call, :with, :teardown
27
28
 
28
29
  should "generate a key given an object and method name" do
29
30
  obj = @myobj
@@ -41,9 +42,7 @@ class Assert::Stub
41
42
  end
42
43
 
43
44
  should "complain when called if no do block was given" do
44
- assert_raises Assert::NotStubbedError do
45
- @myobj.mymeth
46
- end
45
+ assert_raises(Assert::NotStubbedError){ @myobj.mymeth }
47
46
 
48
47
  subject.do = proc{ 'mymeth' }
49
48
  assert_nothing_raised do
@@ -56,9 +55,7 @@ class Assert::Stub
56
55
  end
57
56
 
58
57
  should "complain if stubbing a method that the object doesn't respond to" do
59
- assert_raises Assert::StubError do
60
- Assert::Stub.new(@myobj, :some_other_meth)
61
- end
58
+ assert_raises(Assert::StubError){ Assert::Stub.new(@myobj, :some_other_meth) }
62
59
  end
63
60
 
64
61
  should "complain if stubbed and called with no `do` proc given" do
@@ -187,6 +184,54 @@ class Assert::Stub
187
184
  assert_equal 'four-five-six', mydelegator.myvalargs(4,5,6)
188
185
  end
189
186
 
187
+ should "call to the original method" do
188
+ subject.teardown
189
+
190
+ # no args
191
+ stub = Assert::Stub.new(@myobj, :mymeth){ 'mymeth' }
192
+ assert_equal 'mymeth', stub.call([])
193
+ assert_equal 'meth', stub.call_method([])
194
+
195
+ # static args
196
+ stub = Assert::Stub.new(@myobj, :myval){ |val| val.to_s }
197
+ assert_equal '1', stub.call([1])
198
+ assert_equal 1, stub.call_method([1])
199
+ assert_equal '2', stub.call([2])
200
+ assert_equal 2, stub.call_method([2])
201
+ stub.with(2){ 'two' }
202
+ assert_equal 'two', stub.call([2])
203
+ assert_equal 2, stub.call_method([2])
204
+
205
+ # dynamic args
206
+ stub = Assert::Stub.new(@myobj, :myargs){ |*args| args.join(',') }
207
+ assert_equal '1,2', stub.call([1,2])
208
+ assert_equal [1,2], stub.call_method([1,2])
209
+ assert_equal '3,4,5', stub.call([3,4,5])
210
+ assert_equal [3,4,5], stub.call_method([3,4,5])
211
+ stub.with(3,4,5){ 'three-four-five' }
212
+ assert_equal 'three-four-five', stub.call([3,4,5])
213
+ assert_equal [3,4,5], stub.call_method([3,4,5])
214
+
215
+ # mixed static/dynamic args
216
+ stub = Assert::Stub.new(@myobj, :myvalargs){ |*args| args.join(',') }
217
+ assert_equal '1,2,3', stub.call([1,2,3])
218
+ assert_equal [1,2, [3]], stub.call_method([1,2,3])
219
+ assert_equal '3,4,5', stub.call([3,4,5])
220
+ assert_equal [3,4,[5]], stub.call_method([3,4,5])
221
+ stub.with(3,4,5){ 'three-four-five' }
222
+ assert_equal 'three-four-five', stub.call([3,4,5])
223
+ assert_equal [3,4,[5]], stub.call_method([3,4,5])
224
+
225
+ # blocks
226
+ blkcalled = false
227
+ blk = proc{ blkcalled = true }
228
+ stub = Assert::Stub.new(@myobj, :myblk){ blkcalled = 'true' }
229
+ stub.call([], &blk)
230
+ assert_equal 'true', blkcalled
231
+ stub.call_method([], &blk)
232
+ assert_equal true, blkcalled
233
+ end
234
+
190
235
  should "store and remove itself on asserts main module" do
191
236
  assert_equal subject, Assert.instance_variable_get(subject.ivar_name)
192
237
  subject.teardown
@@ -222,7 +267,7 @@ class Assert::Stub
222
267
  subject{ @stub }
223
268
 
224
269
  should "not raise a stub error when called" do
225
- assert_nothing_raised{ @stub.call(@arg) }
270
+ assert_nothing_raised{ @stub.call([@arg]) }
226
271
  end
227
272
 
228
273
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.15.1
4
+ version: 2.15.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Redding
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2016-03-29 00:00:00 Z
13
+ date: 2016-04-13 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Assertion style testing framework.