spy 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.travis.yml +5 -0
  2. data/Gemfile +1 -0
  3. data/README.md +22 -7
  4. data/Rakefile +2 -0
  5. data/lib/spy.rb +39 -6
  6. data/lib/spy/agency.rb +42 -27
  7. data/lib/spy/call_log.rb +26 -0
  8. data/lib/spy/constant.rb +72 -14
  9. data/lib/spy/core_ext/marshal.rb +1 -0
  10. data/lib/spy/double.rb +17 -0
  11. data/lib/spy/nest.rb +27 -0
  12. data/lib/spy/subroutine.rb +146 -44
  13. data/lib/spy/version.rb +1 -1
  14. data/spec/spy/any_instance_spec.rb +518 -0
  15. data/spec/spy/mock_spec.rb +46 -554
  16. data/spec/spy/mutate_const_spec.rb +21 -63
  17. data/spec/spy/null_object_mock_spec.rb +11 -39
  18. data/spec/spy/partial_mock_spec.rb +3 -62
  19. data/spec/spy/stash_spec.rb +30 -37
  20. data/spec/spy/stub_spec.rb +0 -6
  21. data/spec/spy/to_ary_spec.rb +5 -5
  22. data/test/integration/test_constant_spying.rb +1 -1
  23. data/test/integration/test_instance_method.rb +32 -0
  24. data/test/integration/test_subroutine_spying.rb +7 -4
  25. data/test/spy/test_double.rb +4 -0
  26. data/test/spy/test_subroutine.rb +28 -3
  27. data/test/support/pen.rb +15 -0
  28. metadata +8 -30
  29. data/spec/spy/bug_report_10260_spec.rb +0 -8
  30. data/spec/spy/bug_report_10263_spec.rb +0 -24
  31. data/spec/spy/bug_report_496_spec.rb +0 -18
  32. data/spec/spy/bug_report_600_spec.rb +0 -24
  33. data/spec/spy/bug_report_7611_spec.rb +0 -16
  34. data/spec/spy/bug_report_8165_spec.rb +0 -31
  35. data/spec/spy/bug_report_830_spec.rb +0 -21
  36. data/spec/spy/bug_report_957_spec.rb +0 -22
  37. data/spec/spy/double_spec.rb +0 -12
  38. data/spec/spy/failing_argument_matchers_spec.rb +0 -94
  39. data/spec/spy/options_hash_spec.rb +0 -35
  40. data/spec/spy/precise_counts_spec.rb +0 -68
  41. data/spec/spy/stubbed_message_expectations_spec.rb +0 -47
  42. data/spec/spy/test_double_spec.rb +0 -54
@@ -22,7 +22,7 @@ class TestConstantSpying < MiniTest::Unit::TestCase
22
22
  end
23
23
  end
24
24
 
25
- def setup
25
+ def teardown
26
26
  Spy::Agency.instance.dissolve!
27
27
  end
28
28
 
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ class TestAnyInstanceOf < MiniTest::Unit::TestCase
4
+ class Foo
5
+ def bar
6
+ "foobar"
7
+ end
8
+ end
9
+
10
+ class Bar < Foo
11
+ def bar
12
+ super
13
+ end
14
+ end
15
+
16
+ def teardown
17
+ Spy::Agency.instance.dissolve!
18
+ end
19
+
20
+ def test_it_overides_all_methods
21
+ assert_equal Foo.new.bar, "foobar"
22
+ spy = Spy.on_instance_method(Foo, bar: "timshel")
23
+ assert_equal spy, Spy::Subroutine.get(Foo, :bar, false)
24
+ assert_equal "timshel", Foo.new.bar
25
+ assert_equal "timshel", Foo.new.bar
26
+ assert_equal "timshel", Bar.new.bar
27
+ assert_equal 3, spy.calls.size
28
+
29
+ spy = Spy.off_instance_method(Foo, :bar)
30
+ assert_equal Foo.new.bar, "foobar"
31
+ end
32
+ end
@@ -2,13 +2,16 @@ require 'test_helper'
2
2
 
3
3
  class TestSpy < MiniTest::Unit::TestCase
4
4
  def setup
5
- Spy::Agency.instance.dissolve!
6
5
  @pen = Pen.new
7
6
  end
8
7
 
8
+ def teardown
9
+ Spy::Agency.instance.dissolve!
10
+ end
11
+
9
12
  def test_spy_on_hooks_and_saves_spy_with_array
10
- pen_write_spy, pen_write_hello_spy = Spy::Subroutine.on(@pen, :write, :write_hello)
11
- assert_nil @pen.write(nil)
13
+ pen_write_spy, pen_write_hello_spy = Spy.on(@pen, :write, :write_hello)
14
+ assert_nil @pen.write("hello")
12
15
  assert_nil @pen.write_hello
