-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse_syntax_test.go
More file actions
162 lines (148 loc) · 3.83 KB
/
Copy pathparse_syntax_test.go
File metadata and controls
162 lines (148 loc) · 3.83 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
package spdx
import (
"errors"
"reflect"
"strings"
"testing"
)
func TestParseSyntaxAllowsUnknownIdentifiers(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
want string
err error
}{
{
name: "license",
input: "Future-License-9.9 OR MIT",
want: "Future-License-9.9 OR MIT",
err: ErrInvalidLicenseID,
},
{
name: "license with interior punctuation",
input: "Future.License-9.9 OR MIT",
want: "Future.License-9.9 OR MIT",
err: ErrInvalidLicenseID,
},
{
name: "exception",
input: "GPL-2.0-only WITH Future-exception-9.9",
want: "GPL-2.0-only WITH Future-exception-9.9",
err: ErrInvalidException,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
expression, err := ParseSyntax(test.input)
if err != nil {
t.Fatalf("ParseSyntax(%q): %v", test.input, err)
}
if got := expression.String(); got != test.want {
t.Errorf("ParseSyntax(%q) = %q, want %q", test.input, got, test.want)
}
_, err = ParseStrict(test.input)
if !errors.Is(err, test.err) {
t.Errorf("ParseStrict(%q) error = %v, want %v", test.input, err, test.err)
}
})
}
}
func TestParseSyntaxMatchesStrictAST(t *testing.T) {
t.Parallel()
inputs := []string{
"mit",
"MIT OR Apache-2.0 AND BSD-3-Clause",
"(MIT OR Apache-2.0) AND GPL-2.0-only",
"GPL-2.0-only WITH Classpath-exception-2.0",
"GPL-2.0+",
"LicenseRef-custom AND DocumentRef-vendor:LicenseRef-proprietary",
"LicenseRef-custom.1-2 AND DocumentRef-vendor.1:LicenseRef-proprietary-2",
"NONE",
"NOASSERTION",
}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
t.Parallel()
syntaxExpression, err := ParseSyntax(input)
if err != nil {
t.Fatalf("ParseSyntax(%q): %v", input, err)
}
strictExpression, err := ParseStrict(input)
if err != nil {
t.Fatalf("ParseStrict(%q): %v", input, err)
}
if !reflect.DeepEqual(syntaxExpression, strictExpression) {
t.Errorf(
"ASTs differ:\nParseSyntax: %#v\nParseStrict: %#v",
syntaxExpression,
strictExpression,
)
}
})
}
}
func TestParseSyntaxRejectsMalformedExpressions(t *testing.T) {
t.Parallel()
inputs := []string{
"",
"MIT OR",
"AND MIT",
"(MIT OR Apache-2.0",
"MIT OR Apache-2.0)",
"MIT WITH",
"MIT/Apache-2.0",
"Future_License-1.0",
"-MIT",
"MIT-",
".MIT",
"MIT.",
".",
"-",
"MIT WITH Future/exception",
"MIT WITH -Future-exception",
"MIT WITH Future-exception-",
"MIT WITH .",
"LicenseRef-",
"LicenseRef--",
"LicenseRef-.",
"LicenseRef--custom",
"LicenseRef-custom-",
"LicenseRef-.custom",
"LicenseRef-custom.",
"LicenseRef-invalid/value",
"DocumentRef-vendor",
"DocumentRef--:LicenseRef-custom",
"DocumentRef-.:LicenseRef-custom",
"DocumentRef--vendor:LicenseRef-custom",
"DocumentRef-vendor-:LicenseRef-custom",
"DocumentRef-:LicenseRef-custom",
"DocumentRef-vendor:LicenseRef-",
"DocumentRef-vendor:LicenseRef-invalid/value",
}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
t.Parallel()
if _, err := ParseSyntax(input); err == nil {
t.Errorf("ParseSyntax(%q) succeeded", input)
}
if _, err := ParseStrict(input); err == nil {
t.Errorf("ParseStrict(%q) succeeded", input)
}
})
}
}
func TestParseSyntaxEnforcesLimits(t *testing.T) {
t.Parallel()
oversized := strings.Repeat("X", maxParseLength+1)
if _, err := ParseSyntax(oversized); !errors.Is(err, ErrExpressionTooLarge) {
t.Errorf("oversized expression error = %v, want %v", err, ErrExpressionTooLarge)
}
deeplyNested := strings.Repeat("(", maxParseDepth+1) +
"Future-License-9.9" +
strings.Repeat(")", maxParseDepth+1)
if _, err := ParseSyntax(deeplyNested); !errors.Is(err, ErrExpressionTooLarge) {
t.Errorf("deeply nested expression error = %v, want %v", err, ErrExpressionTooLarge)
}
}