muack 1.0.3 → 1.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7434ef1baf3aaeb9f7772ce6a8eedeb431132ba
4
- data.tar.gz: e55ae09a1166fcd29e2cf50379af01a2d1e508fc
3
+ metadata.gz: a9ba1c6a1c4f4eb8a2a2899c4a140aaf344c0d4b
4
+ data.tar.gz: e258c9b2c1351d7ab3e3d11c40855f07d5791893
5
5
  SHA512:
6
- metadata.gz: fc8ef5fffdfba3f044999cb2afb9e6ac612f2b0fa04db224590e9bbf6d720d4e73f418b509510e61d89a80819dfbfc8d56cef3ed40e1bb1009e48e2b83509ec9
7
- data.tar.gz: 3b1181888ebc6130e215caa86c66e11c083b8dbbb7f6198c64b53fa93efc4a866fbcd7c3ebac8261bc86359eb15a37f9d7da1fd4b309b68b2b6818685a0780dc
6
+ metadata.gz: e3b9ee3b07c99e2a7522086045d7e1eaec8f90b989c8bd3113ad963c98d4735a14b6cfee956156802963fd75ec723559bfeff91b95dd578998c49b67de8ce78a
7
+ data.tar.gz: 0db81e2e647e106f5173436caf64ebc5aa321089068263b64e6dc4b627d28edb730841c1b75406bbbb838da6eeaa86d6399078ff3e7092bf6b243d6f757e02a4
data/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGES
2
2
 
3
+ ## Muack 1.0.4 -- 2014-03-29
4
+
5
+ * Now we could `Muack.verify(obj)` or `Muack.reset(obj)` a single object.
6
+
3
7
  ## Muack 1.0.3 -- 2014-03-11
4
8
 
5
9
  * From now on, `Muack::API.hash_including` could accept `Muack::Satisfy` as
data/README.md CHANGED
@@ -39,7 +39,7 @@ Here's a quick example using [Bacon][].
39
39
  require 'bacon'
40
40
  require 'muack'
41
41
 
42
- include Muack::API
42
+ Bacon::Context.__send__(:include, Muack::API)
43
43
 
44
44
  describe 'Hello' do
45
45
  before{ Muack.reset }
@@ -133,6 +133,42 @@ p Muack.verify # true
133
133
  However you should not mix mocks and stubs with the same method, or you
134
134
  might encounter some unexpected result. Jump to _Caveat_ for more detail.
135
135
 
136
+ ### reset and verify
137
+
138
+ Calling `Muack.reset` is essentially resetting all mocks, returning all
139
+ objects/classes back to their original states. In the very first example,
140
+ we do this in a before block to make sure that we're in a clean state.
141
+
142
+ Calling `Muack.verify` is essentially verifying if all mocks and spies are
143
+ satisfied, if so, it would return true; otherwise, raising an exception.
144
+ Then, no matter verification passed or not, Muack would reset itself.
145
+
146
+ That means we don't really need to call `Muack.reset` in a before block if
147
+ we're pretty sure that all test cases would call `Muack.verify` in the end,
148
+ resetting everything.
149
+
150
+ On the other hand, we could also reset or verify a single object without
151
+ affecting the others. This is helpful in the cases of mocking some very
152
+ basic objects like Time, without causing too much side effect.
153
+
154
+ ``` ruby
155
+ name = 'str'
156
+ stub(name).to_s{ 'hi' }
157
+ stub(Time).new { Time.at(0) }
158
+ mock(Time).now { Time.new }
159
+
160
+ p name.to_s # 'hi'
161
+ p Time.now.to_i # 0
162
+ p Time.new.to_i # 0
163
+ p Muack.verify(Time) # true
164
+ p name.to_s # 'hi'
165
+ p Time.now.to_i > 0 # true
166
+ p Time.new.to_i > 0 # true
167
+ Muack.reset(name)
168
+ p name.to_s # 'str'
169
+ p Muack.verify # true
170
+ ```
171
+
136
172
  #### Anonymous mode
137
173
 
138
174
  Sometimes we just want to stub something without a concrete object in mind.
@@ -3,20 +3,20 @@ require 'muack/session'
3
3
  require 'muack/satisfy'
4
4
 
5
5
  module Muack
6
- def self.verify
7
- session.verify
6
+ def self.verify obj=nil
7
+ session.verify(obj)
8
8
  ensure
9
- reset
9
+ reset(obj)
10
10
  end
11
11
 
12
12
  def self.session
13
13
  Thread.current[:muack_session] ||= Muack::Session.new
14
14
  end
15
15
 
16
- def self.reset
16
+ def self.reset obj=nil
17
17
  session = Thread.current[:muack_session]
18
- session && session.reset
19
- Thread.current[:muack_session] = nil
18
+ session && session.reset(obj)
19
+ Thread.current[:muack_session] = nil unless obj
20
20
  end
21
21
 
22
22
  module API
@@ -14,14 +14,27 @@ module Muack
14
14
  (@others ||= {})["ai #{kls.__id__}"] ||= AnyInstanceOf.new(kls)
15
15
  end
16
16
 
17
- def verify
18
- each_value.all?(&:__mock_verify)
17
+ def verify obj=nil
18
+ if obj
19
+ with(obj, :[]).all?(&:__mock_verify)
20
+ else
21
+ each_value.all?(&:__mock_verify)
22
+ end
19
23
  end
20
24
 
21
- def reset
22
- instance_variable_defined?(:@others) && @others.clear
23
- reverse_each{ |_, m| m.__mock_reset }
24
- clear
25
+ def reset obj=nil
26
+ if obj
27
+ with(obj, :delete).each(&:__mock_reset)
28
+ else
29
+ instance_variable_defined?(:@others) && @others.clear
30
+ reverse_each{ |_, m| m.__mock_reset }
31
+ clear
32
+ end
33
+ end
34
+
35
+ private
36
+ def with obj, meth
37
+ %w[mk sb sy].map{ |k| __send__(meth, "#{k} #{obj.__id__}") }.compact
25
38
  end
26
39
  end
27
40
  end
@@ -3,7 +3,7 @@ require 'bacon'
3
3
  require 'muack'
4
4
 
5
5
  Bacon.summary_on_exit
6
- include Muack::API
6
+ Bacon::Context.__send__(:include, Muack::API)
7
7
 
8
8
  Obj = Object.new
9
9
  Str = 'Moo'
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Muack
3
- VERSION = '1.0.3'
3
+ VERSION = '1.0.4'
4
4
  end
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: muack 1.0.3 ruby lib
2
+ # stub: muack 1.0.4 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "muack"
6
- s.version = "1.0.3"
6
+ s.version = "1.0.4"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["Lin Jen-Shin (godfat)"]
11
- s.date = "2014-03-11"
11
+ s.date = "2014-03-29"
12
12
  s.description = "Muack -- A fast, small, yet powerful mocking library.\n\nInspired by [RR][], and it's 32x times faster (750s vs 23s) than RR\nfor running [Rib][] tests.\n\n[RR]: https://github.com/rr/rr\n[Rib]: https://github.com/godfat/rib"
13
13
  s.email = ["godfat (XD) godfat.org"]
14
14
  s.files = [
@@ -9,6 +9,8 @@ describe 'from README.md' do
9
9
  after{ Muack.reset }
10
10
 
11
11
  Context = Module.new{
12
+ include Muack::API
13
+
12
14
  def results; @results ||= []; end
13
15
  def p res ; results << res ; end
14
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: muack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lin Jen-Shin (godfat)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-11 00:00:00.000000000 Z
11
+ date: 2014-03-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  Muack -- A fast, small, yet powerful mocking library.