|
1 | 1 | // Copyright 2018-2025 the Deno authors. MIT license. |
2 | 2 | import { dedent } from "./unstable_dedent.ts"; |
3 | 3 | import { assertEquals } from "@std/assert"; |
| 4 | +import { stub } from "@std/testing/mock"; |
4 | 5 |
|
5 | 6 | Deno.test("dedent() handles example 1", () => { |
6 | 7 | assertEquals( |
@@ -78,3 +79,99 @@ Deno.test("dedent() handles multiline substitution", () => { |
78 | 79 | `; |
79 | 80 | assertEquals(outer, "1\n2\n3\n4"); |
80 | 81 | }); |
| 82 | + |
| 83 | +Deno.test("dedent() handles mixed tabs and spaces", async (t) => { |
| 84 | + // @ts-ignore augmenting globalThis so we don't need to resort to bare `eval` |
| 85 | + using _ = stub(globalThis, "dedent", dedent); |
| 86 | + |
| 87 | + await t.step("with partial common prefix", () => { |
| 88 | + assertEquals( |
| 89 | + globalThis.eval(`dedent\`\n a\n \tb\n\``), |
| 90 | + " a\n\tb", |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + await t.step("with no common prefix", () => { |
| 95 | + assertEquals( |
| 96 | + globalThis.eval(`dedent\`\n\t a\n \tb\n\``), |
| 97 | + "\t a\n \tb", |
| 98 | + ); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +Deno.test("dedent() handles blank lines correctly", async (t) => { |
| 103 | + // @ts-ignore augmenting globalThis so we don't need to resort to bare `eval` |
| 104 | + using _ = stub(globalThis, "dedent", dedent); |
| 105 | + |
| 106 | + for (const lineEnding of ["\n", "\r\n"]) { |
| 107 | + // CRLF actually doesn't change the output, as literal CRLFs in template literals in JS files are read as `\n` |
| 108 | + // (this behavior is in the JS spec, not library behavior). |
| 109 | + await t.step( |
| 110 | + `${lineEnding === "\n" ? "LF" : "CRLF"} line ending`, |
| 111 | + async (t) => { |
| 112 | + for (const space of [" ", "\t"]) { |
| 113 | + const spaceName = space === " " ? "space" : "tab"; |
| 114 | + await t.step(`${spaceName}s`, async (t) => { |
| 115 | + for (const indent of [0, 1, 2]) { |
| 116 | + for (const between of [0, 1, 2]) { |
| 117 | + // these cases won't fully dedent, which is probably (??) fine |
| 118 | + if (indent === 0 && between !== 0) continue; |
| 119 | + |
| 120 | + const testName = |
| 121 | + `${indent}-${spaceName} indent with ${between} ${spaceName}s between`; |
| 122 | + |
| 123 | + await t.step(testName, () => { |
| 124 | + const source = [ |
| 125 | + "", |
| 126 | + `${space.repeat(indent)}a`, |
| 127 | + space.repeat(between), |
| 128 | + `${space.repeat(indent)}b`, |
| 129 | + "", |
| 130 | + ].join(lineEnding); |
| 131 | + |
| 132 | + const result = globalThis.eval(`dedent\`${source}\``); |
| 133 | + assertEquals(result, "a\n\nb"); |
| 134 | + }); |
| 135 | + |
| 136 | + // these cases will strip the first-line/last-line indents, which is probably (??) fine |
| 137 | + if (indent === 0) continue; |
| 138 | + |
| 139 | + await t.step( |
| 140 | + `${testName} preserves added first-line indent`, |
| 141 | + () => { |
| 142 | + const source = [ |
| 143 | + "", |
| 144 | + `${space.repeat(indent + 1)}a`, |
| 145 | + space.repeat(between), |
| 146 | + `${space.repeat(indent)}b`, |
| 147 | + "", |
| 148 | + ].join(lineEnding); |
| 149 | + |
| 150 | + const result = globalThis.eval(`dedent\`${source}\``); |
| 151 | + assertEquals(result, `${space}a\n\nb`); |
| 152 | + }, |
| 153 | + ); |
| 154 | + |
| 155 | + await t.step( |
| 156 | + `${testName} preserves added last-line indent`, |
| 157 | + () => { |
| 158 | + const source = [ |
| 159 | + "", |
| 160 | + `${space.repeat(indent)}a`, |
| 161 | + space.repeat(between), |
| 162 | + `${space.repeat(indent + 1)}b`, |
| 163 | + "", |
| 164 | + ].join(lineEnding); |
| 165 | + |
| 166 | + const result = globalThis.eval(`dedent\`${source}\``); |
| 167 | + assertEquals(result, `a\n\n${space}b`); |
| 168 | + }, |
| 169 | + ); |
| 170 | + } |
| 171 | + } |
| 172 | + }); |
| 173 | + } |
| 174 | + }, |
| 175 | + ); |
| 176 | + } |
| 177 | +}); |
0 commit comments