@@ -5,198 +5,203 @@ local unexpected_args_error = require 'mach.unexpected_args_error'
5
5
local out_of_order_call_error = require ' mach.out_of_order_call_error'
6
6
local not_all_calls_occurred_error = require ' mach.not_all_calls_occurred_error'
7
7
8
- local expectation = {}
9
- expectation .__index = expectation
10
-
11
- local function create (handle_mock_calls , m )
12
- local o = {
13
- _m = m ,
14
- _call_specified = false ,
15
- _calls = {},
16
- _completed_calls = {},
17
- _handle_mock_calls = handle_mock_calls
18
- }
19
-
20
- setmetatable (o , expectation )
8
+ return function (handle_mock_calls , m )
9
+ local o = {}
10
+ local call_specified = false
11
+ local calls = {}
12
+ local completed_calls = {}
13
+ local ignore_other_calls
14
+
15
+ local function wrap (f )
16
+ return function (self , ...)
17
+ if not self or self == o then
18
+ return f (... )
19
+ else
20
+ return f (self , ... )
21
+ end
22
+ end
23
+ end
21
24
22
- return o
23
- end
25
+ o .and_will_return = wrap (function (...)
26
+ if not call_specified then
27
+ error (' cannot set return value for an unspecified call' , 2 )
28
+ end
24
29
25
- function expectation :and_will_return (...)
26
- if not self ._call_specified then
27
- error (' cannot set return value for an unspecified call' , 2 )
28
- end
30
+ calls [# calls ]:set_return_values (... )
29
31
30
- self ._calls [# self ._calls ]:set_return_values (... )
32
+ return o
33
+ end )
31
34
32
- return self
33
- end
35
+ o .and_will_raise_error = wrap (function (...)
36
+ if not call_specified then
37
+ error (' cannot set error for an unspecified call' , 2 )
38
+ end
34
39
35
- function expectation :and_will_raise_error (...)
36
- if not self ._call_specified then
37
- error (' cannot set error for an unspecified call' , 2 )
38
- end
40
+ calls [# calls ]:set_error (... )
39
41
40
- self ._calls [# self ._calls ]:set_error (... )
42
+ return o
43
+ end )
41
44
42
- return self
43
- end
45
+ o .when = wrap (function (thunk )
46
+ if not call_specified then
47
+ error (' incomplete expectation' , 2 )
48
+ end
44
49
45
- function expectation :when (thunk )
46
- if not self ._call_specified then
47
- error (' incomplete expectation' , 2 )
48
- end
50
+ local current_call_index = 1
49
51
50
- local current_call_index = 1
52
+ local function called (m , name , args )
53
+ local valid_function_found = false
54
+ local incomplete_expectation_found = false
51
55
52
- local function called (m , name , args )
53
- local valid_function_found = false
54
- local incomplete_expectation_found = false
56
+ for i = current_call_index , # calls do
57
+ local call = calls [i ]
55
58
56
- for i = current_call_index , # self . _calls do
57
- local call = self . _calls [ i ]
59
+ if call : function_matches ( m ) then
60
+ valid_function_found = true
58
61
59
- if call :function_matches (m ) then
60
- valid_function_found = true
62
+ if call :args_match (args ) then
63
+ if call :has_fixed_order () and incomplete_expectation_found then
64
+ out_of_order_call_error (name , args , completed_calls , calls , 2 )
65
+ end
61
66
62
- if call :args_match (args ) then
63
- if call :has_fixed_order () and incomplete_expectation_found then
64
- out_of_order_call_error (name , args , self ._completed_calls , self ._calls , 2 )
65
- end
67
+ if call :has_fixed_order () then
68
+ current_call_index = i
69
+ end
66
70
67
- if call :has_fixed_order () then
68
- current_call_index = i
69
- end
71
+ table.remove (calls , i )
70
72
71
- table.remove ( self . _calls , i )
73
+ table.insert ( completed_calls , CompletedCall ( name , args ) )
72
74
73
- table.insert (self ._completed_calls , CompletedCall (name , args ))
75
+ if call :has_error () then
76
+ error (call :get_error ())
77
+ end
74
78
75
- if call :has_error () then
76
- error (call :get_error ())
79
+ return call :get_return_values ()
77
80
end
81
+ end
78
82
79
- return call :get_return_values ()
83
+ if call :is_required () then
84
+ incomplete_expectation_found = true ;
80
85
end
81
86
end
82
87
83
- if call :is_required () then
84
- incomplete_expectation_found = true ;
88
+ if not ignore_other_calls then
89
+ if not valid_function_found then
90
+ unexpected_call_error (name , args , completed_calls , calls , 2 )
91
+ else
92
+ unexpected_args_error (name , args , completed_calls , calls , 2 )
93
+ end
85
94
end
86
95
end
87
96
88
- if not self . _ignore_other_calls then
89
- if not valid_function_found then
90
- unexpected_call_error ( name , args , self . _completed_calls , self . _calls , 2 )
91
- else
92
- unexpected_args_error ( name , args , self . _completed_calls , self . _calls , 2 )
97
+ handle_mock_calls ( called , thunk )
98
+
99
+ for _ , call in pairs ( calls ) do
100
+ if call : is_required () then
101
+ not_all_calls_occurred_error ( completed_calls , calls , 2 )
93
102
end
94
103
end
95
- end
104
+ end )
105
+
106
+ o .after = wrap (function (thunk )
107
+ if not call_specified then
108
+ error (' incomplete expectation' , 2 )
109
+ end
96
110
97
- self ._handle_mock_calls (called , thunk )
111
+ o .when (thunk )
112
+ end )
98
113
99
- for _ , call in pairs (self ._calls ) do
100
- if call :is_required () then
101
- not_all_calls_occurred_error (self ._completed_calls , self ._calls , 2 )
114
+ o .and_then = wrap (function (other )
115
+ for i , call in ipairs (other ._calls ()) do
116
+ if i == 1 then call :fix_order () end
117
+ table.insert (calls , call )
102
118
end
103
- end
104
- end
105
119
106
- function expectation :after (thunk )
107
- if not self ._call_specified then
108
- error (' incomplete expectation' , 2 )
109
- end
120
+ return o
121
+ end )
110
122
111
- self :when (thunk )
112
- end
123
+ o .and_also = wrap (function (other )
124
+ for _ , call in ipairs (other ._calls ()) do
125
+ table.insert (calls , call )
126
+ end
113
127
114
- function expectation :and_then (other )
115
- for i , call in ipairs (other ._calls ) do
116
- if i == 1 then call :fix_order () end
117
- table.insert (self ._calls , call )
118
- end
128
+ return o
129
+ end )
119
130
120
- return self
121
- end
131
+ o .should_be_called_with_any_arguments = wrap (function ()
132
+ if call_specified then
133
+ error (' call already specified' , 2 )
134
+ end
122
135
123
- function expectation : and_also ( other )
124
- for _ , call in ipairs ( other . _calls ) do
125
- table.insert ( self . _calls , call )
126
- end
136
+ call_specified = true
137
+ table.insert ( calls , ExpectedCall ( m , { required = true , args = table.pack (), ignore_args = true }))
138
+ return o
139
+ end )
127
140
128
- return self
129
- end
141
+ o .should_be_called_with = wrap (function (...)
142
+ if call_specified then
143
+ error (' call already specified' , 2 )
144
+ end
130
145
131
- function expectation : should_be_called_with_any_arguments ()
132
- if self . _call_specified then
133
- error ( ' call already specified ' , 2 )
134
- end
146
+ call_specified = true
147
+ table.insert ( calls , ExpectedCall ( m , { required = true , args = table.pack ( ... ) }))
148
+ return o
149
+ end )
135
150
136
- self . _call_specified = true
137
- table.insert ( self . _calls , ExpectedCall ( self . _m , { required = true , args = table.pack (), ignore_args = true }))
138
- return self
139
- end
151
+ o . should_be_called = wrap ( function ()
152
+ if call_specified then
153
+ error ( ' call already specified ' , 2 )
154
+ end
140
155
141
- function expectation :should_be_called_with (...)
142
- if self ._call_specified then
143
- error (' call already specified' , 2 )
144
- end
156
+ return o .should_be_called_with ()
157
+ end )
145
158
146
- self . _call_specified = true
147
- table.insert ( self . _calls , ExpectedCall ( self . _m , { required = true , args = table.pack ( ... ) }))
148
- return self
149
- end
159
+ o . may_be_called_with_any_arguments = wrap ( function ()
160
+ if call_specified then
161
+ error ( ' call already specified ' , 2 )
162
+ end
150
163
151
- function expectation : should_be_called ()
152
- if self . _call_specified then
153
- error ( ' call already specified ' , 2 )
154
- end
164
+ call_specified = true
165
+ table.insert ( calls , ExpectedCall ( m , { required = false , ignore_args = true }))
166
+ return o
167
+ end )
155
168
156
- return self :should_be_called_with ()
157
- end
169
+ o .may_be_called_with = wrap (function (...)
170
+ if call_specified then
171
+ error (' call already specified' , 2 )
172
+ end
158
173
159
- function expectation : may_be_called_with_any_arguments ()
160
- if self . _call_specified then
161
- error ( ' call already specified ' , 2 )
162
- end
174
+ call_specified = true
175
+ table.insert ( calls , ExpectedCall ( m , { required = false , args = table.pack ( ... ) }))
176
+ return o
177
+ end )
163
178
164
- self . _call_specified = true
165
- table.insert ( self . _calls , ExpectedCall ( self . _m , { required = false , ignore_args = true }))
166
- return self
167
- end
179
+ o . may_be_called = wrap ( function ()
180
+ if call_specified then
181
+ error ( ' call already specified ' , 2 )
182
+ end
168
183
169
- function expectation :may_be_called_with (...)
170
- if self ._call_specified then
171
- error (' call already specified' , 2 )
172
- end
184
+ return o .may_be_called_with ()
185
+ end )
173
186
174
- self . _call_specified = true
175
- table.insert ( self . _calls , ExpectedCall ( self . _m , { required = false , args = table.pack ( ... ) }))
176
- return self
177
- end
187
+ o . multiple_times = wrap ( function ( times )
188
+ for i = 1 , times - 1 do
189
+ table.insert ( calls , calls [ # calls ])
190
+ end
178
191
179
- function expectation :may_be_called ()
180
- if self ._call_specified then
181
- error (' call already specified' , 2 )
182
- end
192
+ return o
193
+ end )
183
194
184
- return self :may_be_called_with ()
185
- end
195
+ o .and_other_calls_should_be_ignored = wrap (function ()
196
+ ignore_other_calls = true
197
+ return o
198
+ end )
186
199
187
- function expectation :multiple_times (times )
188
- for i = 1 , times - 1 do
189
- table.insert (self ._calls , self ._calls [# self ._calls ])
200
+ o ._calls = function ()
201
+ return calls
190
202
end
191
203
192
- return self
193
- end
204
+ o .with_other_calls_ignored = o .and_other_calls_should_be_ignored
194
205
195
- function expectation :and_other_calls_should_be_ignored ()
196
- self ._ignore_other_calls = true
197
- return self
206
+ return o
198
207
end
199
-
200
- expectation .with_other_calls_ignored = expectation .and_other_calls_should_be_ignored
201
-
202
- return create
0 commit comments