tix/testing/it.nix

52 lines
961 B
Nix
Raw Normal View History

2024-01-02 11:13:47 +00:00
msg: {
actual,
expected ? {},
asString ? false,
asJSON ? false,
removeDunders ? false,
safeToPrint ? true,
throws ? false,
}: let
2024-01-02 09:02:41 +00:00
preProcess = v:
2024-01-02 11:13:47 +00:00
if removeDunders
then undunder v
else if asString
then toString v
else if asJSON
then builtins.toJSON v
else v;
2024-01-02 09:02:41 +00:00
a = preProcess actual;
e = preProcess expected;
undunder = v:
2024-01-02 11:13:47 +00:00
if builtins.isAttrs v
then builtins.removeAttrs v ["__toString" "__functor"]
else v;
2024-01-02 09:02:41 +00:00
2024-01-02 11:13:47 +00:00
out =
if safeToPrint
then
builtins.toJSON (undunder (
if throws
then (builtins.tryEval actual).value
else {
inherit actual expected;
}
))
else ''{"msg": "cannot be stringified ):"}'';
2024-01-02 09:02:41 +00:00
success =
2024-01-02 11:13:47 +00:00
if throws
then (builtins.tryEval actual).success == false
else (a == e);
in
{
inherit success msg;
}
// (
if success
then {}
else builtins.trace "FAILED ${msg}" {inherit actual expected;}
)