-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconformance_test.go
More file actions
215 lines (186 loc) · 5.64 KB
/
Copy pathconformance_test.go
File metadata and controls
215 lines (186 loc) · 5.64 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package vers
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
type versTestFile struct {
Tests []versTestCase `json:"tests"`
}
type versTestCase struct {
Description string `json:"description"`
TestGroup string `json:"test_group"`
TestType string `json:"test_type"`
Input json.RawMessage `json:"input"`
ExpectedOutput json.RawMessage `json:"expected_output"`
}
type fromNativeInput struct {
NativeRange string `json:"native_range"`
Scheme string `json:"scheme"`
}
type containmentInput struct {
Vers string `json:"vers"`
Version string `json:"version"`
}
type versionCmpInput struct {
InputScheme string `json:"input_scheme"`
Versions []string `json:"versions"`
}
func loadTestFile(t *testing.T, filename string) *versTestFile {
t.Helper()
path := filepath.Join("testdata", "vers-spec", "tests", filename)
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read test file %s: %v", filename, err)
}
var tf versTestFile
if err := json.Unmarshal(data, &tf); err != nil {
t.Fatalf("failed to parse test file %s: %v", filename, err)
}
return &tf
}
func TestConformance_FromNative(t *testing.T) {
files := []string{
"gem_range_from_native_test.json",
"npm_range_from_native_test.json",
"pypi_range_from_native_test.json",
"nuget_range_from_native_test.json",
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
tf := loadTestFile(t, file)
for _, tc := range tf.Tests {
if tc.TestType != "from_native" {
continue
}
var input fromNativeInput
if err := json.Unmarshal(tc.Input, &input); err != nil {
t.Errorf("failed to parse input: %v", err)
continue
}
var expected string
if err := json.Unmarshal(tc.ExpectedOutput, &expected); err != nil {
t.Errorf("failed to parse expected output: %v", err)
continue
}
t.Run(input.NativeRange, func(t *testing.T) {
r, err := ParseNative(input.NativeRange, input.Scheme)
if err != nil {
t.Errorf("ParseNative(%q, %q) error: %v", input.NativeRange, input.Scheme, err)
return
}
got := ToVersString(r, input.Scheme)
if got != expected {
t.Errorf("ParseNative(%q, %q) = %q, want %q", input.NativeRange, input.Scheme, got, expected)
}
})
}
})
}
}
func TestConformance_Containment(t *testing.T) {
files := []string{
"npm_range_containment_test.json",
"pypi_range_containment_test.json",
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
tf := loadTestFile(t, file)
for _, tc := range tf.Tests {
if tc.TestType != "containment" {
continue
}
var input containmentInput
if err := json.Unmarshal(tc.Input, &input); err != nil {
t.Errorf("failed to parse input: %v", err)
continue
}
var expected bool
if err := json.Unmarshal(tc.ExpectedOutput, &expected); err != nil {
t.Errorf("failed to parse expected output: %v", err)
continue
}
t.Run(input.Vers+"_"+input.Version, func(t *testing.T) {
r, err := Parse(input.Vers)
if err != nil {
t.Errorf("Parse(%q) error: %v", input.Vers, err)
return
}
got := r.Contains(input.Version)
if got != expected {
t.Errorf("Parse(%q).Contains(%q) = %v, want %v", input.Vers, input.Version, got, expected)
}
})
}
})
}
}
//nolint:gocognit
func TestConformance_VersionComparison(t *testing.T) {
files := []string{
"nuget_version_cmp_test.json",
"maven_version_cmp_test.json",
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
tf := loadTestFile(t, file)
for _, tc := range tf.Tests {
var input versionCmpInput
if err := json.Unmarshal(tc.Input, &input); err != nil {
t.Errorf("failed to parse input: %v", err)
continue
}
if len(input.Versions) != 2 {
t.Errorf("expected 2 versions, got %d", len(input.Versions))
continue
}
v1, v2 := input.Versions[0], input.Versions[1]
switch tc.TestType {
case "equality":
var expected bool
if err := json.Unmarshal(tc.ExpectedOutput, &expected); err != nil {
t.Errorf("failed to parse expected output: %v", err)
continue
}
t.Run("eq_"+v1+"_"+v2, func(t *testing.T) {
cmp := CompareWithScheme(v1, v2, input.InputScheme)
got := cmp == 0
if got != expected {
t.Errorf("CompareWithScheme(%q, %q, %q) == 0 is %v, want %v (cmp=%d)", v1, v2, input.InputScheme, got, expected, cmp)
}
})
case "comparison":
var expected []string
if err := json.Unmarshal(tc.ExpectedOutput, &expected); err != nil {
t.Errorf("failed to parse expected output: %v", err)
continue
}
if len(expected) != 2 {
t.Errorf("expected 2 versions in output, got %d", len(expected))
continue
}
t.Run("cmp_"+v1+"_"+v2, func(t *testing.T) {
cmp := CompareWithScheme(v1, v2, input.InputScheme)
v1MatchesFirst := CompareWithScheme(v1, expected[0], input.InputScheme) == 0
versionsEqual := expected[0] == expected[1] || CompareWithScheme(expected[0], expected[1], input.InputScheme) == 0
switch {
case versionsEqual:
if cmp != 0 {
t.Errorf("CompareWithScheme(%q, %q, %q) = %d, want 0 (equal versions)", v1, v2, input.InputScheme, cmp)
}
case v1MatchesFirst:
if cmp >= 0 {
t.Errorf("CompareWithScheme(%q, %q, %q) = %d, want < 0 (expected order: %v)", v1, v2, input.InputScheme, cmp, expected)
}
default:
if cmp <= 0 {
t.Errorf("CompareWithScheme(%q, %q, %q) = %d, want > 0 (expected order: %v)", v1, v2, input.InputScheme, cmp, expected)
}
}
})
}
}
})
}
}