| 1 | package spdxexp |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/assert" |
| 7 | ) |
| 8 | |
| 9 | func TestCompareGT(t *testing.T) { |
| 10 | tests := []struct { |
| 11 | name string |
| 12 | first *node |
| 13 | second *node |
| 14 | result bool |
| 15 | }{ |
| 16 | {"expect greater than: GPL-3.0 > GPL-2.0", getLicenseNode("GPL-3.0", false), getLicenseNode("GPL-2.0", false), true}, |
| 17 | {"expect greater than: GPL-3.0-only > GPL-2.0-only", getLicenseNode("GPL-3.0-only", false), getLicenseNode("GPL-2.0-only", false), true}, |
| 18 | {"expect greater than: LPPL-1.3a > LPPL-1.0", getLicenseNode("LPPL-1.3a", false), getLicenseNode("LPPL-1.0", false), true}, |
| 19 | {"expect greater than: LPPL-1.3c > LPPL-1.3a", getLicenseNode("LPPL-1.3c", false), getLicenseNode("LPPL-1.3a", false), true}, |
| 20 | {"expect greater than: AGPL-3.0 > AGPL-1.0", getLicenseNode("AGPL-3.0", false), getLicenseNode("AGPL-1.0", false), true}, |
| 21 | {"expect equal: GPL-2.0-or-later > GPL-2.0-only", getLicenseNode("GPL-2.0-or-later", true), getLicenseNode("GPL-2.0-only", false), false}, |
| 22 | {"expect equal: GPL-2.0-or-later > GPL-2.0", getLicenseNode("GPL-2.0-or-later", true), getLicenseNode("GPL-2.0", false), false}, |
| 23 | {"expect equal: GPL-2.0-only > GPL-2.0", getLicenseNode("GPL-2.0-only", false), getLicenseNode("GPL-2.0", false), false}, |
| 24 | {"expect equal: GPL-3.0 > GPL-3.0", getLicenseNode("GPL-3.0", false), getLicenseNode("GPL-3.0", false), false}, |
| 25 | {"expect equal: MIT > MIT", getLicenseNode("MIT", false), getLicenseNode("MIT", false), false}, |
| 26 | {"expect less than: MPL-1.0 > MPL-2.0", getLicenseNode("MPL-1.0", false), getLicenseNode("MPL-2.0", false), false}, |
| 27 | {"incompatible: MIT > ISC", getLicenseNode("MIT", false), getLicenseNode("ISC", false), false}, |
| 28 | {"incompatible: MIT > GPL-2.0-only", getLicenseNode("MIT", false), getLicenseNode("GPL-2.0-only", false), false}, |
| 29 | {"incompatible: OSL-1.0 > OPL-1.0", getLicenseNode("OSL-1.0", false), getLicenseNode("OPL-1.0", false), false}, |
| 30 | {"not simple license: (MIT OR ISC) > GPL-3.0", getLicenseNode("(MIT OR ISC)", false), getLicenseNode("GPL-3.0", false), false}, // TODO: should it raise error? |
| 31 | } |
| 32 | |
| 33 | for _, test := range tests { |
| 34 | test := test |
| 35 | t.Run(test.name, func(t *testing.T) { |
| 36 | assert.Equal(t, test.result, compareGT(test.first, test.second)) |
| 37 | }) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestCompareEQ(t *testing.T) { |
| 42 | tests := []struct { |
| 43 | name string |
| 44 | first *node |
| 45 | second *node |
| 46 | result bool |
| 47 | }{ |
| 48 | {"expect greater than: GPL-3.0 == GPL-2.0", getLicenseNode("GPL-3.0", false), getLicenseNode("GPL-2.0", false), false}, |
| 49 | {"expect greater than: GPL-3.0-only == GPL-2.0-only", getLicenseNode("GPL-3.0-only", false), getLicenseNode("GPL-2.0-only", false), false}, |
| 50 | {"expect greater than: LPPL-1.3a == LPPL-1.0", getLicenseNode("LPPL-1.3a", false), getLicenseNode("LPPL-1.0", false), false}, |
| 51 | {"expect greater than: LPPL-1.3c == LPPL-1.3a", getLicenseNode("LPPL-1.3c", false), getLicenseNode("LPPL-1.3a", false), false}, |
| 52 | {"expect greater than: AGPL-3.0 == AGPL-1.0", getLicenseNode("AGPL-3.0", false), getLicenseNode("AGPL-1.0", false), false}, |
| 53 | {"expect equal: GPL-2.0-or-later > GPL-2.0-only", getLicenseNode("GPL-2.0-or-later", true), getLicenseNode("GPL-2.0-only", false), true}, |
| 54 | {"expect equal: GPL-2.0-or-later > GPL-2.0", getLicenseNode("GPL-2.0-or-later", true), getLicenseNode("GPL-2.0", false), true}, |
| 55 | {"expect equal: GPL-2.0-only == GPL-2.0", getLicenseNode("GPL-2.0-only", false), getLicenseNode("GPL-2.0", false), true}, |
| 56 | {"expect equal: GPL-3.0 == GPL-3.0", getLicenseNode("GPL-3.0", false), getLicenseNode("GPL-3.0", false), true}, |
| 57 | {"expect equal: MIT == MIT", getLicenseNode("MIT", false), getLicenseNode("MIT", false), true}, |
| 58 | {"expect less than: MPL-1.0 == MPL-2.0", getLicenseNode("MPL-1.0", false), getLicenseNode("MPL-2.0", false), false}, |
| 59 | {"incompatible: MIT == ISC", getLicenseNode("MIT", false), getLicenseNode("ISC", false), false}, |
| 60 | {"incompatible: MIT == GPL-2.0-only", getLicenseNode("MIT", false), getLicenseNode("GPL-2.0-only", false), false}, |
| 61 | {"incompatible: OSL-1.0 == OPL-1.0", getLicenseNode("OSL-1.0", false), getLicenseNode("OPL-1.0", false), false}, |
| 62 | {"not simple license: (MIT OR ISC) == GPL-3.0", getLicenseNode("(MIT OR ISC)", false), getLicenseNode("GPL-3.0", false), false}, // TODO: should it raise error? |
| 63 | } |
| 64 | |
| 65 | for _, test := range tests { |
| 66 | test := test |
| 67 | t.Run(test.name, func(t *testing.T) { |
| 68 | assert.Equal(t, test.result, compareEQ(test.first, test.second)) |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestCompareLT(t *testing.T) { |
| 74 | tests := []struct { |
| 75 | name string |
| 76 | first *node |
| 77 | second *node |
| 78 | result bool |
| 79 | }{ |
| 80 | {"expect greater than: GPL-3.0 < GPL-2.0", getLicenseNode("GPL-3.0", false), getLicenseNode("GPL-2.0", false), false}, |
| 81 | {"expect greater than: GPL-3.0-only < GPL-2.0-only", getLicenseNode("GPL-3.0-only", false), getLicenseNode("GPL-2.0-only", false), false}, |
| 82 | {"expect greater than: LPPL-1.3a < LPPL-1.0", getLicenseNode("LPPL-1.3a", false), getLicenseNode("LPPL-1.0", false), false}, |
| 83 | {"expect greater than: LPPL-1.3c < LPPL-1.3a", getLicenseNode("LPPL-1.3c", false), getLicenseNode("LPPL-1.3a", false), false}, |
| 84 | {"expect greater than: AGPL-3.0 < AGPL-1.0", getLicenseNode("AGPL-3.0", false), getLicenseNode("AGPL-1.0", false), false}, |
| 85 | {"expect greater than: GPL-2.0-or-later < GPL-2.0-only", getLicenseNode("GPL-2.0-or-later", true), getLicenseNode("GPL-2.0-only", false), false}, |
| 86 | {"expect greater than: GPL-2.0-or-later == GPL-2.0", getLicenseNode("GPL-2.0-or-later", true), getLicenseNode("GPL-2.0", false), false}, |
| 87 | {"expect equal: GPL-2.0-only < GPL-2.0", getLicenseNode("GPL-2.0-only", false), getLicenseNode("GPL-2.0", false), false}, |
| 88 | {"expect equal: GPL-3.0 < GPL-3.0", getLicenseNode("GPL-3.0", false), getLicenseNode("GPL-3.0", false), false}, |
| 89 | {"expect equal: MIT < MIT", getLicenseNode("MIT", false), getLicenseNode("MIT", false), false}, |
| 90 | {"expect less than: MPL-1.0 < MPL-2.0", getLicenseNode("MPL-1.0", false), getLicenseNode("MPL-2.0", false), true}, |
| 91 | {"incompatible: MIT < ISC", getLicenseNode("MIT", false), getLicenseNode("ISC", false), false}, |
| 92 | {"incompatible: MIT < GPL-2.0-only", getLicenseNode("MIT", false), getLicenseNode("GPL-2.0-only", false), false}, |
| 93 | {"incompatible: OSL-1.0 < OPL-1.0", getLicenseNode("OSL-1.0", false), getLicenseNode("OPL-1.0", false), false}, |
| 94 | {"not simple license: (MIT OR ISC) < GPL-3.0", getLicenseNode("(MIT OR ISC)", false), getLicenseNode("GPL-3.0", false), false}, // TODO: should it raise error? |
| 95 | } |
| 96 | |
| 97 | for _, test := range tests { |
| 98 | test := test |
| 99 | t.Run(test.name, func(t *testing.T) { |
| 100 | assert.Equal(t, test.result, compareLT(test.first, test.second)) |
| 101 | }) |
| 102 | } |
| 103 | } |