-
Notifications
You must be signed in to change notification settings - Fork 3
/
test1.nim
70 lines (63 loc) · 2.23 KB
/
test1.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import unittest
import json
import options
import sequtils
import "../src/jsonschema"
jsonSchema:
CancelParams:
id?: int or string or float
something?: float
WrapsCancelParams:
cp: CancelParams
name: string
ExtendsCancelParams extends CancelParams:
name: string
WithArrayAndAny:
test?: CancelParams[]
ralph: int[] or float
bob: any
john?: int or nil
NameTest:
"method": string
"result": int
"if": bool
"type": float
suite "basic tests":
test "create and verify":
var wcp = create(WrapsCancelParams,
create(CancelParams, some(10), none(float)), "Hello"
)
check wcp.JsonNode.isValid(WrapsCancelParams) == true
var ecp = create(ExtendsCancelParams, some(10), some(5.3), "Hello")
check ecp.JsonNode.isValid(ExtendsCancelParams) == true
var war = create(WithArrayAndAny, some(@[
create(CancelParams, some(10), some(1.0)),
create(CancelParams, some("hello"), none(float))
]), 2.0, %*{"hello": "world"}, none(NilType))
check war.JsonNode.isValid(WithArrayAndAny) == true
test "edit and verify":
var wcp = create(WrapsCancelParams,
create(CancelParams, some(10), none(float)), "Hello"
)
check wcp.JsonNode.isValid(WrapsCancelParams) == true
wcp.JsonNode["cp"] = %*{"notcancelparams": true}
check wcp.JsonNode.isValid(WrapsCancelParams, false) == true
test "create and access":
var wcp = create(WrapsCancelParams,
create(CancelParams, some(10), none(float)), "Hello"
).JsonNode
# Not compile-time checked
check wcp["name"].getStr == "Hello"
# Compile-time checked access
check WrapsCancelParams(wcp)["name"].getStr == "Hello"
check CancelParams(WrapsCancelParams(wcp)["cp"])["something"] == none(JsonNode)
check CancelParams(WrapsCancelParams(wcp)["cp"])["id"].unsafeGet.getInt == 10
test "allowExtra parameter":
var wcp = create(WrapsCancelParams,
create(CancelParams, some(10), none(float)), "Hello"
).JsonNode
# Add an extra field and check that allowExtra allows it to pass
wcp["extraField"] = newJBool(true)
wcp["cp"]["extraField"] = newJBool(true)
check wcp.isValid(WrapsCancelParams) == false
check wcp.isValid(WrapsCancelParams, allowExtra = true) == true