From 6fea7ff5364bbcb5f4da82e5dbf903e9355a97cf Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 18 Aug 2020 13:06:17 -0700 Subject: [PATCH] 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 --- src/compiler/semver.ts | 6 +++--- src/testRunner/unittests/semver.ts | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/compiler/semver.ts b/src/compiler/semver.ts index 1c77d24d072..8827962deaf 100644 --- a/src/compiler/semver.ts +++ b/src/compiler/semver.ts @@ -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}`; } -} \ No newline at end of file +} diff --git a/src/testRunner/unittests/semver.ts b/src/testRunner/unittests/semver.ts index b3447ca77b0..2e6a61fbad8 100644 --- a/src/testRunner/unittests/semver.ts +++ b/src/testRunner/unittests/semver.ts @@ -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);