-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.erl
executable file
·446 lines (339 loc) · 15.4 KB
/
sample.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
%%%----------------------------------------------------------------------------
%%% File : sample.erl
%%% Description : Sample usage of test functions in erlunit.erl
%%% Version : 0.2.8.2/alpha
%%% Status : alpha
%%% Copyright : (c) 2010 Eonblast Corporation http://www.eonblast.com
%%% License : MIT - http://www.opensource.org/licenses/mit-license.php
%%% Author : H. Diedrich <[email protected]>
%%% Created : 18 Apr 2010
%%% Changed : 03 May 2010 - see CHANGES
%%% Tested on : Erlang R13B01
%%%----------------------------------------------------------------------------
%%%
%%% This program demonstrates the use of the Erlunit test functions.
%%%
%%% It can be read top down. There are explanation sections mixed in.
%%%
%%% You should read the two pages covering samples 1, 2, 3 to get started.
%%%
%%%----------------------------------------------------------------------------
%%%
%%% Prerequisite: Erlang installed. From R12B-4 might do. R13B-1 will.
%%%
%%%----------------------------------------------------------------------------
%%%
%%% Usage: reading and run.
%%%
%%% To find out if it all compiles on your machine:
%%%
%%% # erl
%%% 1> {ok,_} = c(erlunit), {ok,_} = c(sample), sample:run().
%%%
%%%
%%% To try individual samples, do (1, 2, 3, 4, 5, 10):
%%%
%%% # erl
%%% 1> c(erlunit), c(sample), sample:sample1().
%%%
%%%----------------------------------------------------------------------------
%%% : :
%%% : E r l u n i t - is a naive approach at an academically discussed issue. :
%%% : There are Erlang masters on the erlang-questions list, this here source :
%%% : ist not written by one. Beware of copying mistakes if you are learning. :
%%% : :
%%% : Thus consider these two serious contenders who deserve your attention: :
%%% : :
%%% : E U n i t - by Richard Carlsson and Michaêl Rémond. :
%%% : "EUnit is a unit testing framework for Erlang. It is very powerful and :
%%% : flexible, is easy to use, and has small syntactical overhead." :
%%% : http://svn.process-one.net/contribs/trunk/eunit/doc/overview-summary.html
%%% : :
%%% : e t a p - by Jeremy Wall, Paul J. Davis, Nick Gerakines et al. :
%%% : "etap is a collection of Erlang modules that provide a TAP testing :
%%% : client library. These modules allow developers to create extensive and :
%%% : comprehensive tests covering many aspects of application and module :
%%% : development. This includes simple assertions, exceptions, the :
%%% : application behavior and event web requests." :
%%% : - http://github.com/ngerakines/etap/downloads :
%%% : :
%%%----------------------------------------------------------------------------
%%% : :
%%% : Mail to [email protected] with questions and suggestions, I will be :
%%% : happy to answer. - Henning :
%%% : :
%%%----------------------------------------------------------------------------
-module(sample).
-vsn("0.2.8.2/alpha").
-author("H. Diedrich <[email protected]>").
-license("MIT - http://www.opensource.org/licenses/mit-license.php").
-copyright("(c) 2010 Eonblast Corporation http://www.eonblast.com").
%%%----------------------------------------------------------------------------
-export([run/0, sample1/0, sample2/0, sample3/0, sample4/0, sample5/0]).
-export([sample9/0,sample10/0]).
-import(erlunit).
-include("erlunit.hrl").
-compile({nowarn_unused_function, [banner/1]}).
-define(VERSION, "0.2.8.2/alpha").
-define(PROGRAM, "Test Samples").
%%%****************************************************************************
%%% #1 SUPER SIMPLE SAMPLE - SEQUENTIAL CALL STYLE
%%%****************************************************************************
sample1() ->
erlunit:strong_banner("Super simple demonstration #1 - imperative style."),
erlunit:start(),
erlunit:equal(1, 1),
erlunit:execute().
% --- That's it. A complete test program. It checks if 1 equals 1. 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. Another complete test program. Run with
% --- # erl
% --- 1> c(erlunit), c(sample), sample:sample3().
% --- TRY THIS: Add a third suite by copy-pasting the two forelast lines.
% --- Add more checks to suite #2 by copy-pasting the forelast.
%%%****************************************************************************
%%% #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. Another 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. Another complete test program. Run with
% --- # erl
% --- 1> c(erlunit), c(sample), sample:sample5().
%%%****************************************************************************
%%% #9 SIMPLE SAMPLE - MACROS WITH SUITES
%%%****************************************************************************
%%%-------------------------------------------------------------------------#9-
%%%
%%% Macros reduce the boilerplate, i.e. save you a lot of typing and increase
%%% speed of pinpointing an error by adding line numbers to the output auto-
%%% matically. They also add a string representation of your source to a
%%% possible error message. Take a look at erlunit.hrl to check them out.
%%%
%%% E.g. ?ERLUNIT_EQUAL(F, R) is the same as if you wrote
%%% erlunit:equal(fun() -> F end, R, "Expect F == R", ?MODULE, ?LINE)).
%%%
%%% To use macros, you have to include the header erlunit.hrl in your source,
%%% like so:
%%% - include("erlunit.hrl").
%%%
%%%----------------------------------------------------------------------------
sample9() ->
erlunit:strong_banner("Demonstration of Macros"),
erlunit:start(),
erlunit:suite("#1"),
?ERLUNIT_EQUAL(1, 1.0),
?ERLUNIT_EXACT(1, 1),
?ERLUNIT_PASS(1 / 1),
erlunit:suite("#2"),
?ERLUNIT_FAIL(1 / zero()),
erlunit:execute().
% --- That's it. Another complete test program. Run with
% --- # erl
% --- 1> c(erlunit), c(sample), sample:sample9().
%%%****************************************************************************
%%% #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(),
sample9(),
sample10().
% -(o)- %
%%%****************************************************************************
%%% UTILITY
%%%****************************************************************************
%%%
%%%----------------------------------------------------------------------------
%%% This is used to provoke errors but 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