v2 / vlib / x / json2 / strict / strict_test.v
48 lines · 41 sloc · 869 bytes · c51d30bf5309653c6b573ec815268e69a78ea8cc
Raw
1import x.json2.strict
2
3struct StructType[T] {
4mut:
5 val T
6}
7
8struct StructTypeAndOptionType[T] {
9mut:
10 val T
11 option_val ?T
12}
13
14fn test_get_keys_from_json() {
15 json_data := r'
16 {
17 "val": 0,
18 "val1": {"val": 63}
19 }
20 '
21
22 key_structs := strict.get_keys_from_json(strict.tokenize(json_data))
23
24 assert key_structs == [
25 strict.KeyStruct{
26 key: 'val'
27 value_type: .literal
28 token_pos: 1
29 },
30 strict.KeyStruct{
31 key: 'val1'
32 value_type: .map
33 token_pos: 5
34 },
35 ]
36}
37
38fn test_strict_check() {
39 assert strict.strict_check[StructTypeAndOptionType[string]]('{"val": "","val": ""}') == strict.StructCheckResult{
40 duplicates: ['val']
41 superfluous: []
42 }
43
44 assert strict.strict_check[StructTypeAndOptionType[string]]('{"val": "","val2": ""}') == strict.StructCheckResult{
45 duplicates: []
46 superfluous: ['val2']
47 }
48}
49