-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchanges_test.go
More file actions
173 lines (158 loc) · 4.08 KB
/
changes_test.go
File metadata and controls
173 lines (158 loc) · 4.08 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
openrpc "github.com/vmkteam/meta-schema/v2"
"testing"
)
func TestNewDiff(t *testing.T) {
diff, err := NewDiff("testdata/openrpc_old.json", "testdata/openrpc_new.json", Options{ShowMeta: true})
if err != nil {
t.Fatalf("new diff error: %s", err)
}
changesMap := map[CriticalityLevel][]Change{
Breaking: {},
Dangerous: {},
NonBreaking: {},
}
for _, change := range diff.Changes {
changesMap[change.Criticality] = append(changesMap[change.Criticality], change)
}
if diff.Criticality != Breaking {
t.Fatalf("diff.Criticality = %v, wanted %v", diff.Criticality, Breaking)
}
if len(diff.Changes) != 18 {
t.Fatalf("len(diff.Changes) = %v, wanted %v", len(diff.Changes), 17)
}
if len(changesMap[Breaking]) != 7 {
t.Fatalf("len %s changes = %v, wanted %v", Breaking, len(changesMap[Breaking]), 7)
}
if len(changesMap[Dangerous]) != 1 {
t.Fatalf("len %s changes = %v, wanted %v", Dangerous, len(changesMap[Dangerous]), 7)
}
if len(changesMap[NonBreaking]) != 10 {
t.Fatalf("len %s changes = %v, wanted %v", NonBreaking, len(changesMap[NonBreaking]), 7)
}
fmt.Println(diff.String())
}
func Test_detectRequiredInput(t *testing.T) {
doc := &openrpc.OpenrpcDocument{
Methods: []openrpc.MethodOrReference{{
MethodObject: &openrpc.MethodObject{
Name: "method",
Params: []openrpc.ContentDescriptorOrReference{
{
ContentDescriptorObject: &openrpc.ContentDescriptorObject{
Name: "node",
Schema: &openrpc.JSONSchema{
JSONSchemaObject: &openrpc.JSONSchemaObject{
Ref: "#/components/schemas/Node",
},
},
Required: false,
},
},
{
ContentDescriptorObject: &openrpc.ContentDescriptorObject{
Name: "parent",
Schema: &openrpc.JSONSchema{
JSONSchemaObject: &openrpc.JSONSchemaObject{
Ref: "#/components/schemas/Parent",
},
},
Required: false,
},
},
},
}},
},
Components: &openrpc.Components{
Schemas: &openrpc.SchemaMap{
{
JSONSchemaObject: &openrpc.JSONSchemaObject{
Id: "Node",
Title: "Node",
Required: []string{},
Properties: &openrpc.SchemaMap{
{
JSONSchemaObject: &openrpc.JSONSchemaObject{
Id: "node",
Title: "node",
Ref: "#/components/schemas/Node",
},
},
{
JSONSchemaObject: &openrpc.JSONSchemaObject{
Id: "parent",
Title: "parent",
Ref: "#/components/schemas/Parent",
},
},
},
},
},
{
JSONSchemaObject: &openrpc.JSONSchemaObject{
Id: "Parent",
Title: "Parent",
Required: []string{
"node",
},
Properties: &openrpc.SchemaMap{
{
JSONSchemaObject: &openrpc.JSONSchemaObject{
Id: "node",
Title: "node",
Ref: "#/components/schemas/Node",
},
},
},
},
},
},
},
}
isInput := detectRequiredInput("Node", doc, []string{}, 0)
if isInput == false {
t.Fatalf("isInput must be true")
}
}
func Test_matchPath(t *testing.T) {
tests := []struct {
name string
path []string
pattern string
want bool
}{
{
name: "should match pattern 1",
path: []string{"methods", "check.AddRequiredParam", "params", "param2"},
pattern: "methods.*.params",
want: true,
},
{
name: "should match pattern 2",
path: []string{"methods", "check.AddRequiredParam", "params", "param2"},
pattern: "methods",
want: true,
},
{
name: "should not match pattern 1",
path: []string{"methods", "check.AddRequiredParam", "params", "param2"},
pattern: "methods.methods",
want: false,
},
{
name: "should not match pattern 2",
path: []string{"methods", "check.AddRequiredParam", "params", "param2"},
pattern: "methods.*.*.*.*.*",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := matchPath(tt.path, tt.pattern); got != tt.want {
t.Errorf("matchPath() = %v, want %v", got, tt.want)
}
})
}
}