spy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +6 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +133 -0
  5. data/Rakefile +8 -0
  6. data/TODO.md +8 -0
  7. data/lib/spy.rb +259 -0
  8. data/lib/spy/double.rb +11 -0
  9. data/lib/spy/dsl.rb +7 -0
  10. data/lib/spy/version.rb +3 -0
  11. data/spec/spec_helper.rb +39 -0
  12. data/spec/spy/and_call_original_spec.rb +152 -0
  13. data/spec/spy/and_yield_spec.rb +114 -0
  14. data/spec/spy/bug_report_10260_spec.rb +8 -0
  15. data/spec/spy/bug_report_10263_spec.rb +24 -0
  16. data/spec/spy/bug_report_496_spec.rb +18 -0
  17. data/spec/spy/bug_report_600_spec.rb +24 -0
  18. data/spec/spy/bug_report_7611_spec.rb +16 -0
  19. data/spec/spy/bug_report_8165_spec.rb +31 -0
  20. data/spec/spy/bug_report_830_spec.rb +21 -0
  21. data/spec/spy/bug_report_957_spec.rb +22 -0
  22. data/spec/spy/double_spec.rb +12 -0
  23. data/spec/spy/failing_argument_matchers_spec.rb +94 -0
  24. data/spec/spy/hash_excluding_matcher_spec.rb +67 -0
  25. data/spec/spy/hash_including_matcher_spec.rb +90 -0
  26. data/spec/spy/mock_spec.rb +734 -0
  27. data/spec/spy/multiple_return_value_spec.rb +119 -0
  28. data/spec/spy/mutate_const_spec.rb +481 -0
  29. data/spec/spy/nil_expectation_warning_spec.rb +56 -0
  30. data/spec/spy/null_object_mock_spec.rb +107 -0
  31. data/spec/spy/options_hash_spec.rb +35 -0
  32. data/spec/spy/partial_mock_spec.rb +196 -0
  33. data/spec/spy/passing_argument_matchers_spec.rb +142 -0
  34. data/spec/spy/precise_counts_spec.rb +68 -0
  35. data/spec/spy/serialization_spec.rb +110 -0
  36. data/spec/spy/stash_spec.rb +54 -0
  37. data/spec/spy/stub_implementation_spec.rb +62 -0
  38. data/spec/spy/stub_spec.rb +85 -0
  39. data/spec/spy/stubbed_message_expectations_spec.rb +47 -0
  40. data/spec/spy/test_double_spec.rb +57 -0
  41. data/spec/spy/to_ary_spec.rb +40 -0
  42. data/spy.gemspec +21 -0
  43. data/test/spy/test_double.rb +19 -0
  44. data/test/test_helper.rb +6 -0
  45. data/test/test_spy.rb +258 -0
  46. metadata +157 -0
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe "a double receiving to_ary" do
4
+ shared_examples "to_ary" do
5
+ it "returns nil" do
6
+ expect do
7
+ expect(obj.to_ary).to be_nil
8
+ end.to raise_error(NoMethodError)
9
+ end
10
+
11
+ it "doesn't respond" do
12
+ expect(obj).not_to respond_to(:to_ary)
13
+ end
14
+
15
+ it "can be overridden with a stub" do
16
+ Spy.on(obj, :to_ary) { :non_nil_value }
17
+ expect(obj.to_ary).to be(:non_nil_value)
18
+ end
19
+
20
+ it "responds when overriden" do
21
+ Spy.on(obj, :to_ary) { :non_nil_value }
22
+ expect(obj).to respond_to(:to_ary)
23
+ end
24
+
25
+ it "supports Array#flatten" do
26
+ obj = double('foo')
27
+ expect([obj].flatten).to eq([obj])
28
+ end
29
+ end
30
+
31
+ context "double as_null_object" do
32
+ let(:obj) { double('obj').as_null_object }
33
+ include_examples "to_ary"
34
+ end
35
+
36
+ context "double without as_null_object" do
37
+ let(:obj) { double('obj') }
38
+ include_examples "to_ary"
39
+ end
40
+ end
data/spy.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spy/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "spy"
8
+ gem.version = Spy::VERSION
9
+ gem.authors = ["Ryan Ong"]
10
+ gem.email = ["ryanong@gmail.com"]
11
+ gem.description = %q{A simple mocking library that doesn't spies your intelligence.}
12
+ gem.summary = %q{A simple non destructive mocking library.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_development_dependency('minitest', '>= 4.5.0')
20
+ gem.add_development_dependency('rspec')
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class Spy
4
+ class TestDouble < MiniTest::Unit::TestCase
5
+ def test_double_creation
6
+ double = Double.new("NewDouble", :meth_1, :meth_2)
7
+
8
+ assert_nil double.meth_1
9
+ assert_nil double.meth_2
10
+ end
11
+
12
+ def test_double_hash_input
13
+ double = Double.new("NewDouble", meth_1: :meth_1, meth_2: :meth_2)
14
+
15
+ assert_equal :meth_1, double.meth_1
16
+ assert_equal :meth_2, double.meth_2
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'pry'
4
+ require 'pry-nav'
5
+
6
+ require 'spy'
data/test/test_spy.rb ADDED
@@ -0,0 +1,258 @@
1
+ require 'test_helper'
2
+
3
+ class TestSpy < MiniTest::Unit::TestCase
4
+ class Pen
5
+ attr_reader :written, :color
6
+
7
+ def initialize(color = :black)
8
+ @color = color
9
+ @written = []
10
+ end
11
+
12
+ def write(string)
13
+ @written << string
14
+ string
15
+ end
16
+
17
+ def write_block(&block)
18
+ string = yield
19
+ @written << string
20
+ string
21
+ end
22
+
23
+ def write_hello
24
+ write("hello")
25
+ end
26
+
27
+ def write_array(*args)
28
+ args.each do |arg|
29
+ write(arg)
30
+ end
31
+ end
32
+
33
+ def greet(hello = "hello", name)
34
+ write("#{hello} #{name}")
35
+ end
36
+
37
+ def public_method
38
+ end
39
+
40
+ protected
41
+ def protected_method
42
+ end
43
+
44
+ private
45
+ def private_method
46
+ end
47
+
48
+ class << self
49
+ def another
50
+ "another"
51
+ end
52
+ end
53
+ end
54
+
55
+ def setup
56
+ Spy.teardown
57
+ @pen = Pen.new
58
+ end
59
+
60
+ def test_spy_on_hook_and_saves_spy
61
+ pen_write_spy = Spy.on(@pen, :write).and_return("hello")
62
+ assert_equal "hello", @pen.write(nil)
63
+ assert_kind_of Spy, pen_write_spy
64
+ assert_equal [pen_write_spy], Spy.all
65
+ assert pen_write_spy.has_been_called?
66
+ end
67
+
68
+ def test_spy_on_hooks_and_saves_spy_with_array
69
+ pen_write_spy, pen_write_hello_spy = Spy.on(@pen, :write, :write_hello)
70
+ assert_nil @pen.write(nil)
71
+ assert_nil @pen.write_hello
72
+
73
+ assert_kind_of Spy, pen_write_spy
74
+ assert_kind_of Spy, pen_write_hello_spy
75
+ assert_equal [pen_write_spy, pen_write_hello_spy], Spy.all
76
+ assert pen_write_spy.has_been_called?
77
+ assert pen_write_hello_spy.has_been_called?
78
+ end
79
+
80
+ def test_spy_on_hooks_and_saves_spy_with_array
81
+ pen_write_spy, pen_write_hello_spy = Spy.on(@pen, write: "hello", write_hello: "world")
82
+ assert_equal "hello", @pen.write(nil)
83
+ assert_equal "world", @pen.write_hello
84
+
85
+ assert_kind_of Spy, pen_write_spy
86
+ assert_kind_of Spy, pen_write_hello_spy
87
+ assert_equal [pen_write_spy, pen_write_hello_spy], Spy.all
88
+ assert pen_write_spy.has_been_called?
89
+ assert pen_write_hello_spy.has_been_called?
90
+ end
91
+
92
+ def test_spy_can_hook_and_record_a_method_call
93
+ pen_write_spy = Spy.new(@pen, :write)
94
+ pen_write_spy.hook
95
+ refute pen_write_spy.has_been_called?
96
+ @pen.write("hello")
97
+ assert pen_write_spy.has_been_called?
98
+ assert_empty @pen.written
99
+ end
100
+
101
+ def test_spy_can_hook_and_record_a_method_call_on_a_constant
102
+ another_spy = Spy.new(Pen, :another)
103
+ another_spy.hook
104
+ refute another_spy.has_been_called?
105
+ assert_nil Pen.another
106
+ assert another_spy.has_been_called?
107
+ another_spy.unhook
108
+ assert_equal "another", Pen.another
109
+ end
110
+
111
+ def test_spy_can_unhook_a_method
112
+ pen_write_spy = Spy.new(@pen, :write)
113
+ pen_write_spy.hook
114
+ pen_write_spy.unhook
115
+ @pen.write("hello")
116
+ refute pen_write_spy.has_been_called?
117
+ end
118
+
119
+ def test_spy_cannot_hook_a_non_existent_method
120
+ spy = Spy.new(@pen, :no_method)
121
+ assert_raises NameError do
122
+ spy.hook
123
+ end
124
+ end
125
+
126
+ def test_spy_can_hook_a_non_existent_method_if_param_set
127
+ spy = Spy.new(@pen, :no_method).and_return(:yep)
128
+ spy.hook(force: true)
129
+ assert_equal :yep, @pen.no_method
130
+ end
131
+
132
+ def test_spy_and_return_returns_the_set_value
133
+ result = "hello world"
134
+
135
+ Spy.on(@pen, :write).and_return(result)
136
+
137
+ assert_equal result, @pen.write(nil)
138
+ end
139
+
140
+ def test_spy_and_return_can_call_a_block
141
+ result = "hello world"
142
+
143
+ Spy.on(@pen, :write).and_return do |string|
144
+ string.reverse
145
+ end
146
+
147
+ assert_equal result.reverse, @pen.write(result)
148
+ assert_empty @pen.written
149
+ end
150
+
151
+ def test_spy_and_return_can_call_a_block_that_recieves_a_block
152
+ string = "hello world"
153
+
154
+ Spy.on(@pen, :write_block).and_return do |&block|
155
+ block.call
156
+ end
157
+
158
+ result = @pen.write_block do
159
+ string
160
+ end
161
+ assert_equal string, result
162
+ end
163
+
164
+ def test_spy_hook_records_number_of_calls
165
+ pen_write_spy = Spy.on(@pen, :write)
166
+ assert_equal 0, pen_write_spy.calls.size
167
+ 5.times do |i|
168
+ @pen.write("hello world")
169
+ assert_equal i + 1, pen_write_spy.calls.size
170
+ end
171
+ end
172
+
173
+ def test_has_been_called_with?
174
+ pen_write_spy = Spy.on(@pen, :write)
175
+ refute pen_write_spy.has_been_called_with?("hello")
176
+ @pen.write("hello")
177
+ assert pen_write_spy.has_been_called_with?("hello")
178
+ @pen.write("world")
179
+ assert pen_write_spy.has_been_called_with?("hello")
180
+ @pen.write("hello world")
181
+ assert pen_write_spy.has_been_called_with?("hello")
182
+ end
183
+
184
+ def test_spy_hook_records_number_of_calls
185
+ args = ["hello world"]
186
+ block = Proc.new {}
187
+ pen_write_spy = Spy.on(@pen, :write)
188
+ @pen.write(*args, &block)
189
+ call_log = pen_write_spy.calls.first
190
+ assert_equal @pen, call_log.object
191
+ assert_equal args, call_log.args
192
+ assert_equal block, call_log.block
193
+ end
194
+
195
+ def test_that_method_spy_keeps_arity
196
+ Spy.on(@pen, :write)
197
+ @pen.write("hello world")
198
+ assert_raises ArgumentError do
199
+ @pen.write("hello", "world")
200
+ end
201
+
202
+ Spy.on(@pen, :write_hello)
203
+ @pen.write_hello
204
+ assert_raises ArgumentError do
205
+ @pen.write_hello("hello")
206
+ end
207
+
208
+ Spy.on(@pen, :write_array)
209
+ @pen.write_hello
210
+ assert_raises ArgumentError do
211
+ @pen.write_hello("hello")
212
+ end
213
+
214
+ Spy.on(@pen, :greet)
215
+ @pen.greet("bob")
216
+ assert_raises ArgumentError do
217
+ @pen.greet
218
+ end
219
+ assert_raises ArgumentError do
220
+ @pen.greet("hello", "bob", "error")
221
+ end
222
+ end
223
+
224
+ def test_hook_mimics_public_visibility
225
+ Spy.on(@pen, :public_method)
226
+ assert @pen.singleton_class.public_method_defined? :public_method
227
+ refute @pen.singleton_class.protected_method_defined? :public_method
228
+ refute @pen.singleton_class.private_method_defined? :public_method
229
+ end
230
+
231
+ def test_hook_mimics_protected_visibility
232
+ Spy.on(@pen, :protected_method)
233
+ refute @pen.singleton_class.public_method_defined? :protected_method
234
+ assert @pen.singleton_class.protected_method_defined? :protected_method
235
+ refute @pen.singleton_class.private_method_defined? :protected_method
236
+ end
237
+
238
+ def test_hook_mimics_private_visibility
239
+ Spy.on(@pen, :private_method)
240
+ refute @pen.singleton_class.public_method_defined? :private_method
241
+ refute @pen.singleton_class.protected_method_defined? :private_method
242
+ assert @pen.singleton_class.private_method_defined? :private_method
243
+ end
244
+
245
+ def test_spy_off_unhooks_a_method
246
+ pen_write_spy = Spy.on(@pen, :write)
247
+ Spy.off(@pen,:write)
248
+ assert_equal "hello world", @pen.write("hello world")
249
+ refute pen_write_spy.has_been_called?
250
+ end
251
+
252
+ def test_spy_get_can_retrieve_a_spy
253
+ pen_write_spy = Spy.on(@pen, :write).and_return(:hello)
254
+ assert_equal :hello, @pen.write(:world)
255
+ assert_equal pen_write_spy, Spy.get(@pen, :write)
256
+ assert Spy.get(@pen, :write).has_been_called?
257
+ end
258
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Ong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 4.5.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 4.5.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A simple mocking library that doesn't spies your intelligence.
47
+ email:
48
+ - ryanong@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - TODO.md
59
+ - lib/spy.rb
60
+ - lib/spy/double.rb
61
+ - lib/spy/dsl.rb
62
+ - lib/spy/version.rb
63
+ - spec/spec_helper.rb
64
+ - spec/spy/and_call_original_spec.rb
65
+ - spec/spy/and_yield_spec.rb
66
+ - spec/spy/bug_report_10260_spec.rb
67
+ - spec/spy/bug_report_10263_spec.rb
68
+ - spec/spy/bug_report_496_spec.rb
69
+ - spec/spy/bug_report_600_spec.rb
70
+ - spec/spy/bug_report_7611_spec.rb
71
+ - spec/spy/bug_report_8165_spec.rb
72
+ - spec/spy/bug_report_830_spec.rb
73
+ - spec/spy/bug_report_957_spec.rb
74
+ - spec/spy/double_spec.rb
75
+ - spec/spy/failing_argument_matchers_spec.rb
76
+ - spec/spy/hash_excluding_matcher_spec.rb
77
+ - spec/spy/hash_including_matcher_spec.rb
78
+ - spec/spy/mock_spec.rb
79
+ - spec/spy/multiple_return_value_spec.rb
80
+ - spec/spy/mutate_const_spec.rb
81
+ - spec/spy/nil_expectation_warning_spec.rb
82
+ - spec/spy/null_object_mock_spec.rb
83
+ - spec/spy/options_hash_spec.rb
84
+ - spec/spy/partial_mock_spec.rb
85
+ - spec/spy/passing_argument_matchers_spec.rb
86
+ - spec/spy/precise_counts_spec.rb
87
+ - spec/spy/serialization_spec.rb
88
+ - spec/spy/stash_spec.rb
89
+ - spec/spy/stub_implementation_spec.rb
90
+ - spec/spy/stub_spec.rb
91
+ - spec/spy/stubbed_message_expectations_spec.rb
92
+ - spec/spy/test_double_spec.rb
93
+ - spec/spy/to_ary_spec.rb
94
+ - spy.gemspec
95
+ - test/spy/test_double.rb
96
+ - test/test_helper.rb
97
+ - test/test_spy.rb
98
+ homepage: ''
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.23
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: A simple non destructive mocking library.
122
+ test_files:
123
+ - spec/spec_helper.rb
124
+ - spec/spy/and_call_original_spec.rb
125
+ - spec/spy/and_yield_spec.rb
126
+ - spec/spy/bug_report_10260_spec.rb
127
+ - spec/spy/bug_report_10263_spec.rb
128
+ - spec/spy/bug_report_496_spec.rb
129
+ - spec/spy/bug_report_600_spec.rb
130
+ - spec/spy/bug_report_7611_spec.rb
131
+ - spec/spy/bug_report_8165_spec.rb
132
+ - spec/spy/bug_report_830_spec.rb
133
+ - spec/spy/bug_report_957_spec.rb
134
+ - spec/spy/double_spec.rb
135
+ - spec/spy/failing_argument_matchers_spec.rb
136
+ - spec/spy/hash_excluding_matcher_spec.rb
137
+ - spec/spy/hash_including_matcher_spec.rb
138
+ - spec/spy/mock_spec.rb
139
+ - spec/spy/multiple_return_value_spec.rb
140
+ - spec/spy/mutate_const_spec.rb
141
+ - spec/spy/nil_expectation_warning_spec.rb
142
+ - spec/spy/null_object_mock_spec.rb
143
+ - spec/spy/options_hash_spec.rb
144
+ - spec/spy/partial_mock_spec.rb
145
+ - spec/spy/passing_argument_matchers_spec.rb
146
+ - spec/spy/precise_counts_spec.rb
147
+ - spec/spy/serialization_spec.rb
148
+ - spec/spy/stash_spec.rb
149
+ - spec/spy/stub_implementation_spec.rb
150
+ - spec/spy/stub_spec.rb
151
+ - spec/spy/stubbed_message_expectations_spec.rb
152
+ - spec/spy/test_double_spec.rb
153
+ - spec/spy/to_ary_spec.rb
154
+ - test/spy/test_double.rb
155
+ - test/test_helper.rb
156
+ - test/test_spy.rb
157
+ has_rdoc: