Skip to content

Commit a6dcf03

Browse files
authored
Run tests at comptime (#139)
1 parent c343b0e commit a6dcf03

File tree

6 files changed

+52
-38
lines changed

6 files changed

+52
-38
lines changed

json_serialization.nimble

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ let cfg =
3535
(if verbose: "" else: " --verbosity:0 --hints:off") &
3636
" --outdir:build " &
3737
quoteShell("--nimcache:build/nimcache/$projectName") &
38-
" -d:nimOldCaseObjects"
38+
" -d:nimOldCaseObjects -d:serializationTestAllRountrips" &
39+
(if NimMajor >= 2: " -d:unittest2Static" else: "")
3940

4041
proc build(args, path: string) =
4142
exec nimc & " " & lang & " " & cfg & " " & flags & " " & args & " " & path

tests/test_json_flavor.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ suite "Test JsonFlavor":
142142
check y.list.string.len == 0
143143

144144
test "Enum value representation primitives":
145+
NullyFields.flavorEnumRep(EnumAsString)
145146
when NullyFields.flavorEnumRep() == EnumAsString:
146147
check true
147148
elif NullyFields.flavorEnumRep() == EnumAsNumber:

tests/test_line_col.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const testCases = [
122122

123123
suite "Test line col":
124124
for i, tc in testCases:
125-
test $i:
125+
runtimeTest $i:
126126
var stream = unsafeMemoryInput(tc.text)
127127
var lex = init(JsonLexer, stream, tc.flags, tc.conf)
128128
var value: JsonValueRef[uint64]

tests/test_reader.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,16 @@ suite "JsonReader basic test":
146146
r.readValue(valOrig)
147147
# workaround for https://github.com/nim-lang/Nim/issues/24274
148148
let val = valOrig
149+
# TODO: https://github.com/status-im/nim-unittest2/pull/58
150+
doAssert val.seven[] == 555
149151
check:
150152
val.one == JsonString("[1,true,null]")
151153
val.two.num == 123
152154
val.three == "help"
153155
val.four == "012"
154156
val.five == "345"
155157
val.six == true
156-
val.seven[] == 555
158+
#val.seven[] == 555
157159
val.eight == mTwo
158160
val.nine == 77
159161
val.ten == 88

tests/test_serialization.nim

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ proc readValue*(r: var JsonReader[DefaultFlavor], value: var MyCaseObject)
236236
{.pop.}
237237

238238
var
239-
customVisit: TokenRegistry
239+
customVisit {.global, compileTime.}: TokenRegistry
240240

241241
Json.useCustomSerialization(WithCustomFieldRule.intVal):
242242
read:
@@ -377,14 +377,14 @@ suite "toJson tests":
377377
"abc".toJson == "\"abc\""
378378

379379
test "float":
380-
Json.roundtripTest 1.23, "1.23"
381-
Json.roundtripTest 1.23'f32, "1.23"
382-
Json.roundtripTest 1.23'f64, "1.23"
380+
Json.roundtripChecks 1.23, "1.23"
381+
Json.roundtripChecks 1.23'f32, "1.23"
382+
Json.roundtripChecks 1.23'f64, "1.23"
383383

384384
test "enums":
385-
Json.roundtripTest x0, "\"x0\""
386-
Json.roundtripTest x1, "\"x1\""
387-
Json.roundtripTest x2, "\"x2\""
385+
Json.roundtripChecks x0, "\"x0\""
386+
Json.roundtripChecks x1, "\"x1\""
387+
Json.roundtripChecks x2, "\"x2\""
388388
expect UnexpectedTokenError:
389389
discard Json.decode("0", EnumTestX)
390390
expect UnexpectedTokenError:
@@ -406,10 +406,10 @@ suite "toJson tests":
406406
expect UnexpectedValueError:
407407
discard Json.decode("\"0\"", EnumTestX)
408408

409-
Json.roundtripTest y1, "\"y1\""
410-
Json.roundtripTest y3, "\"y3\""
411-
Json.roundtripTest y4, "\"y4\""
412-
Json.roundtripTest y6, "\"y6\""
409+
Json.roundtripChecks y1, "\"y1\""
410+
Json.roundtripChecks y3, "\"y3\""
411+
Json.roundtripChecks y4, "\"y4\""
412+
Json.roundtripChecks y6, "\"y6\""
413413
check:
414414
Json.decode("1", EnumTestY) == y1
415415
Json.decode("3", EnumTestY) == y3
@@ -438,9 +438,9 @@ suite "toJson tests":
438438
expect UnexpectedValueError:
439439
discard Json.decode("\"1\"", EnumTestY)
440440

441-
Json.roundtripTest z1, "\"aaa\""
442-
Json.roundtripTest z2, "\"bbb\""
443-
Json.roundtripTest z3, "\"ccc\""
441+
Json.roundtripChecks z1, "\"aaa\""
442+
Json.roundtripChecks z2, "\"bbb\""
443+
Json.roundtripChecks z3, "\"ccc\""
444444
expect UnexpectedTokenError:
445445
discard Json.decode("0", EnumTestZ)
446446
expect UnexpectedValueError:
@@ -458,9 +458,9 @@ suite "toJson tests":
458458
expect UnexpectedValueError:
459459
discard Json.decode("\"\ud83d\udc3c\"", EnumTestZ)
460460

461-
Json.roundtripTest n1, "\"aaa\""
462-
Json.roundtripTest n2, "\"bbb\""
463-
Json.roundtripTest n3, "\"ccc\""
461+
Json.roundtripChecks n1, "\"aaa\""
462+
Json.roundtripChecks n2, "\"bbb\""
463+
Json.roundtripChecks n3, "\"ccc\""
464464
check:
465465
Json.decode("\"aAA\"", EnumTestN) == n1
466466
Json.decode("\"bBB\"", EnumTestN) == n2
@@ -491,9 +491,9 @@ suite "toJson tests":
491491
expect UnexpectedValueError:
492492
discard Json.decode("\"\ud83d\udc3c\"", EnumTestN)
493493

494-
Json.roundtripTest o1, "\"o1\""
495-
Json.roundtripTest o2, "\"o2\""
496-
Json.roundtripTest o3, "\"o3\""
494+
Json.roundtripChecks o1, "\"o1\""
495+
Json.roundtripChecks o2, "\"o2\""
496+
Json.roundtripChecks o3, "\"o3\""
497497
check:
498498
Json.decode("\"o_1\"", EnumTestO) == o1
499499
Json.decode("\"o_2\"", EnumTestO) == o2
@@ -656,7 +656,10 @@ suite "toJson tests":
656656
let decoded = try:
657657
Json.decode(json, Simple, requireAllFields = true, allowUnknownFields = true)
658658
except SerializationError as err:
659-
checkpoint "Unexpected deserialization failure: " & err.formatMsg("<input>")
659+
when nimvm:
660+
checkpoint "Unexpected deserialization failure: " & err.msg
661+
else:
662+
checkpoint "Unexpected deserialization failure: " & err.formatMsg("<input>")
660663
raise
661664

662665
check:
@@ -737,8 +740,8 @@ suite "toJson tests":
737740
h3 = Json.decode("""{"r":{"distance":3,"x":1,"y":"2"}}""",
738741
HoldsOption, requireAllFields = true)
739742

740-
Json.roundtripTest h1, """{"r":null,"o":{"distance":3,"x":1,"y":"2"}}"""
741-
Json.roundtripTest h2, """{"r":{"distance":3,"x":1,"y":"2"}}"""
743+
Json.roundtripChecks h1, """{"r":null,"o":{"distance":3,"x":1,"y":"2"}}"""
744+
Json.roundtripChecks h2, """{"r":{"distance":3,"x":1,"y":"2"}}"""
742745

743746
check h3 == h2
744747

@@ -754,10 +757,10 @@ suite "toJson tests":
754757
h5 = OtherOptionTest(b: some Meter(2))
755758
h6 = OtherOptionTest(a: some Meter(3), b: some Meter(4))
756759

757-
Json.roundtripTest h3, """{}"""
758-
Json.roundtripTest h4, """{"a":1}"""
759-
Json.roundtripTest h5, """{"b":2}"""
760-
Json.roundtripTest h6, """{"a":3,"b":4}"""
760+
Json.roundtripChecks h3, """{}"""
761+
Json.roundtripChecks h4, """{"a":1}"""
762+
Json.roundtripChecks h5, """{"b":2}"""
763+
Json.roundtripChecks h6, """{"a":3,"b":4}"""
761764

762765
let
763766
arr = @[some h3, some h4, some h5, some h6, none(OtherOptionTest)]
@@ -796,11 +799,11 @@ suite "toJson tests":
796799
# lent iterator error
797800
let a = a
798801
let b = b
799-
Json.roundtripTest NestedOptionTest(c: a, d: b), results[r]
802+
Json.roundtripChecks NestedOptionTest(c: a, d: b), results[r]
800803
r.inc
801804

802-
Json.roundtripTest SeqOptionTest(a: @[some 5.Meter, none Meter], b: Meter(5)), """{"a":[5,null],"b":5}"""
803-
Json.roundtripTest OtherOptionTest2(a: some 5.Meter, b: none Meter, c: some 10.Meter), """{"a":5,"c":10}"""
805+
Json.roundtripChecks SeqOptionTest(a: @[some 5.Meter, none Meter], b: Meter(5)), """{"a":[5,null],"b":5}"""
806+
Json.roundtripChecks OtherOptionTest2(a: some 5.Meter, b: none Meter, c: some 10.Meter), """{"a":5,"c":10}"""
804807

805808
test "Result Opt types":
806809
check:
@@ -812,8 +815,8 @@ suite "toJson tests":
812815
h1 = HoldsResultOpt(o: Opt[Simple].ok Simple(x: 1, y: "2", distance: Meter(3)))
813816
h2 = HoldsResultOpt(r: newSimple(1, "2", Meter(3)))
814817

815-
Json.roundtripTest h1, """{"o":{"distance":3,"x":1,"y":"2"},"r":null}"""
816-
Json.roundtripTest h2, """{"r":{"distance":3,"x":1,"y":"2"}}"""
818+
Json.roundtripChecks h1, """{"o":{"distance":3,"x":1,"y":"2"},"r":null}"""
819+
Json.roundtripChecks h2, """{"r":{"distance":3,"x":1,"y":"2"}}"""
817820

818821
let
819822
h3 = Json.decode("""{"r":{"distance":3,"x":1,"y":"2"}}""",
@@ -828,7 +831,7 @@ suite "toJson tests":
828831

829832
test "Custom field serialization":
830833
let obj = WithCustomFieldRule(str: "test", intVal: 10)
831-
Json.roundtripTest obj, """{"str":"test","intVal":"10"}"""
834+
Json.roundtripChecks obj, """{"str":"test","intVal":"10"}"""
832835

833836
test "Case object as field":
834837
let
@@ -911,7 +914,10 @@ suite "toJson tests":
911914

912915
except SerializationError as e:
913916
echo e.getStackTrace
914-
echo e.formatMsg("<>")
917+
when nimvm:
918+
echo e.msg
919+
else:
920+
echo e.formatMsg("<>")
915921
raise e
916922

917923
test "Holders of JsonString":
@@ -927,7 +933,10 @@ suite "toJson tests":
927933
let decoded = Json.decode(jsonContent, JsonNode)
928934
check decoded["tasks"][0]["label"] == newJString("nimbus-eth2 build")
929935
except SerializationError as err:
930-
checkpoint err.formatMsg("./cases/comments.json")
936+
when nimvm:
937+
checkpoint "<./cases/comments.json> " & err.msg
938+
else:
939+
checkpoint err.formatMsg("./cases/comments.json")
931940
check false
932941

933942
test "A nil cstring":

tests/test_writer.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ suite "Test writer":
188188
check vv == """{"a":null,"b":null,"c":999}"""
189189

190190
test "Enum value representation primitives":
191+
DefaultFlavor.flavorEnumRep(EnumAsString)
191192
when DefaultFlavor.flavorEnumRep() == EnumAsString:
192193
check true
193194
elif DefaultFlavor.flavorEnumRep() == EnumAsNumber:

0 commit comments

Comments
 (0)