Assertions: Difference between revisions

1,435 bytes added ,  2 years ago
No edit summary
Line 1,477:
# which is set by assert_options()
?></lang>
 
=={{header|Picat}}==
Picat does not have a built-in assertion feature; a simple implementation is shown below. Note that predicates and functions must be handled differently:
* predicates: using <code>call/n</code>
* functions: using <code>apply/n</code>
 
The predicate/function that is tested but be "escaped" by <code>$</code> in order to not be evaluated before the test.
<lang Picat>go =>
 
%
% Test predicates
%
S = "ablewasiereisawelba",
assert("test1",$is_palindrome(S)),
assert_failure("test2",$not is_palindrome(S)),
assert("test3",$member(1,1..10)),
assert_failure("test4",$member(1,1..10)), % bad test
nl,
 
%
% Test functions
%
assert("test5",$2+2,4),
assert_failure("test6",$2+2, 5),
 
assert("test7",$to_lowercase("PICAT"),"picat"),
assert_failure("test8",$sort([3,2,1]),[1,3,2]),
nl.
 
is_palindrome(S) =>
S == S.reverse().
 
% Test a predicate with call/1
assert(Name, A) =>
println(Name ++ ": " ++ cond(call(A), "ok", "not ok")).
assert_failure(Name, A) =>
println(Name ++ ": " ++ cond(not call(A), "ok", "not ok")).
 
% Test a function with apply/1
assert(Name, A, Expected) =>
println(Name ++ ": " ++ cond(apply(A) == Expected, "ok", "not ok")).
assert_failure(Name, A, Expected) =>
println(Name ++ ": " ++ cond(apply(A) != Expected , "ok", "not ok")).</lang>
 
{{out}}
<pre>test1: ok
test2: ok
test3: ok
test4: not ok
 
test5: ok
test6: ok
test7: ok
test8: ok</pre>
 
=={{header|PicoLisp}}==
495

edits