This commit is contained in:
chaos 2024-11-07 13:10:18 +00:00
parent e6820c5254
commit 87e114d76d
3 changed files with 58 additions and 8 deletions

View file

@ -1,4 +1,4 @@
import "./testdata/tests_export" as $exportData; import "./testdata/tests_export" as $exportDataArray;
include "journalUtils"; include "journalUtils";
include "testLib"; include "testLib";
import "typeLib" as typeLib; import "typeLib" as typeLib;
@ -7,10 +7,18 @@ def journalUtilsTests:
expectPassed(runTest( expectPassed(runTest(
"invalid input to experienceByTitle"; "invalid input to experienceByTitle";
( (
{} | experienceByTitle("Test") null | experienceByTitle("Test")
); );
. == "experienceByTitle takes a array of experiences as input"; . == "experienceByTitle takes a array of experiences as input";
true true
)) |
expectPassed(runTest(
"experience not found";
(
$exportDataArray[0].experiences | experienceByTitle("Test")
);
. == null;
false
)); ));
def testsMain: def testsMain:

View file

@ -10,13 +10,31 @@ def typecheckingDebug:
true true
else false end; else false end;
def dumpType:
if (. | type) == "object" then
. | [to_entries | sort_by(.key)[] | {"\(.key)": (.value|dumpType)}] | add
elif (. | type) == "array" then
[.[] | dumpType] as $array |
if ($array | length > 0) then
if ($array | all(select(. == $array[0]))) then
[$array[0]]
else
$array
end
else
"array:empty/unknown"
end
else
. | type
end;
def typeErrorText: def typeErrorText:
. as $type | . as $type |
"Type Error Checking '\($type)'"; "Type Error Checking '\($type)'";
def typeErrorText($type): $type | typeErrorText; def typeErrorText($type): $type | typeErrorText;
def typeError($type): def typeError($type):
if typecheckingDebug then debug({$type, value: .}) end | if typecheckingDebug then debug({typeName: $type, value: ., valueType: . | dumpType}) end |
error($type | typeErrorText); error($type | typeErrorText);
def ensureNull: if typecheckingEnabled and (. | type != "null") then typeError("null") end; def ensureNull: if typecheckingEnabled and (. | type != "null") then typeError("null") end;
@ -39,6 +57,14 @@ def ensureNullOr(ensureType):
def ensureKey($type; $key): if (. | has($key) | not) then typeError("\($type):\($key)") end; def ensureKey($type; $key): if (. | has($key) | not) then typeError("\($type):\($key)") end;
def ensureKey($value; $type; $key): $value | ensureKey($type; $key); def ensureKey($value; $type; $key): $value | ensureKey($type; $key);
def ensureWrapError($newType; ensureType):
. as $value |
try (. | ensureType) catch (error(typeErrorText($newType)));
def ensureWrapError($value; $newType; ensureType):
$value | ensureWrapError($newType; ensureType);
# TYPECHECKING=1 required for tests # TYPECHECKING=1 required for tests
def typeLibTests: def typeLibTests:
expectPassed(runTest( expectPassed(runTest(

View file

@ -1,17 +1,33 @@
import "typeLib" as typeLib; import "typeLib" as typeLib;
#["DEBUG:",{"creationDate":"number","ingestions":[{"administrationRoute":"string","consumerName":"null","creationDate":"number","customUnitId":"null","dose":"number","estimatedDoseStandardDeviation":"null","isDoseAnEstimate":"boolean","notes":"string","stomachFullness":"null","substanceName":"string","time":"number","units":"string"}],"isFavorite":"boolean","location":"null","ratings":"array:empty/unknown","sortDate":"number","text":"string","timedNotes":"array:empty/unknown","title":"string"}]
def ensureExperience: def ensureExperience:
. as $experience | . as $experience |
typeLib::ensureObject | typeLib::ensureObject |
debug(. | typeLib::dumpType) |
$experience | $experience |
typeLib::ensureKey("experience"; "title") | typeLib::ensureKey("experience"; "title") |
.title | typeLib::ensureString | .title | typeLib::ensureWrapError("experience:title"; typeLib::ensureString) |
$experience | $experience |
typeLib::ensureKey("experience"; "text") | typeLib::ensureKey("experience"; "text") |
.text | typeLib::ensureString .text | typeLib::ensureWrapError("experience:text"; typeLib::ensureString) |
$experience |
typeLib::ensureKey("experience"; "creationDate") |
.creationDate | typeLib::ensureWrapError("experience:creationDate"; typeLib::ensureNumber) |
$experience |
typeLib::ensureKey("experience"; "sortDate") |
.sortDate | typeLib::ensureWrapError("experience:sortDate"; typeLib::ensureNumber) |
$experience |
typeLib::ensureKey("experience"; "isFavorite") |
.isFavorite | typeLib::ensureWrapError("experience:isFavorite"; typeLib::ensureBool)
; ;
def ensureExportData: def ensureExportData:
@ -22,5 +38,5 @@ def ensureExportData:
typeLib::ensureKey("exportData"; "customUnits") | typeLib::ensureKey("exportData"; "customUnits") |
typeLib::ensureKey("exportData"; "experiences") | typeLib::ensureKey("exportData"; "experiences") |
typeLib::ensureKey("exportData"; "substanceCompanions") | typeLib::ensureKey("exportData"; "substanceCompanions") |
(.experiences[] | ensureExperience) (reduce .experiences[] as $experience (null; $experience | ensureExperience))
end; end;