This commit is contained in:
chaos 2024-11-07 09:10:34 +00:00
parent b9ea89ca3f
commit d595689ccf
3 changed files with 17 additions and 35 deletions

View file

@ -3,8 +3,7 @@
set -eu
SCRIPT_DIR="$(cd -- "$(dirname -- "$0")" && pwd)"
cd "$SCRIPT_DIR/tool"
cd "$SCRIPT_DIR"
jq -n -r -L . "include \"testLib\"; testLibMain"
jq -n -r -L . "include \"tests\"; testsMain" \
--slurpfile "file:testdata/tests_export.json" testdata/tests_export.json
${JQ:-jq} -n -r -L tool -L . "include \"testLib\"; testLibMain"
${JQ:-jq} -n -r -L tool -L . "include \"tests\"; testsMain"

View file

@ -3,11 +3,10 @@ def printTestResult:
if .passed then
"Test \"\(.name)\" Passed"
else
(if (.loc != null) then " at \(.loc.file):\(.loc.line)" else "" end) as $fileLocation |
"Test \"\(.name)\"\($fileLocation) Failed\nReason: \(.reason)\nOutput: \(.output | tojson)"
"Test \"\(.name)\" Failed\nReason: \(.reason)\nOutput: \(.output | tojson)"
end;
def runTest($loc; $name; testExpr; checkResult; $expectError):
def runTest($name; testExpr; checkResult; $expectError):
try (
(null | testExpr) as $output |
if $expectError then
@ -15,23 +14,20 @@ def runTest($loc; $name; testExpr; checkResult; $expectError):
$name,
passed: false,
reason: "Expected error but no error was raised",
$output,
$loc
$output
}
elif ($output | checkResult) then
{
$name,
passed: true,
$output,
$loc
$output
}
else
{
$name,
passed: false,
reason: "Result was different from expected",
$output,
$loc
$output
}
end
) catch (
@ -41,23 +37,20 @@ def runTest($loc; $name; testExpr; checkResult; $expectError):
$name,
passed: false,
reason: "Error caught when no error was expected",
output: $error,
$loc
output: $error
}
elif ($expectError and ($error | checkResult)) then
{
$name,
passed: true,
output: $error,
$loc
output: $error
}
elif ($expectError and ($error | checkResult | not)) then
{
$name,
passed: false,
reason: "Expected error but received different error",
output: $error,
$loc
output: $error
}
else
error("unknown error")
@ -78,42 +71,36 @@ def expectFailed($result): $result | expectFailed;
def testTests:
expectPassed(runTest(
$__loc__;
"passing test";
true;
. == true;
false
)) |
expectPassed(runTest(
$__loc__;
"error expected and equal";
error("error");
. == "error";
true
)) |
expectFailed(runTest(
$__loc__;
"failing test";
true;
. == false;
false
)) |
expectFailed(runTest(
$__loc__;
"error expected but no error";
null;
. == null;
true
)) |
expectFailed(runTest(
$__loc__;
"error not expected";
error("error");
. == null;
false
)) |
expectFailed(runTest(
$__loc__;
"error different";
error("error");
. == "different error";
@ -122,6 +109,6 @@ def testTests:
def testLibMain:
testTests |
empty |
"Tests Passed\n" |
halt_error(0);

View file

@ -1,17 +1,13 @@
#import "./testdata/tests_export" as $exportData;
import "./testdata/tests_export" as $exportData;
include "journalUtils";
include "testLib";
def testsMain:
$ARGS.named["file:testdata/tests_export.json"][0] as $exportData |
expectPassed(runTest(
$__loc__;
"invalid input to experienceByTitle";
(
$exportData.experiences |
experienceByTitle("Test")
{} | experienceByTitle("Test")
);
. == "error";
false
));
. == "experienceByTitle takes a array of experiences as input";
true
)) | "Tests Passed\n" | halt_error(0);