-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrewrite_test.go
More file actions
151 lines (140 loc) · 3.56 KB
/
Copy pathrewrite_test.go
File metadata and controls
151 lines (140 loc) · 3.56 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
package spdx
import (
"slices"
"strings"
"testing"
)
func TestRewriteIdentifiers(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
rewrite map[string]string
want string
identifiers []string
}{
{
name: "license",
input: "BSD-3-Clause",
rewrite: map[string]string{"BSD-3-Clause": "bsd-new"},
want: "bsd-new",
identifiers: []string{
"BSD-3-Clause",
},
},
{
name: "compound expression",
input: "MIT OR Apache-2.0 AND BSD-3-Clause",
rewrite: map[string]string{
"MIT": "mit",
"Apache-2.0": "apache-2.0",
"BSD-3-Clause": "bsd-new",
},
want: "mit OR (apache-2.0 AND bsd-new)",
identifiers: []string{
"MIT",
"Apache-2.0",
"BSD-3-Clause",
},
},
{
name: "plus and exception",
input: "GPL-2.0+ OR GPL-2.0-only WITH Classpath-exception-2.0",
rewrite: map[string]string{
"GPL-2.0": "gpl-2.0",
"GPL-2.0-only": "gpl-2.0",
"Classpath-exception-2.0": "classpath-exception-2.0",
},
want: "gpl-2.0+ OR (gpl-2.0 WITH classpath-exception-2.0)",
identifiers: []string{
"GPL-2.0",
"GPL-2.0-only",
"Classpath-exception-2.0",
},
},
{
name: "license references",
input: "LicenseRef-scancode-mit AND DocumentRef-vendor:LicenseRef-custom",
rewrite: map[string]string{
"LicenseRef-scancode-mit": "mit",
"DocumentRef-vendor:LicenseRef-custom": "unknown-spdx",
},
want: "mit AND unknown-spdx",
identifiers: []string{
"LicenseRef-scancode-mit",
"DocumentRef-vendor:LicenseRef-custom",
},
},
{
name: "repeated identifier",
input: "MIT AND MIT",
rewrite: map[string]string{
"MIT": "mit",
},
want: "mit AND mit",
identifiers: []string{"MIT", "MIT"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
expression, err := ParseStrict(test.input)
if err != nil {
t.Fatal(err)
}
original := expression.String()
var identifiers []string
got := RewriteIdentifiers(expression, func(identifier string) string {
identifiers = append(identifiers, identifier)
return test.rewrite[identifier]
})
if got != test.want {
t.Errorf("RewriteIdentifiers(%q) = %q, want %q", test.input, got, test.want)
}
if !slices.Equal(identifiers, test.identifiers) {
t.Errorf("identifiers = %q, want %q", identifiers, test.identifiers)
}
if expression.String() != original {
t.Errorf("RewriteIdentifiers mutated expression: got %q, want %q", expression, original)
}
})
}
}
func TestRewriteIdentifiersSpecialValues(t *testing.T) {
t.Parallel()
for _, input := range []string{"NONE", "NOASSERTION"} {
expression, err := ParseStrict(input)
if err != nil {
t.Fatal(err)
}
got := RewriteIdentifiers(expression, func(identifier string) string {
t.Fatalf("rewrite called with %q", identifier)
return ""
})
if got != input {
t.Errorf("RewriteIdentifiers(%q) = %q", input, got)
}
}
}
func TestRewriteIdentifiersNilFunction(t *testing.T) {
t.Parallel()
expression, err := ParseStrict("MIT OR Apache-2.0")
if err != nil {
t.Fatal(err)
}
if got := RewriteIdentifiers(expression, nil); got != expression.String() {
t.Errorf("RewriteIdentifiers with nil function = %q", got)
}
}
func BenchmarkRewriteIdentifiers(b *testing.B) {
expression, err := ParseStrict(
"MIT OR GPL-2.0-only WITH Classpath-exception-2.0",
)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
for b.Loop() {
RewriteIdentifiers(expression, strings.ToLower)
}
}