crispy 0.3.0 → 0.3.1

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
2
  SHA1:
3
- metadata.gz: 906a71958302bc1652930a6e9f1c5aa237eaa233
4
- data.tar.gz: 27951041fba2e5b62fb3d78d3b26311455ac5863
3
+ metadata.gz: f0ba38b20d7b6e7700337f66b764927162119554
4
+ data.tar.gz: 010fb0e6c5a5ca038d34233e183765d48e0ba4fd
5
5
  SHA512:
6
- metadata.gz: 569d2a0881f1f684974e70ed3e50c11d73038d1b07a29964521d6d50e6beaad11e3943e2b53455d9140707cc635f5b0e617fd4907608a919c59f990b833695c7
7
- data.tar.gz: 4b8dee5f09c25f3494b22ce3cf9a2f511489fb8d1ef70d430abf8e6b9eae99ae1aadb0c2add730c6037bdc32cbf41032a09f19b42912eb247780d05a5e0ba334
6
+ metadata.gz: 998646e708b914e987754ac308f65fd941f832f3ca0859dff0232651f195dabca73075bf66e2d3b875bc80741665053910b262ff2e5ba111a09d7b5ba7013992
7
+ data.tar.gz: 7e00aba14ee6804286283d024e8a568e40d01ba0eb2df9bcc7c11c0267247a3071281b7667bc5d350e561803d24b95a0744934d358d5ca3e541504e978b2b20f
data/ChangeLog.md CHANGED
@@ -1,8 +1,13 @@
1
+ # 0.3.1 (2015.1.3)
2
+
3
+ - Enhancement: Forget every spy log and reset every stubbed method by resetting. [#25](https://github.com/igrep/crispy/pull/25)
4
+ - Fix bug: Not spied anymore after removing the stubbed methods when resetting. [`cb38ddf`](https://github.com/igrep/crispy/commit/cb38ddf6f0affe2ea884e4a16d7622dca51c1f2d)
5
+
1
6
  # 0.3.0 (2014.12.31)
2
7
 
3
8
  - New Feature: Add stub feature to `ClassSpy`. [#23](https://github.com/igrep/crispy/pull/23)
4
9
  - New Feature: Now double is automatically spied without `spy_into`. [#21](https://github.com/igrep/crispy/pull/21)
5
- - Fix Bug: now `spy_into` replaces all stubber's methods with given stub spec when reinitialize a spy.[0f91579decbe27e6b05bec4b779dd1c3ede24380](https://github.com/igrep/crispy/commit/0f91579decbe27e6b05bec4b779dd1c3ede24380)
10
+ - Fix Bug: now `spy_into` replaces all stubber's methods with given stub spec when reinitialize a spy. [`0f9157`](https://github.com/igrep/crispy/commit/0f91579decbe27e6b05bec4b779dd1c3ede24380)
6
11
  - Fix Bug: reset spy log when `spy_into`-ing an already spied object. e.g. Class.[#20](https://github.com/igrep/crispy/pull/20)
7
12
  - New Feature: `spied?`. [#18](https://github.com/igrep/crispy/pull/18)
8
13
  - Refactor many internal classes and tests. [#21](https://github.com/igrep/crispy/pull/21) [#22](https://github.com/igrep/crispy/pull/22) [#23](https://github.com/igrep/crispy/pull/23) [#24](https://github.com/igrep/crispy/pull/24)
data/README.md CHANGED
@@ -268,11 +268,24 @@ Specify the **fully qualified name of the constant** instead of the constant its
268
268
  => "more cool value!"
269
269
  ```
270
270
 
271
- Then you can recover the stubbed constant value by `CrispyWorld.reset`.
271
+ ### Embedding Crispy into your Testing Framework
272
+
273
+ Remember to reset all the changes made by Crispy, call `CrispyWorld.reset`.
272
274
 
273
275
  ```ruby
274
276
  >> CrispyWorld.reset
275
277
 
278
+ >> spy(object).count_received :your_cool_method
279
+ => 0
280
+ >> spy(object).count_received :your_lovely_method
281
+ => 0
282
+ >> spy(object).received? :your_finalizer
283
+ => false
284
+
285
+ >> object.your_cool_method
286
+ => "cool!"
287
+ >> object.your_lovely_method
288
+ => "lovely!"
276
289
  >> YourCoolClass::YOUR_COOL_CONST
277
290
  => "value before stubbed"
278
291
  ```
@@ -70,6 +70,10 @@ module Crispy
70
70
  @registry[of_class] = spy
71
71
  end
72
72
 
73
+ def self.reset_all
74
+ @registry.each_value {|spy| spy.reinitialize }
75
+ end
76
+
73
77
  end
74
78
  end
75
79
  end
@@ -1,10 +1,14 @@
1
1
  require 'crispy/crispy_received_message'
2
2
  require 'crispy/crispy_internal/spy_base'
3
3
 
4
+ require 'weakref'
5
+
4
6
  module Crispy
5
7
  module CrispyInternal
6
8
  class Spy < SpyBase
7
9
 
10
+ @spies_to_reset = []
11
+
8
12
  attr_reader :received_messages
9
13
 
10
14
  def initialize target, stubs_map = {}
@@ -15,6 +19,8 @@ module Crispy
15
19
 
16
20
  @received_messages = []
17
21
  super
22
+
23
+ self.class.remember_to_reset_later self
18
24
  end
19
25
 
20
26
  def target_to_class target
@@ -37,6 +43,17 @@ module Crispy
37
43
  end
38
44
  private :append_received_message
39
45
 
46
+ def self.remember_to_reset_later spy
47
+ @spies_to_reset << ::WeakRef.new(spy)
48
+ end
49
+
50
+ def self.reset_all
51
+ # get rid of spies of GCed objects
52
+ @spies_to_reset.select! {|spy| spy.weakref_alive? }
53
+
54
+ @spies_to_reset.each {|spy| spy.reinitialize }
55
+ end
56
+
40
57
  end
41
58
  end
42
59
  end
@@ -21,14 +21,7 @@ module Crispy
21
21
 
22
22
  def self.new target, stubs_map = {}
23
23
  spy = self.of_target(target)
24
- if spy
25
- spy.restart
26
- spy.erase_log
27
- spy.reinitialize_stubber stubs_map
28
- spy
29
- else
30
- super
31
- end
24
+ spy ? spy.reinitialize(stubs_map) : super
32
25
  end
33
26
 
34
27
  def self.of_target target
@@ -51,6 +44,13 @@ module Crispy
51
44
  raise NotImplementedError
52
45
  end
53
46
 
47
+ def reinitialize stubs_map = {}
48
+ restart
49
+ erase_log
50
+ reinitialize_stubber stubs_map
51
+ self
52
+ end
53
+
54
54
  def stop
55
55
  @spying = false
56
56
  end
@@ -126,6 +126,7 @@ module Crispy
126
126
 
127
127
  def reinitialize_stubber stubs_map = {}
128
128
  remove_method(*@stubbed_methods)
129
+ @stubbed_methods.each {|stubbed_method| define_wrapper stubbed_method }
129
130
  @stubbed_methods.clear
130
131
  stub stubs_map
131
132
  end
@@ -6,6 +6,8 @@ module Crispy
6
6
 
7
7
  def reset
8
8
  ::Crispy::CrispyInternal::ConstChanger.recover_all
9
+ ::Crispy::CrispyInternal::Spy.reset_all
10
+ ::Crispy::CrispyInternal::ClassSpy.reset_all
9
11
  end
10
12
 
11
13
  end
@@ -1,3 +1,3 @@
1
1
  module Crispy
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -1,10 +1,12 @@
1
1
  class YourCoolClass
2
2
  YOUR_COOL_CONST = 'value before stubbed'.freeze
3
3
  def your_cool_method *args
4
+ 'cool!'
4
5
  end
5
6
  def your_method_without_argument
6
7
  end
7
8
  def your_lovely_method *args
9
+ 'lovely!'
8
10
  end
9
11
  def your_finalizer *args
10
12
  end
data/test/test_crispy.rb CHANGED
@@ -226,6 +226,24 @@ class TestCrispy < MiniTest::Test
226
226
  assert_equal 'xxx' , ObjectClass.stubbed_method3
227
227
  end
228
228
 
229
+ def test_spy_resets_stubbed_methods_after_resetting
230
+ CrispyWorld.reset
231
+ assert_equal 'before stubbed 1', ObjectClass.stubbed_method1
232
+ assert_equal 'before stubbed 2', ObjectClass.stubbed_method2
233
+ end
234
+
235
+ def test_spy_forgets_received_messages_after_resetting
236
+ CrispyWorld.reset
237
+ assert_empty @subject.received_messages
238
+ end
239
+
240
+ def test_spy_still_logs_methods_stubbed_once_after_resetting
241
+ CrispyWorld.reset
242
+
243
+ ObjectClass.stubbed_method1
244
+ assert_equal 1, spy(ObjectClass).count_received(:stubbed_method1)
245
+ end
246
+
229
247
  end
230
248
 
231
249
  class TestCrispySpyIntoInstances < TestCrispy
@@ -274,6 +292,22 @@ class TestCrispy < MiniTest::Test
274
292
  end
275
293
  end
276
294
 
295
+ def test_spy_resets_stubbed_methods_after_resetting
296
+ ::Crispy::CrispyWorld.reset
297
+
298
+ @object_instances.each do|object|
299
+ assert_equal 'method to stub 1 (before stubbed)', object.method_to_stub1
300
+ assert_equal 'method to stub 2 (before stubbed)', object.method_to_stub2
301
+ assert_equal 'method to stub 3 (before stubbed)', object.method_to_stub3
302
+ end
303
+ end
304
+
305
+ def test_spy_forgets_received_messages_after_resetting
306
+ ::Crispy::CrispyWorld.reset
307
+ assert_empty @subject.received_messages
308
+ assert_empty @subject.received_messages_with_receiver
309
+ end
310
+
277
311
  end
278
312
 
279
313
  class TestReceivedMessage < self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crispy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yamamoto Yuji
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-31 00:00:00.000000000 Z
11
+ date: 2015-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler