Skip to content

test warnings #1643

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
May 27, 2023
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
51 changes: 45 additions & 6 deletions test/assert.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,67 @@
import assert from "assert";

function warns(run, expected) {
function warns(run, expected = /warning/i) {
const actual = [];
const warn = console.warn;
let result;
try {
console.warn = (warning) => void actual.push(warning);
run();
assert.strictEqual(actual.length, 1);
result = run();
assert.strictEqual(actual.length, 1, "expected 1 warning");
assert.match(actual[0], expected);
} finally {
console.warn = warn;
}
return result;
}

async function warnsAsync(run, expected = /warning/i) {
const actual = [];
const warn = console.warn;
let result;
try {
console.warn = (warning) => void actual.push(warning);
result = await run();
assert.strictEqual(actual.length, 1, "expected 1 warning");
assert.match(actual[0], expected);
} finally {
console.warn = warn;
}
return result;
}

function doesNotWarn(run) {
const actual = [];
const warn = console.warn;
let result;
try {
console.warn = (warning) => void actual.push(warning);
result = run();
assert.strictEqual(actual.length, 0, "expected 0 warnings");
} finally {
console.warn = warn;
}
return result;
}

async function doesNotWarnAsync(run) {
const actual = [];
const warn = console.warn;
let result;
try {
console.warn = (warning) => void actual.push(warning);
run();
assert.strictEqual(actual.length, 0);
result = await run();
assert.strictEqual(actual.length, 0, "expected 0 warnings");
} finally {
console.warn = warn;
}
return result;
}

export default {...assert, warns, doesNotWarn};
export default {
...assert,
warns,
warnsAsync,
doesNotWarn,
doesNotWarnAsync
};
6 changes: 3 additions & 3 deletions test/plot.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import assert from "assert";
import {promises as fs} from "fs";
import * as path from "path";
import beautify from "js-beautify";
import assert from "./assert.js";
import it from "./jsdom.js";
import * as plots from "./plots/index.js";

for (const [name, plot] of Object.entries(plots)) {
it(`plot ${name}`, async () => {
const root = await plot();
const root = await (name.startsWith("warn") ? assert.warnsAsync : assert.doesNotWarnAsync)(plot);
const ext = root.tagName === "svg" ? "svg" : "html";
for (const svg of root.tagName === "svg" ? [root] : root.querySelectorAll("svg")) {
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.w3.org/2000/svg");
Expand Down Expand Up @@ -59,7 +59,7 @@ for (const [name, plot] of Object.entries(plots)) {
await fs.writeFile(diffile, actual, "utf8");
}

assert(equal, `${name} must match snapshot`);
assert.ok(equal, `${name} must match snapshot`);
});
}

Expand Down
10 changes: 5 additions & 5 deletions test/plots/color-misaligned.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export function colorMisalignedDivergingDomain() {
export function warnMisalignedDivergingDomain() {
return Plot.cellX(d3.range(-5, 6), {x: Plot.identity, fill: Plot.identity}).plot({
color: {legend: true, type: "diverging", domain: [-5, 5, 10]}
});
}

export function colorMisalignedLinearDomain() {
export function warnMisalignedLinearDomain() {
return Plot.cellX(d3.range(11), {fill: Plot.identity}).plot({
color: {legend: true, type: "linear", domain: [0, 10, 20], range: ["red", "blue"]}
});
}

export function colorMisalignedLinearDomainReverse() {
export function warnMisalignedLinearDomainReverse() {
return Plot.cellX(d3.range(11), {fill: Plot.identity}).plot({
color: {legend: true, type: "linear", domain: [0, 10, 20], reverse: true, range: ["red", "blue"]}
});
}

export function colorMisalignedLinearRange() {
export function warnMisalignedLinearRange() {
return Plot.cellX(d3.range(11), {fill: Plot.identity}).plot({
color: {legend: true, type: "linear", domain: [0, 10], range: ["red", "blue", "green"]}
});
}

export function colorMisalignedLinearRangeReverse() {
export function warnMisalignedLinearRangeReverse() {
return Plot.cellX(d3.range(11), {fill: Plot.identity}).plot({
color: {legend: true, type: "linear", domain: [0, 10], reverse: true, range: ["red", "blue", "green"]}
});
Expand Down
47 changes: 33 additions & 14 deletions test/scales/scales-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";
import assert from "assert";
import assert from "../assert.js";
import it from "../jsdom.js";

it("Plot throws an error if an ordinal position scale has a huge inferred domain", () => {
Expand Down Expand Up @@ -411,7 +411,10 @@ it("plot(…).scale(name) handles a reversed diverging scale with a descending d
});

it("plot(…).scale(name) ignores extra domain elements with a diverging scale", async () => {
const plot = Plot.plot({color: {type: "diverging", domain: [-5, 5, 10]}});
const plot = assert.warns(
() => Plot.plot({color: {type: "diverging", domain: [-5, 5, 10]}}),
/domain contains extra/
);
const {interpolate, ...color} = plot.scale("color");
scaleEqual(color, {
type: "diverging",
Expand Down Expand Up @@ -722,9 +725,13 @@ it("plot(…).scale('color') can return a “polylinear” piecewise linear scal
});

it("plot(…).scale('color') ignores extra domain elements with an explicit range", () => {
const plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
color: {type: "linear", domain: [0, 100, 200], range: ["red", "blue"]}
});
const plot = assert.warns(
() =>
Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
color: {type: "linear", domain: [0, 100, 200], range: ["red", "blue"]}
}),
/domain contains extra/
);
scaleEqual(plot.scale("color"), {
type: "linear",
domain: [0, 100],
Expand All @@ -735,9 +742,13 @@ it("plot(…).scale('color') ignores extra domain elements with an explicit rang
});

it("plot(…).scale('color') ignores extra range elements with an explicit range", () => {
const plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
color: {type: "linear", domain: [0, 100], range: ["red", "blue", "green"]}
});
const plot = assert.warns(
() =>
Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
color: {type: "linear", domain: [0, 100], range: ["red", "blue", "green"]}
}),
/range contains extra/
);
scaleEqual(plot.scale("color"), {
type: "linear",
domain: [0, 100],
Expand All @@ -748,9 +759,13 @@ it("plot(…).scale('color') ignores extra range elements with an explicit range
});

it("plot(…).scale('color') ignores extra domain elements with an explicit range when reversed", () => {
const plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
color: {type: "linear", domain: [0, 100, 200], range: ["red", "blue"], reverse: true}
});
const plot = assert.warns(
() =>
Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
color: {type: "linear", domain: [0, 100, 200], range: ["red", "blue"], reverse: true}
}),
/domain contains extra/
);
scaleEqual(plot.scale("color"), {
type: "linear",
domain: [100, 0],
Expand All @@ -761,9 +776,13 @@ it("plot(…).scale('color') ignores extra domain elements with an explicit rang
});

it("plot(…).scale('color') ignores extra range elements with an explicit range when reversed", () => {
const plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
color: {type: "linear", domain: [0, 100], range: ["red", "blue", "green"], reverse: true}
});
const plot = assert.warns(
() =>
Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
color: {type: "linear", domain: [0, 100], range: ["red", "blue", "green"], reverse: true}
}),
/range contains extra/
);
scaleEqual(plot.scale("color"), {
type: "linear",
domain: [100, 0],
Expand Down