13
16
 
14
17
  assert_kind_of Spy::Subroutine, pen_write_spy
@@ -25,7 +28,7 @@ class TestSpy < MiniTest::Unit::TestCase
25
28
 
26
29
  assert_kind_of Spy::Subroutine, pen_write_spy
27
30
  assert_kind_of Spy::Subroutine, pen_write_hello_spy
28
- assert_equal [pen_write_spy, pen_write_hello_spy], Spy::Agency.instance.subroutines
31
+ assert_equal [pen_write_spy, pen_write_hello_spy], Spy::Agency.instance.spies
29
32
  assert pen_write_spy.has_been_called?
30
33
  assert pen_write_hello_spy.has_been_called?
31
34
  end
@@ -2,6 +2,10 @@ require 'test_helper'
2
2
 
3
3
  module Spy
4
4
  class TestDouble < MiniTest::Unit::TestCase
5
+ def teardown
6
+ Spy::Agency.instance.dissolve!
7
+ end
8
+
5
9
  def test_double_creation
6
10
  double = Double.new("NewDouble", :meth_1, :meth_2)
7
11
 
@@ -7,15 +7,18 @@ module Spy
7
7
  end
8
8
 
9
9
  def setup
10
- Agency.instance.dissolve!
11
10
  @pen = Pen.new
12
11
  end
13
12
 
13
+ def teardown
14
+ Spy::Agency.instance.dissolve!
15
+ end
16
+
14
17
  def test_spy_on_hook_and_saves_spy
15
18
  pen_write_spy = spy_on(@pen, :write).and_return("hello")
16
19
  assert_equal "hello", @pen.write(nil)
17
20
  assert_kind_of Subroutine, pen_write_spy
18
- assert_equal [pen_write_spy], Agency.instance.subroutines
21
+ assert_equal [pen_write_spy], Agency.instance.spies
19
22
  assert pen_write_spy.has_been_called?
20
23
  end
21
24
 
@@ -36,10 +39,30 @@ module Spy
36
39
  assert_equal "another", Pen.another
37
40
  end
38
41
 
42
+ def test_spy_can_hook_and_record_a_meta_method_call_on_a_constant
43
+ assert_equal "meta_method", Pen.meta_method
44
+ meta_spy = spy_on(Pen, :meta_method)
45
+ refute meta_spy.has_been_called?
46
+ assert_nil Pen.meta_method
47
+ assert meta_spy.has_been_called?
48
+ meta_spy.unhook
49
+ assert_equal "meta_method", Pen.meta_method
50
+ end
51
+
52
+ def test_spy_can_hook_record_and_unhook_a_meta_method
53
+ assert_equal "meta_method", @pen.meta_method
54
+ meta_spy = spy_on(@pen, :meta_method)
55
+ refute meta_spy.has_been_called?
56
+ assert_nil @pen.meta_method
57
+ assert meta_spy.has_been_called?
58
+ meta_spy.unhook
59
+ assert_equal "meta_method", @pen.meta_method
60
+ end
61
+
39
62
  def test_spy_can_unhook_a_method
40
63
  pen_write_spy = spy_on(@pen, :write)
41
64
  pen_write_spy.unhook
42
- @pen.write("hello")
65
+ assert_equal "hello", @pen.write("hello")
43
66
  refute pen_write_spy.has_been_called?
44
67
  end
45
68
 
@@ -124,11 +147,13 @@ module Spy
124
147
  args = ["hello world"]
125
148
  block = Proc.new {}
126
149
  pen_write_spy = spy_on(@pen, :write)
150
+ called_from = "#{__FILE__}:#{__LINE__ + 1}:in `#{__method__}'"
127
151
  @pen.write(*args, &block)
128
152
  call_log = pen_write_spy.calls.first
129
153
  assert_equal @pen, call_log.object
130
154
  assert_equal args, call_log.args
131
155
  assert_equal block, call_log.block
156
+ assert_equal called_from, call_log.called_from
132
157
  end
133
158
 
134
159
  def test_that_method_spy_keeps_arity
@@ -34,6 +34,10 @@ class Pen
34
34
  def public_method
35
35
  end
36
36
 
37
+ def another
38
+ "another"
39
+ end
40
+
37
41
  protected
38
42
  def protected_method
39
43
  end
@@ -48,3 +52,14 @@ class Pen
48
52
  end
49
53
  end
50
54
  end
55
+
56
+ another = "meta_method"
57
+
58
+ Pen.define_singleton_method(:meta_method) do
59
+ another
60
+ end
61
+
62
+ Pen.send(:define_method, :meta_method) do
63
+ another
64
+ end
65
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-20 00:00:00.000000000 Z
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -67,6 +67,7 @@ extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
69
  - .gitignore
70
+ - .travis.yml
70
71
  - .yardopts
