/
lgtunit.lgt
2440 lines (2140 loc) · 85.6 KB
/
lgtunit.lgt
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This file is part of Logtalk <https://logtalk.org/>
% Copyright 1998-2019 Paulo Moura <pmoura@logtalk.org>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- object(lgtunit,
implements(expanding)).
% avoid a catch-22 due to the local definition
% of the logtalk::trace_event/2 predicate
:- set_logtalk_flag(debug, off).
:- info([
version is 7.0,
author is 'Paulo Moura',
date is 2019/03/21,
comment is 'A unit test framework supporting predicate clause coverage, determinism testing, input/output testing, quick-check testing, and multiple test dialects.'
]).
:- public(cover/1).
:- mode(cover(?entity_identifier), zero_or_more).
:- info(cover/1, [
comment is 'Declares entities being tested for which code coverage information should be collected.',
argnames is ['Entity']
]).
:- public(run/0).
:- mode(run, one).
:- info(run/0, [
comment is 'Runs the unit tests, writing the results to the current output stream.'
]).
:- public(run/1).
:- mode(run(+atom), zero_or_one).
:- mode(run(+list(atom)), zero_or_one).
:- info(run/1, [
comment is 'Runs a unit test or a list of unit tests, writing the results to the current output stream. Runs the global setup and cleanup steps when defined, failing if either step fails.',
argnames is ['Tests']
]).
:- public(run/2).
:- mode(run(+atom, +atom), one).
:- info(run/2, [
comment is 'Runs the unit tests, writing the results to the specified file. Mode can be either "write" (to create a new file) or "append" (to add results to an existing file).',
argnames is ['File', 'Mode']
]).
:- public(run_test_sets/1).
:- mode(run_test_sets(+list(object_identifier)), zero_or_one).
:- info(run_test_sets/1, [
comment is 'Runs two or more test sets as a unified set generating a single code coverage report if one is requested. Fails if the list does not contains at least two test objects.',
argnames is ['TestObjects']
]).
:- public(deterministic/1).
:- meta_predicate(deterministic(0)).
:- mode(deterministic(+callable), zero_or_one).
:- info(deterministic/1, [
comment is 'True if the goal succeeds once without leaving choice-points.',
argnames is ['Goal']
]).
:- public(deterministic/2).
:- meta_predicate(deterministic(0, *)).
:- mode(deterministic(+callable, --atom), zero_or_one).
:- info(deterministic/2, [
comment is 'Reified version of the deterministic/1 predicate. True if the goal succeeds. Returns a boolean value (true or false) indicating if the goal succeeded without leaving choice-points.',
argnames is ['Goal', 'Deterministic']
]).
:- public(assertion/1).
:- meta_predicate(assertion('::')).
:- mode(assertion(+callable), one).
:- info(assertion/1, [
comment is 'True if the assertion goal succeeds. Throws an error using the assertion goal as argument if the assertion goal throws an error or fails.',
argnames is ['Assertion'],
exceptions is [
'Assertion goal fails' - assertion_failure('Assertion'),
'Assertion goal throws Error' - assertion_error('Assertion', 'Error')
]
]).
:- public(assertion/2).
:- meta_predicate(assertion(*, 0)).
:- mode(assertion(+nonvar, +callable), one).
:- info(assertion/2, [
comment is 'True if the assertion goal succeeds. Throws an error using the description as argument if the assertion goal throws an error or fails. The description argument helps to distinguish between different assertions in the same test body.',
argnames is ['Description', 'Assertion'],
exceptions is [
'Assertion goal fails' - assertion_failure('Description'),
'Assertion goal throws Error' - assertion_error('Description', 'Error')
]
]).
:- public(quick_check/3).
:- mode(quick_check(@callable, -callable, ++list(compound)), one).
:- info(quick_check/3, [
comment is 'Reified version of the quick_check/2 predicate. Reports the result as "passed", "failed(Goal)" where Goal is the test that failed, or "error(Error,Template)".',
argnames is ['Template', 'Result', 'Options']
]).
:- public(quick_check/2).
:- mode(quick_check(@callable, ++list(compound)), zero_or_one).
:- info(quick_check/2, [
comment is 'Generates and runs random tests for a given predicate given its mode template. Fails when a random generated test fails printing the test. Accepts an option n(NumberOfTests). Default is to run 100 random tests.',
argnames is ['Template', 'Options']
]).
:- public(quick_check/1).
:- mode(quick_check(@callable), zero_or_one).
:- info(quick_check/1, [
comment is 'Generates and runs 100 random tests for a given predicate given its mode template. Fails when a random generated call fails printing the test.',
argnames is ['Template']
]).
:- public(benchmark/2).
:- meta_predicate(benchmark(0, *)).
:- mode(benchmark(+callable, -float), one).
:- info(benchmark/2, [
comment is 'Benchmarks a goal and returns the total execution time in seconds. Uses cpu clock. Goals that may throw an exception should be wrapped by the catch/3 control construct.',
argnames is ['Goal', 'Time']
]).
:- public(benchmark_reified/3).
:- meta_predicate(benchmark_reified(0, *, *)).
:- mode(benchmark_reified(+callable, -float, -callable), one).
:- info(benchmark_reified/3, [
comment is 'Benchmarks a goal and returns the total execution time in seconds plus its result (success, failure, or error(Error)). Uses cpu clock.',
argnames is ['Goal', 'Time', 'Result']
]).
:- public(benchmark/3).
:- meta_predicate(benchmark(0, *, *)).
:- mode(benchmark(@callable, +positive_integer, -float), one).
:- info(benchmark/3, [
comment is 'Benchmarks a goal by repeating it the specified number of times and returning the total execution time in seconds. Uses cpu clock. Goals that may throw an exception should be wrapped by the catch/3 control construct.',
argnames is ['Goal', 'Repetitions', 'Time']
]).
:- public(benchmark/4).
:- meta_predicate(benchmark(0, *, *, *)).
:- mode(benchmark(@callable, +positive_integer, +atom, -float), one).
:- info(benchmark/4, [
comment is 'Benchmarks a goal by repeating it the specified number of times and returning the total execution time in seconds using the given clock (cpu or wall). Goals that may throw an exception should be wrapped by the catch/3 control construct.',
argnames is ['Goal', 'Repetitions', 'Clock', 'Time']
]).
:- public(variant/2).
:- mode(variant(@term, @term), zero_or_one).
:- info(variant/2, [
comment is 'True when the two arguments are a variant of each other. I.e. if is possible to rename the term variables to make them identical. Useful for checking expected test results that contain variables.',
argnames is ['Term1', 'Term2']
]).
:- public(approximately_equal/3).
:- mode(approximately_equal(+number, +number, +number), zero_or_one).
:- info(approximately_equal/3, [
comment is 'Compares two numbers for approximate equality given an epsilon value using the de facto standard formula abs(Number1 - Number2) =< max(abs(Number1), abs(Number2)) * Epsilon. Type-checked.',
argnames is ['Number1', 'Number2', 'Epsilon']
]).
:- public(essentially_equal/3).
:- mode(essentially_equal(+number, +number, +number), zero_or_one).
:- info(essentially_equal/3, [
comment is 'Compares two numbers for essential equality given an epsilon value using the de facto standard formula abs(Number1 - Number2) =< min(abs(Number1), abs(Number2)) * Epsilon. Type-checked.',
argnames is ['Number1', 'Number2', 'Epsilon']
]).
:- public(tolerance_equal/4).
:- mode(tolerance_equal(+number, +number, +number, +number), zero_or_one).
:- info(tolerance_equal/4, [
comment is 'Compares two numbers for close equality given relative and absolute tolerances using the de facto standard formula abs(Number1 - Number2) =< max(RelativeTolerance * max(abs(Number1), abs(Number2)), AbsoluteTolerance). Type-checked.',
argnames is ['Number1', 'Number2', 'RelativeTolerance', 'AbsoluteTolerance']
]).
:- public(op(700, xfx, ('=~='))).
:- public(('=~=')/2).
:- mode('=~='(+number, +number), zero_or_one).
:- mode('=~='(+list(number), +list(number)), zero_or_one).
:- info(('=~=')/2, [
comment is 'Compares two numbers (or lists of numbers) for approximate equality using 100*epsilon for the absolute error and, if that fails, 99.999% accuracy for the relative error. Note that these precision values may not be adequate for all cases. Type-checked.',
argnames is ['Number1', 'Number2']
]).
:- public(epsilon/1).
:- mode(epsilon(-float), one).
:- info(epsilon/1, [
comment is 'Returns the value of epsilon used in the definition of the (=~=)/2 predicate.',
argnames is ['Epsilon']
]).
:- protected(run_tests/0).
:- mode(run_tests, one).
:- info(run_tests/0, [
comment is 'Runs all defined unit tests.'
]).
:- protected(run_tests/2).
:- mode(run_tests(+list(callable), +atom), one).
:- info(run_tests/2, [
comment is 'Runs a list of defined tests.',
argnames is ['Tests', 'File']
]).
:- protected(run_test_set/0).
:- mode(run_test_set, one).
:- info(run_test_set/0, [
comment is 'Runs a test set as part of running two or more test sets as a unified set.'
]).
:- protected(run_quick_check_tests/2).
:- mode(run_quick_check_tests(@callable, +integer), one_or_error).
:- info(run_quick_check_tests/2, [
comment is 'Runs a list of defined tests.',
argnames is ['Template', 'NumberOfTests']
]).
:- protected(condition/0).
:- mode(condition, zero_or_one).
:- info(condition/0, [
comment is 'Verifies conditions for running the tests. Defaults to the goal true.'
]).
:- protected(setup/0).
:- mode(setup, zero_or_one).
:- info(setup/0, [
comment is 'Setup environment before running the test set. Defaults to the goal true.'
]).
:- protected(cleanup/0).
:- mode(cleanup, zero_or_one).
:- info(cleanup/0, [
comment is 'Cleanup environment after running the test set. Defaults to the goal true.'
]).
:- protected(make/1).
:- mode(make(?atom), zero_or_one).
:- info(make/1, [
comment is 'Make target for automatically running the test set when calling the logtalk_make/1 built-in predicate. No default. Possible values are {all, check}.'
]).
:- protected(note/1).
:- mode(note(?atom), zero_or_one).
:- info(note/1, [
comment is 'Note to be printed after the test results. Defaults to the empty atom.'
]).
:- protected(suppress_text_output/0).
:- mode(suppress_text_output, one).
:- info(suppress_text_output/0, [
comment is 'Suppresses text output. Useful to avoid irrelevant text output from predicates being tested to clutter the test logs.'
]).
:- protected(suppress_binary_output/0).
:- mode(suppress_binary_output, one).
:- info(suppress_binary_output/0, [
comment is 'Suppresses binary output. Useful to avoid irrelevant binary output from predicates being tested to clutter the test logs.'
]).
:- protected(set_text_input/3).
:- mode(set_text_input(+atom, +atom, +list(stream_option)), one).
:- info(set_text_input/3, [
comment is 'Creates a temporary file with the given text contents and opens it for reading referenced by the given alias and using the additional options. If no eof_action/1 option is specified, its value will be the default used by the backend compiler.',
argnames is ['Alias', 'Contents', 'Options']
]).
:- protected(set_text_input/2).
:- mode(set_text_input(+atom, +atom), one).
:- info(set_text_input/2, [
comment is 'Creates a temporary file with the given text contents and opens it for reading referenced by the given alias and using the default end-of-file action for the used backend compiler.',
argnames is ['Alias', 'Contents']
]).
:- protected(set_text_input/1).
:- mode(set_text_input(+atom), one).
:- info(set_text_input/1, [
comment is 'Creates a temporary file with the given text contents, opens it for reading using the default end-of-file action for the used backend compiler, and sets the current input stream to the file.',
argnames is ['Contents']
]).
:- protected(check_text_input/2).
:- mode(check_text_input(+atom, +atom), zero_or_one).
:- info(check_text_input/2, [
comment is 'Checks that the temporary file (referenced with the given alias) being written have the expected text contents.',
argnames is ['Alias', 'Contents']
]).
:- protected(check_text_input/1).
:- mode(check_text_input(+atom), zero_or_one).
:- info(check_text_input/1, [
comment is 'Checks that the temporary file being written have the expected text contents.',
argnames is ['Contents']
]).
:- protected(text_input_assertion/3).
:- mode(text_input_assertion(+atom, +atom, --callable), one).
:- info(text_input_assertion/3, [
comment is 'Returns an assertion for checking that the temporary file (referenced with the given alias) being written have the expected text contents.',
argnames is ['Alias', 'Contents', 'Assertion']
]).
:- protected(text_input_assertion/2).
:- mode(text_input_assertion(+atom, --callable), one).
:- info(text_input_assertion/2, [
comment is 'Returns an assertion for checking that the temporary file being written have the expected text contents.',
argnames is ['Contents', 'Assertion']
]).
:- protected(clean_text_input/0).
:- mode(clean_text_input, one).
:- info(clean_text_input/0, [
comment is 'Cleans the temporary file used when testing text input.'
]).
:- protected(set_binary_input/3).
:- mode(set_binary_input(+atom, +list(byte), +list(stream_option)), one).
:- info(set_binary_input/3, [
comment is 'Creates a temporary file with the given binary contents and opens it for reading referenced by the given alias and using the additional options. If no eof_action/1 option is specified, its value will be the default used by the backend compiler.',
argnames is ['Alias', 'Bytes', 'Options']
]).
:- protected(set_binary_input/2).
:- mode(set_binary_input(+atom, +list(byte)), one).
:- info(set_binary_input/2, [
comment is 'Creates a temporary file with the given binary contents and opens it for reading referenced by the given alias and using the default end-of-file action for the used backend compiler.',
argnames is ['Alias', 'Bytes']
]).
:- protected(set_binary_input/1).
:- mode(set_binary_input(+list(byte)), one).
:- info(set_binary_input/1, [
comment is 'Creates a temporary file with the given binary contents, opens it for reading using the default end-of-file action for the used backend compiler, and sets the current input stream to the file.',
argnames is ['Bytes']
]).
:- protected(check_binary_input/2).
:- mode(check_binary_input(+atom, +list(byte)), zero_or_one).
:- info(check_binary_input/2, [
comment is 'Checks that the temporary file (referenced with the given alias) have the expected binary contents.',
argnames is ['Alias', 'Bytes']
]).
:- protected(check_binary_input/1).
:- mode(check_binary_input(+list(byte)), zero_or_one).
:- info(check_binary_input/1, [
comment is 'Checks that the temporary file have the expected binary contents.',
argnames is ['Bytes']
]).
:- protected(binary_input_assertion/3).
:- mode(binary_input_assertion(+atom, +list(byte), --callable), one).
:- info(binary_input_assertion/3, [
comment is 'Returns an assertion for checking that the temporary file (referenced with the given alias) have the expected binary contents.',
argnames is ['Alias', 'Bytes', 'Assertion']
]).
:- protected(binary_input_assertion/2).
:- mode(binary_input_assertion(+list(byte), --callable), one).
:- info(binary_input_assertion/2, [
comment is 'Returns an assertion for checking that the temporary file have the expected binary contents.',
argnames is ['Bytes', 'Assertion']
]).
:- protected(clean_binary_input/0).
:- mode(clean_binary_input, one).
:- info(clean_binary_input/0, [
comment is 'Cleans the temporary file used when testing binary input.'
]).
:- protected(set_text_output/2).
:- mode(set_text_output(+atom, +atom), one).
:- info(set_text_output/2, [
comment is 'Creates a temporary file with the given text contents and referenced with the given alias.',
argnames is ['Alias', 'Contents']
]).
:- protected(set_text_output/1).
:- mode(set_text_output(+atom), one).
:- info(set_text_output/1, [
comment is 'Creates a temporary file with the given text contents and sets the current output stream to the file.',
argnames is ['Contents']
]).
:- protected(check_text_output/2).
:- mode(check_text_output(+atom, +atom), zero_or_one).
:- info(check_text_output/2, [
comment is 'Checks that the temporary file (referenced with the given alias) being written have the expected text contents.',
argnames is ['Alias', 'Contents']
]).
:- protected(check_text_output/1).
:- mode(check_text_output(+atom), zero_or_one).
:- info(check_text_output/1, [
comment is 'Checks that the temporary file being written have the expected text contents.',
argnames is ['Contents']
]).
:- protected(text_output_assertion/3).
:- mode(text_output_assertion(+atom, +atom, --callable), one).
:- info(text_output_assertion/3, [
comment is 'Returns an assertion for checking that the temporary file (referenced with the given alias) being written have the expected text contents.',
argnames is ['Alias', 'Contents', 'Assertion']
]).
:- protected(text_output_assertion/2).
:- mode(text_output_assertion(+atom, --callable), one).
:- info(text_output_assertion/2, [
comment is 'Returns an assertion for checking that the temporary file being written have the expected text contents.',
argnames is ['Contents', 'Assertion']
]).
:- protected(clean_text_output/0).
:- mode(clean_text_output, one).
:- info(clean_text_output/0, [
comment is 'Cleans the temporary file used when testing text output.'
]).
:- protected(set_binary_output/2).
:- mode(set_binary_output(+atom, +list(byte)), one).
:- info(set_binary_output/2, [
comment is 'Creates a temporary file with the given binary contents and referenced with the given alias.',
argnames is ['Alias', 'Bytes']
]).
:- protected(set_binary_output/1).
:- mode(set_binary_output(+list(byte)), one).
:- info(set_binary_output/1, [
comment is 'Creates a temporary file with the given binary contents and sets the current output stream to the file.',
argnames is ['Bytes']
]).
:- protected(check_binary_output/2).
:- mode(check_binary_output(+atom, +list(byte)), zero_or_one).
:- info(check_binary_output/2, [
comment is 'Checks that the temporary file (referenced with the given alias) have the expected binary contents.',
argnames is ['Alias', 'Bytes']
]).
:- protected(check_binary_output/1).
:- mode(check_binary_output(+list(byte)), zero_or_one).
:- info(check_binary_output/1, [
comment is 'Checks that the temporary file have the expected binary contents.',
argnames is ['Bytes']
]).
:- protected(binary_output_assertion/3).
:- mode(binary_output_assertion(+atom, +list(byte), --callable), one).
:- info(binary_output_assertion/3, [
comment is 'Returns an assertion for checking that the temporary file (referenced with the given alias) have the expected binary contents.',
argnames is ['Alias', 'Bytes', 'Assertion']
]).
:- protected(binary_output_assertion/2).
:- mode(binary_output_assertion(+list(byte), --callable), one).
:- info(binary_output_assertion/2, [
comment is 'Returns an assertion for checking that the temporary file have the expected binary contents.',
argnames is ['Bytes', 'Assertion']
]).
:- protected(clean_binary_output/0).
:- mode(clean_binary_output, one).
:- info(clean_binary_output/0, [
comment is 'Cleans the temporary file used when testing binary output.'
]).
:- protected(create_text_file/2).
:- mode(create_text_file(+atom, +atom), one).
:- info(create_text_file/2, [
comment is 'Creates a text file with the given contents.',
argnames is ['File', 'Contents']
]).
:- protected(create_binary_file/2).
:- mode(create_binary_file(+atom, +list(byte)), one).
:- info(create_binary_file/2, [
comment is 'Creates a binary file with the given contents.',
argnames is ['File', 'Bytes']
]).
:- protected(check_text_file/2).
:- mode(check_text_file(+atom, +atom), zero_or_one).
:- info(check_text_file/2, [
comment is 'Checks that the contents of a text file match the expected contents.',
argnames is ['File', 'Contents']
]).
:- protected(check_binary_file/2).
:- mode(check_binary_file(+atom, +list(byte)), zero_or_one).
:- info(check_binary_file/2, [
comment is 'Checks the contents of a binary file match the expected contents.',
argnames is ['File', 'Bytes']
]).
:- protected(clean_file/1).
:- mode(clean_file(+atom), one).
:- info(clean_file/1, [
comment is 'Closes any existing stream associated with the file and deletes the file if it exists.',
argnames is ['File']
]).
:- protected(closed_input_stream/2).
:- mode(closed_input_stream(-stream, +list(stream_option)), one).
:- info(closed_input_stream/2, [
comment is 'Opens a temporary file with the given options for reading, closes it, and returns its stream handle.',
argnames is ['Stream', 'Options']
]).
:- protected(closed_output_stream/2).
:- mode(closed_output_stream(-stream, +list(stream_option)), zero_or_one).
:- info(closed_output_stream/2, [
comment is 'Opens a temporary file with the given options for writing, closes it, and returns its stream handle.',
argnames is ['Stream', 'Options']
]).
:- protected(stream_position/1).
:- mode(stream_position(-stream_position), one).
:- info(stream_position/1, [
comment is 'Returns a syntactically valid stream position.',
argnames is ['Position']
]).
:- private(running_test_sets_/0).
:- dynamic(running_test_sets_/0).
:- mode(running_test_sets_, zero_or_one).
:- info(running_test_sets_/0, [
comment is 'Internal flag used when running two or more test sets as a unified set.'
]).
:- private(test/3).
:- mode(test(?atom, ?list(variable), ?nonvar), zero_or_more).
:- info(test/3, [
comment is 'Specifies a unit test.',
argnames is ['Identifier', 'Variables', 'Outcome']
]).
:- private(auxiliary_predicate_counter_/1).
:- dynamic(auxiliary_predicate_counter_/1).
:- mode(auxiliary_predicate_counter_(?integer), one_or_more).
:- info(auxiliary_predicate_counter_/1, [
comment is 'Counter for generarting unique auxiliary predicate names.',
argnames is ['Counter']
]).
:- private(test_/2).
:- dynamic(test_/2).
:- mode(test_(?atom, ?compound), zero_or_more).
:- info(test_/2, [
comment is 'Table of defined tests.',
argnames is ['Identifier', 'Test']
]).
:- private(skipped_/1).
:- dynamic(skipped_/1).
:- mode(skipped_(?integer), zero_or_one).
:- info(skipped_/1, [
comment is 'Counter for skipped tests.',
argnames is ['Counter']
]).
:- private(passed_/1).
:- dynamic(passed_/1).
:- mode(passed_(?integer), zero_or_one).
:- info(passed_/1, [
comment is 'Counter for passed tests.',
argnames is ['Counter']
]).
:- private(failed_/1).
:- dynamic(failed_/1).
:- mode(failed_(?callable), zero_or_one).
:- info(failed_/1, [
comment is 'Counter for failed tests.',
argnames is ['Counter']
]).
:- private(fired_/3).
:- dynamic(fired_/3).
:- mode(fired_(?entity_identifier, ?predicate_indicator, ?integer), zero_or_more).
:- info(fired_/3, [
comment is 'Fired clauses when running the unit tests.',
argnames is ['Entity', 'Predicate', 'Clause']
]).
:- private(covered_/4).
:- dynamic(covered_/4).
:- mode(covered_(?entity_identifier, ?callable, ?integer, ?integer), zero_or_more).
:- info(covered_/4, [
comment is 'Auxiliary predicate for collecting statistics on clause coverage.',
argnames is ['Entity', 'Predicate', 'Covered', 'Total']
]).
% we use the structured printing mechanism in order to allow unit tests
% results to be intercepted for alternative reporting by e.g. GUI IDEs
:- uses(logtalk, [print_message/3]).
% library support for quick check
:- uses(type, [check/2, arbitrary/2, shrink/3]).
% library list predicates
:- uses(list, [append/3, length/2, member/2]).
% don't assume that between/3 is a built-in predicate as some backend
% Prolog systems still provide it as a library predicate
:- uses(integer, [between/3]).
% for QuickCheck support
:- uses(random, [maybe/0]).
% by default, run the unit tests
condition.
% by default, no test setup is needed
setup.
% by default, no test cleanup is needed
cleanup.
% by default, no note to be printed after test results
note('').
run(File, Mode) :-
open(File, Mode, Stream, [alias(lgtunit_redirected_output)]),
logtalk::assertz((
message_hook(_, Kind, lgtunit, Tokens) :-
message_prefix_stream(Kind, lgtunit, Prefix, _),
print_message_tokens(lgtunit_redirected_output, Prefix, Tokens)
)),
run,
logtalk::retractall(message_hook(_, _, lgtunit, _)),
close(Stream).
run :-
% save the current output stream
current_output(Output),
retractall(running_test_sets_),
reset_test_counters,
reset_coverage_results,
write_tests_header,
write_tests_object,
( run_condition ->
( run_setup ->
::run_tests,
run_cleanup,
write_tests_results,
write_coverage_results
; tests_skipped
)
; tests_skipped
),
write_tests_footer,
% restore the current output stream
set_output(Output).
run_test_sets([First, Next| Others]) :-
retractall(running_test_sets_),
assertz(running_test_sets_),
reset_coverage_results,
write_tests_header,
run_test_sets_([First, Next| Others]),
write_coverage_results([First, Next| Others]),
write_tests_footer,
retractall(running_test_sets_).
run_test_sets_([]).
run_test_sets_([TestSet| TestSets]) :-
current_object(TestSet),
TestSet::run_test_set,
run_test_sets_(TestSets).
run_test_set :-
% save the current output stream
current_output(Output),
reset_test_counters,
write_tests_object,
( run_condition ->
( run_setup ->
::run_tests,
run_cleanup,
write_tests_results
; tests_skipped
)
; tests_skipped
),
% restore the current output stream
set_output(Output).
run(Test) :-
atom(Test),
!,
run([Test]).
run(Tests) :-
reset_test_counters,
reset_coverage_results,
run_setup,
forall(member(Test, Tests), run_test(Test)),
run_cleanup,
write_tests_results,
write_coverage_results.
run_test(Test) :-
self(Object),
::test_(Test, Spec),
object_property(Object, file(File)),
% save the current input and output streams
current_input(Input), current_output(Output),
run_test(Spec, File, Output),
% restore the current input and output streams
set_input(Input), set_output(Output).
run_condition :-
% expected either success or failure; error means user error
catch(::condition, Error, (broken_step(condition,Error), fail)),
!.
run_setup :-
% expected success; failure or error means user error
( catch(::setup, Error, broken_step(setup,Error)) ->
( var(Error) ->
true
; fail
)
; failed_step(setup),
fail
).
run_cleanup :-
% expected success; failure or error means user error
( catch(::cleanup, Error, broken_step(cleanup,Error)) ->
true
; failed_step(cleanup)
).
run_tests([], _).
run_tests([Test| Tests], File) :-
% save the current input and output streams
current_input(Input), current_output(Output),
run_test(Test, File, Output),
% restore the current input and output streams
set_input(Input), set_output(Output),
run_tests(Tests, File).
% by default, no tests are defined:
run_tests :-
run_tests([], _).
:- meta_predicate(run_test((::), (*), (*))).
% test/3 dialect
run_test(succeeds(Test, Variables, Position, Condition, Setup, Cleanup, Note), File, Output) :-
( run_test_condition(Test, Condition, File, Position, Note, Output) ->
( run_test_setup(Test, Setup, File, Position, Note, Output) ->
( catch(::test(Test, Variables, true), Error, failed_test(Test, File, Position, error_instead_of_success(Error), Note, Output)) ->
( var(Error) ->
passed_test(Test, File, Position, Note, Output)
; true
)
; failed_test(Test, File, Position, failure_instead_of_success, Note, Output)
),
run_test_cleanup(Test, Cleanup, File, Position, Output)
; true
)
; skipped_test(Test, File, Position, Note, Output)
).
run_test(deterministic(Test, Variables, Position, Condition, Setup, Cleanup, Note), File, Output) :-
( run_test_condition(Test, Condition, File, Position, Note, Output) ->
( run_test_setup(Test, Setup, File, Position, Note, Output) ->
( catch(::test(Test, Variables, deterministic(Deterministic)), Error, failed_test(Test, File, Position, error_instead_of_success(Error), Note, Output)) ->
( var(Error) ->
( Deterministic == true ->
passed_test(Test, File, Position, Note, Output)
; non_deterministic_success(Test, File, Position, Note, Output)
)
; true
)
; failed_test(Test, File, Position, failure_instead_of_success, Note, Output)
),
run_test_cleanup(Test, Cleanup, File, Position, Output)
; true
)
; skipped_test(Test, File, Position, Note, Output)
).
run_test(fails(Test, Variables, Position, Condition, Setup, Cleanup, Note), File, Output) :-
( run_test_condition(Test, Condition, File, Position, Note, Output) ->
( run_test_setup(Test, Setup, File, Position, Note, Output) ->
( catch(::test(Test, Variables, fail), Error, failed_test(Test, File, Position, error_instead_of_failure(Error), Note, Output)) ->
( var(Error) ->
failed_test(Test, File, Position, success_instead_of_failure, Note, Output)
; true
)
; passed_test(Test, File, Position, Note, Output)
),
run_test_cleanup(Test, Cleanup, File, Position, Output)
; true
)
; skipped_test(Test, File, Position, Note, Output)
).
run_test(throws(Test, Variables, PossibleErrors, Position, Condition, Setup, Cleanup, Note), File, Output) :-
( run_test_condition(Test, Condition, File, Position, Note, Output) ->
( run_test_setup(Test, Setup, File, Position, Note, Output) ->
( catch(::test(Test, Variables, PossibleErrors), Error, check_error(Test, PossibleErrors, Error, File, Position, Note, Output)) ->
( var(Error) ->
failed_test(Test, File, Position, success_instead_of_error, Note, Output)
; true
)
; failed_test(Test, File, Position, failure_instead_of_error, Note, Output)
),
run_test_cleanup(Test, Cleanup, File, Position, Output)
; true
)
; skipped_test(Test, File, Position, Note, Output)
).
% quick_check/3 dialect
run_test(quick_check(Test, Position, Condition, Setup, Cleanup, Note), File, Output) :-
( run_test_condition(Test, Condition, File, Position, Note, Output) ->
( run_test_setup(Test, Setup, File, Position, Note, Output) ->
( catch(::test(Test, _, quick_check), Error, failed_test(Test, File, Position, Error, Output)) ->
( var(Error) ->
passed_test(Test, File, Position, Note, Output)
; true
)
; failed_test(Test, File, Position, failure_instead_of_success, Note, Output)
),
run_test_cleanup(Test, Cleanup, File, Position, Output)
; true
)
; skipped_test(Test, File, Position, Note, Output)
).
% quick_check/2 dialect
run_test(quick_check(Test, Position), File, Output) :-
( catch(::test(Test, _, quick_check), Error, failed_test(Test, File, Position, Error, Output)) ->
( var(Error) ->
passed_test(Test, File, Position, Output)
; true
)
; failed_test(Test, File, Position, failure_instead_of_success, Output)
).
% other dialects
run_test(succeeds(Test, Position), File, Output) :-
( catch(::test(Test, _, true), Error, failed_test(Test, File, Position, error_instead_of_success(Error), Output)) ->
( var(Error) ->
passed_test(Test, File, Position, Output)
; true
)
; failed_test(Test, File, Position, failure_instead_of_success, Output)
).
run_test(deterministic(Test, Position), File, Output) :-
( catch(::test(Test, _, deterministic(Deterministic)), Error, failed_test(Test, File, Position, error_instead_of_success(Error), Output)) ->
( var(Error) ->
( Deterministic == true ->
passed_test(Test, File, Position, Output)
; non_deterministic_success(Test, File, Position, Output)
)
; true
)
; failed_test(Test, File, Position, failure_instead_of_success, Output)
).
run_test(fails(Test, Position), File, Output) :-
( catch(::test(Test, _, fail), Error, failed_test(Test, File, Position, error_instead_of_failure(Error), Output)) ->
( var(Error) ->
failed_test(Test, File, Position, success_instead_of_failure, Output)
; true
)
; passed_test(Test, File, Position, Output)
).
run_test(throws(Test, PossibleErrors, Position), File, Output) :-
( catch(::test(Test, _, PossibleErrors), Error, check_error(Test, PossibleErrors, Error, File, Position, Output)) ->
( var(Error) ->
failed_test(Test, File, Position, success_instead_of_error, Output)
; true
)
; failed_test(Test, File, Position, failure_instead_of_error, Output)
).
run_test(skipped(Test, Position, Note), File, Output) :-
skipped_test(Test, File, Position, Note, Output).
run_test(skipped(Test, Position), File, Output) :-
skipped_test(Test, File, Position, Output).
check_error(Test, PossibleErrors, Error, File, Position, Output) :-
check_error(Test, PossibleErrors, Error, File, Position, '', Output).
check_error(Test, [PossibleError| PossibleErrors], Error, File, Position, Note, Output) :-
( member(ExpectedError, [PossibleError| PossibleErrors]),
subsumes_term(ExpectedError, Error) ->
passed_test(Test, File, Position, Note, Output)
; failed_test(Test, File, Position, wrong_error(PossibleError, Error), Note, Output)
).
write_tests_header :-
print_message(silent, lgtunit, tests_started),
os::date_time(Year, Month, Day, Hours, Minutes, Seconds, _),
print_message(information, lgtunit, tests_start_date_time(Year, Month, Day, Hours, Minutes, Seconds)).
write_tests_object :-
self(Object),
( object_property(Object, file(File, Directory)) ->
atom_concat(Directory, File, Path),
print_message(information, lgtunit, running_tests_from_object_file(Object, Path))
; % source data information missing
print_message(information, lgtunit, running_tests_from_object(Object))
).
write_tests_results :-
self(Object),
::skipped_(Skipped),
::passed_(Passed),
::failed_(Failed),
Total is Skipped + Passed + Failed,
::note(Note),
print_message(information, lgtunit, tests_results_summary(Object, Total, Skipped, Passed, Failed, Note)),
print_message(information, lgtunit, completed_tests_from_object(Object)).
write_tests_footer :-
os::date_time(Year, Month, Day, Hours, Minutes, Seconds, _),
print_message(information, lgtunit, tests_end_date_time(Year, Month, Day, Hours, Minutes, Seconds)),
print_message(silent, lgtunit, tests_ended).
passed_test(Test, File, Position, Note, Output) :-
self(Object),
increment_passed_tests_counter,
% ensure that any redirection of the current output stream by
% the test itself doesn't affect printing the test results
current_output(Current), set_output(Output),
print_message(information, lgtunit, passed_test(Object, Test, File, Position, Note)),
set_output(Current).
passed_test(Test, File, Position, Output) :-
passed_test(Test, File, Position, '', Output).
non_deterministic_success(Test, File, Position, Note, Output) :-
self(Object),
increment_failed_tests_counter,
% ensure that any redirection of the current output stream by
% the test itself doesn't affect printing the test results
current_output(Current), set_output(Output),
print_message(error, lgtunit, non_deterministic_success(Object, Test, File, Position, Note)),
set_output(Current).
non_deterministic_success(Test, File, Position, Output) :-
non_deterministic_success(Test, File, Position, '', Output).
failed_test(Test, File, Position, Reason, Note, Output) :-
self(Object),
increment_failed_tests_counter,
% ensure that any redirection of the current output stream by
% the test itself doesn't affect printing the test results
current_output(Current), set_output(Output),
print_message(error, lgtunit, failed_test(Object, Test, File, Position, Reason, Note)),
set_output(Current).
failed_test(Test, File, Position, Reason, Output) :-
failed_test(Test, File, Position, Reason, '', Output).
skipped_test(Test, File, Position, Note, Output) :-
self(Object),
increment_skipped_tests_counter,
% ensure that any redirection of the current output stream by
% the test itself doesn't affect printing the test results
current_output(Current), set_output(Output),
print_message(information, lgtunit, skipped_test(Object, Test, File, Position, Note)),
set_output(Current).
skipped_test(Test, File, Position, Output) :-
skipped_test(Test, File, Position, '', Output).
tests_skipped :-
self(Object),
::note(Note),
print_message(information, lgtunit, tests_skipped(Object, Note)).
:- meta_predicate(run_test_condition(*, *, *, *, *, *)).
run_test_condition(Test, Condition, File, Position, Note, Output) :-
option_goal(Condition, Goal),
% expected either success or failure; error means user error
( Goal == true ->
true
; catch(Goal, Error, (failed_test(Test,File,Position,step_error(condition,Error),Note,Output), fail))
).
:- meta_predicate(run_test_setup(*, *, *, *, *, *)).
run_test_setup(Test, Setup, File, Position, Note, Output) :-
option_goal(Setup, Goal),
% expected success; failure or error means user error
( Goal == true ->
true