From 336c406cbe6b99d0d02b7c5ae50d03e60ad759e4 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Fri, 26 May 2023 16:38:24 -0700 Subject: [PATCH 1/2] test warnings --- test/assert.js | 43 +++++++++++++++++-- ...tml => warnMisalignedDivergingDomain.html} | 0 ...n.html => warnMisalignedLinearDomain.html} | 0 ...=> warnMisalignedLinearDomainReverse.html} | 0 ...ge.html => warnMisalignedLinearRange.html} | 0 ... => warnMisalignedLinearRangeReverse.html} | 0 test/plot.js | 6 +-- test/plots/color-misaligned.ts | 10 ++--- test/scales/scales-test.js | 41 ++++++++++++------ 9 files changed, 74 insertions(+), 26 deletions(-) rename test/output/{colorMisalignedDivergingDomain.html => warnMisalignedDivergingDomain.html} (100%) rename test/output/{colorMisalignedLinearDomain.html => warnMisalignedLinearDomain.html} (100%) rename test/output/{colorMisalignedLinearDomainReverse.html => warnMisalignedLinearDomainReverse.html} (100%) rename test/output/{colorMisalignedLinearRange.html => warnMisalignedLinearRange.html} (100%) rename test/output/{colorMisalignedLinearRangeReverse.html => warnMisalignedLinearRangeReverse.html} (100%) diff --git a/test/assert.js b/test/assert.js index 24a81aeb52..8ae5010e83 100644 --- a/test/assert.js +++ b/test/assert.js @@ -1,28 +1,63 @@ import assert from "assert"; -function warns(run, expected) { +function warns(run, expected = /warning/i) { const actual = []; const warn = console.warn; try { console.warn = (warning) => void actual.push(warning); run(); - assert.strictEqual(actual.length, 1); + assert.strictEqual(actual.length, 1, "expected 1 warning"); assert.match(actual[0], expected); } finally { console.warn = warn; } } +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; try { console.warn = (warning) => void actual.push(warning); run(); - assert.strictEqual(actual.length, 0); + assert.strictEqual(actual.length, 0, "expected 0 warnings"); + } finally { + console.warn = warn; + } +} + +async function doesNotWarnAsync(run) { + const actual = []; + const warn = console.warn; + let result; + try { + console.warn = (warning) => void actual.push(warning); + 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 +}; diff --git a/test/output/colorMisalignedDivergingDomain.html b/test/output/warnMisalignedDivergingDomain.html similarity index 100% rename from test/output/colorMisalignedDivergingDomain.html rename to test/output/warnMisalignedDivergingDomain.html diff --git a/test/output/colorMisalignedLinearDomain.html b/test/output/warnMisalignedLinearDomain.html similarity index 100% rename from test/output/colorMisalignedLinearDomain.html rename to test/output/warnMisalignedLinearDomain.html diff --git a/test/output/colorMisalignedLinearDomainReverse.html b/test/output/warnMisalignedLinearDomainReverse.html similarity index 100% rename from test/output/colorMisalignedLinearDomainReverse.html rename to test/output/warnMisalignedLinearDomainReverse.html diff --git a/test/output/colorMisalignedLinearRange.html b/test/output/warnMisalignedLinearRange.html similarity index 100% rename from test/output/colorMisalignedLinearRange.html rename to test/output/warnMisalignedLinearRange.html diff --git a/test/output/colorMisalignedLinearRangeReverse.html b/test/output/warnMisalignedLinearRangeReverse.html similarity index 100% rename from test/output/colorMisalignedLinearRangeReverse.html rename to test/output/warnMisalignedLinearRangeReverse.html diff --git a/test/plot.js b/test/plot.js index 65501d953a..379a4dc39c 100644 --- a/test/plot.js +++ b/test/plot.js @@ -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"); @@ -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`); }); } diff --git a/test/plots/color-misaligned.ts b/test/plots/color-misaligned.ts index 765c974e60..c3ea3e27c7 100644 --- a/test/plots/color-misaligned.ts +++ b/test/plots/color-misaligned.ts @@ -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"]} }); diff --git a/test/scales/scales-test.js b/test/scales/scales-test.js index 662d8ef29f..d0a462e8fc 100644 --- a/test/scales/scales-test.js +++ b/test/scales/scales-test.js @@ -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", () => { @@ -411,7 +411,8 @@ 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]}}); + let plot; + assert.warns(() => (plot = Plot.plot({color: {type: "diverging", domain: [-5, 5, 10]}})), /domain contains extra/); const {interpolate, ...color} = plot.scale("color"); scaleEqual(color, { type: "diverging", @@ -722,9 +723,12 @@ 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"]} - }); + let plot; + assert.warns(() => { + plot = 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], @@ -735,9 +739,12 @@ 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"]} - }); + let plot; + assert.warns(() => { + plot = 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], @@ -748,9 +755,12 @@ 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} - }); + let plot; + assert.warns(() => { + plot = 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], @@ -761,9 +771,12 @@ 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} - }); + let plot; + assert.warns(() => { + plot = 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], From 85d71e9948d549a127060a5165d0acf6d2255499 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Fri, 26 May 2023 16:54:18 -0700 Subject: [PATCH 2/2] warn asserts return value --- test/assert.js | 8 ++++-- test/scales/scales-test.js | 58 +++++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/test/assert.js b/test/assert.js index 8ae5010e83..d70d760d52 100644 --- a/test/assert.js +++ b/test/assert.js @@ -3,14 +3,16 @@ import assert from "assert"; function warns(run, expected = /warning/i) { const actual = []; const warn = console.warn; + let result; try { console.warn = (warning) => void actual.push(warning); - run(); + 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) { @@ -31,13 +33,15 @@ async function warnsAsync(run, expected = /warning/i) { function doesNotWarn(run) { const actual = []; const warn = console.warn; + let result; try { console.warn = (warning) => void actual.push(warning); - run(); + result = run(); assert.strictEqual(actual.length, 0, "expected 0 warnings"); } finally { console.warn = warn; } + return result; } async function doesNotWarnAsync(run) { diff --git a/test/scales/scales-test.js b/test/scales/scales-test.js index d0a462e8fc..736ac984c9 100644 --- a/test/scales/scales-test.js +++ b/test/scales/scales-test.js @@ -411,8 +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 () => { - let plot; - assert.warns(() => (plot = Plot.plot({color: {type: "diverging", domain: [-5, 5, 10]}})), /domain contains extra/); + 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", @@ -723,12 +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", () => { - let plot; - assert.warns(() => { - plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({ - color: {type: "linear", domain: [0, 100, 200], range: ["red", "blue"]} - }); - }, /domain contains extra/); + 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], @@ -739,12 +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", () => { - let plot; - assert.warns(() => { - plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({ - color: {type: "linear", domain: [0, 100], range: ["red", "blue", "green"]} - }); - }, /range contains extra/); + 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], @@ -755,12 +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", () => { - let plot; - assert.warns(() => { - plot = 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/); + 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], @@ -771,12 +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", () => { - let plot; - assert.warns(() => { - plot = 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/); + 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],