71
72
  - Gemfile
72
73
  - LICENSE.txt
@@ -74,6 +75,7 @@ files:
74
75
  - Rakefile
75
76
  - lib/spy.rb
76
77
  - lib/spy/agency.rb
78
+ - lib/spy/call_log.rb
77
79
  - lib/spy/constant.rb
78
80
  - lib/spy/core_ext/marshal.rb
79
81
  - lib/spy/double.rb
@@ -83,35 +85,23 @@ files:
83
85
  - spec/spec_helper.rb
84
86
  - spec/spy/and_call_original_spec.rb
85
87
  - spec/spy/and_yield_spec.rb
86
- - spec/spy/bug_report_10260_spec.rb
87
- - spec/spy/bug_report_10263_spec.rb
88
- - spec/spy/bug_report_496_spec.rb
89
- - spec/spy/bug_report_600_spec.rb
90
- - spec/spy/bug_report_7611_spec.rb
91
- - spec/spy/bug_report_8165_spec.rb
92
- - spec/spy/bug_report_830_spec.rb
93
- - spec/spy/bug_report_957_spec.rb
94
- - spec/spy/double_spec.rb
95
- - spec/spy/failing_argument_matchers_spec.rb
88
+ - spec/spy/any_instance_spec.rb
96
89
  - spec/spy/hash_excluding_matcher_spec.rb
97
90
  - spec/spy/hash_including_matcher_spec.rb
98
91
  - spec/spy/mock_spec.rb
99
92
  - spec/spy/mutate_const_spec.rb
100
93
  - spec/spy/nil_expectation_warning_spec.rb
101
94
  - spec/spy/null_object_mock_spec.rb
102
- - spec/spy/options_hash_spec.rb
103
95
  - spec/spy/partial_mock_spec.rb
104
96
  - spec/spy/passing_argument_matchers_spec.rb
105
- - spec/spy/precise_counts_spec.rb
106
97
  - spec/spy/serialization_spec.rb
107
98
  - spec/spy/stash_spec.rb
108
99
  - spec/spy/stub_implementation_spec.rb
109
100
  - spec/spy/stub_spec.rb
110
- - spec/spy/stubbed_message_expectations_spec.rb
111
- - spec/spy/test_double_spec.rb
112
101
  - spec/spy/to_ary_spec.rb
113
102
  - spy.gemspec
114
103
  - test/integration/test_constant_spying.rb
104
+ - test/integration/test_instance_method.rb
115
105
  - test/integration/test_subroutine_spying.rb
116
106
  - test/spy/test_double.rb
117
107
  - test/spy/test_subroutine.rb
@@ -145,34 +135,22 @@ test_files:
145
135
  - spec/spec_helper.rb
146
136
  - spec/spy/and_call_original_spec.rb
147
137
  - spec/spy/and_yield_spec.rb
148
- - spec/spy/bug_report_10260_spec.rb
149
- - spec/spy/bug_report_10263_spec.rb
150
- - spec/spy/bug_report_496_spec.rb
151
- - spec/spy/bug_report_600_spec.rb
152
- - spec/spy/bug_report_7611_spec.rb
153
- - spec/spy/bug_report_8165_spec.rb
154
- - spec/spy/bug_report_830_spec.rb
155
- - spec/spy/bug_report_957_spec.rb
156
- - spec/spy/double_spec.rb
157
- - spec/spy/failing_argument_matchers_spec.rb
138
+ - spec/spy/any_instance_spec.rb
158
139
  - spec/spy/hash_excluding_matcher_spec.rb
159
140
  - spec/spy/hash_including_matcher_spec.rb
160
141
  - spec/spy/mock_spec.rb
161
142
  - spec/spy/mutate_const_spec.rb
162
143
  - spec/spy/nil_expectation_warning_spec.rb
163
144
  - spec/spy/null_object_mock_spec.rb
164
- - spec/spy/options_hash_spec.rb
165
145
  - spec/spy/partial_mock_spec.rb
166
146
  - spec/spy/passing_argument_matchers_spec.rb
167
- - spec/spy/precise_counts_spec.rb
168
147
  - spec/spy/serialization_spec.rb
169
148
  - spec/spy/stash_spec.rb
170
149
  - spec/spy/stub_implementation_spec.rb
171
150
  - spec/spy/stub_spec.rb
172
- - spec/spy/stubbed_message_expectations_spec.rb
173
- - spec/spy/test_double_spec.rb
174
151
  - spec/spy/to_ary_spec.rb
175
152
  - test/integration/test_constant_spying.rb
153
+ - test/integration/test_instance_method.rb
176
154
  - test/integration/test_subroutine_spying.rb
177
155
  - test/spy/test_double.rb
178
156
  - test/spy/test_subroutine.rb
