forked from Ariel-Rodriguez/sh-semversion-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·204 lines (186 loc) · 5.11 KB
/
test.sh
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
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
set -u
# Include semver_compare tool
source ./semver2.sh
# specific target tests
isLowerThanSpec=(
1.0.0-alpha 1.0.0-alpha.1
# Spec 11.4.4: A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.
1.0.0-alpha 1.0.0-alpha.beta
# Spec 11.4.2 Identifiers with letters or hyphens are compared lexically in ASCII sort order.
1.0.0-alpha 1.0.0-beta
1.0.0-alpha 1.0.0-beta.2
1.0.0-alpha 1.0.0-beta.11
1.0.0-alpha 1.0.0-rc.1
# Spec 11.3 a pre-release version has lower precedence than a normal version
1.0.0-alpha 1.0.0
# Spec 11.4.3 Because Numeric identifiers always have lower precedence than non-numeric identifiers
1.0.0-alpha.1 1.0.0-alpha.beta
1.0.0-alpha.1 1.0.0-beta
1.0.0-alpha.1 1.0.0-beta.2
1.0.0-alpha.1 1.0.0-beta.11
1.0.0-alpha.1 1.0.0-rc.1
1.0.0-alpha.1 1.0.0
1.0.0-alpha.beta 1.0.0-beta
1.0.0-alpha.beta 1.0.0-beta.2
1.0.0-alpha.beta 1.0.0-beta.11
1.0.0-alpha.beta 1.0.0-rc.1
1.0.0-alpha.beta 1.0.0
1.0.0-beta 1.0.0-beta.2
1.0.0-beta 1.0.0-beta.11
1.0.0-beta 1.0.0-rc.1
1.0.0-beta 1.0.0
1.0.0-beta.2 1.0.0-beta.11
1.0.0-beta.2 1.0.0-rc.1
1.0.0-beta.2 1.0.0
1.0.0-beta.11 1.0.0-rc.1
)
# -----
# brute force tests
#
# semver2.0 happy path tests ordered by precedence
# spec 11.4
# 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.
tests=(
1.0.0-0--1
1.0.0-0-0
1.0.0-0-0.alpha
1.0.0-0-1
1.0.0-alpha
1.0.0-alpha.1
1.0.0-alpha.beta
1.0.0-alpha.beta.1
1.0.0-beta
1.0.0-beta.2
1.0.0-beta.11
1.0.0-rc.1
1.0.0+metadata.0.0.0
2.0.0-ALPHA
2.0.0-alpha
# leading zeroes
02.00.01-alpha
02.00.01
02.00.10
02.01.00
02.10.00
)
# others
# 0.0.0+METADATA 0.0.0+METADATA.beta 0
# 0.0.0 0.0.0 0
# 0.0.1 0.0.0 1
# 0.0.0 0.0.1 -1
# 0.0.1-rc v0.0.1-rc 0
# 0.0.1-rc.1 v0.0.1-rc 1
# 0.0.1-rc.0 v0.0.1-rc.2 -1
# v0.0.1-rc.0 0.0.1-rc.0 0
# v0.0.1 v0.0.1-rc.2 1
# v0.0.1 v0.0.1 0
# v0.1.0-rc.0 0.1.0-rc.1 -1
# 0.1.0-rc.1 v0.1.0-rc.1 0
# v0.1.0 v0.1.0-rc.1 1
# v0.1.0-rc.1 v0.1.0 -1
# v0.1.0-rc.9 v0.1.0-rc.10 -1
# v0.1.0-alpha v0.1.0-beta -1
# v0.1.0-alpha.1 v0.1.0-beta.0 -1
_assert_is() {
reason=""
if [ "$3" != "$4" ]; then
printf "FAILED "
reason="expected to be $4 got $3"
else
printf "OK "
fi
case $4 in
"-1")
printf "%s" "$1 < $2"
;;
"1")
printf "%s" "$1 > $2"
;;
*)
printf "%s" "$1 = $2"
;;
esac
printf "%s $reason\n"
if [ -n "$reason" ]; then printf "1"; fi
}
exitcode=0
failcount=0
testscount=0
printf "\nSpecific target tests...\n\n"
for (( i=0; i<=$(( ${#isLowerThanSpec[@]} -1 )); i+=2 ))
do
outcome=$(_assert_is "${isLowerThanSpec[i]}" "${isLowerThanSpec[i+1]}" "$(semver_compare "${isLowerThanSpec[i]}" "${isLowerThanSpec[i+1]}")" "-1")
echo "$outcome"
# increase failcount by 1
testscount=$((testscount + 1))
if [[ "$outcome" == "FAILED "* ]]; then
failcount=$((failcount + 1))
exitcode=1
if [ -n "$*" ] && [ "$1" == "early-exit=true" ]; then
break
fi
fi
done
printf "\n\nBrute force tests...\n\n"
for (( i=0; i<=$(( ${#tests[@]} -1 )); i+=1 ))
do
for (( j=0; j<$(( ${#tests[@]} )); j+=1 ))
do
expected="0"
if [ "$j" -lt "$i" ]; then expected="1"; fi
if [ "$j" -gt "$i" ]; then expected="-1"; fi
outcome=$(_assert_is "${tests[i]}" "${tests[j]}" "$(semver_compare "${tests[i]}" "${tests[j]}")" "$expected")
# print outcome only if optional program parameter contains "print-only-failures"
if [ -n "$*" ] && [[ "$*" == *"print-only-failures"* ]]; then
if [[ "$outcome" == "FAILED "* ]]; then
echo "$outcome"
fi
# print a progress bar
printf "\r"
printf "Progress: %s%%" "$((100 * i / ${#tests[@]}))"
else
printf "%s\n$outcome"
fi
# increase failcount by 1
testscount=$((testscount + 1))
if [[ "$outcome" == "FAILED "* ]]; then
failcount=$((failcount + 1))
exitcode=1
if [ -n "$*" ] && [ "$1" == "early-exit=true" ]; then
printf "\n%sTotal failures $failcount out of $testscount tests OK\n"
exit $exitcode
fi
fi
done
done
printf "\n%sTotal failures $failcount of $testscount tests OK\n"
exit $exitcode
# OK 1.0.0-alpha < 1.0.0-alpha.1
# OK 1.0.0-alpha < 1.0.0-alpha.beta
# OK 1.0.0-alpha < 1.0.0-beta
# OK 1.0.0-alpha < 1.0.0-beta.2
# OK 1.0.0-alpha < 1.0.0-beta.11
# OK 1.0.0-alpha < 1.0.0-rc.1
# OK 1.0.0-alpha < 1.0.0
# OK 1.0.0-alpha.1 < 1.0.0-alpha.beta
# OK 1.0.0-alpha.1 < 1.0.0-beta
# OK 1.0.0-alpha.1 < 1.0.0-beta.2
# OK 1.0.0-alpha.1 < 1.0.0-beta.11
# OK 1.0.0-alpha.1 < 1.0.0-rc.1
# OK 1.0.0-alpha.1 < 1.0.0
# OK 1.0.0-alpha.beta < 1.0.0-beta
# OK 1.0.0-alpha.beta < 1.0.0-beta.2
# OK 1.0.0-alpha.beta < 1.0.0-beta.11
# OK 1.0.0-alpha.beta < 1.0.0-rc.1
# OK 1.0.0-alpha.beta < 1.0.0
# OK 1.0.0-beta < 1.0.0-beta.2
# OK 1.0.0-beta < 1.0.0-beta.11
# OK 1.0.0-beta < 1.0.0-rc.1
# OK 1.0.0-beta < 1.0.0
# OK 1.0.0-beta.2 < 1.0.0-beta.11
# OK 1.0.0-beta.2 < 1.0.0-rc.1
# OK 1.0.0-beta.2 < 1.0.0
# OK 1.0.0-beta.11 < 1.0.0-rc.1
# OK 1.0.0-beta.11 < 1.0.0
# OK 1.0.0-rc.1 < 1.0.0