-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherlunit.erl
executable file
·1481 lines (1096 loc) · 51.9 KB
/
erlunit.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%%----------------------------------------------------------------------------
%%% File : erlunit.erl
%%% Description : Test functions - import this file and use its functions.
%%% Version : 0.2.8.2/alpha
%%% Status : alpha
%%% Copyright : (c) 2010 Eonblast Corporation http://www.eonblast.com
%%% License : MIT - see below
%%% Author : H. Diedrich <[email protected]>
%%% Created : 18 Apr 2010
%%% Changed : 15 May 2010 - see CHANGES
%%% Tested on : Erlang R13B01
%%%----------------------------------------------------------------------------
%%%
%%% This Module contains test functions to test other modules.
%%%
%%% Usage, see sample.erl. Basically -
%%%
%%% either: or: or:
%%% erlunit:start() | Test = erlunit:start(), | erlunit:start()
%%% erlunit:equal(1, 1), | Test ! { equal, 1, 1 }, | ?ERLUNIT_EQUAL(1,1),
%%% ... | ... | ...
%%% erlunit:execute(). | erlunit:execute(). | erlunit:execute().
%%%
%%% There are also suites, names for tests and concurrent execution.
%%%
%%%----------------------------------------------------------------------------
%%%
%%% Start source inspection in sample.erl.
%%%
%%% There are Erlang masters on erlang-questions, this here source is
%%% not written by one. Beware of copying mistakes if you are learning.
%%% There may be a slight advantage to that, the code is kind of plain.
%%%
%%% Mail to [email protected] with questions and suggestions, I will
%%% be quite happy to answer. - Henning
%%%
%%% This bit is dedicated to Joe, who had me smile now and then reading
%%% his book Programming Erlang. Thanks, Joe! You are the man.
%%%
%%% Thanks to Stefan Marr, Fred Hebert and Richard O'Keefe.
%%%
%%%----------------------------------------------------------------------------
%
% Copyright (c) 2010 Eonblast Corporation http://www.eonblast.com
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice, including link, and this permission notice shall
% be included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
% THE SOFTWARE.
%
%%%----------------------------------------------------------------------------
-module(erlunit).
-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([start/0, start/1, execute/0, stats/2]).
-export([suite/1, suite/2]).
-export([true/1, not_true/1, false/1, not_false/1, pass/1, fail/1]).
-export([true/2, not_true/2, false/2, not_false/2, pass/2, fail/2]).
-export([true/3, not_true/3, false/3, not_false/3, pass/3, fail/3]).
-export([true/4, not_true/4, false/4, not_false/4, pass/4, fail/4]).
-export([exits/1, throws/1, error/1]).
-export([exits/2, throws/2, error/2]).
-export([exits/3, throws/3, error/3]).
-export([exits/4, throws/4, error/4]).
-export([exact/2, equal/2, not_equal/2, bigger/2, lesser/2]).
-export([exact/3, equal/3, not_equal/3, bigger/3, lesser/3]).
-export([exact/5, equal/5, not_equal/5, bigger/5, lesser/5]).
-export([echo/1, echo/2]).
-export([banner/1, banner/2, ascii_banner/0, ascii_banner/1, center/2]).
-export([strong_banner/1, strong_banner/2]). % legacy
-export([vecho/2, vecho/3]).
-export([alive/1,delimited/1]).
-export([glist/1, glist_add/3, glist_get/2, glist_get/3, glist_drop/2, glist_loop/0, glist_loop/3]).
-compile([{nowarn_unused_function, [passed/3, failed/3]}]).
%%%----------------------------------------------------------------------------
-define(VERSION, "0.2.8.2/alpha").
-define(LIBRARY, "Erlunit").
-define(COPYRIGHT, "(c) 2010 Eonblast Corporation http://www.eonblast.com").
% -define(PROMPT, "\e[1;30merlunit:\e[0m ").
-define(PROMPT, "erlunit: ").
-define(INDENT, " ").
-define(USRERR, "This is an error in the way you use erlunit, or an error in erlunit itself.").
-define(PRGERR, "This is an error in erlunit itself.").
-define(INVERT, "This is an inverted suite. Fails count for Passes, for testing the tests.").
-define(DEFAULT_SUITE_NAME, "Default Suite").
-define(V1, false). % verbosity settings
% not used: -define(V2, false). % higler level enabled
% not used: -define(V3, false). % means more details.
-define(D3, false). % Debug verbosity:
-define(D4, false). % Numbers mean different areas, not levels.
-define(D5, false). % They are called out as hint in error messages.
-define(LINES, true). % governs display of lines in output.
%%%****************************************************************************
%%% INDIVIDUAL CHECKS
%%%****************************************************************************
%%%
%%% This is repetive to avoid the hiding of details coming with macro use and
%%% more complicated function calls that anonymous function would entail.
%%%
%%% These function can be called by the main process or a suite process.
%%%
%%%---------------------------------------------------------------------checks-
%%% true - check if an expression returns true
%%%----------------------------------------------------------------------------
%%%
%%% For true/0 see (*). That is used for a different purpose.
true(msg) -> "Check for true";
true(A) -> true(A, true(msg)).
true(A, Message) -> true(whereis(suite), A, Message).
true(Suite, A, Message) when is_pid(Suite) -> true(Suite, A, Message, "", "").
true(A, Message, Module, Line) -> true(whereis(suite), A, Message, Module, Line).
true(Suite, A, Message, Module, Line) when is_pid(Suite) ->
AA = payload(Suite, Message, A),
if AA ->
passed(Suite, Message, "evaluates to ~w", [AA]);
true ->
failed(Suite, Message, "evaluates to ~w, should be true but is not | ~w ~w", [AA, Module, Line])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% not_true - in Erlang, this is not necessarily 'false'
%%%----------------------------------------------------------------------------
%%%
not_true(msg) -> "Check for not true";
not_true(A) -> not_true(A, not_true(msg)).
not_true(A, Message) -> not_true(whereis(suite), A, Message).
not_true(Suite, A, Message) when is_pid(Suite) -> not_true(Suite, A, Message, "", "").
not_true(A, Message, Module, Line) -> not_true(whereis(suite), A, Message, Module, Line).
not_true(Suite, A, Message, Module, Line) when is_pid(Suite) ->
AA = payload(Suite, Message, A),
if AA =/= true ->
passed(Suite, Message, "evaluates to ~w, not true, as it should", [AA]);
true ->
failed(Suite, Message, "evaluates to ~w, but should not be true | ~w ~w", [AA, Module, Line])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% false
%%%----------------------------------------------------------------------------
%%%
false(msg) -> "Check for false";
false(A) -> false(A, false(msg)).
false(A, Message) -> false(whereis(suite), A, Message).
false(Suite, A, Message) when is_pid(Suite) -> false(Suite, A, Message, "", "").
false(A, Message, Module, Line) -> false(whereis(suite), A, Message, Module, Line).
false(Suite, A, Message, Module, Line) when is_pid(Suite) ->
AA = payload(Suite, Message, A),
if AA == false ->
passed(Suite, Message, "evaluates to ~w", [AA]);
true ->
failed(Suite, Message, "evaluates to ~w, should be false but is not | ~w ~w", [AA, Module, Line])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% not_false - in Erlang, this is not necessarily 'true'
%%%----------------------------------------------------------------------------
%%%
not_false(msg) -> "Check for not false";
not_false(A) -> not_false(A, not_false(msg)).
not_false(A, Message) -> not_false(whereis(suite), A, Message).
not_false(Suite, A, Message) when is_pid(Suite) -> not_false(Suite, A, Message, "", "").
not_false(A, Message, Module, Line) -> not_false(whereis(suite), A, Message, Module, Line).
not_false(Suite, A, Message, Module, Line) when is_pid(Suite) ->
AA = payload(Suite, Message, A),
if AA =/= false ->
passed(Suite, Message, "evaluates to ~w, not false, as it should", [AA]);
true ->
failed(Suite, Message, "evaluates to ~w, but should not be false | ~w ~w", [AA, Module, Line])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% pass - ok means: Fun throws NO exception
%%%----------------------------------------------------------------------------
%%%
pass(msg) -> "Check for passing without exception";
pass(Fun) when is_function(Fun) -> pass(Fun, pass(msg)).
pass(Fun, Message) when is_function(Fun) -> pass(whereis(suite), Fun, Message).
pass(Suite, Fun, Message) when is_function(Fun) -> pass(Suite, Fun, Message, "", "").
pass(Fun, Message, Module, Line) when is_function(Fun) -> pass(whereis(suite), Fun, Message, Module, Line).
pass(Suite, Fun, Message, Module, Line) when is_function(Fun) ->
try
Fun(),
passed(Suite, Message, "passes ok")
catch
throw:Term ->
failed(Suite, Message, "throws exception (~w) but should pass ok | ~w ~w~n~p", [Term, Module, Line, erlang:get_stacktrace()]);
exit:Reason ->
failed(Suite, Message, "make exit (~w) but should pass ok | ~w ~w~n~p", [Reason, Module, Line, erlang:get_stacktrace()]);
error:Reason ->
failed(Suite, Message, "runs into error (~w) but should pass ok | ~w ~w~n~p", [Reason, Module, Line, erlang:get_stacktrace()])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% fail - ok when Fun throws an exception
%%%----------------------------------------------------------------------------
%%%
fail(msg) -> "Check for exception";
fail(Fun) when is_function(Fun) -> fail(Fun, fail(msg)).
fail(Fun, Message) when is_function(Fun) -> fail(whereis(suite), Fun, Message).
fail(Suite, Fun, Message) when is_function(Fun) -> fail(Suite, Fun, Message, "", "").
fail(Fun, Message, Module, Line) when is_function(Fun) -> fail(whereis(suite), Fun, Message, Module, Line).
fail(Suite, Fun, Message, Module, Line) when is_function(Fun) ->
try
Result = Fun(),
failed(Suite, Message, "passes ok (~w) but should fail | ~w ~w~n~p", [Result, Module, Line, erlang:get_stacktrace()])
catch
throw:Term ->
passed(Suite, Message, "throws exception (~w), failing as it should", [Term]);
exit:Reason ->
passed(Suite, Message, "makes exit (~w), failing as it should", [Reason]);
error:Reason ->
passed(Suite, Message, "runs into error (~w), failing as it should", [Reason])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% throws - ok when Fun throws an exception
%%%----------------------------------------------------------------------------
%%%
throws(msg) -> "Check for throw";
throws(Fun) when is_function(Fun) -> throws(Fun, throws(msg)).
throws(Fun, Message) when is_function(Fun) -> throws(whereis(suite), Fun, Message).
throws(Suite, Fun, Message) when is_function(Fun) -> throws(Suite, Fun, Message, "", "").
throws(Fun, Message, Module, Line) when is_function(Fun) -> throws(whereis(suite), Fun, Message, Module, Line).
throws(Suite, Fun, Message, Module, Line) when is_function(Fun) ->
try
Fun(),
failed(Suite, Message, "passes ok but should throw and fail | ~w ~w~n~p", [Module, Line, erlang:get_stacktrace()])
catch
throw:_Term ->
passed(Suite, Message, "throws exception, as it should");
exit:_Reason ->
failed(Suite, Message, "makes exit, but should throw and and fail | ~w ~w~n~p", [Module,Line, erlang:get_stacktrace()]);
error:_Reason ->
failed(Suite, Message, "runs into error, but should throw and fail | ~w ~w~n~p", [Module,Line, erlang:get_stacktrace()])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% exits - ok when Fun calls exit
%%%----------------------------------------------------------------------------
%%%
exits(msg) -> "Check for exit being called";
exits(Fun) when is_function(Fun) -> exits(Fun, exits(msg)).
exits(Fun, Message) when is_function(Fun) -> exits(whereis(suite), Fun, Message).
exits(Suite, Fun, Message) when is_function(Fun) -> exits(Suite, Fun, Message, "", "").
exits(Fun, Message, Module, Line) when is_function(Fun) -> exits(whereis(suite), Fun, Message, Module, Line).
exits(Suite, Fun, Message, Module, Line) when is_function(Fun) ->
try
Fun(),
failed(Suite, Message, "passes ok but should throw and fail | ~w ~w~n~p", [Module,Line, erlang:get_stacktrace()])
catch
throw:_Term ->
failed(Suite, Message, "throws exception, as it should | ~w ~w~n~p", [Module,Line, erlang:get_stacktrace()]);
exit:_Reason ->
passed(Suite, Message, "makes exit, but should throw and and fail");
error:_Reason ->
failed(Suite, Message, "runs into error, but should throw and fail | ~w ~w~n~p", [Module,Line, erlang:get_stacktrace()])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% error - ok when Fun runs into error
%%%----------------------------------------------------------------------------
%%%
error(msg) -> "Check for having error";
error(Fun) when is_function(Fun) -> error(Fun, error(msg)).
error(Fun, Message) when is_function(Fun) -> error(whereis(suite), Fun, Message).
error(Suite, Fun, Message) when is_function(Fun) -> error(Suite, Fun, Message, "", "").
error(Fun, Message, Module, Line) when is_function(Fun) -> error(whereis(suite), Fun, Message, Module, Line).
error(Suite, Fun, Message, Module, Line) when is_function(Fun) ->
try
Fun(),
failed(Suite, Message, "passes ok but should run into error | ~w ~w~n~p", [Module, Line, erlang:get_stacktrace()])
catch
throw:_Term ->
failed(Suite, Message, "throws exception, but should run into error | ~w ~w~n~p", [Module,Line, erlang:get_stacktrace()]);
exit:_Reason ->
failed(Suite, Message, "makes exit, but should run into error | ~w ~w~n~p", [Module,Line, erlang:get_stacktrace()]);
error:_Reason ->
passed(Suite, Message, "runs into error, as it should")
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% equal
%%%----------------------------------------------------------------------------
%%%
equal(msg) -> "Equality check".
equal(A, B) -> equal(A, B, equal(msg)).
equal(A, B, Message) -> equal(whereis(suite), A, B, Message).
equal(Suite, A, B, Message) when is_pid(Suite) -> equal(Suite, A, B, Message, "", "").
equal(A, B, Message, Module, Line) -> equal(whereis(suite), A, B, Message, Module, Line).
equal(Suite, A, B, Message, Module, Line) when is_pid(Suite) ->
try
AA = payload(Suite, Message, A),
BB = payload(Suite, Message, B),
if
AA == BB ->
passed(Suite, Message, "~w as it should", [AA]);
true ->
failed(Suite, Message, "~w /= ~w but should be equal | ~w ~w", [AA, BB, Module, Line])
end
catch _:_ -> nil
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% exact
%%%----------------------------------------------------------------------------
%%%
exact(msg) -> "Exact Equality check".
exact(A, B) -> exact(A, B, exact(msg)).
exact(A, B, Message) -> exact(whereis(suite), A, B, Message).
exact(Suite, A, B, Message) when is_pid(Suite) -> exact(Suite, A, B, Message, "", "").
exact(A, B, Message, Module, Line) -> exact(whereis(suite), A, B, Message, Module, Line).
exact(Suite, A, B, Message, Module, Line) when is_pid(Suite) ->
try
AA = payload(Suite, Message, A),
BB = payload(Suite, Message, B),
if
AA =:= BB ->
passed(Suite, Message, "~w as it should", [AA]);
true ->
failed(Suite, Message, "~w /= ~w but should be exact | ~w ~w", [AA, BB, Module, Line])
end
catch _:_ -> nil
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% not_equal
%%%----------------------------------------------------------------------------
not_equal(msg) -> "Non-Equality check".
not_equal(A, B) -> not_equal(A, B, not_equal(msg)).
not_equal(A, B, Message) -> not_equal(whereis(suite), A, B, Message).
not_equal(Suite, A, B, Message) when is_pid(Suite) -> not_equal(Suite, A, B, Message, "", "").
not_equal(A, B, Message, Module, Line) -> not_equal(whereis(suite), A, B, Message, Module, Line).
not_equal(Suite, A, B, Message, Module, Line) when is_pid(Suite) ->
AA = payload(Suite, Message, A),
BB = payload(Suite, Message, B),
if
AA /= BB ->
passed(Suite, Message, "~w /= ~w as it should", [AA, BB]);
true ->
failed(Suite, Message, "~w == ~w but should NOT be equal | ~w ~w", [AA, BB, Module, Line])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% bigger
%%%----------------------------------------------------------------------------
%%%
bigger(msg) -> "Check if bigger".
bigger(A, B) -> bigger(A, B, bigger(msg)).
bigger(A, B, Message) -> bigger(whereis(suite), A, B, Message).
bigger(Suite, A, B, Message) when is_pid(Suite) -> bigger(Suite, A, B, Message, "", "").
bigger(A, B, Message, Module, Line) -> bigger(whereis(suite), A, B, Message, Module, Line).
bigger(Suite, A, B, Message, Module, Line) when is_pid(Suite) ->
AA = payload(Suite, Message, A),
BB = payload(Suite, Message, B),
if
AA > BB ->
passed(Suite, Message, "~w > ~w as it should", [A, B]);
true ->
failed(Suite, Message, "~w <= ~w but should be bigger | ~w ~w", [A, B, Module, Line])
end.
%%% checks
%%%----------------------------------------------------------------------------
%%% lesser
%%%----------------------------------------------------------------------------
%%%
lesser(msg) -> "Non-Equality check".
lesser(A, B) -> lesser(A, B, lesser(msg)).
lesser(A, B, Message) -> lesser(whereis(suite), A, B, Message).
lesser(Suite, A, B, Message) when is_pid(Suite) -> lesser(Suite, A, B, Message, "", "").
lesser(A, B, Message, Module, Line) -> lesser(whereis(suite), A, B, Message, Module, Line).
lesser(Suite, A, B, Message, Module, Line) when is_pid(Suite) ->
AA = payload(Suite, Message, A),
BB = payload(Suite, Message, B),
if
AA < BB ->
passed(Suite, Message, "~w < ~w as it should", [A, B]);
true ->
failed(Suite, Message, "~w >= ~w but should be lesser | ~w ~w", [A, B, Module, Line])
end.
%%%****************************************************************************
%%% COUNTER CALLS
%%%****************************************************************************
%%%
%%% These function can be called by the main process or a suite process.
%%%
%%%----------------------------------------------------------------------------
%%% passed - echo positive result and count it
%%%----------------------------------------------------------------------------
passed(Suite, Message, Result) -> passed(Suite, Message, Result, []).
passed(Suite, Message, Result, ResultParameter) ->
try
Suite ! passed,
Options = get(options),
% this works for both main process or suite processes
case lists:member(nopasses,Options) of
true -> nil;
false ->
SuiteName = glist_get(suitenames, Suite, ""),
% io:format(?PROMPT ++ "+ ok | ~s~s~s~s | " ++ Result ++ ".~n",
io:format(?PROMPT ++ "+ ok | ~s~s~s~s~n" ++ ?PROMPT ++ " | --> " ++ Result ++ ".~n",
[SuiteName, iff(SuiteName,": ",""),
"", % iff(length(SuiteName) > ((width()-15)*2), <<13,10,?INDENT," ` ">>,""),
delimited(Message) | delimited(ResultParameter)])
end
catch
Type:Reason -> io:format("~n~n~nCrashed while printing success message. "
++ ?PRGERR ++"~n ~p ~p~n~n ~p ~n~n", [Type, Reason, erlang:get_stacktrace()])
end.
%%%----------------------------------------------------------------------------
%%% failed - echo negative result and count it
%%%----------------------------------------------------------------------------
failed(Suite, Message, Result) -> failed(Suite, Message, Result, []).
failed(Suite, Message, Result, ResultParameter) ->
try
Suite ! failed,
% this works for both main process or suite processes
Options = get(options),
FailTag =
case lists:member(inverted,get(flags)) of
true -> "FAIL (but inverted for testing the tests = ok)";
false -> "FAIL"
end,
case lists:member(nofails,Options) of
true -> nil;
false ->
% colorizing
case lists:member(colors,Options) of
true -> Red = "\e[1;31m", UnRed = "\e[0m";
false -> Red = "", UnRed = ""
end,
SuiteName = glist_get(suitenames, Suite, ""),
io:format(?PROMPT ++ "~s | ##### ~s~s ~s~s~s | " ++ delimited(Result) ++ ". #####~n",
[FailTag, SuiteName, iff(SuiteName,":",""), Red, delimited(Message), UnRed | delimited(ResultParameter)])
end
catch
Type:Reason -> io:format("~n~n~nCrashed while printing failure message. "
++ ?PRGERR ++"~n ~p ~p~n~n~p~n~n", [Type, Reason, erlang:get_stacktrace()])
end.
%%%----------------------------------------------------------------------------
%%% crashed - echo negative result and count it
%%%----------------------------------------------------------------------------
crashed(Suite, Reason) ->
% Suite ! crashed,
%
% Options = get(options),
%
% case lists:member(nocrashes,Options) of
% true -> nil;
% false ->
% % colorizing
% case lists:member(colors,Options) of
% true -> Red = "\e[1;31m", UnRed = "\e[0m";
% false -> Red = "", UnRed = ""
% end,
Red = "\e[1;31m", UnRed = "\e[0m",
SuiteName = glist_get(suitenames, Suite, ""),
io:format(?PROMPT ++ "CRASH | XXXXX ~s~s ~s~w ~w~s XXXXX~n",
[SuiteName, iff(SuiteName,":",""), Red, Reason, erlang:get_stacktrace(), UnRed]).
% end.
%%%----------------------------------------------------------------------------
%%% kinda cut messages short, whatever gives
%%%----------------------------------------------------------------------------
-define(DELIMIT, 100).
-define(DELIMIT_REPLACE, "....").
delimited([]) ->
[];
delimited(List) when is_list(List), length(List) > ?DELIMIT ->
delimited(lists:append(lists:sublist(List, ?DELIMIT - length(?DELIMIT_REPLACE)), ?DELIMIT_REPLACE));
delimited(List) when is_list(List) ->
[ delimited(X) || X <- List ];
delimited(Tuple) when is_tuple(Tuple) ->
list_to_tuple([ delimited(X) || X <- tuple_to_list(Tuple) ]);
delimited(Item) when is_binary(Item), size(Item) > ?DELIMIT ->
<<Limited:?DELIMIT/binary, _/binary>> = Item,
<<Limited/binary, ".....">>;
delimited(Item) ->
Item.
%%%****************************************************************************
%%% MAIN TEST
%%%****************************************************************************
%%%
%%% You can have multiple Main Tests, that are collections of suites
%%% but they should probably be executed in sequence, not in parallel.
%%%
%%%----------------------------------------------------------------------------
%%% Start main
%%%----------------------------------------------------------------------------
start() -> start([]).
start(Options) when is_list(Options) ->
ascii_banner(),
echo("Start of Tests."),
register(main, self()),
put(options, Options),
Stats = spawn(fun() -> stats("Overall", Options) end),
force_register(stats, Stats),
glist(suitenames), % mind you, before first suite() call.
DefaultSuite = spawn(fun() -> suite(?DEFAULT_SUITE_NAME, [], self()) end),
force_register(suite, DefaultSuite),
self() ! { add, DefaultSuite, ?DEFAULT_SUITE_NAME },
main.
%%%----------------------------------------------------------------------------
%%% execute - work the list of queued checks main
%%%----------------------------------------------------------------------------
execute() ->
execute_loop(start, [], []).
%%% non-exported: --------------------------------------------------------main-
execute_loop(Phase, SuitesActive, SuitesToPrint) ->
vecho(?D3, "Main process phase: ~w | active suites: ~w | to print: ~w", [Phase, SuitesActive, SuitesToPrint]),
receive
Rcv ->
vecho(?D3, "Main process receives: ~w", [Rcv]),
case Rcv of
%%%-suite-wait----------------------------------------------------main-
{ add, Suite, Name } ->
if
Phase == start ->
Suite ! done,
execute_loop(Phase, [ Suite | SuitesActive ], lists:append(SuitesToPrint, [Suite]) );
% protection vs misuse
true ->
exit("Too late to add ~s. " ++ ?USRERR ++ " Try D3.", [Name])
end;
{ done, Suite } ->
vecho(?D3, "~s done.", [glist_get(suitenames, Suite, "*")]),
self() ! check_done,
execute_loop(Phase, lists:delete(Suite, SuitesActive), SuitesToPrint );
check_done ->
% print_suites must be sent only once. Problem: check_done can come
% in multiple times with empty SuitesActive list, depending on Suite message sequences.
% Therefore, the if, and the phase forced to 'print'.
if
(Phase /= print) and (SuitesActive == []) ->
self() ! print_suites,
NewPhase = print; % must be here, not later.
true ->
NewPhase = Phase
end,
execute_loop(NewPhase, SuitesActive, SuitesToPrint);
%%%-printing------------------------------------------------------main-
print_suites ->
% There is always at least one suite, at least the default suite.
% Print one and wait for ack in the form of { printed, Suite }, below.
[ Suite | LeftToPrint ] = SuitesToPrint,
Suite ! print,
execute_loop(Phase, SuitesActive, LeftToPrint);
{ printed, _Suite } ->
if
SuitesToPrint == [] -> self() ! print_summary;
true -> self() ! print_suites
end,
execute_loop(Phase, SuitesActive, SuitesToPrint );
print_summary ->
stats ! print,
self() ! finis,
execute_loop(finish, SuitesActive, SuitesToPrint);
%%%-wrap-up-------------------------------------------------------main-
finis ->
timer:sleep(200), % allow rogue processes to finish
force_unregister(suitenames),
force_unregister(suite),
force_unregister(stats),
force_unregister(main),
complete;
Any ->
vecho(?D3, "Handed on from main process to suite: ~w~n", [Any]),
suite ! Any,
execute_loop(Phase, SuitesActive, SuitesToPrint)
end
after 3000 ->
exit("Main process stalled. " ++ ?USRERR ++ " Try 3D.")
end.
% TODO: return a value from stats: ok, fail or error.
%%%****************************************************************************
%%% SUITE
%%%****************************************************************************
%%%
%%%----------------------------------------------------------------------------
%%% Create Suite Process suite
%%%----------------------------------------------------------------------------
%%% expects to be called by the main process ----------------------------------
suite(Nom) -> suite(Nom, []).
suite(Nom, Flags) when is_list(Nom), is_list(Flags) ->
vecho(?V1, "~s~nStarting Suite ~s.", [line(), Nom]),
put(flags, Flags),
case lists:member(inverted, Flags) of
true -> strong_banner(Nom ++ ": " ++ ?INVERT, 0);
_ -> nil
end,
Options = get(options),
Suite = spawn(fun() -> suite("Suite " ++ Nom, Flags, Options, self()) end),
glist_add(suitenames, Suite, "Suite " ++ Nom),
main ! { add, Suite, Nom },
% register as default suite
force_register(suite, Suite),
Suite.
%%%
%%%----------------------------------------------------------------------------
%%% Create Suite Process suite
%%%----------------------------------------------------------------------------
%%% non-exported: -------------------------------------------------------------
suite(Nom, Flags, Caller) -> suite(Nom, Flags, [], Caller).
suite(Nom, Flags, Options, Caller) ->
put(options, Options), % replicates main process options
suite_loop(Nom, Flags, Caller, nil, 0, 0, 0),
exit(1000).
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed) ->
try
receive
%% counters suite
%% --------------------------------------------------------------------
%% Note that these counter calls are processed way later than
%% the check calls, because the latter are fielded, usually,
%% in fast succession, thus are queued first in line, before
%% even the first of them are executed, which then, in turn,
%% generate the below counter calls
passed ->
{ Signal, Pass, Fail } = inversion(Flags, passed),
stats ! Signal,
suite_loop(Nom, Flags, Caller, Sub, Passed + Pass, Failed + Fail, Crashed);
failed ->
{ Signal, Pass, Fail } = inversion(Flags, failed),
stats ! Signal,
suite_loop(Nom, Flags, Caller, Sub, Passed + Pass, Failed + Fail, Crashed);
crashed ->
stats ! crashed,
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed +1, Crashed +1);
% doubled, see (***)
%% check: true suite
%% --------------------------------------------------------------------
{ true, A } ->
true(self(), A, true(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ true, A, Message } ->
true(self(), A, Message ),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: not_true suite
%% --------------------------------------------------------------------
{ not_true, A } ->
not_true(self(), A, not_true(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ not_true, A, Message } ->
not_true(self(), A, Message ),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: false suite
%% --------------------------------------------------------------------
{ false, A } ->
false(self(), A, false(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ false, A, Message } ->
false(self(), A, Message ),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: not_false suite
%% --------------------------------------------------------------------
{ not_false, A } ->
not_false(self(), A, not_false(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ not_false, A, Message } ->
not_false(self(), A, Message ),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: pass suite
%% --------------------------------------------------------------------
{ pass, A } ->
pass(self(), A, pass(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ pass, A, Message } ->
pass(self(), A, Message ),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: fail suite
%% --------------------------------------------------------------------
{ fail, A } ->
fail(self(), A, fail(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ fail, A, Message } ->
fail(self(), A, Message ),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: throws suite
%% --------------------------------------------------------------------
{ throws, A } ->
throws(self(), A, throws(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ throws, A, Message } ->
throws(self(), A, Message ),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: error suite
%% --------------------------------------------------------------------
{ error, A } ->
error(self(), A, error(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ error, A, Message } ->
error(self(), A, Message ),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: exits suite
%% --------------------------------------------------------------------
{ exits, A } ->
exits(self(), A, exits(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ exits, A, Message } ->
exits(self(), A, Message ),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: equal suite
%% --------------------------------------------------------------------
{ equal, A, B } ->
equal(self(), A, B, equal(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ equal, A, B, Message } ->
equal(self(), A, B, Message),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: exact suite
%% --------------------------------------------------------------------
{ exact, A, B } ->
exact(self(), A, B, exact(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
{ exact, A, B, Message } ->
exact(self(), A, B, Message),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);
%% check: not_equal suite
%% --------------------------------------------------------------------
{ not_equal, A, B } ->
not_equal(self(), A, B, not_equal(msg)),
suite_loop(Nom, Flags, Caller, Sub, Passed, Failed, Crashed);