jeremymcanally-stump 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/stump/proxy.rb +103 -0
- data/stump.gemspec +4 -3
- metadata +3 -2
data/lib/stump/proxy.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
class Object
|
2
|
+
# Creates a proxy method on an object. In this setup, it places an expectation on an object (like a mock)
|
3
|
+
# but still calls the original method. So if you want to make sure the method is called and still return
|
4
|
+
# its value, or simply want to invoke the side effects of a method and return a stubbed value, then you can
|
5
|
+
# do that.
|
6
|
+
#
|
7
|
+
# ==== Examples
|
8
|
+
#
|
9
|
+
# class Parrot
|
10
|
+
# def speak!
|
11
|
+
# puts @words
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# def say_this(words)
|
15
|
+
# @words = words
|
16
|
+
# "I shall say #{words}!"
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# # => test/your_test.rb
|
21
|
+
# sqawky = Parrot.new
|
22
|
+
# sqawky.proxy!(:say_this)
|
23
|
+
# # Proxy method still calls original method...
|
24
|
+
# sqawky.say_this("hey") # => "I shall say hey!"
|
25
|
+
# sqawky.speak! # => "hey"
|
26
|
+
#
|
27
|
+
# sqawky.proxy!(:say_this, "herro!")
|
28
|
+
# # Even though we return a stubbed value...
|
29
|
+
# sqawky.say_this("these words") # => "herro!"
|
30
|
+
# # ...the side effects are still there.
|
31
|
+
# sqawky.speak! # => "these words"
|
32
|
+
#
|
33
|
+
# TODO: This implementation is still very rough. Needs refactoring and refining. Won't
|
34
|
+
# work on ActiveRecord attributes, for example.
|
35
|
+
#
|
36
|
+
def proxy!(method, options = {}, &block)
|
37
|
+
Stump::Mocks.add([self, method])
|
38
|
+
|
39
|
+
if respond_to?(method)
|
40
|
+
proxy_existing_method(method, options, &block)
|
41
|
+
else
|
42
|
+
proxy_missing_method(method, options, &block)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
def proxy_existing_method(method, options = {}, &block)
|
48
|
+
method_alias = "__old_#{method}"
|
49
|
+
|
50
|
+
meta_eval do
|
51
|
+
module_eval("alias #{method_alias} #{method}")
|
52
|
+
end
|
53
|
+
|
54
|
+
behavior = if options[:return]
|
55
|
+
lambda do |*args|
|
56
|
+
raise ArgumentError if args.length != method(method_alias.to_sym).arity
|
57
|
+
|
58
|
+
Stump::Mocks.verify([self, method])
|
59
|
+
|
60
|
+
if method(method_alias.to_sym).arity == 0
|
61
|
+
send(method_alias)
|
62
|
+
else
|
63
|
+
send(method_alias, *args)
|
64
|
+
end
|
65
|
+
|
66
|
+
return options[:return]
|
67
|
+
end
|
68
|
+
else
|
69
|
+
lambda do |*args|
|
70
|
+
raise ArgumentError if args.length != method(method_alias.to_sym).arity
|
71
|
+
|
72
|
+
Stump::Mocks.verify([self, method])
|
73
|
+
|
74
|
+
if method(method_alias.to_sym).arity == 0
|
75
|
+
return send(method_alias)
|
76
|
+
else
|
77
|
+
return send(method_alias, *args)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
meta_def method, &behavior
|
83
|
+
end
|
84
|
+
|
85
|
+
def proxy_missing_method(method, options = {}, &block)
|
86
|
+
behavior = if options[:return]
|
87
|
+
lambda do |*args|
|
88
|
+
Stump::Mocks.verify([self, method])
|
89
|
+
|
90
|
+
method_missing(method, args)
|
91
|
+
return options[:return]
|
92
|
+
end
|
93
|
+
else
|
94
|
+
lambda do |*args|
|
95
|
+
Stump::Mocks.verify([self, method])
|
96
|
+
|
97
|
+
method_missing(method, args)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
meta_def method, &behavior
|
102
|
+
end
|
103
|
+
end
|
data/stump.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "stump"
|
3
|
-
s.version = "0.0.
|
4
|
-
s.date = "2008-10-
|
3
|
+
s.version = "0.0.2"
|
4
|
+
s.date = "2008-10-30"
|
5
5
|
s.summary = "Stubbing and mocking that isn't painful."
|
6
6
|
s.email = "jeremy@entp.com"
|
7
7
|
s.homepage = "http://github.com/jeremymcanally/stump"
|
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
"lib/stump/mock.rb",
|
27
27
|
"lib/stump/mocks.rb",
|
28
28
|
"lib/stump/stub.rb",
|
29
|
+
"lib/stump/proxy.rb",
|
29
30
|
"lib/stump/version.rb",
|
30
31
|
"lib/stump/core_ext/test_case.rb",
|
31
32
|
"setup.rb"
|
@@ -40,4 +41,4 @@ Gem::Specification.new do |s|
|
|
40
41
|
|
41
42
|
s.rdoc_options = ["--main", "README.rdoc"]
|
42
43
|
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
|
43
|
-
end
|
44
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jeremymcanally-stump
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy McAnally
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-10-
|
12
|
+
date: 2008-10-30 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- lib/stump/mock.rb
|
39
39
|
- lib/stump/mocks.rb
|
40
40
|
- lib/stump/stub.rb
|
41
|
+
- lib/stump/proxy.rb
|
41
42
|
- lib/stump/version.rb
|
42
43
|
- lib/stump/core_ext/test_case.rb
|
43
44
|
- setup.rb
|