-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgover_test.go
138 lines (121 loc) · 3.42 KB
/
gover_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gover
import (
"reflect"
"testing"
)
func TestCompare(t *testing.T) { test2(t, compareTests, "Compare", Compare) }
var compareTests = []testCase2[string, string, int]{
{"", "", 0},
{"x", "x", 0},
{"", "x", 0},
{"1", "1.1", -1},
{"1.5", "1.6", -1},
{"1.5", "1.10", -1},
{"1.6", "1.6.1", -1},
{"1.19", "1.19.0", 0},
{"1.19rc1", "1.19", -1},
{"1.20", "1.20.0", 0},
{"1.20rc1", "1.20", -1},
{"1.21", "1.21.0", -1},
{"1.21", "1.21rc1", -1},
{"1.21rc1", "1.21.0", -1},
{"1.6", "1.19", -1},
{"1.19", "1.19.1", -1},
{"1.19rc1", "1.19", -1},
{"1.19rc1", "1.19.1", -1},
{"1.19rc1", "1.19rc2", -1},
{"1.19.0", "1.19.1", -1},
{"1.19rc1", "1.19.0", -1},
{"1.19alpha3", "1.19beta2", -1},
{"1.19beta2", "1.19rc1", -1},
{"1.1", "1.99999999999999998", -1},
{"1.99999999999999998", "1.99999999999999999", -1},
}
func TestParse(t *testing.T) { test1(t, parseTests, "Parse", Parse) }
var parseTests = []testCase1[string, Version]{
{"1", Version{"1", "0", "0", "", ""}},
{"1.2", Version{"1", "2", "0", "", ""}},
{"1.2.3", Version{"1", "2", "3", "", ""}},
{"1.2rc3", Version{"1", "2", "", "rc", "3"}},
{"1.20", Version{"1", "20", "0", "", ""}},
{"1.21", Version{"1", "21", "", "", ""}},
{"1.21rc3", Version{"1", "21", "", "rc", "3"}},
{"1.21.0", Version{"1", "21", "0", "", ""}},
{"1.24", Version{"1", "24", "", "", ""}},
{"1.24rc3", Version{"1", "24", "", "rc", "3"}},
{"1.24.0", Version{"1", "24", "0", "", ""}},
{"1.999testmod", Version{"1", "999", "", "testmod", ""}},
{"1.99999999999999999", Version{"1", "99999999999999999", "", "", ""}},
}
func TestLang(t *testing.T) { test1(t, langTests, "Lang", Lang) }
var langTests = []testCase1[string, string]{
{"1.2rc3", "1.2"},
{"1.2.3", "1.2"},
{"1.2", "1.2"},
{"1", "1"},
{"1.999testmod", "1.999"},
}
func TestIsLang(t *testing.T) { test1(t, isLangTests, "IsLang", IsLang) }
var isLangTests = []testCase1[string, bool]{
{"1.2rc3", false},
{"1.2.3", false},
{"1.999testmod", false},
{"1.22", true},
{"1.21", true},
{"1.20", false}, // == 1.20.0
{"1.19", false}, // == 1.20.0
{"1.3", false}, // == 1.3.0
{"1.2", false}, // == 1.2.0
{"1", false}, // == 1.0.0
}
func TestIsValid(t *testing.T) { test1(t, isValidTests, "IsValid", IsValid) }
var isValidTests = []testCase1[string, bool]{
{"1.2rc3", true},
{"1.2.3", true},
{"1.999testmod", true},
{"1.600+auto", false},
{"1.22", true},
{"1.21.0", true},
{"1.21rc2", true},
{"1.21", true},
{"1.20.0", true},
{"1.20", true},
{"1.19", true},
{"1.3", true},
{"1.2", true},
{"1", true},
}
type testCase1[In, Out any] struct {
in In
out Out
}
type testCase2[In1, In2, Out any] struct {
in1 In1
in2 In2
out Out
}
type testCase3[In1, In2, In3, Out any] struct {
in1 In1
in2 In2
in3 In3
out Out
}
func test1[In, Out any](t *testing.T, tests []testCase1[In, Out], name string, f func(In) Out) {
t.Helper()
for _, tt := range tests {
if out := f(tt.in); !reflect.DeepEqual(out, tt.out) {
t.Errorf("%s(%v) = %v, want %v", name, tt.in, out, tt.out)
}
}
}
func test2[In1, In2, Out any](t *testing.T, tests []testCase2[In1, In2, Out], name string, f func(In1, In2) Out) {
t.Helper()
for _, tt := range tests {
if out := f(tt.in1, tt.in2); !reflect.DeepEqual(out, tt.out) {
t.Errorf("%s(%+v, %+v) = %+v, want %+v", name, tt.in1, tt.in2, out, tt.out)
}
}
}