|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import type { Assertion } from 'vitest'; |
|
|
import { expect } from 'vitest'; |
|
|
import type { TextBuffer } from '../ui/components/shared/text-buffer.js'; |
|
|
|
|
|
|
|
|
|
|
|
const invalidCharsRegex = /[\b\x1b]/; |
|
|
|
|
|
function toHaveOnlyValidCharacters(this: Assertion, buffer: TextBuffer) { |
|
|
|
|
|
const { isNot } = this as any; |
|
|
let pass = true; |
|
|
const invalidLines: Array<{ line: number; content: string }> = []; |
|
|
|
|
|
for (let i = 0; i < buffer.lines.length; i++) { |
|
|
const line = buffer.lines[i]; |
|
|
if (line.includes('\n')) { |
|
|
pass = false; |
|
|
invalidLines.push({ line: i, content: line }); |
|
|
break; |
|
|
} |
|
|
if (invalidCharsRegex.test(line)) { |
|
|
pass = false; |
|
|
invalidLines.push({ line: i, content: line }); |
|
|
} |
|
|
} |
|
|
|
|
|
return { |
|
|
pass, |
|
|
message: () => |
|
|
`Expected buffer ${isNot ? 'not ' : ''}to have only valid characters, but found invalid characters in lines:\n${invalidLines |
|
|
.map((l) => ` [${l.line}]: "${l.content}"`) /* This line was changed */ |
|
|
.join('\n')}`, |
|
|
actual: buffer.lines, |
|
|
expected: 'Lines with no line breaks, backspaces, or escape codes.', |
|
|
}; |
|
|
} |
|
|
|
|
|
expect.extend({ |
|
|
toHaveOnlyValidCharacters, |
|
|
|
|
|
} as any); |
|
|
|
|
|
|
|
|
declare module 'vitest' { |
|
|
interface Assertion<T> { |
|
|
toHaveOnlyValidCharacters(): T; |
|
|
} |
|
|
interface AsymmetricMatchersContaining { |
|
|
toHaveOnlyValidCharacters(): void; |
|
|
} |
|
|
} |
|
|
|