|
| 1 | +// |
| 2 | +// Copyright 2018 Cristian Maglie. All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | +// |
| 6 | + |
| 7 | +package semver |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRelaxedVersionComparator(t *testing.T) { |
| 17 | + sign := map[int]string{1: ">", 0: "=", -1: "<"} |
| 18 | + ascending := func(list ...*RelaxedVersion) { |
| 19 | + for i := range list[0 : len(list)-1] { |
| 20 | + a := list[i] |
| 21 | + b := list[i+1] |
| 22 | + comp := a.CompareTo(b) |
| 23 | + fmt.Printf("%s %s %s\n", a, sign[comp], b) |
| 24 | + require.Equal(t, comp, -1) |
| 25 | + require.True(t, a.LessThan(b)) |
| 26 | + require.True(t, a.LessThanOrEqual(b)) |
| 27 | + require.False(t, a.Equal(b)) |
| 28 | + require.False(t, a.GreaterThanOrEqual(b)) |
| 29 | + require.False(t, a.GreaterThan(b)) |
| 30 | + |
| 31 | + comp = b.CompareTo(a) |
| 32 | + fmt.Printf("%s %s %s\n", b, sign[comp], a) |
| 33 | + require.Equal(t, comp, 1) |
| 34 | + require.False(t, b.LessThan(a)) |
| 35 | + require.False(t, b.LessThanOrEqual(a)) |
| 36 | + require.False(t, b.Equal(a)) |
| 37 | + require.True(t, b.GreaterThanOrEqual(a)) |
| 38 | + require.True(t, b.GreaterThan(a)) |
| 39 | + } |
| 40 | + } |
| 41 | + equal := func(list ...*RelaxedVersion) { |
| 42 | + for _, a := range list { |
| 43 | + for _, b := range list { |
| 44 | + comp := a.CompareTo(b) |
| 45 | + fmt.Printf("%s %s %s\n", a, sign[comp], b) |
| 46 | + require.Equal(t, comp, 0) |
| 47 | + require.False(t, a.LessThan(b)) |
| 48 | + require.True(t, a.LessThanOrEqual(b)) |
| 49 | + require.True(t, a.Equal(b)) |
| 50 | + require.True(t, a.GreaterThanOrEqual(b)) |
| 51 | + require.False(t, a.GreaterThan(b)) |
| 52 | + |
| 53 | + comp = b.CompareTo(a) |
| 54 | + fmt.Printf("%s %s %s\n", b, sign[comp], a) |
| 55 | + require.Equal(t, comp, 0) |
| 56 | + require.False(t, b.LessThan(a)) |
| 57 | + require.True(t, b.LessThanOrEqual(a)) |
| 58 | + require.True(t, b.Equal(a)) |
| 59 | + require.True(t, b.GreaterThanOrEqual(a)) |
| 60 | + require.False(t, b.GreaterThan(a)) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + ascending( |
| 65 | + ParseRelaxed("6_2"), |
| 66 | + ParseRelaxed("alpha"), |
| 67 | + ParseRelaxed("beta"), |
| 68 | + ParseRelaxed("gamma"), |
| 69 | + ParseRelaxed("1.0.0-alpha"), |
| 70 | + ParseRelaxed("1.0.0-alpha.1"), |
| 71 | + ParseRelaxed("1.0.0-alpha.beta"), |
| 72 | + ParseRelaxed("1.0.0-beta"), |
| 73 | + ParseRelaxed("1.0.0-beta.2"), |
| 74 | + ParseRelaxed("1.0.0-beta.11"), |
| 75 | + ParseRelaxed("1.0.0-rc.1"), |
| 76 | + ParseRelaxed("1.0.0"), |
| 77 | + ParseRelaxed("1.0.1"), |
| 78 | + ParseRelaxed("1.1.1"), |
| 79 | + ParseRelaxed("2.1.1"), |
| 80 | + ) |
| 81 | + equal( |
| 82 | + ParseRelaxed(""), |
| 83 | + ParseRelaxed("0"), |
| 84 | + ParseRelaxed("0.0"), |
| 85 | + ParseRelaxed("0.0.0"), |
| 86 | + ParseRelaxed("0+aaa"), |
| 87 | + ParseRelaxed("0.0+aaa"), |
| 88 | + ParseRelaxed("0.0.0+aaa"), |
| 89 | + ParseRelaxed("0+aaa.bbb"), |
| 90 | + ParseRelaxed("0.0+aaa.bbb"), |
| 91 | + ParseRelaxed("0.0.0+aaa.bbb"), |
| 92 | + ) |
| 93 | +} |
0 commit comments