-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.erl
355 lines (266 loc) · 11.2 KB
/
template.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
%%%----------------------------------------------------------------------------
%%% File : template.erl
%%% Description : Template of mininal test functions using erlunit.erl
%%% Version : 0.2.8.2/alpha
%%% Status : alpha
%%% Copyright : Public Domain
%%% License : MIT - http://www.opensource.org/licenses/mit-license.php
%%% Author : H. Diedrich <[email protected]>
%%% Created : 24 Apr 2010
%%% Changed : 24 Apr 2010 - see CHANGES
%%% Tested on : Erlang R13B01
%%%----------------------------------------------------------------------------
%%%
%%% Delete the above and roll your own. Maybe keep a copy of the original file.
%%%
%%% This is a very simple sample. Connect it with your own stuff by
%%%
%%% Then, you could read a bit in sample.erl to get started.
%%%
%%%----------------------------------------------------------------------------
%%%
%%% Prerequisite: Erlang installed. From R12B-4 might do. R13B-1 will.
%%%
%%%----------------------------------------------------------------------------
%%%
%%% Usage:
%%%
%%% Run it though to find out if it all compiles on your machine:
%%%
%%% # erl
%%% 1> {ok,_} = c(erlunit), {ok,_} = c(sample), sample:run().
%%%
%%%----------------------------------------------------------------------------
%%%
%%% Mail to [email protected] with questions and suggestions, I will be happy
%%% to answer. - Henning
%%%
%%%----------------------------------------------------------------------------
-module(template).
-import(erlunit).
%%%****************************************************************************
%%% TEMPLATE - SEQUENTIAL CALL STYLE
%%%****************************************************************************
run() ->
erlunit:start(),
erlunit:equal(1, 1),
erlunit:execute().
% --- That's it. A complete test program. Run with
% --- # erl
% --- 1> c(erlunit), c(sample), sample:sample1().
% --- TRY THIS: Alter to erlunit:equal(1, 2) and run again.
% --- The test will 'fail' and say so.
%%%-------------------------------------------------------------------------#1-
%%%
%%% Tests are written to test functions of your own source. There is a
%%% a lot to be said for writing tests for your source and even write
%%% test before you write your source: to clarify and define your task
%%% and to make life easier once you change implementations.
%%%
%%% See http://www.c2.com/cgi/wiki?TestDrivenDevelopment for more.
%%%
%%% Note that your code comes into play where 1 is used in this first
%%% sample. Eg. to test your function Foo() that is expected to always
%%% result into 42 when fed with a bar, you'd write a test like this:
%%% equal(Foo(bar), 42). This way you have a test coded that will make
%%% sure that Foo(bar) still results into 42, whenever you run it, no
%%% matter how many source iterations later you come back to it.
%%%
%%%----------------------------------------------------------------------------
%%%****************************************************************************
%%% #2 SUPER SIMPLE SAMPLE - MESSAGE PASSING STYLE
%%%****************************************************************************
%%%-------------------------------------------------------------------------#2-
%%%
%%% The above was a sequential example and if you come from an impera-
%%% tive language you might feel more at home with it. Why not use it.
%%%
%%% The following notation uses Erlang message passing to achieve the
%%% same results. If you use that you will be able to execute tests
%%% concurrently later.
%%%
%%%----------------------------------------------------------------------------
sample2() ->
erlunit:strong_banner("Super simple demonstration #2 - message passing style."),
Test = erlunit:start(),
Test ! { equal, 1, 1 },
erlunit:execute().
% --- That's it. A complete test program. Run with
% --- # erl
% --- 1> c(erlunit), c(sample), sample:sample2().
%%%****************************************************************************
%%% #3 SUPER SIMPLE SAMPLE: SUITES, SEQUENTIAL
%%%****************************************************************************
%%%-------------------------------------------------------------------------#3-
%%%
%%% 'Suites' are used to group individual tests.
%%%
%%% A suite is created with erlunit:suite("<suite name>"). Tests are
%%% grouped into the suite last created, by default.
%%%
%%%----------------------------------------------------------------------------
sample3() ->
erlunit:strong_banner("Super simple demonstration #3 - suites."),
erlunit:start(),
erlunit:suite("#1"),
erlunit:equal(100, 100),
erlunit:suite("#2"),
erlunit:not_equal(a, b),
erlunit:execute().
% --- That's it. A complete test program. Run with
% --- # erl
% --- 1> c(erlunit), c(sample), sample:sample3().
%%%****************************************************************************
%%% #4 SUPER SIMPLE SAMPLE: SREE SUITES, SEQUENTIAL
%%%****************************************************************************
sample4() ->
erlunit:strong_banner("Super simple demonstration #4 - concurrent suites."),
erlunit:start(),
erlunit:suite("#1"),
erlunit:equal(100, 100),
erlunit:suite("#2"),
erlunit:not_equal(1, 2),
erlunit:suite("#3"),
erlunit:not_equal("A", 3),
erlunit:execute().
% --- That's it. A complete test program. Run with
% --- # erl
% --- 1> c(erlunit), c(sample), sample:sample4().
%%%****************************************************************************
%%% #5 SIMPLE SAMPLE - WITH SUITES
%%%****************************************************************************
%%%-------------------------------------------------------------------------#5-
%%%
%%% Suites can be run concurrently. Running this sample will show how the
%%% individual checks of both suits are neatly interleaving.
%%%
%%% This only works with the message passing notation. It exploits the effect
%%% that the suites are 'loaded' with checks pretty fast and the actual
%%% execution of the checks doesn't start before erlunit:execute() is called.
%%%
%%% Which in effect mostly means: wait for the suites to get done. And so it
%%% waits and gives the suites the chance to use the cpu time between them.
%%%
%%%----------------------------------------------------------------------------
sample5() ->
erlunit:strong_banner("Simple demonstration of test functions: parallel with suites."),
erlunit:echo("Suites will run concurrently and their output will *interleave*!"),
erlunit:start(),
Suite1 = erlunit:suite("#1"),
Suite1 ! { true, 1 == 1 },
% bug, harden: Suite1 ! { true, 2, 2 }, (right would be Suite1 ! { true, 2 == 2 },
Suite1 ! { equal, 3, 3 },
Suite1 ! { equal, 4, 4 },
Suite1 ! { equal, 5, 5 },
Suite1 ! { equal, 6, 6 },
Suite1 ! { equal, 7, 7 },
Suite1 ! { equal, 8, 8 },
Suite1 ! { equal, 9, 9 },
Suite1 ! { equal, 10, 10 },
Suite2 = erlunit:suite("#2"),
Suite2 ! { not_equal, 1, 2 },
Suite2 ! { not_equal, 1, 3 },
Suite2 ! { not_equal, 1, 4 },
Suite2 ! { not_equal, 1, 5 },
Suite2 ! { not_equal, 1, 6 },
Suite2 ! { not_equal, 1, 7 },
Suite2 ! { not_equal, 1, 8 },
Suite2 ! { not_equal, 1, 9 },
Suite2 ! { not_equal, 1, 10 },
erlunit:execute().
% --- That's it. A complete test program. Run with
% --- # erl
% --- 1> c(erlunit), c(sample), sample:sample5().
%%%****************************************************************************
%%% #10 MORE STRUCTURED SAMPLE - SUITES
%%%****************************************************************************
%%%------------------------------------------------------------------------#10-
%%%
%%% Suites are well suited to be put in functions to structure your code.
%%%
%%%----------------------------------------------------------------------------
sample10() ->
erlunit:strong_banner("Demonstrating the use of suite functions."),
erlunit:start(),
suite1(),
suite2(),
erlunit:execute().
% -(o)- %
%%%****************************************************************************
%%% SAMPLE SUITES
%%%****************************************************************************
%%%
%%%----------------------------------------------------------------------------
%%% #1: Simple Suite
%%%----------------------------------------------------------------------------
suite1() ->
erlunit:suite("#1/Simple"),
erlunit:true(1 == 1),
erlunit:equal(1, 1),
erlunit:fail(fun() -> 1 / zero() end).
% using zero() for 0 to avoid Erlang compile time warnings
%%%
%%%----------------------------------------------------------------------------
%%% #2: same as above, with names for individual checks
%%%----------------------------------------------------------------------------
suite2() ->
erlunit:suite("#2/Simple (verbose)"),
erlunit:echo("Suite #2 gives names to individual checks"),
erlunit:true(1 == 1, "One Equals One"),
erlunit:false(1 == 0, "One Equals Zero"),
erlunit:not_true(1 == 2, "One Does Not Equal Two"),
erlunit:not_false(foo, "Foo Is Not True"),
erlunit:not_false(1 == 1, "One Not Not Equals One"),
erlunit:not_false(bar, "Bar Is Not False"),
erlunit:equal(1, 1, "Once More Equality"),
erlunit:not_equal(1, 0, "And Non-Equality"),
erlunit:pass(fun() -> 1 + 0 end, "Trivial Fun"),
erlunit:fail(fun() -> 1 / zero() end, "Div By Zero").
% using zero() for 0 to avoid Erlang compile time warnings
% run_suite2() ->
%
% erlunit:suite("2"),
%
% erlunit:echo("This suite is intended to have failures"),
%
% erlunit:equal(1,1,"1 and 1 are equal"),
% erlunit:notequal(1,1,"1 and 1 are not equal"),
% erlunit:lesser(1,1,"1 is lesser than 1"),
% erlunit:bigger(1,1,"1 is bigger than 1"),
%
% erlunit:suite_end().
%%%****************************************************************************
%%% run - run all samples
%%%****************************************************************************
run() ->
erlunit:strong_banner("ALL SAMPLES"),
sample1(),
sample2(),
sample3(),
sample4(),
sample5(),
sample10().
% -(o)- %
%%%****************************************************************************
%%% UTILITY
%%%****************************************************************************
%%%
%%%----------------------------------------------------------------------------
%%% This is used to avoid Erlang compile time warnings.
%%%----------------------------------------------------------------------------
zero() -> 0.
%%%****************************************************************************
%%% SCREEN UTILITY
%%%****************************************************************************
%%%
%%%----------------------------------------------------------------------------
%%% Light Banner
%%%----------------------------------------------------------------------------
%%% Left-aligned banner with program name, version and message text:
banner(Message) ->
io:format("~s~n~s ~s~n~s~n~s~n",[erlunit:dashline(), ?PROGRAM, ?VERSION, Message, erlunit:dashline()]).
%%%----------------------------------------------------------------------------
%%% Further reading
%%%----------------------------------------------------------------------------
%%% The actual test functions are in erlunit.erl
%%% View online at http://github.com/hdiedrich/erlunit/blob/master/erlunit.erl