mockery 0.4.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 +106 -0
- data/lib/mockery/call.rb +40 -0
- data/lib/mockery/controller.rb +75 -0
- data/lib/mockery/mock.rb +158 -0
- data/lib/mockery/mock_class_factory.rb +54 -0
- data/lib/mockery/record_control.rb +54 -0
- data/lib/mockery/recorder.rb +48 -0
- data/tests/acc/acc_tests.rb +18 -0
- data/tests/acc/mockery/mockery_test.rb +105 -0
- data/tests/unit/mockery/call_test.rb +92 -0
- data/tests/unit/mockery/controller_test.rb +207 -0
- data/tests/unit/mockery/mock_class_factory_test.rb +207 -0
- data/tests/unit/mockery/mock_test.rb +247 -0
- data/tests/unit/mockery/record_control_test.rb +163 -0
- data/tests/unit/mockery/recorder_test.rb +67 -0
- data/tests/unit/unit_tests.rb +29 -0
- metadata +57 -0
@@ -0,0 +1,48 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Name: Mockery::Recorder (lib/mockery/recorder.rb)
|
4
|
+
#
|
5
|
+
# Description:
|
6
|
+
#
|
7
|
+
# The Recorder class makes a record of the events
|
8
|
+
# in the 'record' block.
|
9
|
+
#
|
10
|
+
#
|
11
|
+
# Copyright 2005 Gary Shea
|
12
|
+
#
|
13
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
14
|
+
# you may not use this file except in compliance with the License.
|
15
|
+
# You may obtain a copy of the License at
|
16
|
+
#
|
17
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
18
|
+
#
|
19
|
+
# Unless required by applicable law or agreed to in writing, software
|
20
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
21
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
22
|
+
# See the License for the specific language governing permissions and
|
23
|
+
# limitations under the License.
|
24
|
+
#
|
25
|
+
|
26
|
+
require 'mockery/call'
|
27
|
+
|
28
|
+
module Mockery
|
29
|
+
|
30
|
+
class Recorder
|
31
|
+
|
32
|
+
attr_reader :history
|
33
|
+
|
34
|
+
def initialize(klass = nil)
|
35
|
+
@klass = klass
|
36
|
+
@history = []
|
37
|
+
end
|
38
|
+
|
39
|
+
def method_missing(*args)
|
40
|
+
call = Mockery::Call.new
|
41
|
+
call.digest(self, args)
|
42
|
+
@history << call
|
43
|
+
return call
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
$:.unshift('lib')
|
3
|
+
$: << 'tests/acc'
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require 'test/unit/ui/console/testrunner'
|
7
|
+
|
8
|
+
require 'mockery/mockery_test'
|
9
|
+
|
10
|
+
class Tests
|
11
|
+
|
12
|
+
def self.suite
|
13
|
+
suite = Test::Unit::TestSuite.new
|
14
|
+
suite << MockeryTest.suite
|
15
|
+
return suite
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Name: MockeryTest (tests/acc/mockery/mockery_test.rb)
|
4
|
+
#
|
5
|
+
# Description:
|
6
|
+
#
|
7
|
+
# Acceptance tests for the Mockery package
|
8
|
+
#
|
9
|
+
#
|
10
|
+
# Copyright 2005 Gary Shea
|
11
|
+
#
|
12
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
13
|
+
# you may not use this file except in compliance with the License.
|
14
|
+
# You may obtain a copy of the License at
|
15
|
+
#
|
16
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
17
|
+
#
|
18
|
+
# Unless required by applicable law or agreed to in writing, software
|
19
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
20
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
21
|
+
# See the License for the specific language governing permissions and
|
22
|
+
# limitations under the License.
|
23
|
+
#
|
24
|
+
|
25
|
+
require 'test/unit'
|
26
|
+
|
27
|
+
require 'mockery/controller'
|
28
|
+
|
29
|
+
class MockeryTest < Test::Unit::TestCase
|
30
|
+
|
31
|
+
def test_basic_history_fail
|
32
|
+
|
33
|
+
testee_class = Class.new
|
34
|
+
testee_class.class_eval {
|
35
|
+
def route(where, x)
|
36
|
+
end
|
37
|
+
}
|
38
|
+
|
39
|
+
mockery = Mockery::Controller.new(testee_class)
|
40
|
+
mockery.record do |testee|
|
41
|
+
testee.left(23)
|
42
|
+
end
|
43
|
+
mockery.try do |testee|
|
44
|
+
testee.route('left', 23)
|
45
|
+
end
|
46
|
+
assert_equal(false, mockery.validate)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_basic_history_success
|
50
|
+
|
51
|
+
testee_class = Class.new
|
52
|
+
testee_class.class_eval {
|
53
|
+
def route(where, x)
|
54
|
+
if where == 'left'
|
55
|
+
left(x)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
def left(value)
|
59
|
+
end
|
60
|
+
}
|
61
|
+
|
62
|
+
mockery = Mockery::Controller.new(testee_class)
|
63
|
+
mockery.record do |testee|
|
64
|
+
testee.left(23)
|
65
|
+
end
|
66
|
+
mockery.try do |testee|
|
67
|
+
testee.route('left', 23)
|
68
|
+
end
|
69
|
+
assert_equal(true, mockery.validate, mockery.error_report)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_multi_class_history_success
|
73
|
+
|
74
|
+
class_a = Class.new
|
75
|
+
class_a.class_eval {
|
76
|
+
attr_writer :b
|
77
|
+
def start(x)
|
78
|
+
show(x - 1)
|
79
|
+
@b.show(x + 1)
|
80
|
+
end
|
81
|
+
def show(x)
|
82
|
+
puts "a.show(#{x})"
|
83
|
+
end
|
84
|
+
}
|
85
|
+
|
86
|
+
class_b = Class.new
|
87
|
+
class_b.class_eval {
|
88
|
+
def show(x)
|
89
|
+
puts "b.show(#{x})"
|
90
|
+
end
|
91
|
+
}
|
92
|
+
|
93
|
+
mockery = Mockery::Controller.new(class_a, class_b)
|
94
|
+
mockery.record do |a, b, ctl|
|
95
|
+
a.show(22)
|
96
|
+
b.show(24)
|
97
|
+
end
|
98
|
+
mockery.try do |a, b|
|
99
|
+
a.b = b
|
100
|
+
a.start(23)
|
101
|
+
end
|
102
|
+
assert_equal(true, mockery.validate, mockery.error_report)
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Name: CallTest (tests/unit/mockery/call_test.rb)
|
4
|
+
#
|
5
|
+
# Description:
|
6
|
+
#
|
7
|
+
# Unit tests for the Mockery::Call object
|
8
|
+
#
|
9
|
+
#
|
10
|
+
# Copyright 2005 Gary Shea
|
11
|
+
#
|
12
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
13
|
+
# you may not use this file except in compliance with the License.
|
14
|
+
# You may obtain a copy of the License at
|
15
|
+
#
|
16
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
17
|
+
#
|
18
|
+
# Unless required by applicable law or agreed to in writing, software
|
19
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
20
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
21
|
+
# See the License for the specific language governing permissions and
|
22
|
+
# limitations under the License.
|
23
|
+
#
|
24
|
+
|
25
|
+
require 'test/unit'
|
26
|
+
|
27
|
+
require 'mockery/call'
|
28
|
+
|
29
|
+
class CallTest < Test::Unit::TestCase
|
30
|
+
|
31
|
+
def test_digest
|
32
|
+
|
33
|
+
obj = Object.new
|
34
|
+
|
35
|
+
c = Mockery::Call.new
|
36
|
+
c.digest(obj, ['blork', 'a', 1])
|
37
|
+
|
38
|
+
cr = Mockery::Call.new
|
39
|
+
cr.method_name = 'blork'
|
40
|
+
cr.args = ['a', 1]
|
41
|
+
|
42
|
+
assert_equal(cr, c)
|
43
|
+
|
44
|
+
# the caller is not compared by '=='
|
45
|
+
assert_equal(obj, c.caller)
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_equals
|
50
|
+
|
51
|
+
caller1 = Object.new
|
52
|
+
caller2 = Object.new
|
53
|
+
|
54
|
+
c1 = Mockery::Call.new
|
55
|
+
c1.method_name = 'blork'
|
56
|
+
c1.args = ['a', 1]
|
57
|
+
c1.caller = caller1
|
58
|
+
c1.return_value = 23
|
59
|
+
|
60
|
+
c2 = Mockery::Call.new
|
61
|
+
c2.method_name = 'blork'
|
62
|
+
c2.args = ['a', 1]
|
63
|
+
c2.caller = caller1
|
64
|
+
c1.return_value = 23
|
65
|
+
|
66
|
+
assert_equal(c1, c2)
|
67
|
+
|
68
|
+
c2.caller = caller2
|
69
|
+
assert_equal(true, c1 == c2)
|
70
|
+
c2.caller = caller1
|
71
|
+
assert_equal(true, c1 == c2)
|
72
|
+
|
73
|
+
c2.return_value = 24
|
74
|
+
assert_equal(true, c1 == c2)
|
75
|
+
c2.return_value = 23
|
76
|
+
assert_equal(true, c1 == c2)
|
77
|
+
|
78
|
+
c2.method_name = 'blink'
|
79
|
+
assert_equal(false, c1 == c2)
|
80
|
+
c2.method_name = 'blork'
|
81
|
+
assert_equal(true, c1 == c2)
|
82
|
+
|
83
|
+
c2.method_name = 'blink'
|
84
|
+
assert_equal(false, c1 == c2)
|
85
|
+
c2.method_name = 'blork'
|
86
|
+
assert_equal(true, c1 == c2)
|
87
|
+
|
88
|
+
c2.args = ['a']
|
89
|
+
assert_equal(false, c1 == c2)
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Name: ControllerTest (tests/unit/mockery/controller_test.rb)
|
4
|
+
#
|
5
|
+
# Description:
|
6
|
+
#
|
7
|
+
# Unit tests for the Mockery::Controller object
|
8
|
+
#
|
9
|
+
#
|
10
|
+
# Copyright 2005 Gary Shea
|
11
|
+
#
|
12
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
13
|
+
# you may not use this file except in compliance with the License.
|
14
|
+
# You may obtain a copy of the License at
|
15
|
+
#
|
16
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
17
|
+
#
|
18
|
+
# Unless required by applicable law or agreed to in writing, software
|
19
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
20
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
21
|
+
# See the License for the specific language governing permissions and
|
22
|
+
# limitations under the License.
|
23
|
+
#
|
24
|
+
|
25
|
+
require 'test/unit'
|
26
|
+
|
27
|
+
require 'mockery/controller'
|
28
|
+
|
29
|
+
class ControllerTest < Test::Unit::TestCase
|
30
|
+
|
31
|
+
def test_initialize
|
32
|
+
|
33
|
+
# the controller is initialized with some number of classes.
|
34
|
+
# each class presented to the controller will have a corresponding
|
35
|
+
# object available in each of
|
36
|
+
# the successive blocks.
|
37
|
+
#
|
38
|
+
# the controller creates and stashes a Mock object for each
|
39
|
+
# of the classes presented.
|
40
|
+
|
41
|
+
ctl_class = Class.new(Mockery::Controller)
|
42
|
+
ctl_class.class_eval {
|
43
|
+
attr_reader :mocks
|
44
|
+
}
|
45
|
+
|
46
|
+
klasses = [Class.new, Class.new]
|
47
|
+
ctl = ctl_class.new(klasses[0], klasses[1])
|
48
|
+
|
49
|
+
assert_equal(Array, ctl.mocks.class)
|
50
|
+
assert_equal(2, ctl.mocks.size)
|
51
|
+
|
52
|
+
mock = ctl.mocks[0]
|
53
|
+
def mock.klass
|
54
|
+
return @klass
|
55
|
+
end
|
56
|
+
assert_equal(klasses[0], mock.klass)
|
57
|
+
|
58
|
+
mock = ctl.mocks[1]
|
59
|
+
def mock.klass
|
60
|
+
return @klass
|
61
|
+
end
|
62
|
+
assert_equal(klasses[1], mock.klass)
|
63
|
+
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_record
|
68
|
+
|
69
|
+
ctl_class = Class.new(Mockery::Controller)
|
70
|
+
ctl_class.class_eval {
|
71
|
+
attr_reader :mocks
|
72
|
+
}
|
73
|
+
|
74
|
+
klasses = [Class.new, Class.new]
|
75
|
+
ctl = ctl_class.new(klasses[0], klasses[1])
|
76
|
+
|
77
|
+
ctl.record do |obj0, obj1, rc|
|
78
|
+
assert_equal(Mockery::RecordControl, rc.class)
|
79
|
+
call = obj0.glorbl(23)
|
80
|
+
call.return_value = 'dog'
|
81
|
+
call = obj1.snurf('blork')
|
82
|
+
call.return_value = 1.7
|
83
|
+
end
|
84
|
+
|
85
|
+
# verify that recorder 0 has the right contents
|
86
|
+
|
87
|
+
mock = ctl.mocks[0]
|
88
|
+
recorder = mock.recorder
|
89
|
+
def recorder.history
|
90
|
+
return @history
|
91
|
+
end
|
92
|
+
|
93
|
+
assert_equal(1, recorder.history.size)
|
94
|
+
assert_equal(Mockery::Call, recorder.history[0].class)
|
95
|
+
|
96
|
+
call = recorder.history[0]
|
97
|
+
assert_equal(recorder, call.caller)
|
98
|
+
assert_equal(:glorbl, call.method_name)
|
99
|
+
assert_equal([23], call.args)
|
100
|
+
assert_equal('dog', call.return_value)
|
101
|
+
|
102
|
+
# verify that recorder 1 has the right contents
|
103
|
+
|
104
|
+
mock = ctl.mocks[1]
|
105
|
+
recorder = mock.recorder
|
106
|
+
def recorder.history
|
107
|
+
return @history
|
108
|
+
end
|
109
|
+
|
110
|
+
assert_equal(1, recorder.history.size)
|
111
|
+
assert_equal(Mockery::Call, recorder.history[0].class)
|
112
|
+
|
113
|
+
call = recorder.history[0]
|
114
|
+
assert_equal(recorder, call.caller)
|
115
|
+
assert_equal(:snurf, call.method_name)
|
116
|
+
assert_equal(['blork'], call.args)
|
117
|
+
assert_equal(1.7, call.return_value)
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_try
|
122
|
+
|
123
|
+
# a collection of mocks is presented to the
|
124
|
+
# user as arguments to a block. the mocks
|
125
|
+
# are build by the #mocked_object method of Mock,
|
126
|
+
# which takes no arguments. the last argument
|
127
|
+
# is a 'controller'. the controller is used
|
128
|
+
# to temporarily pause recording.
|
129
|
+
|
130
|
+
ctl_class = Class.new(Mockery::Controller)
|
131
|
+
ctl_class.class_eval {
|
132
|
+
attr_accessor :mocks
|
133
|
+
}
|
134
|
+
ctl = ctl_class.new(Class.new)
|
135
|
+
|
136
|
+
mock_class = Class.new
|
137
|
+
mock_class.class_eval {
|
138
|
+
attr_writer :ret_instance
|
139
|
+
def mocked_object
|
140
|
+
return @ret_instance
|
141
|
+
end
|
142
|
+
}
|
143
|
+
|
144
|
+
mock0 = mock_class.new
|
145
|
+
ret0 = Object.new
|
146
|
+
mock0.ret_instance = ret0
|
147
|
+
|
148
|
+
mock1 = mock_class.new
|
149
|
+
ret1 = Object.new
|
150
|
+
mock1.ret_instance = ret1
|
151
|
+
|
152
|
+
# note that this discards the Object.new placeholder
|
153
|
+
# in the #new above
|
154
|
+
ctl.mocks = [mock0, mock1]
|
155
|
+
|
156
|
+
# try() creates the necessary objects for the block,
|
157
|
+
# whether the block is empty or not
|
158
|
+
|
159
|
+
ctl.try {|m1, m2|
|
160
|
+
assert_equal(ret0, m1)
|
161
|
+
assert_equal(ret1, m2)
|
162
|
+
}
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_validate
|
166
|
+
|
167
|
+
# validate steps through the mocks looking for
|
168
|
+
# an invalid one (meaning its #validate method
|
169
|
+
# returns false), and returns false if found.
|
170
|
+
# otherwise it returns true
|
171
|
+
|
172
|
+
ctl_class = Class.new(Mockery::Controller)
|
173
|
+
ctl_class.class_eval {
|
174
|
+
attr_accessor :mocks
|
175
|
+
}
|
176
|
+
ctl = ctl_class.new(Class.new)
|
177
|
+
|
178
|
+
mock_class = Class.new
|
179
|
+
mock_class.class_eval {
|
180
|
+
attr_writer :ret_valid
|
181
|
+
def validate
|
182
|
+
return @ret_valid
|
183
|
+
end
|
184
|
+
}
|
185
|
+
|
186
|
+
m1 = mock_class.new
|
187
|
+
m1.ret_valid = true
|
188
|
+
|
189
|
+
m2 = mock_class.new
|
190
|
+
m2.ret_valid = true
|
191
|
+
|
192
|
+
m3 = mock_class.new
|
193
|
+
m3.ret_valid = false
|
194
|
+
|
195
|
+
# first test with true only
|
196
|
+
|
197
|
+
ctl.mocks = [m1, m2]
|
198
|
+
assert_equal(true, ctl.validate)
|
199
|
+
|
200
|
+
# now with a false
|
201
|
+
|
202
|
+
ctl.mocks = [m1, m3, m2]
|
203
|
+
assert_equal(false, ctl.validate)
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|