@@ -1,8 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "An RSpec Mock" do
4
- it "hides internals in its inspect representation" do
5
- m = double('cup')
6
- expect(m.inspect).to match /#<RSpec::Mocks::Mock:0x[a-f0-9.]+ @name="cup">/
7
- end
8
- end
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Double" do
4
- let(:test_double) { double }
5
-
6
- specify "when one example has an expectation inside the block passed to should_receive" do
7
- Spy.stub(test_double, :msg).and_return do |arg|
8
- expect(arg).to be_true #this call exposes the problem
9
- end
10
- begin
11
- test_double.msg(false)
12
- rescue Exception
13
- end
14
- end
15
-
16
- specify "then the next example should behave as expected instead of saying" do
17
- test_double_spy = Spy.stub(test_double, :foobar)
18
- test_double.foobar
19
- expect(test_double_spy).to have_been_called
20
- test_double.foobar
21
- expect(test_double_spy.calls.count).to equal(2)
22
- end
23
- end
24
-
@@ -1,18 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module BugReport496
4
- describe "a message expectation on a base class object" do
5
- class BaseClass
6
- end
7
-
8
- class SubClass < BaseClass
9
- end
10
-
11
- it "is received" do
12
- new_spy = Spy.on(BaseClass, :new)
13
- SubClass.new
14
- expect(new_spy.calls.count).to equal(1)
15
- end
16
- end
17
- end
18
-
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module BugReport600
4
- describe "stubbing a class method" do
5
- class ExampleClass
6
- def self.method_that_uses_define_method
7
- define_method "defined_method" do |attributes|
8
- load_address(address, attributes)
9
- end
10
- end
11
- end
12
-
13
- it "works" do
14
- define_method_spy = Spy.on(ExampleClass, :define_method)
15
- ExampleClass.method_that_uses_define_method
16
-
17
- expect(define_method_spy).to have_been_called_with("defined_method")
18
- end
19
-
20
- it "restores the original method" do
21
- ExampleClass.method_that_uses_define_method
22
- end
23
- end
24
- end
@@ -1,16 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Bug7611
4
- describe "A Partial Mock" do
5
- class Foo; end
6
- class Bar < Foo; end
7
-
8
- it "respects subclasses" do
9
- Spy.on(Foo, :new).and_return(Object.new)
10
- end
11
-
12
- it "should" do
13
- expect(Bar.new.class).to eq Bar
14
- end
15
- end
16
- end
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "An object where respond_to? is true and does not have method" do
4
- # When should_receive(message) is sent to any object, the Proxy sends
5
- # respond_to?(message) to that object to see if the method should be proxied.
6
- #
7
- # If respond_to? itself is proxied, then when the Proxy sends respond_to?
8
- # to the object, the proxy is invoked and responds yes (if so set in the spec).
9
- # When the object does NOT actually respond to `message`, an exception is thrown
10
- # when trying to proxy it.
11
- #
12
- # The fix was to keep track of whether `respond_to?` had been proxied and, if
13
- # so, call the munged copy of `respond_to?` on the object.
14
-
15
- it "does not raise an exception for Object" do
16
- obj = Object.new
17
- obj.should_receive(:respond_to?).with(:foobar).and_return(true)
18
- obj.should_receive(:foobar).and_return(:baz)
19
- expect(obj.respond_to?(:foobar)).to be_true
20
- expect(obj.foobar).to eq :baz
21
- end
22
-
23
- it "does not raise an exception for mock" do
24
- obj = double("obj")
25
- obj.should_receive(:respond_to?).with(:foobar).and_return(true)
26
- obj.should_receive(:foobar).and_return(:baz)
27
- expect(obj.respond_to?(:foobar)).to be_true
28
- expect(obj.foobar).to eq :baz
29
- end
30
-
31
- end
@@ -1,21 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe 'Calling a method that catches StandardError' do
6
- class Foo
7
- def self.foo
8
- bar
9
- rescue StandardError
10
- end
11
- end
12
-
13
- it 'still reports mock failures' do
14
- Foo.should_not_receive :bar
15
- expect {
16
- Foo.foo
17
- }.to raise_error(RSpec::Mocks::MockExpectationError)
18
- end
19
- end
20
- end
21
- end
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec
4
- module Mocks
5
- describe "stubbing a base class class method" do
6
- before do
7
- @base_class = Class.new
8
- @concrete_class = Class.new(@base_class)
9
-
10
- Spy.stub(@base_class, :find).and_return "stubbed_value"
11
- end
12
-
13
- it "returns the value for the stub on the base class" do
14
- expect(@base_class.find).to eq "stubbed_value"
15
- end
16
-
17
- it "returns the value for the descendent class" do
18
- expect(@concrete_class.find).to eq "stubbed_value"
19
- end
20
- end
21
- end
22
- end