journal/tool/lib/testLib.jq

115 lines
2.2 KiB
Plaintext
Raw Normal View History

2024-11-07 08:58:38 +00:00
def printTestResult:
. as $result | $result |
if .passed then
"Test \"\(.name)\" Passed"
else
2024-11-07 09:10:34 +00:00
"Test \"\(.name)\" Failed\nReason: \(.reason)\nOutput: \(.output | tojson)"
2024-11-07 08:58:38 +00:00
end;
2024-11-07 09:10:34 +00:00
def runTest($name; testExpr; checkResult; $expectError):
2024-11-07 08:58:38 +00:00
try (
(null | testExpr) as $output |
if $expectError then
{
$name,
passed: false,
reason: "Expected error but no error was raised",
2024-11-07 09:10:34 +00:00
$output
2024-11-07 08:58:38 +00:00
}
elif ($output | checkResult) then
{
$name,
passed: true,
2024-11-07 09:10:34 +00:00
$output
2024-11-07 08:58:38 +00:00
}
else
{
$name,
passed: false,
reason: "Result was different from expected",
2024-11-07 09:10:34 +00:00
$output
2024-11-07 08:58:38 +00:00
}
end
) catch (
. as $error |
if ($expectError | not) then
{
$name,
passed: false,
reason: "Error caught when no error was expected",
2024-11-07 09:10:34 +00:00
output: $error
2024-11-07 08:58:38 +00:00
}
elif ($expectError and ($error | checkResult)) then
{
$name,
passed: true,
2024-11-07 09:10:34 +00:00
output: $error
2024-11-07 08:58:38 +00:00
}
elif ($expectError and ($error | checkResult | not)) then
{
$name,
passed: false,
reason: "Expected error but received different error",
2024-11-07 09:10:34 +00:00
output: $error
2024-11-07 08:58:38 +00:00
}
else
error("unknown error")
end
);
def expectPassed:
if (.passed | not) then
error(. | printTestResult)
end;
def expectPassed($result): $result | expectPassed;
def expectFailed:
if (.passed) then
error(. | printTestResult)
end;
def expectFailed($result): $result | expectFailed;
def testTests:
expectPassed(runTest(
"passing test";
true;
. == true;
false
)) |
expectPassed(runTest(
"error expected and equal";
error("error");
. == "error";
true
)) |
expectFailed(runTest(
"failing test";
true;
. == false;
false
)) |
expectFailed(runTest(
"error expected but no error";
null;
. == null;
true
)) |
expectFailed(runTest(
"error not expected";
error("error");
. == null;
false
)) |
expectFailed(runTest(
"error different";
error("error");
. == "different error";
true
));
def testLibMain:
testTests |
2024-11-07 09:10:34 +00:00
"Tests Passed\n" |
2024-11-07 08:58:38 +00:00
halt_error(0);