stubble 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +4 -15
- data/lib/stubble.rb +38 -4
- data/test/stubble_test.rb +31 -0
- metadata +4 -4
data/Rakefile
CHANGED
@@ -2,6 +2,9 @@ require 'rake'
|
|
2
2
|
require 'rake/testtask'
|
3
3
|
require 'rake/rdoctask'
|
4
4
|
|
5
|
+
require "rubygems"
|
6
|
+
require "rake/gempackagetask"
|
7
|
+
|
5
8
|
desc 'Default: run unit tests.'
|
6
9
|
task :default => :test
|
7
10
|
|
@@ -22,20 +25,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
22
25
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
26
|
end
|
24
27
|
|
25
|
-
require "rubygems"
|
26
|
-
require "rake/gempackagetask"
|
27
|
-
require "rake/rdoctask"
|
28
|
-
|
29
|
-
require "rake/testtask"
|
30
|
-
Rake::TestTask.new do |t|
|
31
|
-
t.libs << "test"
|
32
|
-
t.test_files = FileList["test/**/*_test.rb"]
|
33
|
-
t.verbose = true
|
34
|
-
end
|
35
|
-
|
36
|
-
|
37
|
-
task :default => ["test"]
|
38
|
-
|
39
28
|
# This builds the actual gem. For details of what all these options
|
40
29
|
# mean, and other ones you can add, check the documentation here:
|
41
30
|
#
|
@@ -45,7 +34,7 @@ spec = Gem::Specification.new do |s|
|
|
45
34
|
|
46
35
|
# Change these as appropriate
|
47
36
|
s.name = "stubble"
|
48
|
-
s.version = "0.1.
|
37
|
+
s.version = "0.1.1"
|
49
38
|
s.summary = "(experimental) simple stubbing"
|
50
39
|
s.author = "Matthew Rudy Jacobs"
|
51
40
|
s.email = "MatthewRudyJacobs@gmail.com"
|
data/lib/stubble.rb
CHANGED
@@ -32,8 +32,14 @@ module Stubble
|
|
32
32
|
@tracking.delete(method_name)
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
36
|
-
@stubs[method_name]
|
35
|
+
def raise!(method_name, exception)
|
36
|
+
@stubs[method_name] ||= []
|
37
|
+
@stubs[method_name] << RaiseStub.new(exception)
|
38
|
+
end
|
39
|
+
|
40
|
+
def stub!(method_name, value)
|
41
|
+
@stubs[method_name] ||= []
|
42
|
+
@stubs[method_name] << ReturnStub.new(value)
|
37
43
|
end
|
38
44
|
|
39
45
|
def unstub!(method_name)
|
@@ -57,6 +63,26 @@ module Stubble
|
|
57
63
|
end
|
58
64
|
module_function :add_stubble!
|
59
65
|
|
66
|
+
class ReturnStub
|
67
|
+
def initialize(value)
|
68
|
+
@value = value
|
69
|
+
end
|
70
|
+
|
71
|
+
def perform
|
72
|
+
return @value
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class RaiseStub
|
77
|
+
def initialize(exception)
|
78
|
+
@exception = exception
|
79
|
+
end
|
80
|
+
|
81
|
+
def perform
|
82
|
+
raise @exception
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
60
86
|
module StubMethods
|
61
87
|
|
62
88
|
def track!(method_name)
|
@@ -65,7 +91,7 @@ module Stubble
|
|
65
91
|
class_eval <<-RUBY
|
66
92
|
def #{method_name}_with_stubble(*args, &block)
|
67
93
|
if stubbed_return = self._stubble.called!(#{method_name.inspect}, args, block)
|
68
|
-
stubbed_return
|
94
|
+
stubbed_return.perform
|
69
95
|
else
|
70
96
|
#{method_name}_without_stubble(*args, &block)
|
71
97
|
end
|
@@ -81,7 +107,15 @@ module Stubble
|
|
81
107
|
|
82
108
|
def stub!(method_name, values)
|
83
109
|
track!(method_name)
|
84
|
-
|
110
|
+
values.each do |value|
|
111
|
+
self._stubble.stub!(method_name, value)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def raise!(method_name, exception)
|
116
|
+
track!(method_name)
|
117
|
+
|
118
|
+
self._stubble.raise!(method_name, exception)
|
85
119
|
end
|
86
120
|
|
87
121
|
def unstub!(method_name)
|
data/test/stubble_test.rb
CHANGED
@@ -122,6 +122,37 @@ class StubbleTest < Test::Unit::TestCase
|
|
122
122
|
assert_equal 2, this.some_method
|
123
123
|
end
|
124
124
|
|
125
|
+
test "stub! - can be done progressively" do
|
126
|
+
this = StubThis.new
|
127
|
+
assert_equal 1, this.some_method
|
128
|
+
|
129
|
+
Stubble.add_stubble!(this)
|
130
|
+
this.stub!(:some_method, [:a])
|
131
|
+
this.stub!(:some_method, [:b, :c])
|
132
|
+
|
133
|
+
assert_equal :a, this.some_method
|
134
|
+
assert_equal :b, this.some_method
|
135
|
+
assert_equal :c, this.some_method
|
136
|
+
|
137
|
+
assert_equal 2, this.some_method
|
138
|
+
end
|
139
|
+
|
140
|
+
class SomeMagicError < RuntimeError ; end
|
141
|
+
|
142
|
+
test "raise!" do
|
143
|
+
this = StubThis.new
|
144
|
+
assert_equal 1, this.some_method
|
145
|
+
|
146
|
+
Stubble.add_stubble!(this)
|
147
|
+
this.raise!(:some_method, SomeMagicError)
|
148
|
+
|
149
|
+
assert_raise(SomeMagicError) do
|
150
|
+
this.some_method
|
151
|
+
end
|
152
|
+
|
153
|
+
assert_equal 2, this.some_method
|
154
|
+
end
|
155
|
+
|
125
156
|
test "unstub! - cancels a stub" do
|
126
157
|
this = StubThis.new
|
127
158
|
assert_equal 1, this.some_method
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stubble
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matthew Rudy Jacobs
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-02-01 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|