interpl

Experiments with intepreters in Prolog
git clone https://ccx.te2000.cz/git/interpl
Log | Files | Refs | README

dotexp_tests.pl (6771B)


      1 :- use_module(library(debug), [assertion/1]).
      2 
      3 :- use_module(dotexp).
      4 
      5 portray_long :- portray_depth(32).
      6 portray_depth(Depth) :-
      7 	set_prolog_flag(answer_write_options, [quoted(true), portrayed(true), max_depth(Depth), spacing(next_argument)]),
      8 	set_prolog_flag(debugger_write_options, [quoted(true), portrayed(true), max_depth(Depth), spacing(next_argument)]).
      9 
     10 test_one_result(Template, Goal, Output) :-
     11 	findall(Template, Goal, Results),
     12 	% write(Results), nl,
     13 	assertion(length(Results, 1)),
     14 	Results = [Output].
     15 
     16 codes_to_ast(Codes, Result) :-
     17 	test_one_result(E, phrase(dotexp(E), Codes), Result).
     18 
     19 ast_to_ir(AST, Result) :-
     20 	test_one_result(IR, ast_dotexp(AST, IR), Result).
     21 
     22 codes_to_ir(Codes, IR) :-
     23 	codes_to_ast(Codes, AST),
     24 	ast_to_ir(AST, IR).
     25 
     26 ir_to_sexp(IR, Result) :-
     27 	test_one_result(E, dotexp_lists(IR, E), Result).
     28 
     29 codes_to_sexp(Codes, Result) :-
     30 	codes_to_ast(Codes, AST),
     31 	ast_to_ir(AST, IR),
     32 	ir_to_sexp(IR, Result).
     33 
     34 :- begin_tests(dotexp_ast).
     35 
     36 test(dotexp_atom) :-
     37 	codes_to_ast(`a`, Expression),
     38 	assertion(Expression =@= a).
     39 
     40 test(dotexp_atom2) :-
     41 	codes_to_ast(`hello-world!`, Expression),
     42 	assertion(Expression =@= 'hello-world!').
     43 
     44 test(dotexp_number) :-
     45 	codes_to_ast(`0`, Expression),
     46 	assertion(Expression =@= 0).
     47 
     48 test(dotexp_number2) :-
     49 	codes_to_ast(`31337`, Expression),
     50 	assertion(Expression =@= 31337).
     51 
     52 test(dotexp_list_empty) :-
     53 	codes_to_ast(`()`, Expression),
     54 	assertion(Expression =@= s([d([ws([])])])).
     55 
     56 test(dotexp_list_one_atom) :-
     57 	codes_to_ast(`(a)`, Expression),
     58 	assertion(Expression =@= s([d([ws([a])])])).
     59 
     60 test(dotexp_list_two_atoms) :-
     61 	codes_to_ast(`(a b)`, Expression),
     62 	assertion(Expression =@= s([d([ws([a, b])])])).
     63 
     64 test(dotexp_multi_list_two_and_one_atom) :-
     65 	codes_to_ast(`(a b; c)`, Expression),
     66 	assertion(Expression =@= s([d([ws([a, b])]), d([ws([c])])])).
     67 
     68 test(dotexp_atom_dot_atom_atom) :-
     69 	codes_to_ast(`(a .b c)`, Expression),
     70 	assertion(Expression =@= s([d([ws([a]), ws([b, c])])])).
     71 
     72 test(dotexp_def) :-
     73 	codes_to_ast(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
     74 	assertion(Expression =@=
     75 		s([d([ws([define, proc, s([d([ws([lambda, s([d([ws([e, f, g])])]), s([d([ws([lambda, s([d([ws([])])]), e])])])])])])])])])
     76 	).
     77 
     78 test(numbers_choose_as_a) :-
     79 	codes_to_ast(`(numbers .choose .as a)`, Expression),
     80 	assertion(Expression =@= s([d([ws([numbers]), ws([choose]), ws([as, a])])])).
     81 
     82 :- end_tests(dotexp_ast).
     83 
     84 
     85 :- begin_tests(dotexp_ir).
     86 
     87 test(dotexp_atom) :-
     88 	ast_to_ir(a, Expression),
     89 	assertion(Expression =@= a).
     90 
     91 test(dotexp_atom2) :-
     92 	ast_to_ir('hello-world!', Expression),
     93 	assertion(Expression =@= 'hello-world!').
     94 
     95 test(dotexp_number) :-
     96 	ast_to_ir(0, Expression),
     97 	assertion(Expression =@= 0).
     98 
     99 test(dotexp_number2) :-
    100 	ast_to_ir(31337, Expression),
    101 	assertion(Expression =@= 31337).
    102 
    103 test(dotexp_list_empty) :-
    104 	ast_to_ir(s([d([ws([])])]), Expression),
    105 	assertion(Expression =@= list([])).
    106 
    107 test(dotexp_list_one_atom) :-
    108 	ast_to_ir(s([d([ws([a])])]), Expression),
    109 	assertion(Expression =@= list([a])).
    110 
    111 test(dotexp_list_two_atoms) :-
    112 	ast_to_ir(s([d([ws([a, b])])]), Expression),
    113 	assertion(Expression =@= list([a, b])).
    114 
    115 test(dotexp_multi_list_two_and_one_atom) :-
    116 	ast_to_ir(s([d([ws([a, b])]), d([ws([c])])]), Expression),
    117 	assertion(Expression =@= multi([list([a, b]), list([c])])).
    118 
    119 test(dotexp_atom_dot_atom_atom) :-
    120 	ast_to_ir(s([d([ws([a]), ws([b, c])])]), Expression),
    121 	assertion(Expression =@= dot(b, list([a]), list([c]))).
    122 
    123 test(numbers_choose_as_a) :-
    124 	ast_to_ir(s([d([ws([numbers]), ws([choose]), ws([as, a])])]), Expression),
    125 	assertion(Expression =@= dot(as, dot(choose, list([numbers]), list([])), list([a]))).
    126 
    127 :- end_tests(dotexp_ir).
    128 
    129 
    130 :- begin_tests(dotexp_lists).
    131 test(de_l_atom) :-
    132 	ir_to_sexp(a, Expression),
    133 	assertion(Expression =@= a).
    134 
    135 test(de_l_number) :-
    136 	ir_to_sexp(0, Expression),
    137 	assertion(Expression =@= 0).
    138 
    139 test(de_l_number2) :-
    140 	ir_to_sexp(31337, Expression),
    141 	assertion(Expression =@= 31337).
    142 
    143 test(de_l_list_empty) :-
    144 	ir_to_sexp(list([]), Expression),
    145 	assertion(Expression =@= []).
    146 
    147 test(de_l_list_one_atom) :-
    148 	ir_to_sexp(list([a]), Expression),
    149 	assertion(Expression =@= [a]).
    150 
    151 test(de_l_list_two_atoms) :-
    152 	ir_to_sexp(list([a, b]), Expression),
    153 	assertion(Expression =@= [a, b]).
    154 
    155 test(de_l_multi_list_two_and_one_atom) :-
    156 	ir_to_sexp(multi([list([a, b]), list([c])]), Expression),
    157 	assertion(Expression =@= [[a, b], [c]]).
    158 
    159 test(de_l_atom_dot_atom_atom) :-
    160 	ir_to_sexp(dot(b, list([a]), list([c])), Expression),
    161 	assertion(Expression =@= [b, a, c]).
    162 
    163 test(de_l_atom_atom_dot_atom_atom) :-
    164 	ir_to_sexp(dot(c, list([a, b]), list([d])), Expression),
    165 	assertion(Expression =@= [c, [a, b], d]).
    166 
    167 test(numbers_choose_as_a) :-
    168 	ir_to_sexp(dot(as, dot(choose, list([numbers]), list([])), list([a])), Expression),
    169 	assertion(Expression =@= [as, [choose, numbers], a]).
    170 
    171 :- end_tests(dotexp_lists).
    172 
    173 :- begin_tests(dotexp).
    174 
    175 test(dotexp_atom) :-
    176 	codes_to_ir(`a`, Expression),
    177 	assertion(Expression =@= a).
    178 
    179 test(dotexp_atom2) :-
    180 	codes_to_ir(`hello-world!`, Expression),
    181 	assertion(Expression =@= 'hello-world!').
    182 
    183 test(dotexp_number) :-
    184 	codes_to_ir(`0`, Expression),
    185 	assertion(Expression =@= 0).
    186 
    187 test(dotexp_number2) :-
    188 	codes_to_ir(`31337`, Expression),
    189 	assertion(Expression =@= 31337).
    190 
    191 test(dotexp_list_empty) :-
    192 	codes_to_ir(`()`, Expression),
    193 	assertion(Expression =@= list([])).
    194 
    195 test(dotexp_list_one_atom) :-
    196 	codes_to_ir(`(a)`, Expression),
    197 	assertion(Expression =@= list([a])).
    198 
    199 test(dotexp_list_two_atoms) :-
    200 	codes_to_ir(`(a b)`, Expression),
    201 	assertion(Expression =@= list([a, b])).
    202 
    203 test(dotexp_multi_list_two_and_one_atom) :-
    204 	codes_to_ir(`(a b; c)`, Expression),
    205 	assertion(Expression =@= multi([list([a, b]), list([c])])).
    206 
    207 test(dotexp_atom_dot_atom_atom) :-
    208 	codes_to_ir(`(a .b c)`, Expression),
    209 	assertion(Expression =@= dot(b, list([a]), list([c]))).
    210 
    211 test(dotexp_atom_atom_dot_atom_atom) :-
    212 	codes_to_ir(`(a b .c d)`, Expression),
    213 	assertion(Expression =@= dot(c, list([a, b]), list([d]))).
    214 
    215 test(dotexp_def) :-
    216 	codes_to_ir(`(define proc (lambda (e f g) (lambda () e)))`, Expression),
    217 	assertion(Expression =@= list([define, proc, list([lambda, list([e, f, g]), list([lambda, list([]), e])])])).
    218 
    219 test(dotexp_qq) :-
    220 	Expression = {|dotexp||(define proc (lambda (e f g) (lambda () e)))|},
    221 	assertion(Expression =@= [define, proc, [lambda, [e, f, g], [lambda, [], e]]]).
    222 
    223 test(numbers_choose_as_a) :-
    224 	codes_to_sexp(`(numbers .choose .as a)`, Expression),
    225 	assertion(Expression =@= [as, [choose, numbers], a]).
    226 
    227 test(par_numbers_choose_as_a) :-
    228 	codes_to_sexp(`((numbers) .choose .as a)`, Expression),
    229 	assertion(Expression =@= [as, [choose, [numbers]], a]).
    230 
    231 test(dotexp_qq2) :-
    232 	Expression = {|dotexp||(numbers .choose .as a)|},
    233 	assertion(Expression =@= [as, [choose, numbers], a]).
    234 
    235 :- end_tests(dotexp).
    236