refactor(license): use goyacc for license parser (#3824)

This commit is contained in:
Teppei Fukuda
2023-03-14 09:27:17 +02:00
committed by GitHub
parent 00c763bc10
commit 2bb25e766b
22 changed files with 1493 additions and 705 deletions

View File

@@ -1,83 +1,56 @@
package expression
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNormalizeForSPDX(t *testing.T) {
func TestNormalize(t *testing.T) {
tests := []struct {
name string
license string
fn NormalizeFunc
want string
wantErr string
}{
{
name: "happy path",
name: "SPDX, space",
license: "AFL 2.0",
fn: NormalizeForSPDX,
want: "AFL-2.0",
},
{
name: "happy path with WITH section",
name: "SPDX, exception",
license: "AFL 2.0 with Linux-syscall-note exception",
fn: NormalizeForSPDX,
want: "AFL-2.0 WITH Linux-syscall-note-exception",
},
{
name: "SPDX, invalid chars",
license: "LGPL_2.1_only or MIT OR BSD-3>Clause",
fn: NormalizeForSPDX,
want: "LGPL-2.1-only OR MIT OR BSD-3-Clause",
},
{
name: "upper",
license: "LGPL-2.1-only OR MIT",
fn: strings.ToUpper,
want: "LGPL-2.1-ONLY OR MIT",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, NormalizeForSPDX(tt.license), "NormalizeWithExpression(%v)", tt.license)
})
}
}
got, err := Normalize(tt.license, tt.fn)
if tt.wantErr != "" {
assert.ErrorContains(t, err, tt.wantErr)
return
}
func TestJoin(t *testing.T) {
tests := []struct {
name string
inputElements []string
inputOperator Operator
expect string
}{
{
name: "happy path single license",
inputElements: []string{"MIT"},
inputOperator: AND,
expect: "MIT",
},
{
name: "happy path multi license",
inputElements: []string{"MIT", "GPL1.0"},
inputOperator: AND,
expect: "MIT AND GPL1.0",
},
{
name: "happy path multi license with AND operator",
inputElements: []string{"MIT", "GPL1.0 AND GPL2.0"},
inputOperator: AND,
expect: "MIT AND GPL1.0 AND GPL2.0",
},
{
name: "happy path multi license with OR operator",
inputElements: []string{"MIT", "GPL1.0 OR GPL2.0"},
inputOperator: OR,
expect: "MIT OR GPL1.0 OR GPL2.0",
},
{
name: "happy path multi license with OR operator, separator AND",
inputElements: []string{"MIT", "GPL1.0 OR GPL2.0"},
inputOperator: AND,
expect: "MIT AND (GPL1.0 OR GPL2.0)",
},
{
name: "happy path multi license with AND operator, separator OR",
inputElements: []string{"MIT", "GPL1.0 AND GPL2.0"},
inputOperator: OR,
expect: "MIT OR (GPL1.0 AND GPL2.0)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Join(tt.inputElements, tt.inputOperator)
assert.Equal(t, tt.expect, got)
require.NoError(t, err)
assert.Equalf(t, tt.want, got, "NormalizeWithExpression(%v)", tt.license)
})
}
}