-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompliance_test.go
More file actions
135 lines (123 loc) · 3.29 KB
/
compliance_test.go
File metadata and controls
135 lines (123 loc) · 3.29 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package piml
import (
"encoding/json"
"os"
"reflect"
"testing"
)
type ComplianceTestCase struct {
Name string `json:"name"`
Piml string `json:"piml"`
Json json.RawMessage `json:"json"`
Note string `json:"note"`
}
func TestCompliance(t *testing.T) {
data, err := os.ReadFile("../piml/tests/compliance.json")
if err != nil {
t.Fatalf("Failed to read compliance.json: %v", err)
}
var tests []ComplianceTestCase
if err := json.Unmarshal(data, &tests); err != nil {
t.Fatalf("Failed to parse compliance.json: %v", err)
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
var v interface{}
var err error
switch tc.Name {
case "Simple String":
s := struct {
Key string `piml:"key" json:"key"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
case "Empty Array":
s := struct {
List []string `piml:"list" json:"list"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
case "Empty Object":
s := struct {
Map map[string]string `piml:"map" json:"map"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
case "Integer":
s := struct {
Val int `piml:"val" json:"val"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
case "String looking like int":
s := struct {
Val int `piml:"val" json:"val"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
case "Nested List":
s := struct {
List []string `piml:"list" json:"list"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
case "Nested Object":
type Child struct {
Child string `piml:"child" json:"child"`
}
s := struct {
Parent Child `piml:"parent" json:"parent"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
case "List of Objects":
type Item struct {
Name string `piml:"name" json:"name"`
}
s := struct {
Users []Item `piml:"users" json:"users"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
case "Multiline String", "Multiline String with Blank Lines":
s := struct {
Desc string `piml:"desc" json:"desc"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
case "Keys with spaces":
s := struct {
MyKey string `piml:"my key" json:"my key"`
}{}
err = Unmarshal([]byte(tc.Piml), &s)
v = s
default:
t.Fatalf("Unknown test case: %s", tc.Name)
}
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
// Marshal Go struct to JSON
gotJSONBytes, err := json.Marshal(v)
if err != nil {
t.Fatalf("JSON Marshal failed: %v", err)
}
// Normalize JSONs (compact)
var expectedObj interface{}
if err := json.Unmarshal(tc.Json, &expectedObj); err != nil {
t.Fatalf("Failed to parse expected JSON: %v", err)
}
// Re-marshal to get canonical JSON string (to match gotJSONBytes)
expectedJSONBytes, _ := json.Marshal(expectedObj)
// We need to decode gotJSONBytes back to object to compare generic structure
// because field order might differ, etc.
var gotObj interface{}
if err := json.Unmarshal(gotJSONBytes, &gotObj); err != nil {
t.Fatalf("Failed to parse got JSON: %v", err)
}
if !reflect.DeepEqual(gotObj, expectedObj) {
t.Errorf("Mismatch!\nExpected: %s\nGot: %s", string(expectedJSONBytes), string(gotJSONBytes))
}
})
}
}