|
| 1 | +package spdx |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRewriteIdentifiers(t *testing.T) { |
| 10 | + t.Parallel() |
| 11 | + |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + input string |
| 15 | + rewrite map[string]string |
| 16 | + want string |
| 17 | + identifiers []string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "license", |
| 21 | + input: "BSD-3-Clause", |
| 22 | + rewrite: map[string]string{"BSD-3-Clause": "bsd-new"}, |
| 23 | + want: "bsd-new", |
| 24 | + identifiers: []string{ |
| 25 | + "BSD-3-Clause", |
| 26 | + }, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "compound expression", |
| 30 | + input: "MIT OR Apache-2.0 AND BSD-3-Clause", |
| 31 | + rewrite: map[string]string{ |
| 32 | + "MIT": "mit", |
| 33 | + "Apache-2.0": "apache-2.0", |
| 34 | + "BSD-3-Clause": "bsd-new", |
| 35 | + }, |
| 36 | + want: "mit OR (apache-2.0 AND bsd-new)", |
| 37 | + identifiers: []string{ |
| 38 | + "MIT", |
| 39 | + "Apache-2.0", |
| 40 | + "BSD-3-Clause", |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "plus and exception", |
| 45 | + input: "GPL-2.0+ OR GPL-2.0-only WITH Classpath-exception-2.0", |
| 46 | + rewrite: map[string]string{ |
| 47 | + "GPL-2.0": "gpl-2.0", |
| 48 | + "GPL-2.0-only": "gpl-2.0", |
| 49 | + "Classpath-exception-2.0": "classpath-exception-2.0", |
| 50 | + }, |
| 51 | + want: "gpl-2.0+ OR (gpl-2.0 WITH classpath-exception-2.0)", |
| 52 | + identifiers: []string{ |
| 53 | + "GPL-2.0", |
| 54 | + "GPL-2.0-only", |
| 55 | + "Classpath-exception-2.0", |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "license references", |
| 60 | + input: "LicenseRef-scancode-mit AND DocumentRef-vendor:LicenseRef-custom", |
| 61 | + rewrite: map[string]string{ |
| 62 | + "LicenseRef-scancode-mit": "mit", |
| 63 | + "DocumentRef-vendor:LicenseRef-custom": "unknown-spdx", |
| 64 | + }, |
| 65 | + want: "mit AND unknown-spdx", |
| 66 | + identifiers: []string{ |
| 67 | + "LicenseRef-scancode-mit", |
| 68 | + "DocumentRef-vendor:LicenseRef-custom", |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "repeated identifier", |
| 73 | + input: "MIT AND MIT", |
| 74 | + rewrite: map[string]string{ |
| 75 | + "MIT": "mit", |
| 76 | + }, |
| 77 | + want: "mit AND mit", |
| 78 | + identifiers: []string{"MIT", "MIT"}, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, test := range tests { |
| 83 | + t.Run(test.name, func(t *testing.T) { |
| 84 | + t.Parallel() |
| 85 | + |
| 86 | + expression, err := ParseStrict(test.input) |
| 87 | + if err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + original := expression.String() |
| 91 | + var identifiers []string |
| 92 | + got := RewriteIdentifiers(expression, func(identifier string) string { |
| 93 | + identifiers = append(identifiers, identifier) |
| 94 | + return test.rewrite[identifier] |
| 95 | + }) |
| 96 | + if got != test.want { |
| 97 | + t.Errorf("RewriteIdentifiers(%q) = %q, want %q", test.input, got, test.want) |
| 98 | + } |
| 99 | + if !slices.Equal(identifiers, test.identifiers) { |
| 100 | + t.Errorf("identifiers = %q, want %q", identifiers, test.identifiers) |
| 101 | + } |
| 102 | + if expression.String() != original { |
| 103 | + t.Errorf("RewriteIdentifiers mutated expression: got %q, want %q", expression, original) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestRewriteIdentifiersSpecialValues(t *testing.T) { |
| 110 | + t.Parallel() |
| 111 | + |
| 112 | + for _, input := range []string{"NONE", "NOASSERTION"} { |
| 113 | + expression, err := ParseStrict(input) |
| 114 | + if err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + got := RewriteIdentifiers(expression, func(identifier string) string { |
| 118 | + t.Fatalf("rewrite called with %q", identifier) |
| 119 | + return "" |
| 120 | + }) |
| 121 | + if got != input { |
| 122 | + t.Errorf("RewriteIdentifiers(%q) = %q", input, got) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestRewriteIdentifiersNilFunction(t *testing.T) { |
| 128 | + t.Parallel() |
| 129 | + |
| 130 | + expression, err := ParseStrict("MIT OR Apache-2.0") |
| 131 | + if err != nil { |
| 132 | + t.Fatal(err) |
| 133 | + } |
| 134 | + if got := RewriteIdentifiers(expression, nil); got != expression.String() { |
| 135 | + t.Errorf("RewriteIdentifiers with nil function = %q", got) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func BenchmarkRewriteIdentifiers(b *testing.B) { |
| 140 | + expression, err := ParseStrict( |
| 141 | + "MIT OR GPL-2.0-only WITH Classpath-exception-2.0", |
| 142 | + ) |
| 143 | + if err != nil { |
| 144 | + b.Fatal(err) |
| 145 | + } |
| 146 | + |
| 147 | + b.ReportAllocs() |
| 148 | + for b.Loop() { |
| 149 | + RewriteIdentifiers(expression, strings.ToLower) |
| 150 | + } |
| 151 | +} |
0 commit comments