spy_rb 0.1.5 → 0.2.0

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: 8961d7ee67921ac362c6fc641cc406cfc794262b
4
- data.tar.gz: 950d59327fceab861d83df830251e847e4474c0b
3
+ metadata.gz: 9c9735d3d8cbb2057082d9e5bc41ca566de15ac3
4
+ data.tar.gz: ab3225f2107d4adb711b0d357dcfc51ebad01cb5
5
5
  SHA512:
6
- metadata.gz: bd5e5a986047c5ad17c8f05807f2846f6bc142825af545b9eb954d9db7a429628b97f2da61e6abd45635c446b035c4e7f8c74c2cba9eff72ef48477d8332a1b6
7
- data.tar.gz: 0a893d712ea97c103e9b62795fe6312ba763b85877037f74f38891dccbe8af9aacf74facc030ee023428563e9599fc23bb68184a48a21e36fd3d57795cb1137a
6
+ metadata.gz: 15f2c77adff4bff6479b40b103e2d0283752e9626ecef92181822ccf42cf3ea6d682e586763340b982a4db2d238a698dc0430016ca3b60d8210a38374bd2d373
7
+ data.tar.gz: defdc2587687c8deaffff5baa84bd880680bdb5e040e9eb9c255d3af3108ee33e4bfa051cf0ffa9711d09ebe8de102ec74f32ebfeca71b6d776f53b1ad59c130
data/lib/spy/api.rb CHANGED
@@ -10,14 +10,15 @@ module Spy
10
10
  def on(*args)
11
11
  case args.length
12
12
  when 2
13
- return core.add_spy *(args << :method)
13
+ spied, msg = *args
14
+ return core.add_spy(spied, spied.method(msg))
14
15
  end
15
16
  raise ArgumentError
16
17
  end
17
18
 
18
19
  # TODO docs
19
- def on_any_instance(mod, msg)
20
- core.add_spy(mod, msg, :instance_method)
20
+ def on_any_instance(spied, msg)
21
+ core.add_spy(spied, spied.instance_method(msg))
21
22
  end
22
23
 
23
24
  # Stops spying on the method and restores its original functionality
@@ -37,9 +38,11 @@ module Spy
37
38
  when 1
38
39
  return core.remove_all_spies if args.first == :all
39
40
  when 2
40
- return core.remove_spy *(args << :method)
41
+ spied, msg = *args
42
+ return core.remove_spy(spied, spied.method(msg))
41
43
  when 3
42
- return core.remove_spy *args
44
+ spied, msg, method_type = *args
45
+ return core.remove_spy(spied, spied.send(method_type, msg))
43
46
  end
44
47
  raise ArgumentError
45
48
  end
@@ -31,13 +31,19 @@ module Spy
31
31
  # Add a slicker interface that abstracts away Collection::Entry
32
32
  module SpyHelper
33
33
  def <<(spy)
34
- entry = Collection::Entry.new(spy.receiver, spy.msg, spy.method_type)
34
+ receiver = spy.original.is_a?(Method) ? spy.original.receiver : nil
35
+ name = spy.original.name
36
+ klass = spy.original.class
37
+ entry = Collection::Entry.new(receiver, name, klass)
35
38
  entry.value = spy
36
39
  insert entry
37
40
  end
38
41
 
39
- def pop(receiver, msg, method_type)
40
- remove Collection::Entry.new(receiver, msg, method_type)
42
+ def pop(method)
43
+ receiver = method.is_a?(Method) ? method.receiver : nil
44
+ name = method.name
45
+ klass = method.class
46
+ remove Collection::Entry.new(receiver, name, klass)
41
47
  end
42
48
  end
43
49
 
data/lib/spy/core.rb CHANGED
@@ -8,14 +8,14 @@ module Spy
8
8
  @spy_collection ||= Collection.new
9
9
  end
10
10
 
11
- def add_spy(receiver, msg, method_type)
12
- spy = Instance.new(receiver, msg, method_type)
11
+ def add_spy(spied, method)
12
+ spy = Instance.new(spied, method)
13
13
  spy_collection << spy
14
14
  spy.start
15
15
  end
16
16
 
17
- def remove_spy(receiver, msg, method_type)
18
- spy = spy_collection.pop(receiver, msg, method_type)
17
+ def remove_spy(spied, method)
18
+ spy = spy_collection.pop(method)
19
19
  spy.stop
20
20
  end
21
21
 
