-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.erl
286 lines (257 loc) · 7.01 KB
/
lib.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
-module(lib).
-author(josef).
-export([seq/1, seq/2
,par/1, par/2
,choice/1, choice/2
,perm/1, star/1, call/2, empty/0
% Running stubs
,stub/1, check_stub/0
% Functions used under the hood
,undefined_call/3, stub_proc/1]).
-record(seq, { accepting :: boolean()
, left :: stub()
, right :: stub()
}).
-record(seq_list, { accepting :: boolean()
, stub_list :: list(stub())
}).
-record(par, { accepting :: boolean()
, left :: stub()
, right :: stub()
}).
-record(par_list, { accepting :: boolean()
, stub_list :: list(stub())
}).
-record(choice, { accepting :: boolean()
, left :: stub()
, right :: stub()
}).
-record(choice_list, { accepting :: boolean()
, stub_list :: list(stub())
}).
-record(perm_list, { accepting :: boolean()
, stub_list :: list(stub())
}).
-record(star, { accepting = true :: boolean()
, body :: stub()
, copy :: stub()
, progress :: boolean()
}).
-record(call, { accepting = false :: boolean()
, mfa :: mfa()
, result :: term()
}).
-record(empty, { accepting = true :: boolean() }).
-type stub() :: #seq{} | #seq_list{}
| #par{} | #par_list{}
| #choice{} | #choice_list{}
| #perm_list{}
| #star{}
| #call{}
| #empty{}.
% Sequential composition of stubs
seq(StubList) when is_list(StubList)->
#seq_list{ accepting = lists:all(fun accepting/1, StubList)
, stub_list = StubList
}.
seq(S1,S2) ->
#seq{ accepting = accepting(S1) andalso accepting(S2)
, left = S1
, right = S2
}.
% Parallel composition of stubs
par(ParList) when is_list(ParList) ->
#par_list{ accepting = lists:all(fun accepting/1,ParList)
, stub_list = ParList
}.
par(S1,S2) ->
#par{ accepting = accepting(S1) andalso accepting(S2)
, left = S1
, right = S2
}.
% The software under test can behave in different ways.
choice(S1, S2) ->
#choice{ accepting = accepting(S1) orelse accepting(S2)
, left = S1
, right = S2
}.
choice(ChoiceList) when is_list(ChoiceList) ->
#choice_list{ accepting = lists:any(fun accepting/1,ChoiceList)
, stub_list = ChoiceList
}.
% Permutations of stubs
perm(PermList) when is_list(PermList) ->
#perm_list{ accepting = lists:all(fun accepting/1,PermList)
, stub_list = PermList
}.
% A stub mayb be repeat an indefinite number of times.
% Invariant during stepping: body /= copy <=> progress
star(S) ->
#star{ accepting = true
, body = S
, copy = S
, progress = false
}.
% Stub a function call
call(MFA={_,_,_}, Result) ->
#call{ accepting = false
, mfa = MFA
, result = Result
}.
% No stub at all
empty() ->
#empty{ accepting = false }.
%% Accepting predicate
%% Returns true if the stub can accept the empty call sequence
accepting(#seq { accepting = A }) -> A;
accepting(#seq_list { accepting = A }) -> A;
accepting(#par { accepting = A }) -> A;
accepting(#par_list { accepting = A }) -> A;
accepting(#choice { accepting = A }) -> A;
accepting(#choice_list{ accepting = A }) -> A;
accepting(#perm_list { accepting = A }) -> A;
accepting(#star { accepting = A }) -> A;
accepting(#call { accepting = A }) -> A;
accepting(#empty { accepting = A }) -> A.
%% Operational semantics
-spec step(mfa(), stub()) -> {ok , term(), stub()}
| {fail, term()}.
step(MFA, #call{ mfa = MFA, result = Result}) ->
{ok, Result, empty()};
step(_MFA, CALL = #call{}) ->
{fail, {expected_call, CALL}};
step(MFA, #seq{ left = S1, right = S2}) ->
case step(MFA, S1) of
{ok, Result, S} ->
{ok, Result, seq(S,S2)};
Fail = {fail, Reason1} ->
case accepting(S1) of
true ->
case step(MFA, S2) of
Res = {ok, _, _} ->
Res;
{fail, _Reason2} ->
{fail, Reason1} %% FIXME: Merge Reason1 and Reason2
end;
false ->
Fail
end
end;
step(MFA, #seq_list{ stub_list = SeqList }) ->
step_seq_list(MFA, SeqList);
step(MFA, #par{ left = S1, right = S2 }) ->
case step(MFA, S1) of
{ok, Result, S} ->
{ok, Result, par(S, S2)};
{fail, Reason1} ->
case step(MFA, S2) of
{ok, Result, S} ->
{ok, Result, par(S1, S)};
{fail, _Reason2} ->
{fail, Reason1} %% FIXME: Figure out a better failure message
end
end;
step(MFA, #choice{ left = S1, right = S2 }) ->
case step(MFA, S1) of
Result = {ok, _, _} ->
Result;
{fail, Reason1} ->
case step(MFA, S2) of
Result = {ok, _, _} ->
Result;
{fail, _Reason2} ->
{fail, Reason1} %% FIXME: Better failure message
end
end;
step(MFA, Star = #star{ body = Body, copy = Copy, progress = Prog }) ->
case step(MFA, Body) of
{ok, Result, S } ->
{ok, Result, Star#star{ body = S, progress = true }};
Fail = {fail, _Reason} ->
case accepting(Body) andalso Prog of
true ->
step(MFA, Star#star{ body = Copy, progress = false });
false ->
Fail
end
end;
step(MFA, #empty{}) ->
{fail, {unexpected_call, MFA}}.
% FIXME: Complete the definition of step/2.
step_seq_list(MFA, []) ->
{fail, {no_call_matching, MFA}};
step_seq_list(MFA, [S|SeqList]) ->
case step(MFA, S) of
{ok, Result, #empty{}} ->
{ok, Result, #seq_list{
accepting =
lists:all(fun accepting/1, SeqList),
stub_list =
SeqList
}};
Fail = {fail, _Reason} ->
case accepting(S) of
true ->
step_seq_list(MFA, SeqList);
false ->
Fail
end
end.
%% Install the stubs
-spec stub(stub()) -> ok.
stub(Stub) ->
% Create a new process which maintains the state of the stub
Pid = spawn(?MODULE, stub_proc, Stub),
% Register a name for the process so that we don't have to
% pass around the Pid.
register(stub_proc, Pid),
% Install new call handler
OLD = process_flag(error_handler, ?MODULE),
put(old_error_handler, OLD).
-spec check_stub() -> boolean().
% Must be called in the same process that called stub/1
check_stub() ->
stub_proc ! {finalize, self()},
receive
Result ->
unregister(stub_proc),
OLD = get(old_error_handler),
process_flag(error_handler, OLD),
erase(old_error_handler),
Result
end.
stub_proc(Stub) ->
receive
{call, MFA, Pid} ->
case step(MFA, Stub) of
{ok, Result, S} ->
Pid ! {ok, Result},
stub_proc(S);
Fail = {fail, Reason} ->
Pid ! Fail,
stub_failed(Reason)
end;
{finalize, Pid} ->
Pid ! accepting(Stub)
end.
stub_failed(Reason) ->
receive
{call, _, Pid} ->
Pid ! {fail, Reason};
{finalize, Pid} ->
Pid ! false
end.
undefined_call(Module, Func, Args) ->
% We currently don't stub loaded modules
case code:if_loaded(Module) of
true ->
error_handler:undefined_call(Module, Func, Args);
false ->
stub_proc ! {call, {Module, Func, Args}, self()},
receive
{ok, Result} ->
Result;
{fail, Reason} ->
throw(Reason)
end
end.