-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrewrite_stream_test.go
More file actions
79 lines (75 loc) · 2.15 KB
/
Copy pathrewrite_stream_test.go
File metadata and controls
79 lines (75 loc) · 2.15 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
package spdx
import (
"fmt"
"strings"
"testing"
)
func cloneRewritten(expression Expression, rewrite func(string) string) Expression {
switch e := expression.(type) {
case *License:
out := &License{ID: rewrite(e.ID), Plus: e.Plus}
if e.Exception != "" {
out.Exception = rewrite(e.Exception)
}
return out
case *LicenseRef:
return &License{ID: rewrite(e.String())}
case *AndExpression:
return &AndExpression{Left: cloneRewritten(e.Left, rewrite), Right: cloneRewritten(e.Right, rewrite)}
case *OrExpression:
return &OrExpression{Left: cloneRewritten(e.Left, rewrite), Right: cloneRewritten(e.Right, rewrite)}
case *SpecialValue:
return &SpecialValue{Value: e.Value}
default:
panic("unexpected expression type")
}
}
func FuzzRewriteMatchesClone(f *testing.F) {
for _, input := range []string{"MIT", "MIT OR Apache-2.0 AND ISC", "GPL-2.0+ OR GPL-2.0 WITH Classpath-exception-2.0", "LicenseRef-custom", "DocumentRef-x:LicenseRef-y AND (MIT OR ISC)", "NONE"} {
f.Add(input, false)
f.Add(input, true)
}
f.Fuzz(func(t *testing.T, input string, erase bool) {
if len(input) > 4096 {
t.Skip()
}
expression, err := ParseSyntax(input)
if err != nil {
return
}
original := expression.String()
rewrite := func(id string) string {
if erase && strings.Contains(id, "exception") {
return ""
}
return strings.ToLower(id)
}
want := cloneRewritten(expression, rewrite).String()
if got := RewriteIdentifiers(expression, rewrite); got != want {
t.Fatalf("%q: got %q, want %q", input, got, want)
}
if expression.String() != original {
t.Fatal("input expression mutated")
}
})
}
func BenchmarkRewriteSize(b *testing.B) {
for _, size := range []int{1, 4, 32, 128} {
expression, err := ParseSyntax(strings.TrimSuffix(strings.Repeat("MIT OR ", size), " OR "))
if err != nil {
b.Fatal(err)
}
for _, clone := range []bool{false, true} {
b.Run(fmt.Sprintf("%d/clone=%t", size, clone), func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
if clone {
_ = cloneRewritten(expression, strings.ToLower).String()
} else {
_ = RewriteIdentifiers(expression, strings.ToLower)
}
}
})
}
}
}