spies 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +1 -0
- data/README.md +2 -2
- data/lib/spy.rb +76 -1
- data/lib/spy/call.rb +11 -1
- data/lib/spy/version.rb +1 -1
- data/spies.gemspec +2 -0
- metadata +30 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89291a4e20caa35e1a7315e244bf97f8c24912e0
|
4
|
+
data.tar.gz: 6a695fbb4d361366d3bff0d05abfb49841c77a1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e69fa38537a2d76b70b6ba66b2db62d5f0b83b4035db86bbbf543ae6d5a25f2105d0e042c637c6960a73dd54897c67ef43004045c947d1356935df6b4084ac0
|
7
|
+
data.tar.gz: 40e7ef09c780259cb68cfbebe6ca59a2d8bec128f139c4957c452730b6cbf233e36d80f1332bc97d7ea43ce206641b41bee57ab72c6c113018ffd0dfea915709
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
# Ruby Spy [![Build Status](https://travis-ci.org/patbenatar/ruby-spy.svg?branch=master)](https://travis-ci.org/patbenatar/ruby-spy) [![Coverage Status](https://coveralls.io/repos/github/patbenatar/ruby-spy/badge.svg?branch=master)](https://coveralls.io/github/patbenatar/ruby-spy?branch=master)
|
1
|
+
# Ruby Spy [![Build Status](https://travis-ci.org/patbenatar/ruby-spy.svg?branch=master)](https://travis-ci.org/patbenatar/ruby-spy) [![Coverage Status](https://coveralls.io/repos/github/patbenatar/ruby-spy/badge.svg?branch=master)](https://coveralls.io/github/patbenatar/ruby-spy?branch=master) [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/patbenatar/ruby-spy/master)
|
2
2
|
|
3
3
|
Test spies for Ruby. Why? For fun.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
```
|
7
|
+
```ruby
|
8
8
|
gem 'spies', require: 'spy'
|
9
9
|
```
|
10
10
|
|
data/lib/spy.rb
CHANGED
@@ -6,6 +6,12 @@ class Spy
|
|
6
6
|
|
7
7
|
THREAD_LOCAL_ACTIVE_SPIES_KEY = 'ruby_spy_active_spies'.freeze
|
8
8
|
|
9
|
+
##
|
10
|
+
# Active spies in the current thread.
|
11
|
+
#
|
12
|
+
# @return [Array<Spy>] instances of `Spy` that have active method spies on
|
13
|
+
# their target obj
|
14
|
+
#
|
9
15
|
def self.active_spies
|
10
16
|
Thread.current[THREAD_LOCAL_ACTIVE_SPIES_KEY] ||= []
|
11
17
|
end
|
@@ -18,10 +24,32 @@ class Spy
|
|
18
24
|
active_spies.delete(spy)
|
19
25
|
end
|
20
26
|
|
27
|
+
##
|
28
|
+
# Spy on an instance, class, or module.
|
29
|
+
#
|
30
|
+
# By default, this will spy on all user-defined methods (not methods defined
|
31
|
+
# on Ruby's base Class).
|
32
|
+
#
|
33
|
+
# @param obj [Object] the instance, class, or module to spy on
|
34
|
+
# @param method_name [Symbol] optionally limit spying to a single method
|
35
|
+
#
|
36
|
+
# @return [Spy] an active instance of `Spy` for the given `obj`
|
37
|
+
#
|
21
38
|
def self.on(obj, method_name = nil)
|
22
39
|
spy_with_options(obj, method_name)
|
23
40
|
end
|
24
41
|
|
42
|
+
##
|
43
|
+
# Spy on all instances of a class.
|
44
|
+
#
|
45
|
+
# By default, this will spy on all user-defined methods (not methods defined
|
46
|
+
# on Ruby's base Class).
|
47
|
+
#
|
48
|
+
# @param obj [Object] the class to spy on
|
49
|
+
# @param method_name [Symbol] optionally limit spying to a single method
|
50
|
+
#
|
51
|
+
# @return [Spy] an active instance of `Spy` for the given `obj`
|
52
|
+
#
|
25
53
|
def self.on_all_instances_of(obj, method_name = nil)
|
26
54
|
spy_with_options(obj, method_name, all_instances: true)
|
27
55
|
end
|
@@ -33,6 +61,13 @@ class Spy
|
|
33
61
|
end
|
34
62
|
end
|
35
63
|
|
64
|
+
##
|
65
|
+
# Remove all active Spy instances in the current thread and restore their
|
66
|
+
# spied methods to the original state.
|
67
|
+
#
|
68
|
+
# @yield Optionally provide a block, and spies created within the block will
|
69
|
+
# be cleaned, leaving the outer scope untouched. Blocks can be nested.
|
70
|
+
#
|
36
71
|
def self.clean
|
37
72
|
if block_given?
|
38
73
|
outer_active_spies = active_spies
|
@@ -45,10 +80,17 @@ class Spy
|
|
45
80
|
Thread.current[THREAD_LOCAL_ACTIVE_SPIES_KEY] = outer_active_spies
|
46
81
|
end
|
47
82
|
else
|
48
|
-
active_spies.dup.each
|
83
|
+
active_spies.dup.each do |spy|
|
84
|
+
rspec_mock?(spy) ? unregister(spy) : spy.clean
|
85
|
+
end
|
49
86
|
end
|
50
87
|
end
|
51
88
|
|
89
|
+
def self.rspec_mock?(obj)
|
90
|
+
defined?(RSpec::Mocks::Double) && obj.is_a?(RSpec::Mocks::Double)
|
91
|
+
end
|
92
|
+
|
93
|
+
# @return [Object] the instance, class, or module being spied on by this spy
|
52
94
|
attr_reader :obj
|
53
95
|
|
54
96
|
def initialize(obj, all_instances: false)
|
@@ -58,16 +100,38 @@ class Spy
|
|
58
100
|
@spied_methods_map = {}
|
59
101
|
end
|
60
102
|
|
103
|
+
##
|
104
|
+
# Spy on all user-defined methods on `obj`
|
105
|
+
#
|
106
|
+
# @return [Spy] self
|
107
|
+
#
|
61
108
|
def on_all
|
62
109
|
all_methods.each { |m| spy_on(m) }
|
63
110
|
Spy.register(self)
|
111
|
+
self
|
64
112
|
end
|
65
113
|
|
114
|
+
##
|
115
|
+
# Spy on a single method on `obj`
|
116
|
+
#
|
117
|
+
# @param method_name [Symbol] the method to spy on
|
118
|
+
#
|
119
|
+
# @return [Spy] self
|
120
|
+
#
|
66
121
|
def on(method_name)
|
67
122
|
spy_on(method_name)
|
68
123
|
Spy.register(self)
|
124
|
+
self
|
69
125
|
end
|
70
126
|
|
127
|
+
##
|
128
|
+
# Information about the calls received by this spy
|
129
|
+
#
|
130
|
+
# @param method_name [Symbol] optionally filter results to the given method
|
131
|
+
#
|
132
|
+
# @return [Array<Call>] set of `Call` objects containing information about
|
133
|
+
# each method call since the spy was activated.
|
134
|
+
#
|
71
135
|
def calls(method_name = nil)
|
72
136
|
if method_name
|
73
137
|
@calls.select { |c| c.method_name == method_name }
|
@@ -76,11 +140,22 @@ class Spy
|
|
76
140
|
end
|
77
141
|
end
|
78
142
|
|
143
|
+
##
|
144
|
+
# Remove spy and return spied methods on `obj` to the original state.
|
145
|
+
#
|
146
|
+
# @return [Spy] self
|
147
|
+
#
|
79
148
|
def clean
|
80
149
|
spied_methods_map.keys.each { |m| remove_spy(m) }
|
81
150
|
Spy.unregister(self)
|
151
|
+
self
|
82
152
|
end
|
83
153
|
|
154
|
+
##
|
155
|
+
# Check if spy is actively spying on any methods on `obj`
|
156
|
+
#
|
157
|
+
# @return [Boolean] dirty state
|
158
|
+
#
|
84
159
|
def dirty?
|
85
160
|
spied_methods_map.keys.any?
|
86
161
|
end
|
data/lib/spy/call.rb
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
class Spy::Call
|
2
|
-
|
2
|
+
# @return [Object] object that received the method call
|
3
|
+
attr_reader :receiver
|
4
|
+
|
5
|
+
# @return [Symbol] name of the method called
|
6
|
+
attr_reader :method_name
|
7
|
+
|
8
|
+
# @return [Array<Object>] arguments given to the method call
|
9
|
+
attr_reader :args
|
10
|
+
|
11
|
+
# @return [Proc] block given to the method call, if present
|
12
|
+
attr_reader :block
|
3
13
|
|
4
14
|
def initialize(receiver, method_name, args, block)
|
5
15
|
@receiver = receiver
|
data/lib/spy/version.rb
CHANGED
data/spies.gemspec
CHANGED
@@ -30,4 +30,6 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency 'pry-byebug', '~> 3.4'
|
31
31
|
spec.add_development_dependency 'rubocop', '~> 0.41.2'
|
32
32
|
spec.add_development_dependency 'coveralls'
|
33
|
+
spec.add_development_dependency 'yard'
|
34
|
+
spec.add_development_dependency 'redcarpet'
|
33
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Giancola
|
@@ -114,6 +114,34 @@ dependencies:
|
|
114
114
|
- - ">="
|
115
115
|
- !ruby/object:Gem::Version
|
116
116
|
version: '0'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: yard
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: redcarpet
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
117
145
|
description: Ruby test spies
|
118
146
|
email:
|
119
147
|
- nick@philosophie.is
|
@@ -125,6 +153,7 @@ files:
|
|
125
153
|
- ".rspec"
|
126
154
|
- ".rubocop.yml"
|
127
155
|
- ".travis.yml"
|
156
|
+
- ".yardopts"
|
128
157
|
- Gemfile
|
129
158
|
- Guardfile
|
130
159
|
- LICENSE.txt
|