spice_packages/semver/semver.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Std imports | ||
| 2 | import "std/data/vector"; | ||
| 3 | |||
| 4 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 5 | // Semantic versions and version constraints, as used by Spice Shaker and the Spice Rack registry | ||
| 6 | // | ||
| 7 | // Supported clauses, which can be combined with commas or spaces (all of them have to match): | ||
| 8 | // * any version 1.2.3 exactly 1.2.3 (also =1.2.3) | ||
| 9 | // ^1.2.3 >=1.2.3 <2.0.0 ~1.2.3 >=1.2.3 <1.3.0 | ||
| 10 | // 1.2.x same as ~1.2.0 1.x same as ^1.0.0 | ||
| 11 | // >=1.2.3 >1.2.3 <=1.2.3 <1.2.3 | ||
| 12 | // | ||
| 13 | // The module only uses String and Vector, no libc functions beyond those, so it also builds for WebAssembly. | ||
| 14 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 15 | |||
| 16 | 504 | public type Semver struct { | |
| 17 | 252 | public long major = 0l | |
| 18 | 252 | public long minor = 0l | |
| 19 | 252 | public long patch = 0l | |
| 20 | public String pre | ||
| 21 | 252 | public bool valid = false | |
| 22 | } | ||
| 23 | |||
| 24 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 25 | // Internal helpers | ||
| 26 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Parse a non-negative decimal number with at most 18 digits. Returns -1 for anything else. | ||
| 30 | */ | ||
| 31 | 335 | f<long> semverParseNumber(const String& text) { | |
| 32 | 335 | const unsigned long len = text.getLength(); | |
| 33 | 335 | if len == 0l || len > 18l { return -1l; } | |
| 34 | 329 | long value = 0l; | |
| 35 | 645 | for unsigned long i = 0l; i < len; i++ { | |
| 36 | 335 | const char c = text[i]; | |
| 37 | 335 | if c < '0' || c > '9' { return -1l; } | |
| 38 | 316 | value = value * 10l + cast<long>(cast<int>(c) - 48); | |
| 39 | 316 | } | |
| 40 | 310 | return value; | |
| 41 | } | ||
| 42 | |||
| 43 | 6 | f<String> semverFormatNumber(long value) { | |
| 44 | // Version numbers are never negative, parseSemver only accepts digits | ||
| 45 | 6 | if value <= 0l { return String("0"); } | |
| 46 | 3 | String digits; | |
| 47 | 3 | long rest = value; | |
| 48 | 9 | while rest != 0l { | |
| 49 | 6 | digits.append(cast<char>(48 + cast<int>(rest % 10l))); | |
| 50 | 6 | rest /= 10l; | |
| 51 | 6 | } | |
| 52 | 3 | digits.reverse(); | |
| 53 | 3 | return digits; | |
| 54 | } | ||
| 55 | |||
| 56 | /** | ||
| 57 | * Everything from the given index on, or an empty string if the index is at or past the end | ||
| 58 | */ | ||
| 59 | 92 | f<String> semverTail(const String& text, unsigned long from) { | |
| 60 | 92 | if from >= text.getLength() { return String(""); } | |
| 61 | 89 | return text.getSubstring(from); | |
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Split by a separator character. Empty parts are kept. | ||
| 66 | */ | ||
| 67 | 249 | f<Vector<String>> semverSplit(const String& text, char separator) { | |
| 68 | 249 | Vector<String> parts; | |
| 69 | 249 | String current; | |
| 70 | 249 | const unsigned long len = text.getLength(); | |
| 71 | 1491 | for unsigned long i = 0l; i < len; i++ { | |
| 72 | 1242 | const char c = text[i]; | |
| 73 | 1242 | if c == separator { | |
| 74 | 317 | parts.pushBack(current); | |
| 75 | 317 | current = String(""); | |
| 76 | 317 | } else { | |
| 77 | 925 | current.append(c); | |
| 78 | 925 | } | |
| 79 | 1242 | } | |
| 80 | 249 | parts.pushBack(current); | |
| 81 | 498 | return parts; | |
| 82 | 249 | } | |
| 83 | |||
| 84 | /** | ||
| 85 | * Byte-wise (ASCII) ordering of two strings | ||
| 86 | */ | ||
| 87 | 1 | f<bool> semverIsBefore(const String& a, const String& b) { | |
| 88 | 1 | const unsigned long n = a.getLength() < b.getLength() ? a.getLength() : b.getLength(); | |
| 89 | 1 | for unsigned long i = 0l; i < n; i++ { | |
| 90 | 1 | if a[i] != b[i] { return a[i] < b[i]; } | |
| 91 | ✗ | } | |
| 92 | ✗ | return a.getLength() < b.getLength(); | |
| 93 | } | ||
| 94 | |||
| 95 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 96 | // Versions | ||
| 97 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 98 | |||
| 99 | /** | ||
| 100 | * Parse "1.2.3", "v1.2.3", "1.2", "1.2.3-beta.1" or "1.2.3+build". Missing minor and patch numbers are 0, build | ||
| 101 | * metadata is dropped. The result has valid == false if the text is not a version. | ||
| 102 | */ | ||
| 103 | 126 | public f<Semver> parseSemver(const String& input) { | |
| 104 | 126 | Semver version; | |
| 105 | 126 | String text = input.trim(); | |
| 106 | 126 | if text.startsWith("v") || text.startsWith("V") { text = semverTail(text, 1l); } | |
| 107 | 126 | const long plus = text.find('+'); | |
| 108 | 126 | if plus >= 0l { text = text.getSubstring(0l, plus); } | |
| 109 | 126 | const long dash = text.find('-'); | |
| 110 | 126 | if dash >= 0l { | |
| 111 | 26 | version.pre = semverTail(text, cast<unsigned long>(dash) + 1l); | |
| 112 | 26 | text = text.getSubstring(0l, dash); | |
| 113 | 26 | if version.pre.isEmpty() { return version; } | |
| 114 | 26 | } | |
| 115 | 125 | const Vector<String> parts = semverSplit(text, '.'); | |
| 116 | 125 | if parts.getSize() > 3l { return version; } | |
| 117 | 429 | for unsigned long i = 0l; i < parts.getSize(); i++ { | |
| 118 | 321 | const long number = semverParseNumber(parts.get(i)); | |
| 119 | 321 | if number < 0l { return version; } | |
| 120 | 305 | if i == 0l { version.major = number; } | |
| 121 | 195 | else if i == 1l { version.minor = number; } | |
| 122 | 195 | else { version.patch = number; } | |
| 123 | 305 | } | |
| 124 | 108 | version.valid = true; | |
| 125 | 216 | return version; | |
| 126 | 126 | } | |
| 127 | |||
| 128 | /** | ||
| 129 | * A complete version as required for package.version in spice.toml: three numbers, no leading 'v', no whitespace | ||
| 130 | */ | ||
| 131 | 17 | public f<bool> isStrictSemver(const String& input) { | |
| 132 | 17 | if input.startsWith("v") || input.startsWith("V") || input != input.trim() { return false; } | |
| 133 | 15 | String core = input; | |
| 134 | 15 | const long plus = core.find('+'); | |
| 135 | 15 | if plus >= 0l { core = core.getSubstring(0l, plus); } | |
| 136 | 15 | const long dash = core.find('-'); | |
| 137 | 15 | if dash >= 0l { core = core.getSubstring(0l, dash); } | |
| 138 | 15 | const Vector<String> parts = semverSplit(core, '.'); | |
| 139 | 15 | return parts.getSize() == 3l && parseSemver(input).valid; | |
| 140 | 15 | } | |
| 141 | |||
| 142 | /** | ||
| 143 | * "major.minor.patch", followed by "-pre" for a pre-release | ||
| 144 | */ | ||
| 145 | 2 | public f<String> Semver.toString() { | |
| 146 | 2 | String text = semverFormatNumber(this.major) + "." + semverFormatNumber(this.minor) + "." + | |
| 147 | 2 | semverFormatNumber(this.patch); | |
| 148 | 2 | if !this.pre.isEmpty() { text += "-" + this.pre; } | |
| 149 | 4 | return text; | |
| 150 | 2 | } | |
| 151 | |||
| 152 | /** | ||
| 153 | * Pre-release identifiers are compared piece by piece, numerically where both are numbers. Numbers sort before text. | ||
| 154 | */ | ||
| 155 | 5 | f<int> semverCompareIdentifiers(const String& a, const String& b) { | |
| 156 | 5 | const Vector<String> pa = semverSplit(a, '.'); | |
| 157 | 5 | const Vector<String> pb = semverSplit(b, '.'); | |
| 158 | 5 | const unsigned long n = pa.getSize() < pb.getSize() ? pa.getSize() : pb.getSize(); | |
| 159 | 8 | for unsigned long i = 0l; i < n; i++ { | |
| 160 | 7 | const long na = semverParseNumber(pa.get(i)); | |
| 161 | 7 | const long nb = semverParseNumber(pb.get(i)); | |
| 162 | 7 | if na >= 0l && nb >= 0l { | |
| 163 | 2 | if na != nb { return na < nb ? -1 : 1; } | |
| 164 | 5 | } else if na >= 0l { | |
| 165 | 1 | return -1; | |
| 166 | 4 | } else if nb >= 0l { | |
| 167 | ✗ | return 1; | |
| 168 | 4 | } else if pa.get(i) != pb.get(i) { | |
| 169 | 2 | return semverIsBefore(pa.get(i), pb.get(i)) ? -1 : 1; | |
| 170 | 3 | } | |
| 171 | 3 | } | |
| 172 | 1 | if pa.getSize() == pb.getSize() { return 0; } | |
| 173 | 2 | return pa.getSize() < pb.getSize() ? -1 : 1; | |
| 174 | 5 | } | |
| 175 | |||
| 176 | /** | ||
| 177 | * Returns -1, 0 or 1. A pre-release sorts before its release. | ||
| 178 | */ | ||
| 179 | 59 | public f<int> compareSemver(const Semver& a, const Semver& b) { | |
| 180 | 59 | if a.major != b.major { return a.major < b.major ? -1 : 1; } | |
| 181 | 42 | if a.minor != b.minor { return a.minor < b.minor ? -1 : 1; } | |
| 182 | 24 | if a.patch != b.patch { return a.patch < b.patch ? -1 : 1; } | |
| 183 | 16 | if a.pre.isEmpty() && b.pre.isEmpty() { return 0; } | |
| 184 | 6 | if a.pre.isEmpty() { return 1; } | |
| 185 | 6 | if b.pre.isEmpty() { return -1; } | |
| 186 | 5 | return semverCompareIdentifiers(a.pre, b.pre); | |
| 187 | } | ||
| 188 | |||
| 189 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 190 | // Constraints | ||
| 191 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 192 | |||
| 193 | /** | ||
| 194 | * A single clause like ">=1.2.3", "^1.2", "~2.0.1", "1.2.x" or "*", turned into a range [lower, upper). | ||
| 195 | * A missing upper bound is marked by upper.valid == false. | ||
| 196 | */ | ||
| 197 | 126 | type SemverClause struct { | |
| 198 | 63 | public bool valid = false | |
| 199 | 63 | public bool any = false | |
| 200 | public String op // "", "=", ">=", ">", "<=", "<"; ranges (^, ~, wildcards) are turned into lower/upper | ||
| 201 | public Semver lower | ||
| 202 | public Semver upper | ||
| 203 | } | ||
| 204 | |||
| 205 | 63 | f<SemverClause> semverParseClause(const String& input) { | |
| 206 | 63 | SemverClause clause; | |
| 207 | 63 | String text = input.trim(); | |
| 208 | 63 | if text.isEmpty() || text == "*" || text == "x" || text == "X" { | |
| 209 | 7 | clause.valid = true; | |
| 210 | 7 | clause.any = true; | |
| 211 | 14 | return clause; | |
| 212 | } | ||
| 213 | 56 | if text.startsWith(">=") || text.startsWith("<=") { | |
| 214 | 9 | clause.op = text.getSubstring(0l, 2l); | |
| 215 | 9 | clause.lower = parseSemver(semverTail(text, 2l)); | |
| 216 | 9 | clause.valid = clause.lower.valid; | |
| 217 | 18 | return clause; | |
| 218 | 9 | } | |
| 219 | 47 | if text.startsWith(">") || text.startsWith("<") || text.startsWith("=") { | |
| 220 | 7 | clause.op = text.getSubstring(0l, 1l); | |
| 221 | 7 | text = semverTail(text, 1l); | |
| 222 | 7 | if clause.op != "=" { | |
| 223 | 6 | clause.lower = parseSemver(text); | |
| 224 | 6 | clause.valid = clause.lower.valid; | |
| 225 | 12 | return clause; | |
| 226 | } | ||
| 227 | 1 | clause.op = String(""); | |
| 228 | 7 | } | |
| 229 | |||
| 230 | 41 | const bool caret = text.startsWith("^"); | |
| 231 | 41 | const bool tilde = text.startsWith("~"); | |
| 232 | 41 | if caret || tilde { text = semverTail(text, 1l); } | |
| 233 | |||
| 234 | // Wildcards: "1.x", "1.2.*" | ||
| 235 | 41 | bool wildcard = false; | |
| 236 | 41 | const Vector<String> pieces = semverSplit(text, '.'); | |
| 237 | 41 | String concrete; | |
| 238 | 41 | unsigned long given = 0l; | |
| 239 | 138 | for unsigned long i = 0l; i < pieces.getSize(); i++ { | |
| 240 | 103 | const String& piece = pieces.get(i); | |
| 241 | 103 | if piece == "x" || piece == "X" || piece == "*" { | |
| 242 | // Everything after a wildcard has to be a wildcard as well | ||
| 243 | 12 | for unsigned long k = i; k < pieces.getSize(); k++ { | |
| 244 | 7 | const String& rest = pieces.get(k); | |
| 245 | 7 | if rest != "x" && rest != "X" && rest != "*" { return clause; } | |
| 246 | 6 | } | |
| 247 | 5 | wildcard = true; | |
| 248 | 5 | break; | |
| 249 | } | ||
| 250 | 97 | if i > 0l { concrete += "."; } | |
| 251 | 97 | concrete += piece; | |
| 252 | 97 | given++; | |
| 253 | 97 | } | |
| 254 | 40 | if wildcard && (caret || tilde || given == 0l) { return clause; } | |
| 255 | 39 | clause.lower = parseSemver(concrete); | |
| 256 | 39 | if !clause.lower.valid { return clause; } | |
| 257 | 36 | clause.valid = true; | |
| 258 | |||
| 259 | 36 | if !caret && !tilde && !wildcard { | |
| 260 | 6 | clause.op = String("="); | |
| 261 | 12 | return clause; | |
| 262 | 6 | } | |
| 263 | 30 | clause.upper.valid = true; | |
| 264 | 30 | if wildcard || tilde { | |
| 265 | 7 | if given == 1l { clause.upper.major = clause.lower.major + 1l; } | |
| 266 | 5 | else { clause.upper.major = clause.lower.major; clause.upper.minor = clause.lower.minor + 1l; } | |
| 267 | 7 | } else { | |
| 268 | // Caret: everything up to the next change of the left-most non-zero number | ||
| 269 | 23 | if clause.lower.major > 0l || given == 1l { clause.upper.major = clause.lower.major + 1l; } | |
| 270 | 4 | else if clause.lower.minor > 0l || given == 2l { clause.upper.minor = clause.lower.minor + 1l; } | |
| 271 | 4 | else { clause.upper.patch = clause.lower.patch + 1l; } | |
| 272 | 23 | } | |
| 273 | 60 | return clause; | |
| 274 | 63 | } | |
| 275 | |||
| 276 | 39 | f<bool> SemverClause.matches(const Semver& version) { | |
| 277 | 39 | if this.any { return true; } | |
| 278 | 34 | const int cmp = compareSemver(version, this.lower); | |
| 279 | 34 | if this.op == "=" { return cmp == 0; } | |
| 280 | 31 | if this.op == ">=" { return cmp >= 0; } | |
| 281 | 25 | if this.op == ">" { return cmp > 0; } | |
| 282 | 24 | if this.op == "<=" { return cmp <= 0; } | |
| 283 | 24 | if this.op == "<" { return cmp < 0; } | |
| 284 | 20 | return cmp >= 0 && (!this.upper.valid || compareSemver(version, this.upper) < 0); | |
| 285 | } | ||
| 286 | |||
| 287 | /** | ||
| 288 | * Split a constraint expression into clauses. Operators written with a space (">= 1.2") are joined with their version. | ||
| 289 | */ | ||
| 290 | 58 | f<Vector<String>> semverSplitClauses(const String& constraint) { | |
| 291 | // Commas and spaces both separate clauses | ||
| 292 | 58 | String normalized; | |
| 293 | 58 | const unsigned long len = constraint.getLength(); | |
| 294 | 425 | for unsigned long i = 0l; i < len; i++ { | |
| 295 | 367 | const char c = constraint[i]; | |
| 296 | 367 | normalized.append(c == ',' ? ' ' : c); | |
| 297 | 367 | } | |
| 298 | 58 | const Vector<String> tokens = semverSplit(normalized, ' '); | |
| 299 | 58 | Vector<String> clauses; | |
| 300 | 58 | String pendingOperator; | |
| 301 | 133 | for unsigned long i = 0l; i < tokens.getSize(); i++ { | |
| 302 | 75 | const String& token = tokens.get(i); | |
| 303 | 75 | if token.isEmpty() { continue; } | |
| 304 | 69 | const bool operatorOnly = token == ">=" || token == "<=" || token == ">" || token == "<" || token == "=" || | |
| 305 | 69 | token == "^" || token == "~"; | |
| 306 | 69 | if operatorOnly { pendingOperator = token; continue; } | |
| 307 | 66 | clauses.pushBack(pendingOperator + token); | |
| 308 | 66 | pendingOperator = String(""); | |
| 309 | 66 | } | |
| 310 | 58 | if !pendingOperator.isEmpty() { clauses.pushBack(pendingOperator); } | |
| 311 | 116 | return clauses; | |
| 312 | 58 | } | |
| 313 | |||
| 314 | /** | ||
| 315 | * Check whether a text is a valid constraint expression | ||
| 316 | */ | ||
| 317 | 23 | public f<bool> isValidConstraint(const String& constraint) { | |
| 318 | 23 | const String trimmed = constraint.trim(); | |
| 319 | 23 | if trimmed.isEmpty() { return false; } | |
| 320 | 22 | const Vector<String> clauses = semverSplitClauses(constraint); | |
| 321 | 40 | for unsigned long i = 0l; i < clauses.getSize(); i++ { | |
| 322 | 23 | const SemverClause clause = semverParseClause(clauses.get(i)); | |
| 323 | 23 | if !clause.valid { return false; } | |
| 324 | 23 | } | |
| 325 | 17 | return true; | |
| 326 | 23 | } | |
| 327 | |||
| 328 | /** | ||
| 329 | * Check a version against a constraint expression. Invalid versions and invalid constraints match nothing. | ||
| 330 | */ | ||
| 331 | 41 | public f<bool> satisfies(const Semver& version, const String& constraint) { | |
| 332 | 41 | if !version.valid { return false; } | |
| 333 | 36 | const Vector<String> clauses = semverSplitClauses(constraint); | |
| 334 | 60 | for unsigned long i = 0l; i < clauses.getSize(); i++ { | |
| 335 | 40 | const SemverClause clause = semverParseClause(clauses.get(i)); | |
| 336 | 40 | if !clause.valid || !clause.matches(version) { return false; } | |
| 337 | 40 | } | |
| 338 | 20 | return true; | |
| 339 | 36 | } | |
| 340 | |||
| 341 | /** | ||
| 342 | * Pick the highest version out of a list that satisfies the constraint. Pre-releases are only considered if | ||
| 343 | * nothing else matches. Returns the index into the list, or -1. | ||
| 344 | */ | ||
| 345 | 4 | public f<long> pickBestVersion(const Vector<String>& versions, const String& constraint) { | |
| 346 | 4 | long best = -1l; | |
| 347 | 4 | long bestPre = -1l; | |
| 348 | 24 | for unsigned long i = 0l; i < versions.getSize(); i++ { | |
| 349 | 20 | const Semver candidate = parseSemver(versions.get(i)); | |
| 350 | 20 | if !satisfies(candidate, constraint) { continue; } | |
| 351 | 8 | const bool stable = candidate.pre.isEmpty(); | |
| 352 | 8 | const long current = stable ? best : bestPre; | |
| 353 | 8 | bool better = current < 0l; | |
| 354 | 8 | if !better { | |
| 355 | 3 | const Semver currentVersion = parseSemver(versions.get(cast<unsigned long>(current))); | |
| 356 | 3 | better = compareSemver(candidate, currentVersion) > 0; | |
| 357 | 3 | } | |
| 358 | 8 | if better && stable { best = cast<long>(i); } | |
| 359 | 8 | if better && !stable { bestPre = cast<long>(i); } | |
| 360 | 20 | } | |
| 361 | 4 | return best >= 0l ? best : bestPre; | |
| 362 | } | ||
| 363 | |||
| 364 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 365 | // Tests | ||
| 366 | // --------------------------------------------------------------------------------------------------------------------- | ||
| 367 | |||
| 368 | 1 | #[test] | |
| 369 | f<bool> testSemverParse() { | ||
| 370 | 1 | const Semver a = parseSemver(String("v1.2.3-beta.1+abc")); | |
| 371 | 1 | assert a.valid && a.major == 1l && a.minor == 2l && a.patch == 3l && a.pre == "beta.1"; | |
| 372 | 1 | const Semver b = parseSemver(String("2")); | |
| 373 | 1 | assert b.valid && b.major == 2l && b.minor == 0l; | |
| 374 | 1 | assert !parseSemver(String("1.x")).valid; | |
| 375 | 1 | assert !parseSemver(String("abc")).valid; | |
| 376 | 1 | assert !parseSemver(String("1.2.3.4")).valid; | |
| 377 | 1 | assert !parseSemver(String("1..2")).valid; | |
| 378 | 1 | assert !parseSemver(String("1.2.3-")).valid; | |
| 379 | 1 | assert !parseSemver(String("v")).valid; | |
| 380 | 1 | assert !parseSemver(String("")).valid; | |
| 381 | 1 | assert !parseSemver(String("-1.0.0")).valid; | |
| 382 | 1 | assert !parseSemver(String("1234567890123456789.0.0")).valid; | |
| 383 | 1 | assert isStrictSemver(String("1.2.3")); | |
| 384 | 1 | assert isStrictSemver(String("1.2.3-rc.1")); | |
| 385 | 1 | assert isStrictSemver(String("1.2.3+build.5")); | |
| 386 | 1 | assert !isStrictSemver(String("v1.2.3")); | |
| 387 | 1 | assert !isStrictSemver(String("1.2")); | |
| 388 | 1 | assert !isStrictSemver(String(" 1.2.3")); | |
| 389 | 1 | return true; | |
| 390 | 1 | } | |
| 391 | |||
| 392 | 1 | #[test] | |
| 393 | f<bool> testSemverToString() { | ||
| 394 | 1 | Semver a = parseSemver(String("v1.20.300-rc.1+build")); | |
| 395 | 1 | assert a.toString() == "1.20.300-rc.1"; | |
| 396 | 1 | Semver b = parseSemver(String("0")); | |
| 397 | 1 | assert b.toString() == "0.0.0"; | |
| 398 | 1 | return true; | |
| 399 | 1 | } | |
| 400 | |||
| 401 | 1 | #[test] | |
| 402 | f<bool> testSemverCompare() { | ||
| 403 | 1 | assert compareSemver(parseSemver(String("1.2.3")), parseSemver(String("1.2.4"))) < 0; | |
| 404 | 1 | assert compareSemver(parseSemver(String("1.10.0")), parseSemver(String("1.9.0"))) > 0; | |
| 405 | 1 | assert compareSemver(parseSemver(String("1.0.0-alpha")), parseSemver(String("1.0.0"))) < 0; | |
| 406 | 1 | assert compareSemver(parseSemver(String("1.0.0-alpha.2")), parseSemver(String("1.0.0-alpha.10"))) < 0; | |
| 407 | 1 | assert compareSemver(parseSemver(String("1.0.0-alpha")), parseSemver(String("1.0.0-alpha.1"))) < 0; | |
| 408 | 1 | assert compareSemver(parseSemver(String("1.0.0-1")), parseSemver(String("1.0.0-alpha"))) < 0; | |
| 409 | 1 | assert compareSemver(parseSemver(String("1.0.0-beta")), parseSemver(String("1.0.0-alpha"))) > 0; | |
| 410 | 1 | assert compareSemver(parseSemver(String("1.0.0")), parseSemver(String("v1.0.0"))) == 0; | |
| 411 | 1 | return true; | |
| 412 | 1 | } | |
| 413 | |||
| 414 | 1 | #[test] | |
| 415 | f<bool> testSemverConstraints() { | ||
| 416 | 1 | const Semver v = parseSemver(String("1.4.2")); | |
| 417 | 1 | assert satisfies(v, String("*")); | |
| 418 | 1 | assert satisfies(v, String("1.4.2")); | |
| 419 | 1 | assert satisfies(v, String("=1.4.2")); | |
| 420 | 1 | assert !satisfies(v, String("1.4.3")); | |
| 421 | 1 | assert satisfies(v, String("^1.2.0")); | |
| 422 | 1 | assert satisfies(v, String("^1")); | |
| 423 | 1 | assert !satisfies(v, String("^2.0.0")); | |
| 424 | 1 | assert satisfies(v, String("~1.4.0")); | |
| 425 | 1 | assert !satisfies(v, String("~1.3.0")); | |
| 426 | 1 | assert satisfies(v, String("1.4.x")); | |
| 427 | 1 | assert satisfies(v, String("1.x")); | |
| 428 | 1 | assert !satisfies(v, String("2.x")); | |
| 429 | 1 | assert satisfies(v, String(">=1.0.0, <2.0.0")); | |
| 430 | 1 | assert satisfies(v, String(">= 1.0.0 < 1.5")); | |
| 431 | 1 | assert !satisfies(v, String(">1.4.2")); | |
| 432 | 1 | assert !satisfies(v, String("latest")); | |
| 433 | 1 | assert !satisfies(parseSemver(String("abc")), String("*")); | |
| 434 | 1 | assert satisfies(parseSemver(String("0.2.5")), String("^0.2.1")); | |
| 435 | 1 | assert !satisfies(parseSemver(String("0.3.0")), String("^0.2.1")); | |
| 436 | 1 | assert satisfies(parseSemver(String("0.0.3")), String("^0.0.3")); | |
| 437 | 1 | assert !satisfies(parseSemver(String("0.0.4")), String("^0.0.3")); | |
| 438 | 1 | assert isValidConstraint(String("^1.2.0")); | |
| 439 | 1 | assert isValidConstraint(String(">=1.0.0, <2.0.0")); | |
| 440 | 1 | assert isValidConstraint(String("1.2.x")); | |
| 441 | 1 | assert isValidConstraint(String("*")); | |
| 442 | 1 | assert !isValidConstraint(String("")); | |
| 443 | 1 | assert !isValidConstraint(String("^1.x")); | |
| 444 | 1 | assert !isValidConstraint(String("1.x.3")); | |
| 445 | 1 | assert !isValidConstraint(String("latest")); | |
| 446 | 1 | assert !isValidConstraint(String(">=")); | |
| 447 | 1 | return true; | |
| 448 | 1 | } | |
| 449 | |||
| 450 | 1 | #[test] | |
| 451 | f<bool> testPickBest() { | ||
| 452 | 1 | Vector<String> versions; | |
| 453 | 1 | versions.pushBack(String("v1.0.0")); | |
| 454 | 1 | versions.pushBack(String("v1.3.0")); | |
| 455 | 1 | versions.pushBack(String("v2.0.0")); | |
| 456 | 1 | versions.pushBack(String("v1.9.0-rc.1")); | |
| 457 | 1 | versions.pushBack(String("not-a-version")); | |
| 458 | 1 | assert pickBestVersion(versions, String("^1.0.0")) == 1l; | |
| 459 | 1 | assert pickBestVersion(versions, String("*")) == 2l; | |
| 460 | 1 | assert pickBestVersion(versions, String("^3.0.0")) == -1l; | |
| 461 | 1 | assert pickBestVersion(versions, String(">=1.9.0-rc.0, <2.0.0")) == 3l; | |
| 462 | 1 | return true; | |
| 463 | 1 | } | |
| 464 |