From 4058a3fd85677669f978ba8d11e3b1e2f3903bcd Mon Sep 17 00:00:00 2001 From: William Chargin Date: Fri, 27 Jul 2018 12:30:28 -0700 Subject: [PATCH] Extract `dedent` to `util`, adding tests (#538) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: There have been a couple of occasions on which we’ve considered using it, but didn’t want to require from `app/`. Test Plan: Unit tests added, with full coverage. wchargin-branch: extract-dedent --- src/app/server.js | 2 +- src/{app => util}/dedent.js | 0 src/util/dedent.test.js | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) rename src/{app => util}/dedent.js (100%) create mode 100644 src/util/dedent.test.js diff --git a/src/app/server.js b/src/app/server.js index fbb2eae..db117c4 100644 --- a/src/app/server.js +++ b/src/app/server.js @@ -7,7 +7,7 @@ import {match, RouterContext} from "react-router"; import {createRoutes} from "./createRoutes"; import {resolveTitleFromPath} from "./routeData"; -import dedent from "./dedent"; +import dedent from "../util/dedent"; export default function render( locals: {+path: string, +assets: {[string]: string}}, diff --git a/src/app/dedent.js b/src/util/dedent.js similarity index 100% rename from src/app/dedent.js rename to src/util/dedent.js diff --git a/src/util/dedent.test.js b/src/util/dedent.test.js new file mode 100644 index 0000000..d8b64bc --- /dev/null +++ b/src/util/dedent.test.js @@ -0,0 +1,47 @@ +// @flow + +import dedent from "./dedent"; + +describe("util/dedent", () => { + it("dedents a simple example", () => { + const actual = dedent`\ + hello + good + world + `; + const expected = "hello\n good\nworld\n"; + expect(actual).toEqual(expected); + }); + + it("interpolates components", () => { + const ell = "l"; + const actual = dedent`\ + he${ell}${ell}o + good + wor${ell}d + `; + const expected = "hello\n good\nworld\n"; + expect(actual).toEqual(expected); + }); + + it("does not strip leading whitespace in components", () => { + // See: https://github.com/wchargin/wchargin.github.io/commit/06475d4cc44a0437c911dd2d4d6275be4381142e + const code = 'if (true) {\n console.log("hi");\n}'; + const actual = dedent`\ +
${code}
+ `; + const expected = `
${code}
\n`; + expect(actual).toEqual(expected); + }); + + it("does not strip trailing backslashes", () => { + // See: https://github.com/wchargin/wchargin.github.io/commit/06475d4cc44a0437c911dd2d4d6275be4381142e + const code = "printf '%s' \\\n wat"; + const actual = dedent`\ + $ cat foo.sh + ${code} + `; + const expected = `$ cat foo.sh\n${code}\n`; + expect(actual).toEqual(expected); + }); +});