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 +4 -4
- data/CHANGES.md +4 -0
- data/README.md +37 -1
- data/lib/muack.rb +6 -6
- data/lib/muack/session.rb +19 -6
- data/lib/muack/test.rb +1 -1
- data/lib/muack/version.rb +1 -1
- data/muack.gemspec +3 -3
- data/test/test_from_readme.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a9ba1c6a1c4f4eb8a2a2899c4a140aaf344c0d4b
|
|
4
|
+
data.tar.gz: e258c9b2c1351d7ab3e3d11c40855f07d5791893
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e3b9ee3b07c99e2a7522086045d7e1eaec8f90b989c8bd3113ad963c98d4735a14b6cfee956156802963fd75ec723559bfeff91b95dd578998c49b67de8ce78a
|
|
7
|
+
data.tar.gz: 0db81e2e647e106f5173436caf64ebc5aa321089068263b64e6dc4b627d28edb730841c1b75406bbbb838da6eeaa86d6399078ff3e7092bf6b243d6f757e02a4
|
data/CHANGES.md
CHANGED
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.
|
data/lib/muack.rb
CHANGED
|
@@ -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
|
data/lib/muack/session.rb
CHANGED
|
@@ -14,14 +14,27 @@ module Muack
|
|
|
14
14
|
(@others ||= {})["ai #{kls.__id__}"] ||= AnyInstanceOf.new(kls)
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def verify
|
|
18
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
data/lib/muack/test.rb
CHANGED
data/lib/muack/version.rb
CHANGED
data/muack.gemspec
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
|
2
|
-
# stub: muack 1.0.
|
|
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.
|
|
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
|
+
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 = [
|
data/test/test_from_readme.rb
CHANGED
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.
|
|
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
|
+
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.
|