unit-testingtestingprologswi-prologplunit

Test in Prolog. How to run a unit test that checks if my output file matches the my text file?


I'm implementing a natural language generator using prolog (swipl).

I have a .txt test file with some of the phrases I should be able to generate in this format:

[goal,identify,type_object,animal,object,cat,event,ran away,when,[last,mont],where,[]]
[which,cats,ran away,last,month,?]

[goal,identify,type_object,animal,object,dog,event,ran,when,[last,mont],where,[]]
[which,dogs,ran away,last,year,?]

and so on...

How can I use plunit (or something else?) to check if all the elements of my test file are in my output file returning true/false?


Solution

  • read/1 might be what you are looking for:

    Suppose I define a fact p/1:

    p([a,b,c]).
    

    then I can read a term from standard input and compare ( lines starting with |: are denoted as user input by SWI Prolog, your implementation might differ):

    ?- read(X), p(X).
    |: [a,b,c].
    
    X = [a, b, c].
    
    ?- read(X), p(X).
    |: [].
    
    false.