|
| 1 | +local semver = require("crates.semver") |
| 2 | + |
| 3 | +local function match(version, requirement) |
| 4 | + local v = semver.parse_version(version) |
| 5 | + local r = semver.parse_requirement(requirement) |
| 6 | + return semver.matches_requirement(v, r) |
| 7 | +end |
| 8 | + |
| 9 | +local function assert_match(version, requirement) |
| 10 | + if not match(version, requirement) then |
| 11 | + error(string.format("version should match `%s` `%s`", version , requirement), 2) |
| 12 | + end |
| 13 | +end |
| 14 | + |
| 15 | +local function assert_error(version, requirement) |
| 16 | + if match(version, requirement) then |
| 17 | + error(string.format("version shouldn't match `%s` `%s`", version , requirement), 2) |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +describe("match requirements", function() |
| 22 | + it("caret", function() |
| 23 | + assert_match("2.0.3", "^2.0.3") |
| 24 | + assert_match("2.0.5", "^2.0.3") |
| 25 | + assert_match("2.0.5", "^2.0") |
| 26 | + assert_match("2.0.5", "^2") |
| 27 | + assert_match("0.7.5", "^0.7.3") |
| 28 | + assert_match("0.7.3", "^0.7.3") |
| 29 | + assert_match("0.7.5", "^0.7") |
| 30 | + assert_match("0.7.5", "^0") |
| 31 | + assert_match("0.0.3", "^0.0.3") |
| 32 | + assert_match("0.0.5", "^0.0") |
| 33 | + assert_match("0.0.5", "^0.0") |
| 34 | + assert_match("0.0.5", "^0") |
| 35 | + assert_match("0.9.5", "^0") |
| 36 | + assert_match("1.2.0-rc.0", "^1.2.0-rc.0") |
| 37 | + assert_match("1.2.0-rc.2", "^1.2.0-rc.0") |
| 38 | + assert_match("1.2.0", "^1.2.0-rc.0") |
| 39 | + |
| 40 | + assert_error("2.0.2", "^2.0.3") |
| 41 | + assert_error("3.0.0", "^2.0.3") |
| 42 | + assert_error("2.0.5", "^2.1") |
| 43 | + assert_error("3.0.0", "^2.1") |
| 44 | + assert_error("1.0.5", "^2") |
| 45 | + assert_error("3.0.1", "^2") |
| 46 | + assert_error("0.7.2", "^0.7.3") |
| 47 | + assert_error("0.8.0", "^0.7") |
| 48 | + assert_error("1.0.0", "^0") |
| 49 | + assert_error("0.0.3", "^0.0.2") |
| 50 | + assert_error("0.0.3", "^0.0.4") |
| 51 | + assert_error("0.1.0", "^0.0") |
| 52 | + assert_error("1.0.0", "^0") |
| 53 | + assert_error("1.2.0-rc.0", "^1.2.0-rc.2") |
| 54 | + assert_error("1.2.0-rc.6", "^1.2.0") |
| 55 | + end) |
| 56 | + |
| 57 | + it("tilde", function() |
| 58 | + assert_match("2.0.5", "~2.0.3") |
| 59 | + assert_match("2.0.5", "~2.0") |
| 60 | + assert_match("2.0.0", "~2.0") |
| 61 | + assert_match("2.0.5", "~2") |
| 62 | + |
| 63 | + assert_error("2.0.5", "~2.0.6") |
| 64 | + assert_error("2.1.0", "~2.0.6") |
| 65 | + assert_error("2.0.5", "~2.1") |
| 66 | + assert_error("4.0.0", "~3") |
| 67 | + assert_error("2.4.8", "~3") |
| 68 | + end) |
| 69 | + |
| 70 | + it("wildcard", function() |
| 71 | + assert_match("2.0.5", "2.0.*") |
| 72 | + assert_match("2.0.5", "2.*") |
| 73 | + assert_match("2.0.5", "*") |
| 74 | + |
| 75 | + assert_error("2.1.3", "2.3.*") |
| 76 | + assert_error("2.4.3", "2.3.*") |
| 77 | + assert_error("1.0.5", "2.*") |
| 78 | + assert_error("3.0.5", "2.*") |
| 79 | + end) |
| 80 | + |
| 81 | + it("equals", function() |
| 82 | + assert_match("2.0.5", "=2.0.5") |
| 83 | + assert_match("2.0.5", "=2.0") |
| 84 | + assert_match("2.0.5", "=2") |
| 85 | + assert_match("2.0.5-beta.6", "=2.0.5-beta.6") |
| 86 | + |
| 87 | + assert_error("2.0.5", "=2.0.6") |
| 88 | + assert_error("2.0.5", "=2.0.3") |
| 89 | + assert_error("2.0.5", "=2.1.5") |
| 90 | + assert_error("2.0.5", "=2.1") |
| 91 | + assert_error("2.0.5", "=3") |
| 92 | + assert_error("2.0.5", "=1") |
| 93 | + assert_error("2.0.5-beta.6", "=2.0.5") |
| 94 | + assert_error("2.0.5-beta.6", "=2.0") |
| 95 | + assert_error("2.0.5-beta.6", "=2") |
| 96 | + end) |
| 97 | + |
| 98 | + it("less than", function() |
| 99 | + assert_match("2.0.5", "<2.0.6") |
| 100 | + assert_match("2.0.5", "<2.1") |
| 101 | + assert_match("2.0.5", "<3") |
| 102 | + assert_match("2.0.0-pre.0", "<2.0.0") |
| 103 | + |
| 104 | + assert_error("2.1.5", "<2.1.5") |
| 105 | + assert_error("2.1.5", "<2.1.4") |
| 106 | + assert_error("2.1.5", "<2.0.6") |
| 107 | + assert_error("2.1.5", "<2.1") |
| 108 | + assert_error("2.1.5", "<2") |
| 109 | + assert_error("2.0.1-pre.0", "<2.0.0") |
| 110 | + end) |
| 111 | + |
| 112 | + it("less than or equal", function() |
| 113 | + assert_match("2.1.5", "<=2.1.6") |
| 114 | + assert_match("2.1.5", "<=2.1.5") |
| 115 | + assert_match("2.1.5", "<=2.1") |
| 116 | + assert_match("2.1.5", "<=2.1") |
| 117 | + assert_match("2.1.5", "<=3") |
| 118 | + assert_match("2.1.5", "<=2") |
| 119 | + assert_match("3.0.0-beta.3", "<=3") |
| 120 | + assert_match("3.1.1-beta.3", "<=3.1") |
| 121 | + |
| 122 | + assert_error("2.1.5", "<=2.1.4") |
| 123 | + assert_error("2.1.5", "<=2.0.6") |
| 124 | + assert_error("2.1.5", "<=2.0") |
| 125 | + assert_error("2.1.5", "<=1") |
| 126 | + assert_error("3.1.1-beta.3", "<=3.0") |
| 127 | + end) |
| 128 | + |
| 129 | + it("greater than", function() |
| 130 | + assert_match("2.4.5", ">2.4.3") |
| 131 | + assert_match("2.4.5", ">2.3") |
| 132 | + assert_match("2.4.5", ">1") |
| 133 | + assert_match("2.0.0-beta.3", ">1.9.9") |
| 134 | + |
| 135 | + assert_error("2.1.5", ">2.1.5") |
| 136 | + assert_error("2.1.5", ">2.1.6") |
| 137 | + assert_error("2.1.5", ">2.2.0") |
| 138 | + assert_error("2.1.5", ">2.1") |
| 139 | + assert_error("2.1.5", ">2") |
| 140 | + assert_error("2.0.0-beta.3", ">2") |
| 141 | + end) |
| 142 | + |
| 143 | + it("greater than or equal", function() |
| 144 | + assert_match("2.1.5", ">=2.0.6") |
| 145 | + assert_match("2.1.5", ">=2.1.5") |
| 146 | + assert_match("2.1.5", ">=2.1") |
| 147 | + assert_match("2.1.5", ">=2.1") |
| 148 | + assert_match("2.1.5", ">=2") |
| 149 | + assert_match("2.0.1-beta.3", ">=2.0.0") |
| 150 | + |
| 151 | + assert_error("2.1.5", ">=2.1.6") |
| 152 | + assert_error("2.1.5", ">=2.3.3") |
| 153 | + assert_error("2.1.5", ">=2.2") |
| 154 | + assert_error("2.1.5", ">=3") |
| 155 | + assert_error("2.0.0-beta.3", ">=2") |
| 156 | + end) |
| 157 | +end) |
0 commit comments