Skip to content

Add unit tests for VersionRange #40114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -388,4 +388,4 @@ namespace ts {
function formatComparator(comparator: Comparator) {
return `${comparator.operator}${comparator.operand}`;
}
}
}
23 changes: 23 additions & 0 deletions src/testRunner/unittests/semver.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down