github.com/stulluk/snapd@v0.0.0-20210611110309-f6d5d5bd24b0/spdx/parser_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package spdx_test
    21  
    22  import (
    23  	"testing"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/spdx"
    28  )
    29  
    30  func Test(t *testing.T) { TestingT(t) }
    31  
    32  type spdxSuite struct{}
    33  
    34  var _ = Suite(&spdxSuite{})
    35  
    36  func (s *spdxSuite) TestParseHappy(c *C) {
    37  	for _, t := range []string{
    38  		"GPL-2.0",
    39  		"GPL-2.0+",
    40  		"GPL-2.0 AND BSD-2-Clause",
    41  		"GPL-2.0 OR BSD-2-Clause",
    42  		"GPL-2.0 WITH GCC-exception-3.1",
    43  		"(GPL-2.0 AND BSD-2-Clause)",
    44  		"GPL-2.0 AND (BSD-2-Clause OR 0BSD)",
    45  		"(BSD-2-Clause OR 0BSD) AND GPL-2.0 WITH GCC-exception-3.1",
    46  		"((GPL-2.0 AND (BSD-2-Clause OR 0BSD)) OR GPL-3.0) ",
    47  	} {
    48  		err := spdx.ValidateLicense(t)
    49  		c.Check(err, IsNil, Commentf("input: %q", t))
    50  	}
    51  }
    52  
    53  func (s *spdxSuite) TestParseError(c *C) {
    54  	for _, t := range []struct {
    55  		inp    string
    56  		errStr string
    57  	}{
    58  		{"", "empty expression"},
    59  		{"GPL-2.0++", `unknown license: GPL-2.0\+\+`},
    60  		{"GPL-3.0 AND ()", "empty expression"},
    61  		{"()", "empty expression"},
    62  
    63  		{"FOO", `unknown license: FOO`},
    64  		{"GPL-3.0 xxx", `unexpected string: "xxx"`},
    65  		{"GPL-2.0 GPL-3.0", `missing AND or OR between "GPL-2.0" and "GPL-3.0"`},
    66  		{"(GPL-2.0))", `unexpected "\)"`},
    67  		{"(GPL-2.0", `expected "\)" got ""`},
    68  		{"OR", "missing license before OR"},
    69  		{"OR GPL-2.0", "missing license before OR"},
    70  		{"GPL-2.0 OR", "missing license after OR"},
    71  		{"GPL-2.0 WITH BAR", "unknown license exception: BAR"},
    72  		{"GPL-2.0 WITH (foo)", `"\(" not allowed after WITH`},
    73  		{"(BSD-2-Clause OR 0BSD) WITH GCC-exception-3.1", `expected license name before WITH`},
    74  	} {
    75  		err := spdx.ValidateLicense(t.inp)
    76  		c.Check(err, ErrorMatches, t.errStr, Commentf("input: %q", t.inp))
    77  	}
    78  }