spy_rb 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/spy/callbacks/when.rb +13 -0
- data/lib/spy/callbacks/with_args.rb +14 -0
- data/lib/spy/instance.rb +33 -8
- data/lib/spy/version.rb +3 -0
- metadata +5 -3
- data/lib/spy/collection_entry.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8961d7ee67921ac362c6fc641cc406cfc794262b
|
4
|
+
data.tar.gz: 950d59327fceab861d83df830251e847e4474c0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd5e5a986047c5ad17c8f05807f2846f6bc142825af545b9eb954d9db7a429628b97f2da61e6abd45635c446b035c4e7f8c74c2cba9eff72ef48477d8332a1b6
|
7
|
+
data.tar.gz: 0a893d712ea97c103e9b62795fe6312ba763b85877037f74f38891dccbe8af9aacf74facc030ee023428563e9599fc23bb68184a48a21e36fd3d57795cb1137a
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Spy
|
2
|
+
module Callbacks
|
3
|
+
class WithArgs
|
4
|
+
def initialize(*args)
|
5
|
+
puts 'Spy::Callbacks::WithArgs is deprecated; use Spy::Callbacks::When instead'
|
6
|
+
@match_args = args
|
7
|
+
end
|
8
|
+
|
9
|
+
def before_call(*args)
|
10
|
+
@match_args == args
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/spy/instance.rb
CHANGED
@@ -1,34 +1,39 @@
|
|
1
|
+
require 'spy/callbacks/with_args'
|
2
|
+
require 'spy/callbacks/when'
|
3
|
+
|
1
4
|
# An instance of a spied method
|
2
5
|
# - Holds a reference to the original method
|
3
6
|
# - Wraps the original method
|
4
7
|
# - Provides hooks for callbacks
|
5
8
|
module Spy
|
6
9
|
class Instance
|
7
|
-
attr_reader :receiver, :method_type, :msg, :original, :call_count
|
10
|
+
attr_reader :receiver, :method_type, :msg, :original, :call_count, :visibility
|
8
11
|
|
9
12
|
def initialize(receiver, msg, method_type)
|
10
13
|
@msg = msg
|
11
14
|
@receiver = receiver
|
12
15
|
@method_type = method_type
|
16
|
+
@before_filters = []
|
17
|
+
@call_count = 0
|
13
18
|
|
14
19
|
# Cache the original method for unwrapping later
|
15
20
|
@original = @receiver.send(method_type, msg)
|
16
|
-
@
|
17
|
-
@match_args = []
|
21
|
+
@visibility = extract_visibility
|
18
22
|
end
|
19
23
|
|
20
24
|
def start
|
21
25
|
context = self
|
22
26
|
original.owner.instance_eval do
|
23
27
|
define_method context.msg do |*args|
|
28
|
+
context.before_call(*args)
|
24
29
|
if context.original.respond_to? :bind
|
25
30
|
result = context.original.bind(self).call(*args)
|
26
31
|
else
|
27
32
|
result = context.original.call(*args)
|
28
33
|
end
|
29
|
-
context.after_call(result, *args)
|
30
34
|
result
|
31
35
|
end
|
36
|
+
send(context.visibility, context.msg)
|
32
37
|
end
|
33
38
|
self
|
34
39
|
end
|
@@ -41,14 +46,34 @@ module Spy
|
|
41
46
|
self
|
42
47
|
end
|
43
48
|
|
44
|
-
def
|
45
|
-
|
46
|
-
@call_count += 1
|
49
|
+
def before_call(*args)
|
50
|
+
@call_count += 1 if @before_filters.all? {|f| f.before_call(*args)}
|
47
51
|
end
|
48
52
|
|
49
53
|
def with_args(*args)
|
50
|
-
|
54
|
+
add_before_filter Callbacks::WithArgs.new(*args)
|
55
|
+
end
|
56
|
+
|
57
|
+
def when(&block)
|
58
|
+
add_before_filter Callbacks::When.new(block)
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def add_before_filter(filter)
|
64
|
+
@before_filters << filter
|
51
65
|
self
|
52
66
|
end
|
67
|
+
|
68
|
+
def extract_visibility
|
69
|
+
owner = @original.owner
|
70
|
+
[:public, :protected, :private].each do |vis|
|
71
|
+
query = "#{vis}_method_defined?"
|
72
|
+
if owner.respond_to?(query) && owner.send(query, @msg)
|
73
|
+
return vis
|
74
|
+
end
|
75
|
+
end
|
76
|
+
raise NoMethodError, "couldn't find method #{@msg} belonging to #{owner}"
|
77
|
+
end
|
53
78
|
end
|
54
79
|
end
|
data/lib/spy/version.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spy_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Bodah
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Spy brings everything that's great about Sinon.JS to Ruby. Mocking frameworks
|
14
14
|
work by stubbing out functionality. Spy works by listening in on functionality and
|
@@ -21,12 +21,14 @@ extra_rdoc_files: []
|
|
21
21
|
files:
|
22
22
|
- lib/spy.rb
|
23
23
|
- lib/spy/api.rb
|
24
|
+
- lib/spy/callbacks/when.rb
|
25
|
+
- lib/spy/callbacks/with_args.rb
|
24
26
|
- lib/spy/collection.rb
|
25
27
|
- lib/spy/collection/entry.rb
|
26
|
-
- lib/spy/collection_entry.rb
|
27
28
|
- lib/spy/core.rb
|
28
29
|
- lib/spy/errors.rb
|
29
30
|
- lib/spy/instance.rb
|
31
|
+
- lib/spy/version.rb
|
30
32
|
homepage: https://github.com/jbodah/spy_rb
|
31
33
|
licenses:
|
32
34
|
- MIT
|
data/lib/spy/collection_entry.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
module Spy
|
2
|
-
class CollectionEntry < Struct.new(:receiver, :msg, :method_type)
|
3
|
-
attr_accessor :value
|
4
|
-
|
5
|
-
def key
|
6
|
-
"#{receiver.object_id}|#{msg}|#{method_type}"
|
7
|
-
end
|
8
|
-
|
9
|
-
def ==(other)
|
10
|
-
key == other.key
|
11
|
-
end
|
12
|
-
|
13
|
-
module ClassMethods
|
14
|
-
def parse(key)
|
15
|
-
new *parse_key(key)
|
16
|
-
end
|
17
|
-
|
18
|
-
def parse_key(key)
|
19
|
-
parts = key.split('|')
|
20
|
-
parts[0] = find_object(parts[0].to_i)
|
21
|
-
parts[1] = parts[1].to_sym
|
22
|
-
parts[2] = parts[2].to_sym
|
23
|
-
parts
|
24
|
-
end
|
25
|
-
|
26
|
-
# Looks up an object in the global ObjectSpace
|
27
|
-
def find_object(object_id)
|
28
|
-
ObjectSpace._id2ref(object_id)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
extend ClassMethods
|
33
|
-
end
|
34
|
-
end
|