aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/semver/semver_test.v
blob: 77c8cd7f1e5f45a11534234e83d83335cafe0f96 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import semver

struct TestVersion {
	raw        string
	major      int
	minor      int
	patch      int
	prerelease string
	metadata   string
}

struct TestRange {
	raw_version       string
	range_satisfied   string
	range_unsatisfied string
}

struct TestCoerce {
	invalid string
	valid   string
}

const (
	versions_to_test         = [
		TestVersion{'1.2.4', 1, 2, 4, '', ''},
		TestVersion{'1.2.4-prerelease-1', 1, 2, 4, 'prerelease-1', ''},
		TestVersion{'1.2.4+20191231', 1, 2, 4, '', '20191231'},
		TestVersion{'1.2.4-prerelease-1+20191231', 1, 2, 4, 'prerelease-1', '20191231'},
		TestVersion{'1.2.4+20191231-prerelease-1', 1, 2, 4, '', '20191231-prerelease-1'},
	]
	ranges_to_test           = [
		TestRange{'1.1.0', '1.1.0', '1.1.1'},
		TestRange{'1.1.0', '=1.1.0', '=1.1.1'},
		TestRange{'1.1.0', '>=1.0.0', '<1.1.0'},
		TestRange{'1.1.0', '>=1.0.0 <=1.1.0', '>=1.0.0 <1.1.0'},
		TestRange{'2.3.1', '>=1.0.0 <=1.1.0 || >2.0.0 <2.3.4', '>=1.0.0 <1.1.0'},
		TestRange{'2.3.1', '>=1.0.0 <=1.1.0 || >2.0.0 <2.3.4', '>=1.0.0 <1.1.0 || >4.0.0 <5.0.0'},
		TestRange{'2.3.1', '~2.3.0', '~2.4.0'},
		TestRange{'3.0.0', '~3.0.0', '~4.0.0'},
		TestRange{'2.3.1', '^2.0.0', '^2.4.0'},
		TestRange{'0.3.1', '^0.3.0', '^2.4.0'},
		TestRange{'0.0.4', '^0.0.1', '^0.1.0'},
		TestRange{'2.3.4', '^0.0.1 || ^2.3.0', '^3.1.0 || ^4.2.0'},
		TestRange{'2.3.4', '>2 || <3', '>3 || >4'},
		TestRange{'2.3.4', '2.3.4 - 2.3.5', '2.5.1 - 2.8.3'},
		TestRange{'2.3.4', '2.2 - 2.3', '2.4 - 2.8'},
		TestRange{'2.3.4', '2.3.x', '2.4.x'},
		TestRange{'2.3.4', '2.x', '3.x'},
		TestRange{'2.3.4', '*', '3.x'},
	]
	coerce_to_test           = [
		TestCoerce{'1.2.0.4', '1.2.0'},
		TestCoerce{'1.2.0', '1.2.0'},
		TestCoerce{'1.2', '1.2.0'},
		TestCoerce{'1', '1.0.0'},
		TestCoerce{'1-alpha', '1.0.0-alpha'},
		TestCoerce{'1+meta', '1.0.0+meta'},
		TestCoerce{'1-alpha+meta', '1.0.0-alpha+meta'},
	]
	invalid_versions_to_test = [
		'a.b.c',
		'1.2',
		'1.2.x',
		'1.2.3.4',
		'1.2.3-alpha@',
		'1.2.3+meta%',
	]
	invalid_ranges_to_test   = [
		'^a',
		'~b',
		'a - c',
		'>a',
		'a',
		'a.x',
	]
)

fn test_from() {
	for item in versions_to_test {
		ver := semver.from(item.raw) or {
			assert false
			return
		}
		assert ver.major == item.major
		assert ver.minor == item.minor
		assert ver.patch == item.patch
		assert ver.metadata == item.metadata
		assert ver.prerelease == item.prerelease
	}
	for ver in invalid_versions_to_test {
		semver.from(ver) or {
			assert true
			continue
		}
		assert false
	}
}

fn test_increment() {
	version1 := semver.build(1, 2, 3)
	version1_inc := version1.increment(.major)
	assert version1_inc.major == 2
	assert version1_inc.minor == 0
	assert version1_inc.patch == 0
	version2_inc := version1.increment(.minor)
	assert version2_inc.major == 1
	assert version2_inc.minor == 3
	assert version2_inc.patch == 0
	version3_inc := version1.increment(.patch)
	assert version3_inc.major == 1
	assert version3_inc.minor == 2
	assert version3_inc.patch == 4
}

fn test_compare() {
	first := semver.build(1, 0, 0)
	patch := semver.build(1, 0, 1)
	minor := semver.build(1, 2, 3)
	major := semver.build(2, 0, 0)
	assert first.le(first)
	assert first.ge(first)
	assert !first.lt(first)
	assert !first.gt(first)
	assert patch.ge(first)
	assert first.le(patch)
	assert !first.ge(patch)
	assert !patch.le(first)
	assert patch.gt(first)
	assert first.lt(patch)
	assert !first.gt(patch)
	assert !patch.lt(first)
	assert minor.gt(patch)
	assert patch.lt(minor)
	assert !patch.gt(minor)
	assert !minor.lt(patch)
	assert major.gt(minor)
	assert minor.lt(major)
	assert !minor.gt(major)
	assert !major.lt(minor)
}

fn test_satisfies() {
	for item in ranges_to_test {
		ver := semver.from(item.raw_version) or {
			assert false
			return
		}
		assert ver.satisfies(item.range_satisfied)
		assert !ver.satisfies(item.range_unsatisfied)
	}
}

fn test_satisfies_invalid() {
	ver := semver.from('1.0.0') or {
		assert false
		return
	}
	for item in invalid_ranges_to_test {
		assert ver.satisfies(item) == false
	}
}

fn test_coerce() {
	for item in coerce_to_test {
		valid := semver.from(item.valid) or {
			assert false
			return
		}
		fixed := semver.coerce(item.invalid) or {
			assert false
			return
		}
		assert fixed.eq(valid)
	}
}

fn test_coerce_invalid() {
	semver.coerce('a') or {
		assert true
		return
	}
	assert false
}

fn test_is_valid() {
	for item in versions_to_test {
		assert semver.is_valid(item.raw)
	}
	for item in invalid_versions_to_test {
		assert semver.is_valid(item) == false
	}
}