Skip to content

Commit

Permalink
increasing JSON IO tests branch and line coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenvinju committed Dec 26, 2024
1 parent ae54d3e commit dea3dd5
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import Node;

loc targetFile = |memory://test-tmp/test-<"<uuidi()>">.json|;

bool writeRead(type[&T] returnType, &T dt, value (value x) normalizer = value(value x) { return x; }, bool dateTimeAsInt=false) {
bool writeRead(type[&T] returnType, &T dt, value (value x) normalizer = value(value x) { return x; }, bool dateTimeAsInt=false, bool unpackedLocations=false, bool explicitConstructorNames=false, bool explicitDataTypes=false) {
dt = visit (dt) {
// reals must fit in double
case real r => fitDouble(r)
// integers must not overflow
case int i => i % floor(pow(2, 10)) when abs(i) > pow(2, 10)
}

json = asJSON(dt, dateTimeAsInt=dateTimeAsInt);
readBack = normalizer(parseJSON(returnType, json));
json = asJSON(dt, dateTimeAsInt=dateTimeAsInt, unpackedLocations=unpackedLocations, explicitConstructorNames=explicitConstructorNames, explicitDataTypes=explicitDataTypes);
readBack = normalizer(parseJSON(returnType, json, explicitConstructorNames=explicitConstructorNames, explicitDataTypes=explicitDataTypes));
if (readBack !:= normalizer(dt) /* ignores additional src fields */) {
println("What is read back, a <type(typeOf(readBack),())>:");
iprintln(readBack);
Expand All @@ -48,6 +48,7 @@ test bool jsonWithRat1(rat dt) = writeRead(#rat, dt);
test bool jsonWithNum1(num dt) = writeRead(#num, dt, normalizer=numNormalizer);

test bool jsonWithLoc1(loc dt) = writeRead(#loc, dt);
test bool jsonWithLoc2(loc dt) = writeRead(#loc, dt, unpackedLocations=true);
test bool jsonWithStr1(str dt) = writeRead(#str, dt);
test bool jsonWithDatetime1(datetime dt) = writeRead(#datetime, dt);
test bool jsonWithDatetime2(datetime dt) = writeRead(#datetime, dt, dateTimeAsInt=true);
Expand All @@ -58,6 +59,10 @@ test bool jsonWithNode1(node dt) = writeRead(#node, dt, normalizer = toDefaultR

test bool jsonWithDATA11(DATA1 dt) = writeRead(#DATA1, dt);
test bool jsonWithDATA21(DATA2 dt) = writeRead(#DATA2, dt);
test bool jsonWithDATA12(DATA1 dt) = writeRead(#DATA1, dt, explicitDataTypes=true);
test bool jsonWithDATA22(DATA2 dt) = writeRead(#DATA2, dt, explicitDataTypes=true);
test bool jsonWithDATA13(DATA1 dt) = writeRead(#DATA1, dt, explicitConstructorNames=true);
test bool jsonWithDATA23(DATA2 dt) = writeRead(#DATA2, dt, explicitConstructorNames=true);

@synopsis{all values can be written and read again}
@description{
Expand Down

0 comments on commit dea3dd5

Please sign in to comment.