Skip to content

Commit 6fea7ff

Browse files
authored
Add unit tests for VersionRange (microsoft#40114)
* Add unit tests for VersionRange Make it easier to understand the intended semantics next time I have to read this code. * I miss prettier
1 parent dbab46c commit 6fea7ff

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/compiler/semver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ namespace ts {
8484
return compareValues(this.major, other.major)
8585
|| compareValues(this.minor, other.minor)
8686
|| compareValues(this.patch, other.patch)
87-
|| comparePrerelaseIdentifiers(this.prerelease, other.prerelease);
87+
|| comparePrereleaseIdentifiers(this.prerelease, other.prerelease);
8888
}
8989

9090
increment(field: "major" | "minor" | "patch") {
@@ -120,7 +120,7 @@ namespace ts {
120120
};
121121
}
122122

123-
function comparePrerelaseIdentifiers(left: readonly string[], right: readonly string[]) {
123+
function comparePrereleaseIdentifiers(left: readonly string[], right: readonly string[]) {
124124
// https://semver.org/#spec-item-11
125125
// > When major, minor, and patch are equal, a pre-release version has lower precedence
126126
// > than a normal version.
@@ -388,4 +388,4 @@ namespace ts {
388388
function formatComparator(comparator: Comparator) {
389389
return `${comparator.operator}${comparator.operand}`;
390390
}
391-
}
391+
}

src/testRunner/unittests/semver.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
namespace ts {
22
import theory = Utils.theory;
33
describe("unittests:: semver", () => {
4+
describe("VersionRange", () => {
5+
function assertVersionRange(version: string, good: string[], bad: string[]): () => void {
6+
return () => {
7+
const range = VersionRange.tryParse(version)!;
8+
assert(range);
9+
for (const g of good) {
10+
assert.isTrue(range.test(g), g);
11+
}
12+
for (const b of bad) {
13+
assert.isFalse(range.test(b), b);
14+
}
15+
};
16+
}
17+
it("< works", assertVersionRange("<3.8.0", ["3.6", "3.7"], ["3.8", "3.9", "4.0"]));
18+
it("<= works", assertVersionRange("<=3.8.0", ["3.6", "3.7", "3.8"], ["3.9", "4.0"]));
19+
it("> works", assertVersionRange(">3.8.0", ["3.9", "4.0"], ["3.6", "3.7", "3.8"]));
20+
it(">= works", assertVersionRange(">=3.8.0", ["3.8", "3.9", "4.0"], ["3.6", "3.7"]));
21+
22+
it("< works with prerelease", assertVersionRange("<3.8.0-0", ["3.6", "3.7"], ["3.8", "3.9", "4.0"]));
23+
it("<= works with prerelease", assertVersionRange("<=3.8.0-0", ["3.6", "3.7"], ["3.8", "3.9", "4.0"]));
24+
it("> works with prerelease", assertVersionRange(">3.8.0-0", ["3.8", "3.9", "4.0"], ["3.6", "3.7"]));
25+
it(">= works with prerelease", assertVersionRange(">=3.8.0-0", ["3.8", "3.9", "4.0"], ["3.6", "3.7"]));
26+
});
427
describe("Version", () => {
528
function assertVersion(version: Version, [major, minor, patch, prerelease, build]: [number, number, number, string[]?, string[]?]) {
629
assert.strictEqual(version.major, major);

0 commit comments

Comments
 (0)