[m-users.] Testing framework

Sean Charles (emacstheviking) objitsu at gmail.com
Sun May 23 16:47:05 AEST 2021


When I say simple I -mean- simple M, here is the complete code, tests and the, ahem, framework, run_test/5, …so far it helped me find two little ‘quirks’, since corrected! It’s so beginner level it probably isn’t much use to anybody other than myself!

:- module lexertest.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module list, lexer, maybe, string.
:- import_module errors.

main(!IO) :-
    io.format("\nRunning some lexer tests...\n", [], !IO),
    basic_token_recognition_tests(!IO),
    io.format("\n\n", [], !IO).

:- pred basic_token_recognition_tests(io::di, io::uo) is det.
basic_token_recognition_tests(!IO) :-
    run_test($line, "     ( ) [ ] { }",
        [ op(pos(5,1,5)), cp(pos(7,1,7)), ol(pos(9,1,9))
        , cl(pos(11,1,11)), om(pos(13,1,13)), cm(pos(15,1,15))
        ], !IO),
    run_test($line, " Stock-Token", [tk(pos(1,1,1), "Stock-Token")], !IO),
    run_test($line, " /*Blocked*/", [cn(pos(1,1,1), "/*Blocked*/")], !IO),
    run_test($line, " ;Say it", [c1(pos(1,1,1), ";Say it")], !IO),
    run_test($line, "'string1'", [s1(pos(0,1,0), "string1")], !IO),
    run_test($line, "\"string2\"", [s2(pos(0,1,0), "string2")], !IO),
        %% HARDER
    run_test($line, "A/*mean*/TEST'of'\"the\";lexer",
    [ tk(pos(0,1,0), "A")
    , cn(pos(1,1,1), "/*mean*/")
    , tk(pos(9,1,9), "TEST")
    , s1(pos(13,1,13), "of")
    , s2(pos(17,1,17), "the")
    , c1(pos(22,1,22), ";lexer")
    ], !IO),
        %% String ESCAPES
    run_test($line, "'string\\\'thing'",   [s1(pos(0,1,0), "string\\\'thing")], !IO),
    run_test($line, "\"string\\\"thing\"", [s2(pos(0,1,0), "string\\\"thing")], !IO)
    .

    % S is the source code.
    % E is the expected token list.
:- pred run_test(int::in, string::in, list(lexer.token)::in, io::di, io::uo) is det.
run_test(Line, S, Exp, !IO) :-
    lexer.on_string(S, Lx, !IO),
    % lexer.dump_lexer(Lx, !IO),
    Status = lex_error(Lx),
    % io.format("lex_error: %s", [s(string(Status))], !IO),
    ( if Status = yes(ok_eob) then
        (if Exp = tokens(Lx) then
            io.format(" ✓ test passed: line: %04i %s\n", [i(Line), s(S)], !IO)
        else
            io.format("\n✘ test failed: line: %04i\nexp: %s\ngot: %s\n\n",
                [i(Line), s(string(Exp)), s(string(tokens(Lx)))], !IO)
        )
    else io.format("\nlex failed: %s\n", [s(string(Status))], !IO)
    ).

Sean.


> On 23 May 2021, at 03:16, M McDonough <foolkingcrown at gmail.com> wrote:
> 
>> I rolled a very simple one and it seems to have gone well, I can now add more tests to my lexer package and to the rest of the code as I write it. A typical output from the test Makefile process:
> 
> Out of curiosity, do you have a link to the code?
> 
> There is another Mercury test framework available too:
> https://osdn.net/projects/transunit/
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20210523/fcfd67e1/attachment.html>


More information about the users mailing list