@@ -0,0 +1,34 @@
1
+ module Spy
2
+ class Instance
3
+ module Strategy
4
+ class Intercept
5
+ def initialize(spy, intercept_target)
6
+ @spy = spy
7
+ @intercept_target = intercept_target
8
+ end
9
+
10
+ def apply
11
+ spy = @spy
12
+ @intercept_target.class_eval do
13
+ define_method spy.original.name do |*args|
14
+ spy.before_call(*args)
15
+ if spy.original.is_a?(UnboundMethod)
16
+ spy.original.bind(self).call(*args)
17
+ else
18
+ spy.original.call(*args)
19
+ end
20
+ end
21
+ send(spy.visibility, spy.original.name)
22
+ end
23
+ end
24
+
25
+ def undo
26
+ spy = @spy
27
+ @intercept_target.class_eval do
28
+ remove_method spy.original.name
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ module Spy
2
+ class Instance
3
+ module Strategy
4
+ class Wrap
5
+ def initialize(spy)
6
+ @spy = spy
7
+ end
8
+
9
+ def apply
10
+ spy = @spy
11
+ spy.original.owner.class_eval do
12
+ define_method spy.original.name do |*args|
13
+ spy.before_call(*args)
14
+ if spy.original.is_a?(UnboundMethod)
15
+ spy.original.bind(self).call(*args)
16
+ else
17
+ spy.original.call(*args)
18
+ end
19
+ end
20
+ send(spy.visibility, spy.original.name)
21
+ end
22
+ end
23
+
24
+ def undo
25
+ spy = @spy
26
+ spy.original.owner.class_eval do
27
+ remove_method spy.original.name
28
+ define_method spy.original.name, spy.original
29
+ send(spy.visibility, spy.original.name)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'spy/instance/strategy/wrap'
2
+ require 'spy/instance/strategy/intercept'
3
+
4
+ module Spy
5
+ class Instance
6
+ module Strategy
7
+ class << self
8
+ def factory_build(spy)
9
+ if spy.original.is_a?(Method)
10
+ if spy.original.owner == spy.spied.singleton_class
11
+ Strategy::Wrap.new(spy)
12
+ else
13
+ Strategy::Intercept.new(spy, spy.spied.singleton_class)
14
+ end
15
+ else
16
+ if spy.original.owner == spy.spied
17
+ Strategy::Wrap.new(spy)
18
+ else
19
+ Strategy::Intercept.new(spy, spy.spied)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/spy/instance.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'spy/callbacks/with_args'
2
2
  require 'spy/callbacks/when'
3
+ require 'spy/instance/strategy'
3
4
 
4
5
  # An instance of a spied method
5
6
  # - Holds a reference to the original method
@@ -7,42 +8,24 @@ require 'spy/callbacks/when'
7
8
  # - Provides hooks for callbacks
8
9
  module Spy
9
10
  class Instance
10
- attr_reader :receiver, :method_type, :msg, :original, :call_count, :visibility
11
+ attr_reader :original, :spied, :strategy, :call_count, :visibility
11
12
 
12
- def initialize(receiver, msg, method_type)
13
- @msg = msg
14
- @receiver = receiver
15
- @method_type = method_type
13
+ def initialize(spied, original)
14
+ @spied = spied
15
+ @original = original
16
+ @visibility = extract_visibility
16
17
  @before_filters = []
17
18
  @call_count = 0
18
-
19
- # Cache the original method for unwrapping later
20
- @original = @receiver.send(method_type, msg)
21
- @visibility = extract_visibility
19
+ @strategy = Strategy.factory_build(self)
22
20
  end
23
21
 
24
22
  def start
25
- context = self
26
- original.owner.instance_eval do
27
- define_method context.msg do |*args|
28
- context.before_call(*args)
29
- if context.original.respond_to? :bind
30
- result = context.original.bind(self).call(*args)
31
- else
32
- result = context.original.call(*args)
33
- end
34
- result
35
- end
36
- send(context.visibility, context.msg)
37
- end
23
+ @strategy.apply
38
24
  self
39
25
  end
40
26
 
41
27
  def stop
42
- context = self
43
- original.owner.instance_eval do
44
- define_method context.msg, context.original
45
- end
28
+ @strategy.undo
46
29
  self
47
30
  end
48
31
 
@@ -69,11 +52,11 @@ module Spy
69
52
  owner = @original.owner
70
53
  [:public, :protected, :private].each do |vis|
71
54
  query = "#{vis}_method_defined?"
72
- if owner.respond_to?(query) && owner.send(query, @msg)
55
+ if owner.respond_to?(query) && owner.send(query, @original.name)
73
56
  return vis
74
57
  end
75
58
  end
76
- raise NoMethodError, "couldn't find method #{@msg} belonging to #{owner}"
59
+ raise NoMethodError, "couldn't find method #{@original.name} belonging to #{owner}"
77
60
  end
78
61
  end
79
62
  end
data/lib/spy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Spy
2
- VERSION = '0.1.5'
2
+ VERSION = '0.2.0'
3
3
  end
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.5
4
+ version: 0.2.0
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-18 00:00:00.000000000 Z
11
+ date: 2015-02-27 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
@@ -28,6 +28,9 @@ files:
28
28
  - lib/spy/core.rb
29
29
  - lib/spy/errors.rb
30
30
  - lib/spy/instance.rb
31
+ - lib/spy/instance/strategy.rb
32
+ - lib/spy/instance/strategy/intercept.rb
33
+ - lib/spy/instance/strategy/wrap.rb
31
34
  - lib/spy/version.rb
32
35
  homepage: https://github.com/jbodah/spy_rb
33
36
  licenses:
@@ -54,4 +57,3 @@ signing_key:
54
57
  specification_version: 4
55
58
  summary: SinonJS-style Test Spies for Ruby
56
59
  test_files: []
57
- has_rdoc: