Skip to content
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
52 changes: 52 additions & 0 deletions packages/compute-baseline/src/browser-compat-data/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ describe("Release", function () {
});

describe("compare()", function () {
it("throws when comparing between two browsers", function () {
const cr = browser("chrome");
const ed = browser("edge");

assert.throws(
() => cr.version("100").inRange(ed.version("79"), cr.version("125")),
Error,
);
});

it("returns 0 for equivalent releases", function () {
const chrome100 = browser("chrome").version("100");
assert.equal(chrome100.compare(chrome100), 0);
Expand Down Expand Up @@ -87,4 +97,46 @@ describe("Release", function () {
assert.equal(safariPreview.isPrerelease(), true);
});
});

describe("inRange()", function () {
it("throws when comparing between two browsers", function () {
const cr = browser("chrome");
const fx = browser("firefox");

assert.throws(() => cr.version("50").inRange(fx.version("50")), Error);
});

it("handles closed ranges", function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for when the version equals the end version, to show the range is exclusive of the end point?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with 7c2bbad.

const cr = browser("chrome");

// Start of range is inclusive
assert.equal(
cr.version("1").inRange(cr.version("1"), cr.version("125")),
true,
);

// End of range is exclusive
assert.equal(
cr.version("20").inRange(cr.version("1"), cr.version("20")),
false,
);

assert.equal(
cr.version("1").inRange(cr.version("10"), cr.version("15")),
false,
);
assert.equal(
cr.version("100").inRange(cr.version("10"), cr.version("15")),
false,
);
});

it("handles open ranges", function () {
const cr = browser("chrome");
assert.equal(cr.version("1").inRange(cr.version("1")), true);
assert.equal(cr.version("1").inRange(cr.version("10")), false);
assert.equal(cr.version("100").inRange(cr.version("10")), true);
assert.equal(cr.version("preview").inRange(cr.version("10")), true);
});
});
});
13 changes: 13 additions & 0 deletions packages/compute-baseline/src/browser-compat-data/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ export class Release {
return this.releaseIndex - otherRelease.releaseIndex;
}

/**
* Check if this release is the same as or after a starting release and,
* optionally, before an ending release.
*/
inRange(start: Release, end?: Release): boolean {
const onOrAfterStart = this.compare(start) >= 0;
if (end) {
const beforeEnd = this.compare(end) < 0;
return onOrAfterStart && beforeEnd;
}
return onOrAfterStart;
}

isPrerelease(): boolean {
if (["beta", "nightly", "planned"].includes(this.data.status)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,18 @@ describe("statements", function () {
assert.equal(s.version_removed, "2");
});

it("returns false for undefined", function () {
const s = new SupportStatement({ version_added: "1" });
it("returns false", function () {
const s = new SupportStatement({
version_added: "1",
version_removed: false,
});
assert.equal(s.version_removed, false);
});

it("returns undefined", function () {
const s = new SupportStatement({ version_added: "1" });
assert.equal(s.version_removed, undefined);
});
});
});

Expand Down Expand Up @@ -163,6 +171,7 @@ describe("statements", function () {
);
});
});

describe("#supportedBy", function () {
it("returns an array of releases represented by the statement", function () {
const st = new RealSupportStatement(
Expand All @@ -186,5 +195,148 @@ describe("statements", function () {
assert.equal(rels.length, browser("chrome").releases.length - 10);
});
});

describe("supportedIn()", function () {
it("throws when browser is undefined", function () {
const cr = browser("chrome");
const statement = new RealSupportStatement({ version_added: "1" });
assert.throws(() => statement.supportedIn(cr.current()), Error);
});

it("throws when release does not correspond to the statement's browser", function () {
const statement = new RealSupportStatement(
{ version_added: "1" },
browser("chrome"),
);
assert.throws(
() => statement.supportedIn(browser("firefox").current()),
Error,
);
});

it("returns supported when release is on after version_added", function () {
const cr = browser("chrome");
const unranged = new RealSupportStatement({ version_added: "100" }, cr);
const ranged = new RealSupportStatement({ version_added: "≤100" }, cr);

assert.equal(unranged.supportedIn(cr.version("100")).supported, true);
assert.equal(unranged.supportedIn(cr.version("101")).supported, true);
assert.equal(unranged.supportedIn(cr.current()).supported, true);
assert.equal(
unranged.supportedIn(cr.releases.at(-1) as any).supported,
true,
);

assert.equal(ranged.supportedIn(cr.version("99")).supported, null);
assert.equal(ranged.supportedIn(cr.version("100")).supported, true);
assert.equal(ranged.supportedIn(cr.version("101")).supported, true);
assert.equal(ranged.supportedIn(cr.current()).supported, true);
assert.equal(
ranged.supportedIn(cr.releases.at(-1) as any).supported,
true,
);
});

it("returns supported when release is on after version_added and before version_removed", function () {
const cr = browser("chrome");
const unranged = new RealSupportStatement(
{ version_added: "100", version_removed: "125" },
cr,
);
const ranged = new RealSupportStatement(
{ version_added: "≤100", version_removed: "125" },
cr,
);

assert.equal(unranged.supportedIn(cr.version("99")).supported, false);
assert.equal(unranged.supportedIn(cr.version("100")).supported, true);
assert.equal(unranged.supportedIn(cr.version("101")).supported, true);
assert.equal(unranged.supportedIn(cr.version("124")).supported, true);
assert.equal(unranged.supportedIn(cr.version("125")).supported, false);

assert.equal(ranged.supportedIn(cr.version("99")).supported, null);
assert.equal(ranged.supportedIn(cr.version("100")).supported, true);
assert.equal(ranged.supportedIn(cr.version("101")).supported, true);
assert.equal(ranged.supportedIn(cr.version("124")).supported, true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not the 125 assert here, so it's the same asserts for unranged and ranged? (You might remove 101 if you want to reduce the number of rows.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with 4e0110b.

assert.equal(unranged.supportedIn(cr.version("125")).supported, false);
});

it("returns unknown support when release is before ranged version_added", function () {
const cr = browser("chrome");
const rangedOpen = new RealSupportStatement(
{ version_added: "≤100" },
cr,
);
const rangedClosed = new RealSupportStatement(
{ version_added: "≤100", version_removed: "125" },
cr,
);

assert.equal(rangedOpen.supportedIn(cr.version("99")).supported, null);
assert.equal(
rangedClosed.supportedIn(cr.version("99")).supported,
null,
);
});

it("returns unknown support when release is after version_added and before ranged version_removed", function () {
const cr = browser("chrome");
const rangedEnd = new RealSupportStatement(
{ version_added: "100", version_removed: "≤125" },
cr,
);

assert.equal(rangedEnd.supportedIn(cr.version("100")).supported, true);
assert.equal(rangedEnd.supportedIn(cr.version("101")).supported, null);
assert.equal(rangedEnd.supportedIn(cr.version("124")).supported, null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also test 101 here to make it clear that there's just a single release we're sure about?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with 4e0110b.

assert.equal(rangedEnd.supportedIn(cr.version("125")).supported, false);
});

it("returns unsupported when statement is version_added false", function () {
const cr = browser("chrome");
const statement = new RealSupportStatement(
{ version_added: false },
cr,
);

for (const release of cr.releases) {
assert.equal(statement.supportedIn(release).supported, false);
}
});

it("returns unsupported when release is before fixed version_added", function () {
const cr = browser("chrome");
const unranged = new RealSupportStatement({ version_added: "100" }, cr);
assert.equal(unranged.supportedIn(cr.version("99")).supported, false);
});

it("returns unsupported when release is on or after version_removed", function () {
const cr = browser("chrome");

const unranged = new RealSupportStatement(
{ version_added: "1", version_removed: "10" },
cr,
);
assert.equal(unranged.supportedIn(cr.version("10")).supported, false);
assert.equal(unranged.supportedIn(cr.version("11")).supported, false);
assert.equal(unranged.supportedIn(cr.current()).supported, false);
assert.equal(
unranged.supportedIn(cr.releases.at(-1) as any).supported,
false,
);

const ranged = new RealSupportStatement(
{ version_added: "≤5", version_removed: "10" },
cr,
);
assert.equal(ranged.supportedIn(cr.version("10")).supported, false);
assert.equal(ranged.supportedIn(cr.version("11")).supported, false);
assert.equal(ranged.supportedIn(cr.current()).supported, false);
assert.equal(
ranged.supportedIn(cr.releases.at(-1) as any).supported,
false,
);
});
});
});
});
Loading