-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_test.go
112 lines (107 loc) · 5.42 KB
/
version_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
package rbxver
import (
"io"
"testing"
)
// Tests for ParseBytes and Parse.
var tests = []struct {
s string // Input string.
f Format // Input format.
v Version // Expected version.
n int // Expected read bytes.
e error // Expected error.
str *Version // If Parse, only compare this.
}{
{s: "", f: Any, v: Version{0, 0, 0, 0, Any}, n: 0, e: io.ErrUnexpectedEOF},
{s: "", f: Dot, v: Version{0, 0, 0, 0, Any}, n: 0, e: io.ErrUnexpectedEOF},
{s: "", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 0, e: io.ErrUnexpectedEOF},
{s: " ", f: Any, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: " ", f: Dot, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: " ", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: "a", f: Any, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: "a", f: Dot, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: "a", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: "ab", f: Any, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: "ab", f: Dot, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: "ab", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: ".", f: Any, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: ".", f: Dot, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: ".", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: ",", f: Any, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: ",", f: Dot, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: ",", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: "1", f: Any, v: Version{1, 0, 0, 0, Any}, n: 1, e: io.ErrUnexpectedEOF},
{s: "1", f: Dot, v: Version{1, 0, 0, 0, Any}, n: 1, e: io.ErrUnexpectedEOF},
{s: "1", f: Comma, v: Version{1, 0, 0, 0, Any}, n: 1, e: io.ErrUnexpectedEOF},
{s: " 1", f: Any, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: " 1", f: Dot, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: " 1", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: "12", f: Any, v: Version{12, 0, 0, 0, Any}, n: 2, e: io.ErrUnexpectedEOF},
{s: "12", f: Dot, v: Version{12, 0, 0, 0, Any}, n: 2, e: io.ErrUnexpectedEOF},
{s: "12", f: Comma, v: Version{12, 0, 0, 0, Any}, n: 2, e: io.ErrUnexpectedEOF},
{s: "12.34.56.78", f: Any, v: Version{12, 34, 56, 78, Dot}, n: 11, e: nil},
{s: "12.34.56.78", f: Dot, v: Version{12, 34, 56, 78, Dot}, n: 11, e: nil},
{s: "12.34.56.78", f: Comma, v: Version{12, 0, 0, 0, Any}, n: 2, e: ErrSyntax},
{s: "12.34,56.78", f: Any, v: Version{12, 34, 0, 0, Any}, n: 5, e: ErrSyntax},
{s: "12.34,56.78", f: Dot, v: Version{12, 34, 0, 0, Any}, n: 5, e: ErrSyntax},
{s: "12.34,56.78", f: Comma, v: Version{12, 0, 0, 0, Any}, n: 2, e: ErrSyntax},
{s: "12,34,56,78", f: Any, v: Version{12, 0, 0, 0, Any}, n: 2, e: ErrSyntax},
{s: "12,34,56,78", f: Dot, v: Version{12, 0, 0, 0, Any}, n: 2, e: ErrSyntax},
{s: "12,34,56,78", f: Comma, v: Version{12, 0, 0, 0, Any}, n: 2, e: ErrSyntax},
{s: "12, 34, 56, 78", f: Any, v: Version{12, 34, 56, 78, Comma}, n: 14, e: nil},
{s: "12, 34, 56, 78", f: Dot, v: Version{12, 0, 0, 0, Any}, n: 2, e: ErrSyntax},
{s: "12, 34, 56, 78", f: Comma, v: Version{12, 34, 56, 78, Comma}, n: 14, e: nil},
{s: " 12 . 34 . 56 . 78 ", f: Any, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: " 12 . 34 . 56 . 78 ", f: Dot, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: " 12 . 34 . 56 . 78 ", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 0, e: ErrSyntax},
{s: "12.34. 56 . 78 ", f: Any, v: Version{12, 34, 0, 0, Any}, n: 6, e: ErrSyntax},
{s: "12.34. 56 . 78 ", f: Dot, v: Version{12, 34, 0, 0, Any}, n: 6, e: ErrSyntax},
{s: "12.34. 56 . 78 ", f: Comma, v: Version{12, 0, 0, 0, Any}, n: 2, e: ErrSyntax},
{s: "0.123.1.1234567", f: Any, v: Version{0, 123, 1, 1234567, Dot}, n: 15, e: nil},
{s: "0.123.1.1234567", f: Dot, v: Version{0, 123, 1, 1234567, Dot}, n: 15, e: nil},
{s: "0.123.1.1234567", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 1, e: ErrSyntax},
{s: "0 . 123 .1. 1234567", f: Any, v: Version{0, 0, 0, 0, Any}, n: 1, e: ErrSyntax},
{s: "0 . 123 .1. 1234567", f: Dot, v: Version{0, 0, 0, 0, Any}, n: 1, e: ErrSyntax},
{s: "0 . 123 .1. 1234567", f: Comma, v: Version{0, 0, 0, 0, Any}, n: 1, e: ErrSyntax},
{s: "0.123.-1.1234567", f: Any, v: Version{0, 123, 0, 0, Any}, n: 6, e: ErrSyntax},
{s: "0.123.1.1234567trailingdata", f: Dot, v: Version{0, 123, 1, 1234567, Dot}, n: 15, e: nil, str: &Version{}},
}
var fmtstr = [...]string{
"Any",
"Dot",
"Comma",
}
func TestParseBytes(t *testing.T) {
for _, test := range tests {
v, n, err := ParseBytes([]byte(test.s), test.f)
if v != test.v {
t.Errorf("ParseBytes(%q, %s): expected version %v, got %v", test.s, fmtstr[test.f], test.v, v)
}
if n != test.n {
t.Errorf("ParseBytes(%q, %s): expected bytes %d, got %d", test.s, fmtstr[test.f], test.n, n)
}
if err != test.e {
t.Errorf("ParseBytes(%q, %s): expected error %v, got %v", test.s, fmtstr[test.f], test.e, err)
}
}
}
func TestParse(t *testing.T) {
for _, test := range tests {
v := Parse(test.s, test.f)
if test.str != nil {
if v != *test.str {
t.Errorf("Parse(%q, %s): expected version %v, got %v", test.s, fmtstr[test.f], *test.str, v)
}
} else {
if test.e == nil {
if v != test.v {
t.Errorf("Parse(%q, %s): expected version %v, got %v", test.s, fmtstr[test.f], test.v, v)
}
} else {
if v != (Version{}) {
t.Errorf("Parse(%q, %s): expected zero version, got %v", test.s, fmtstr[test.f], v)
}
}
}
}
}