|
| 1 | +package compat |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/evanw/esbuild/internal/test" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCompareVersions(t *testing.T) { |
| 11 | + t.Helper() |
| 12 | + |
| 13 | + check := func(a v, b Semver, expected rune) { |
| 14 | + t.Helper() |
| 15 | + |
| 16 | + at := fmt.Sprintf("%d.%d.%d", a.major, a.minor, a.patch) |
| 17 | + bt := b.String() |
| 18 | + |
| 19 | + t.Run(fmt.Sprintf("%q ? %q", at, bt), func(t *testing.T) { |
| 20 | + observed := '=' |
| 21 | + if result := compareVersions(a, b); result < 0 { |
| 22 | + observed = '<' |
| 23 | + } else if result > 0 { |
| 24 | + observed = '>' |
| 25 | + } |
| 26 | + if observed != expected { |
| 27 | + test.AssertEqual(t, fmt.Sprintf("%c", observed), fmt.Sprintf("%c", expected)) |
| 28 | + } |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + check(v{0, 0, 0}, Semver{}, '=') |
| 33 | + |
| 34 | + check(v{1, 0, 0}, Semver{}, '>') |
| 35 | + check(v{0, 1, 0}, Semver{}, '>') |
| 36 | + check(v{0, 0, 1}, Semver{}, '>') |
| 37 | + |
| 38 | + check(v{0, 0, 0}, Semver{Parts: []int{1}}, '<') |
| 39 | + check(v{0, 0, 0}, Semver{Parts: []int{0, 1}}, '<') |
| 40 | + check(v{0, 0, 0}, Semver{Parts: []int{0, 0, 1}}, '<') |
| 41 | + |
| 42 | + check(v{0, 4, 0}, Semver{Parts: []int{0, 5, 0}}, '<') |
| 43 | + check(v{0, 5, 0}, Semver{Parts: []int{0, 5, 0}}, '=') |
| 44 | + check(v{0, 6, 0}, Semver{Parts: []int{0, 5, 0}}, '>') |
| 45 | + |
| 46 | + check(v{0, 5, 0}, Semver{Parts: []int{0, 5, 1}}, '<') |
| 47 | + check(v{0, 5, 0}, Semver{Parts: []int{0, 5, 0}}, '=') |
| 48 | + check(v{0, 5, 1}, Semver{Parts: []int{0, 5, 0}}, '>') |
| 49 | + |
| 50 | + check(v{0, 5, 0}, Semver{Parts: []int{0, 5}}, '=') |
| 51 | + check(v{0, 5, 1}, Semver{Parts: []int{0, 5}}, '>') |
| 52 | + |
| 53 | + check(v{1, 0, 0}, Semver{Parts: []int{1}}, '=') |
| 54 | + check(v{1, 1, 0}, Semver{Parts: []int{1}}, '>') |
| 55 | + check(v{1, 0, 1}, Semver{Parts: []int{1}}, '>') |
| 56 | + |
| 57 | + check(v{1, 2, 0}, Semver{Parts: []int{1, 2}, PreRelease: "-pre"}, '>') |
| 58 | + check(v{1, 2, 1}, Semver{Parts: []int{1, 2}, PreRelease: "-pre"}, '>') |
| 59 | + check(v{1, 1, 0}, Semver{Parts: []int{1, 2}, PreRelease: "-pre"}, '<') |
| 60 | + |
| 61 | + check(v{1, 2, 3}, Semver{Parts: []int{1, 2, 3}, PreRelease: "-pre"}, '>') |
| 62 | + check(v{1, 2, 2}, Semver{Parts: []int{1, 2, 3}, PreRelease: "-pre"}, '<') |
| 63 | +} |
0 commit comments