|
| 1 | +import { expect, it, vi } from "vite-plus/test"; |
| 2 | +import { unified } from "unified"; |
| 3 | +import remarkParse from "remark-parse"; |
| 4 | + |
| 5 | +import { remarkKeepWindowsPaths, remarkKeepWindowsProsePaths } from "./markdownWindowsPaths.ts"; |
| 6 | + |
| 7 | +const plugins = [ |
| 8 | + ["paths and destinations", remarkKeepWindowsPaths], |
| 9 | + ["prose paths", remarkKeepWindowsProsePaths], |
| 10 | +] as const; |
| 11 | + |
| 12 | +it.each(plugins)("%s limits drive-path checks to the word containing each escape", (_, plugin) => { |
| 13 | + const processor = unified().use(remarkParse).use(plugin); |
| 14 | + const identifiers = Array.from({ length: 200 }, (_, index) => `metric_name_${index}`); |
| 15 | + const markdown = `Supported metrics: ${identifiers.map((name) => name.replaceAll("_", "\\_")).join(", ")}.`; |
| 16 | + const pattern = /(?:^|\W)[A-Za-z]:(?:[\\/]\S*)?$/.source; |
| 17 | + const test = RegExp.prototype.test; |
| 18 | + let inspectedCharacters = 0; |
| 19 | + using _test = vi.spyOn(RegExp.prototype, "test").mockImplementation(function ( |
| 20 | + this: RegExp, |
| 21 | + value: string, |
| 22 | + ) { |
| 23 | + if (this.source === pattern) inspectedCharacters += value.length; |
| 24 | + return test.call(this, value); |
| 25 | + }); |
| 26 | + |
| 27 | + expect(processor.parse(markdown)).toMatchObject({ |
| 28 | + children: [ |
| 29 | + { |
| 30 | + type: "paragraph", |
| 31 | + children: [{ type: "text", value: `Supported metrics: ${identifiers.join(", ")}.` }], |
| 32 | + }, |
| 33 | + ], |
| 34 | + }); |
| 35 | + expect(inspectedCharacters).toBeGreaterThan(0); |
| 36 | + expect(inspectedCharacters).toBeLessThan(markdown.length * 2); |
| 37 | +}); |
| 38 | + |
| 39 | +it.each(plugins)( |
| 40 | + "%s preserves prose paths and ordinary escapes across whitespace boundaries", |
| 41 | + (_, plugin) => { |
| 42 | + const processor = unified().use(remarkParse).use(plugin); |
| 43 | + for (const whitespace of [" ", "\t", "\u00a0", "\u2003", "\u202f", "\ufeff"]) { |
| 44 | + const markdown = String.raw`Before${whitespace}C:\Users\me\_build${whitespace}\*literal\*`; |
| 45 | + expect(processor.parse(markdown)).toMatchObject({ |
| 46 | + children: [ |
| 47 | + { |
| 48 | + type: "paragraph", |
| 49 | + children: [ |
| 50 | + { |
| 51 | + type: "text", |
| 52 | + value: String.raw`Before${whitespace}C:\Users\me\_build${whitespace}*literal*`, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + ], |
| 57 | + }); |
| 58 | + } |
| 59 | + }, |
| 60 | +); |
| 61 | + |
| 62 | +it.each(plugins)("%s finds each escape's word after earlier escapes in the text", (_, plugin) => { |
| 63 | + const processor = unified().use(remarkParse).use(plugin); |
| 64 | + const markdown = String.raw`one\_two C:\a\_b \*x\* three\_four D:\c\_d \*y\* *em*E:\e\_f \*z\*`; |
| 65 | + expect(processor.parse(markdown)).toMatchObject({ |
| 66 | + children: [ |
| 67 | + { |
| 68 | + type: "paragraph", |
| 69 | + children: [ |
| 70 | + { type: "text", value: String.raw`one_two C:\a\_b *x* three_four D:\c\_d *y* ` }, |
| 71 | + { type: "emphasis" }, |
| 72 | + { type: "text", value: String.raw`E:\e\_f *z*` }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + ], |
| 76 | + }); |
| 77 | +}); |
0 commit comments