@@ -5,198 +5,203 @@ local unexpected_args_error = require 'mach.unexpected_args_error'
55local out_of_order_call_error = require ' mach.out_of_order_call_error'
66local not_all_calls_occurred_error = require ' mach.not_all_calls_occurred_error'
77
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
2124
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
2429
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 (... )
2931
30- self ._calls [# self ._calls ]:set_return_values (... )
32+ return o
33+ end )
3134
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
3439
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 (... )
3941
40- self ._calls [# self ._calls ]:set_error (... )
42+ return o
43+ end )
4144
42- return self
43- end
45+ o .when = wrap (function (thunk )
46+ if not call_specified then
47+ error (' incomplete expectation' , 2 )
48+ end
4449
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
4951
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
5155
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 ]
5558
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
5861
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
6166
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
6670
67- if call :has_fixed_order () then
68- current_call_index = i
69- end
71+ table.remove (calls , i )
7072
71- table.remove ( self . _calls , i )
73+ table.insert ( completed_calls , CompletedCall ( name , args ) )
7274
73- table.insert (self ._completed_calls , CompletedCall (name , args ))
75+ if call :has_error () then
76+ error (call :get_error ())
77+ end
7478
75- if call :has_error () then
76- error (call :get_error ())
79+ return call :get_return_values ()
7780 end
81+ end
7882
79- return call :get_return_values ()
83+ if call :is_required () then
84+ incomplete_expectation_found = true ;
8085 end
8186 end
8287
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
8594 end
8695 end
8796
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 )
93102 end
94103 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
96110
97- self ._handle_mock_calls (called , thunk )
111+ o .when (thunk )
112+ end )
98113
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 )
102118 end
103- end
104- end
105119
106- function expectation :after (thunk )
107- if not self ._call_specified then
108- error (' incomplete expectation' , 2 )
109- end
120+ return o
121+ end )
110122
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
113127
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 )
119130
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
122135
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 )
127140
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
130145
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 )
135150
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
140155
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 )
145158
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
150163
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 )
155168
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
158173
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 )
163178
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
168183
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 )
173186
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
178191
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 )
183194
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 )
186199
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
190202 end
191203
192- return self
193- end
204+ o .with_other_calls_ignored = o .and_other_calls_should_be_ignored
194205
195- function expectation :and_other_calls_should_be_ignored ()
196- self ._ignore_other_calls = true
197- return self
206+ return o
198207end
199-
200- expectation .with_other_calls_ignored = expectation .and_other_calls_should_be_ignored
201-
202- return create
0 commit comments