|
| 1 | +import * as assert from "assert"; |
| 2 | +import { MarkdownPrefixStripper } from "../../../src/modelFactory/markdownPrefixStripper"; |
| 3 | + |
| 4 | +suite("MarkdownPrefixStripper tests", () => { |
| 5 | + |
| 6 | + suite("strip()", () => { |
| 7 | + |
| 8 | + test("strips blockquote prefix from all lines", () => { |
| 9 | + const input = "> | col1 | col2 |\n> |------|------|\n> | a | b |"; |
| 10 | + const result = createSut().strip(input); |
| 11 | + |
| 12 | + assert.strictEqual(result.strippedText, "| col1 | col2 |\n|------|------|\n| a | b |"); |
| 13 | + assert.deepStrictEqual(result.prefixes, ["> ", "> ", "> "]); |
| 14 | + }); |
| 15 | + |
| 16 | + test("strips nested blockquote prefix from all lines", () => { |
| 17 | + const input = ">> | col1 | col2 |\n>> |------|\n>> | a |"; |
| 18 | + const result = createSut().strip(input); |
| 19 | + |
| 20 | + assert.strictEqual(result.strippedText, "| col1 | col2 |\n|------|\n| a |"); |
| 21 | + assert.deepStrictEqual(result.prefixes, [">> ", ">> ", ">> "]); |
| 22 | + }); |
| 23 | + |
| 24 | + test("strips blockquote prefix with spaces before >", () => { |
| 25 | + const input = " > | col1 |\n > |------|\n > | a |"; |
| 26 | + const result = createSut().strip(input); |
| 27 | + |
| 28 | + assert.strictEqual(result.strippedText, "| col1 |\n|------|\n| a |"); |
| 29 | + assert.deepStrictEqual(result.prefixes, [" > ", " > ", " > "]); |
| 30 | + }); |
| 31 | + |
| 32 | + test("strips numbered list marker when followed by whitespace and pipe", () => { |
| 33 | + const input = "1.\t|col1|col2|\n\t|-|-|\n\t|a|b|"; |
| 34 | + const result = createSut().strip(input); |
| 35 | + |
| 36 | + assert.strictEqual(result.strippedText, "\t|col1|col2|\n\t|-|-|\n\t|a|b|"); |
| 37 | + assert.deepStrictEqual(result.prefixes, ["1.", "", ""]); |
| 38 | + }); |
| 39 | + |
| 40 | + test("strips numbered list marker with closing parenthesis", () => { |
| 41 | + const input = "1) |col1|\n |-|\n |a|"; |
| 42 | + const result = createSut().strip(input); |
| 43 | + |
| 44 | + assert.strictEqual(result.strippedText, " |col1|\n |-|\n |a|"); |
| 45 | + assert.deepStrictEqual(result.prefixes, ["1)", "", ""]); |
| 46 | + }); |
| 47 | + |
| 48 | + test("strips bullet list marker (-) when followed by whitespace and pipe", () => { |
| 49 | + const input = "- |col1|col2|\n |-|-|\n |x|y|"; |
| 50 | + const result = createSut().strip(input); |
| 51 | + |
| 52 | + assert.strictEqual(result.strippedText, " |col1|col2|\n |-|-|\n |x|y|"); |
| 53 | + assert.deepStrictEqual(result.prefixes, ["-", "", ""]); |
| 54 | + }); |
| 55 | + |
| 56 | + test("strips bullet list marker (*) when followed by whitespace and pipe", () => { |
| 57 | + const input = "* |col1|col2|\n |-|-|\n |x|y|"; |
| 58 | + const result = createSut().strip(input); |
| 59 | + |
| 60 | + assert.strictEqual(result.strippedText, " |col1|col2|\n |-|-|\n |x|y|"); |
| 61 | + assert.deepStrictEqual(result.prefixes, ["*", "", ""]); |
| 62 | + }); |
| 63 | + |
| 64 | + test("strips bullet list marker (+) when followed by whitespace and pipe", () => { |
| 65 | + const input = "+ |col1|col2|\n |-|-|\n |x|y|"; |
| 66 | + const result = createSut().strip(input); |
| 67 | + |
| 68 | + assert.strictEqual(result.strippedText, " |col1|col2|\n |-|-|\n |x|y|"); |
| 69 | + assert.deepStrictEqual(result.prefixes, ["+", "", ""]); |
| 70 | + }); |
| 71 | + |
| 72 | + test("does not strip list marker when no pipe follows", () => { |
| 73 | + const input = "1. Just a list item\n- Another item\n* Third item"; |
| 74 | + const result = createSut().strip(input); |
| 75 | + |
| 76 | + assert.strictEqual(result.strippedText, input); |
| 77 | + assert.deepStrictEqual(result.prefixes, ["", "", ""]); |
| 78 | + }); |
| 79 | + |
| 80 | + test("strips combined blockquote and list marker", () => { |
| 81 | + const input = "> 1. |col1|\n> |-|\n> |a|"; |
| 82 | + const result = createSut().strip(input); |
| 83 | + |
| 84 | + assert.strictEqual(result.strippedText, " |col1|\n|-|\n|a|"); |
| 85 | + assert.deepStrictEqual(result.prefixes, ["> 1.", "> ", "> "]); |
| 86 | + }); |
| 87 | + |
| 88 | + test("does not strip content that merely starts with whitespace", () => { |
| 89 | + const input = "\t|col1|col2|\n\t|-|-|\n\t|a|b|"; |
| 90 | + const result = createSut().strip(input); |
| 91 | + |
| 92 | + assert.strictEqual(result.strippedText, input); |
| 93 | + assert.deepStrictEqual(result.prefixes, ["", "", ""]); |
| 94 | + }); |
| 95 | + |
| 96 | + test("handles mixed prefixed and non-prefixed lines", () => { |
| 97 | + const input = "Some text\n> |col1|\n> |-|\n> |a|\nMore text"; |
| 98 | + const result = createSut().strip(input); |
| 99 | + |
| 100 | + assert.strictEqual(result.strippedText, "Some text\n|col1|\n|-|\n|a|\nMore text"); |
| 101 | + assert.deepStrictEqual(result.prefixes, ["", "> ", "> ", "> ", ""]); |
| 102 | + }); |
| 103 | + |
| 104 | + test("is no-op for plain table without prefix", () => { |
| 105 | + const input = "|col1|col2|\n|-|-|\n|a|b|"; |
| 106 | + const result = createSut().strip(input); |
| 107 | + |
| 108 | + assert.strictEqual(result.strippedText, input); |
| 109 | + assert.deepStrictEqual(result.prefixes, ["", "", ""]); |
| 110 | + }); |
| 111 | + |
| 112 | + test("preserves line endings", () => { |
| 113 | + const input = "> |col1|\r\n> |-|\r\n> |a|"; |
| 114 | + const result = createSut().strip(input); |
| 115 | + |
| 116 | + assert.strictEqual(result.strippedText, "|col1|\r\n|-|\r\n|a|"); |
| 117 | + }); |
| 118 | + |
| 119 | + test("strips multi-digit numbered list marker", () => { |
| 120 | + const input = "10. |col1|\n |-|\n |a|"; |
| 121 | + const result = createSut().strip(input); |
| 122 | + |
| 123 | + assert.strictEqual(result.strippedText, " |col1|\n |-|\n |a|"); |
| 124 | + assert.deepStrictEqual(result.prefixes, ["10.", "", ""]); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + suite("restore()", () => { |
| 129 | + |
| 130 | + test("prepends stored prefixes to each line", () => { |
| 131 | + const text = "| col1 |\n|------|\n| a |"; |
| 132 | + const prefixes = ["> ", "> ", "> "]; |
| 133 | + const result = createSut().restore(text, prefixes); |
| 134 | + |
| 135 | + assert.strictEqual(result, "> | col1 |\n> |------|\n> | a |"); |
| 136 | + }); |
| 137 | + |
| 138 | + test("handles mixed prefixes", () => { |
| 139 | + const text = "\t| col1 |\n\t|------|\n\t| a |"; |
| 140 | + const prefixes = ["1.", "", ""]; |
| 141 | + const result = createSut().restore(text, prefixes); |
| 142 | + |
| 143 | + assert.strictEqual(result, "1.\t| col1 |\n\t|------|\n\t| a |"); |
| 144 | + }); |
| 145 | + |
| 146 | + test("handles empty prefixes as no-op", () => { |
| 147 | + const text = "|col1|\n|-|\n|a|"; |
| 148 | + const prefixes = ["", "", ""]; |
| 149 | + const result = createSut().restore(text, prefixes); |
| 150 | + |
| 151 | + assert.strictEqual(result, text); |
| 152 | + }); |
| 153 | + |
| 154 | + test("preserves line endings", () => { |
| 155 | + const text = "|col1|\r\n|-|\r\n|a|"; |
| 156 | + const prefixes = ["> ", "> ", "> "]; |
| 157 | + const result = createSut().restore(text, prefixes); |
| 158 | + |
| 159 | + assert.strictEqual(result, "> |col1|\r\n> |-|\r\n> |a|"); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + suite("roundtrip", () => { |
| 164 | + |
| 165 | + test("strip() + restore() preserves original text with blockquote prefix", () => { |
| 166 | + const original = "> | col1 | col2 |\n> |------|------|\n> | a | b |"; |
| 167 | + const sut = createSut(); |
| 168 | + const { strippedText, prefixes } = sut.strip(original); |
| 169 | + const restored = sut.restore(strippedText, prefixes); |
| 170 | + |
| 171 | + assert.strictEqual(restored, original); |
| 172 | + }); |
| 173 | + |
| 174 | + test("strip() + restore() preserves original text with list marker prefix", () => { |
| 175 | + const original = "1.\t|col1|col2|\n\t|-|-|\n\t|a|b|"; |
| 176 | + const sut = createSut(); |
| 177 | + const { strippedText, prefixes } = sut.strip(original); |
| 178 | + const restored = sut.restore(strippedText, prefixes); |
| 179 | + |
| 180 | + assert.strictEqual(restored, original); |
| 181 | + }); |
| 182 | + |
| 183 | + test("strip() + restore() preserves text without any prefix", () => { |
| 184 | + const original = "|col1|col2|\n|-|-|\n|a|b|"; |
| 185 | + const sut = createSut(); |
| 186 | + const { strippedText, prefixes } = sut.strip(original); |
| 187 | + const restored = sut.restore(strippedText, prefixes); |
| 188 | + |
| 189 | + assert.strictEqual(restored, original); |
| 190 | + }); |
| 191 | + |
| 192 | + test("strip() + restore() preserves text with CRLF endings", () => { |
| 193 | + const original = "> |col1|\r\n> |-|\r\n> |a|"; |
| 194 | + const sut = createSut(); |
| 195 | + const { strippedText, prefixes } = sut.strip(original); |
| 196 | + const restored = sut.restore(strippedText, prefixes); |
| 197 | + |
| 198 | + assert.strictEqual(restored, original); |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + function createSut(): MarkdownPrefixStripper { |
| 203 | + return new MarkdownPrefixStripper(); |
| 204 | + } |
| 205 | +}); |
0 commit comments