待辦事項 #48606

Some examples of how to use the library

啟用日期: 2023-09-07 18:47 最後更新: 2023-09-07 18:47

回報者:
負責人:
類型:
狀態:
開啟 [Owner assigned]
元件:
(無)
里程碑:
(無)
優先權:
4
嚴重程度:
2
處理結果:
檔案:

細節

Copied from PR #1 in the old github project.

The following should be written to a new file, "examples.m":

% This work is hereby placed into the public domain.
%

:- module examples.

%==============================================================================%
% Some basic examples of unit tests
:- interface.
%==============================================================================%

:- import_module io.

%-----------------------------------------------------------------------------%

:- pred main(io::di, io::uo) is det.

%==============================================================================%
:- implementation.
%==============================================================================%

:- use_module transunit.
:- use_module transunit.compare.
:- use_module transunit.compare_test.
:- use_module unit.
:- import_module int.
:- use_module maybe.

%-----------------------------------------------------------------------------%
% This test succeeds if the expression-under-test (2 + 2) and expected result
% (4) unify.
:- pred t0(unit.unit::in) is semidet.
t0(_) :-
    Res = 2 + 2,
    Res = 4.

%-----------------------------------------------------------------------------%
% This test is intended to be called from the higher-order "run_test"
% predicate.
:- pred t1(int::in, int::out) is det.
t1(X, Y) :-
    Y = X - 2.

%-----------------------------------------------------------------------------%

:- pred add_two(int::in, int::out) is det.
add_two(X, Y) :-
    Y = X + 2.

%-----------------------------------------------------------------------------%
% This is my favourite way to write tests.
% Not as much higher-order syntax as the above example, but run_result_test
% helps us out.
:- pred check_maths(maybe.maybe_error::out, io::di, io::uo) is det.
check_maths(Result, !IO) :-
    transunit.compare_test.run_test(add_two, 2, 5, Result).

%-----------------------------------------------------------------------------%
% This predicate runs through the different test cases.
% The unit test harness is just an executable, built using "mmc --make".
main(!IO) :-
    transunit.run_test(t0, "Addition", !IO),
    transunit.run_result_test((pred(Result::out, IO::di, IO::uo) is det :-
        transunit.compare_test.run_test(t1, 5, 3, Result)), "t1", !IO),
    transunit.run_result_test(check_maths, "Maths", !IO).

Ticket History (1/1 Histories)

2023-09-07 18:47 Updated by: poldy
  • New Ticket "Some examples of how to use the library" created

Attachment File List

No attachments

編輯

You are not logged in. I you are not logged in, your comment will be treated as an anonymous post. » 登入