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 set -eu
SCRIPT_DIR="$(cd -- "$(dirname -- "$0")" && pwd)" SCRIPT_DIR="$(cd -- "$(dirname -- "$0")" && pwd)"
cd "$SCRIPT_DIR/tool" cd "$SCRIPT_DIR"
jq -n -r -L . "include \"testLib\"; testLibMain" ${JQ:-jq} -n -r -L tool -L . "include \"testLib\"; testLibMain"
jq -n -r -L . "include \"tests\"; testsMain" \ ${JQ:-jq} -n -r -L tool -L . "include \"tests\"; testsMain"
--slurpfile "file:testdata/tests_export.json" testdata/tests_export.json

View file

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

View file

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