Skip to content

Commit 572e2f8

Browse files
authored
Spec comptime tests (#133)
1 parent b65fd6a commit 572e2f8

File tree

8 files changed

+77
-43
lines changed

8 files changed

+77
-43
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- Customizable parser strictness including support for non-standard extensions
2525
- Well-defined handling of malformed / malicious inputs with configurable parsing limits
2626
- Fuzzing and comprehensive manual test coverage
27+
- Since v0.4.4, compile time encode/decode is supported. This means you can initialize a const value using decode. It is also ok to use it inside a static block or other Nim VM code.
2728

2829
<!-- ANCHOR_END: Features -->
2930

json_serialization.nimble

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ license = "Apache License 2.0"
1717
skipDirs = @["tests", "fuzzer"]
1818

1919
requires "nim >= 1.6.0",
20+
"faststreams >= 0.5.0",
2021
"serialization",
2122
"stew >= 0.2.0",
2223
"results"

tests/test_lexer.nim

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,7 @@ suite "lexer test suite":
279279
var conf = defaultJsonReaderConf
280280
conf.fractionDigitsLimit = 3
281281
testScanNumber(".1234", ".123", error = errFracDigitLimit, conf = conf)
282-
283282
testScanNumber("1234.5555", "1234.5555")
284-
285-
conf = defaultJsonReaderConf
286-
conf.fractionDigitsLimit = 3
287283
testScanNumber("1234.1234", "1234.123", error = errFracDigitLimit, conf = conf)
288284

289285
test "scanNumber exponent part string":
@@ -343,11 +339,7 @@ suite "lexer test suite":
343339
var conf = defaultJsonReaderConf
344340
conf.fractionDigitsLimit = 3
345341
testScanNumber(".1234", JsonVoid(), error = errFracDigitLimit, conf = conf)
346-
347342
testScanNumber("1234.5555", JsonVoid())
348-
349-
conf = defaultJsonReaderConf
350-
conf.fractionDigitsLimit = 3
351343
testScanNumber("1234.1234", JsonVoid(), error = errFracDigitLimit, conf = conf)
352344

353345
test "scanNumber exponent part JsonVoid":
@@ -411,11 +403,7 @@ suite "lexer test suite":
411403
conf.fractionDigitsLimit = 3
412404
testScanNumber(".1234", JsonNumber[string](fraction: "123"),
413405
error = errFracDigitLimit, conf = conf)
414-
415406
testScanNumber("1234.5555", JsonNumber[string](integer: "1234", fraction: "5555"))
416-
417-
conf = defaultJsonReaderConf
418-
conf.fractionDigitsLimit = 3
419407
testScanNumber("1234.1234", JsonNumber[string](integer: "1234", fraction: "123"),
420408
error = errFracDigitLimit, conf = conf)
421409

@@ -501,7 +489,6 @@ suite "lexer test suite":
501489

502490
testScanNumber("1234.5555", JsonNumber[uint64](integer: 1234, fraction: "5555"))
503491

504-
conf = defaultJsonReaderConf
505492
conf.fractionDigitsLimit = 3
506493
testScanNumber("1234.1234", JsonNumber[uint64](integer: 1234, fraction: "123"),
507494
error = errFracDigitLimit, conf = conf)

tests/test_parser.nim

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,18 @@ suite "Custom iterators":
5252
exponent == 789
5353

5454
test "customStringValueIt":
55-
var text: string
56-
var r = toReader "\"hello \\t world\""
57-
r.customStringValueIt:
58-
text.add it
59-
60-
expect JsonReaderError:
61-
r.customStringValueIt(10):
55+
proc customTest() =
56+
var text: string
57+
var r = toReader "\"hello \\t world\""
58+
r.customStringValueIt:
6259
text.add it
6360

64-
check text == "hello \t world"
61+
expect JsonReaderError:
62+
r.customStringValueIt(10):
63+
text.add it
64+
65+
check text == "hello \t world"
66+
customTest()
6567

6668
suite "Public parser":
6769
test "parseArray":
@@ -222,16 +224,26 @@ suite "Public parser":
222224
val = r.parseFloat(float64)
223225
check val == -56009000.0
224226

227+
proc inputFile(fileName: string): InputStream =
228+
when nimvm:
229+
let data = staticRead(pathRelativeTo(fileName, "tests"))
230+
unsafeMemoryInput(data)
231+
else:
232+
memFileInput(fileName)
233+
225234
template testParseAsString(fileName: string) =
226235
try:
227-
var stream = memFileInput(fileName)
236+
var stream = inputFile(fileName)
228237
var r = JsonReader[DefaultFlavor].init(stream)
229238
let val = r.parseAsString()
230239
var xr = toReader val.string
231240
let xval = xr.parseAsString()
232241
check val == xval
233242
except JsonReaderError as ex:
234-
debugEcho ex.formatMsg(fileName)
243+
when nimvm:
244+
debugEcho fileName, ex.msg
245+
else:
246+
debugEcho ex.formatMsg(fileName)
235247
check false
236248

237249
test "parseAsString":

tests/test_reader.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ suite "JsonReader basic test":
163163
val.fourteen == SecondObject(one: "world", two: false)
164164

165165
except JsonReaderError as ex:
166-
debugEcho ex.formatMsg("jsonText3")
166+
when nimvm:
167+
debugEcho "jsonText3", ex.msg
168+
else:
169+
debugEcho ex.formatMsg("jsonText3")
167170
check false
168171

169172
test "Special Types":

tests/test_spec.nim

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ import
1717
include
1818
../json_serialization/lexer
1919

20-
proc yCase(fileName, name: string): bool {.raises:[IOError].} =
21-
var stream = memFileInput(fileName)
20+
proc inputFile(fileName: string): InputStream {.raises: [IOError].} =
21+
when nimvm:
22+
let data = staticRead(pathRelativeTo(fileName, "tests"))
23+
unsafeMemoryInput(data)
24+
else:
25+
memFileInput(fileName)
26+
27+
proc yCase(fileName, name: string): bool {.raises: [IOError].} =
28+
var stream = inputFile(fileName)
2229
var lex = init(JsonLexer, stream)
2330
var value: string
2431
lex.scanValue(value)
@@ -28,8 +35,8 @@ proc yCase(fileName, name: string): bool {.raises:[IOError].} =
2835
return false
2936
true
3037

31-
proc nCase(fileName, name: string): bool {.raises:[IOError].} =
32-
var stream = memFileInput(fileName)
38+
proc nCase(fileName, name: string): bool {.raises: [IOError].} =
39+
var stream = inputFile(fileName)
3340
var lex = init(JsonLexer, stream, {})
3441
var value: string
3542
lex.scanValue(value)
@@ -43,19 +50,33 @@ proc nCase(fileName, name: string): bool {.raises:[IOError].} =
4350
return false
4451
true
4552

46-
for fileName in walkDirRec(parsingPath):
47-
let (_, name) = fileName.splitPath()
48-
if name.startsWith("y_"):
49-
doAssert yCase(fileName, name)
50-
elif name.startsWith("n_"):
51-
doAssert nCase(fileName, name)
52-
# test cases starts with i_ are allowed to
53-
# fail or success depending on the implementation details
54-
elif name.startsWith("i_"):
53+
proc test() {.raises: [OSError, IOError].} =
54+
var checked = 0
55+
for fileName in walkDirRec(parsingPath):
56+
let (_, name) = fileName.splitPath()
57+
if name.startsWith("y_"):
58+
doAssert yCase(fileName, name)
59+
inc checked
60+
elif name.startsWith("n_"):
61+
doAssert nCase(fileName, name)
62+
inc checked
63+
# test cases starts with i_ are allowed to
64+
# fail or success depending on the implementation details
65+
elif name.startsWith("i_"):
66+
if name notin allowedToFail:
67+
doAssert yCase(fileName, name)
68+
inc checked
69+
70+
for fileName in walkDirRec(transformPath):
71+
let (_, name) = fileName.splitPath()
5572
if name notin allowedToFail:
5673
doAssert yCase(fileName, name)
74+
inc checked
75+
76+
doAssert checked == 328, $checked
5777

58-
for fileName in walkDirRec(transformPath):
59-
let (_, name) = fileName.splitPath()
60-
if name notin allowedToFail:
61-
doAssert yCase(fileName, name)
78+
static:
79+
test()
80+
echo "static ok"
81+
test()
82+
echo "ok"

tests/test_valueref.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func jsonBool(x: bool): JsonValueRef[uint64] =
1818
func jsonNull(): JsonValueRef[uint64] =
1919
JsonValueRef[uint64](kind: JsonValueKind.Null)
2020

21-
suite "Test JsonValueRef":
21+
template valueRefVars() {.dirty.} =
2222
let objA = JsonValueRef[uint64](
2323
kind: JsonValueKind.Object,
2424
objVal: [
@@ -105,7 +105,9 @@ suite "Test JsonValueRef":
105105
].toOrderedTable
106106
)
107107

108+
suite "Test JsonValueRef":
108109
test "Test table keys equality":
110+
valueRefVars()
109111
check objA != objAB
110112
check objA == objA2
111113
check objA != objABNull
@@ -122,6 +124,7 @@ suite "Test JsonValueRef":
122124
check objInObjAB != objInObjABNull
123125

124126
test "Test compare":
127+
valueRefVars()
125128
check compare(objA, objAB) == false
126129
check compare(objA, objA2) == true
127130
check compare(objA, objABNull) == true

tests/utils.nim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# those terms.
99

1010
import
11-
strutils
11+
strutils, os
1212

1313
# `dedent` exists in newer Nim version and doesn't behave the same
1414
func test_dedent*(s: string): string =
@@ -21,6 +21,12 @@ func test_dedent*(s: string): string =
2121
if indent < minIndent: minIndent = indent
2222
s.unindent(minIndent)
2323

24+
proc pathRelativeTo*(fileName, base: string): string =
25+
try:
26+
relativePath(fileName, base)
27+
except Exception as err:
28+
raise newException(Defect, err.msg)
29+
2430
const
2531
parsingPath* = "tests/test_vectors/test_parsing"
2632
transformPath* = "tests/test_vectors/test_transform"

0 commit comments

Comments
 (0)