Add unit tests for VersionRange (#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
This commit is contained in:
Nathan Shively-Sanders 2020-08-18 13:06:17 -07:00 committed by GitHub
parent dbab46c363
commit 6fea7ff536
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View file

@ -84,7 +84,7 @@ namespace ts {
return compareValues(this.major, other.major)
|| compareValues(this.minor, other.minor)
|| compareValues(this.patch, other.patch)
|| comparePrerelaseIdentifiers(this.prerelease, other.prerelease);
|| comparePrereleaseIdentifiers(this.prerelease, other.prerelease);
}
increment(field: "major" | "minor" | "patch") {
@ -120,7 +120,7 @@ namespace ts {
};
}
function comparePrerelaseIdentifiers(left: readonly string[], right: readonly string[]) {
function comparePrereleaseIdentifiers(left: readonly string[], right: readonly string[]) {
// https://semver.org/#spec-item-11
// > When major, minor, and patch are equal, a pre-release version has lower precedence
// > than a normal version.
@ -388,4 +388,4 @@ namespace ts {
function formatComparator(comparator: Comparator) {
return `${comparator.operator}${comparator.operand}`;
}
}
}

View file

@ -1,6 +1,29 @@
namespace ts {
import theory = Utils.theory;
describe("unittests:: semver", () => {
describe("VersionRange", () => {
function assertVersionRange(version: string, good: string[], bad: string[]): () => void {
return () => {
const range = VersionRange.tryParse(version)!;
assert(range);
for (const g of good) {
assert.isTrue(range.test(g), g);
}
for (const b of bad) {
assert.isFalse(range.test(b), b);
}
};
}
it("< works", assertVersionRange("<3.8.0", ["3.6", "3.7"], ["3.8", "3.9", "4.0"]));
it("<= works", assertVersionRange("<=3.8.0", ["3.6", "3.7", "3.8"], ["3.9", "4.0"]));
it("> works", assertVersionRange(">3.8.0", ["3.9", "4.0"], ["3.6", "3.7", "3.8"]));
it(">= works", assertVersionRange(">=3.8.0", ["3.8", "3.9", "4.0"], ["3.6", "3.7"]));
it("< works with prerelease", assertVersionRange("<3.8.0-0", ["3.6", "3.7"], ["3.8", "3.9", "4.0"]));
it("<= works with prerelease", assertVersionRange("<=3.8.0-0", ["3.6", "3.7"], ["3.8", "3.9", "4.0"]));
it("> works with prerelease", assertVersionRange(">3.8.0-0", ["3.8", "3.9", "4.0"], ["3.6", "3.7"]));
it(">= works with prerelease", assertVersionRange(">=3.8.0-0", ["3.8", "3.9", "4.0"], ["3.6", "3.7"]));
});
describe("Version", () => {
function assertVersion(version: Version, [major, minor, patch, prerelease, build]: [number, number, number, string[]?, string[]?]) {
assert.strictEqual(version.major, major);