From db44594241a6f9732be8483e80dacbfd186dfd47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Sat, 26 Oct 2024 15:00:39 -0400 Subject: [PATCH] add `asssertLinesMatch` utility that matches a line range against a regex --- .../obsidian-testing-framework/src/util.ts | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/obsidian-testing-framework/src/util.ts b/packages/obsidian-testing-framework/src/util.ts index 83a8b25..d8d661a 100644 --- a/packages/obsidian-testing-framework/src/util.ts +++ b/packages/obsidian-testing-framework/src/util.ts @@ -53,10 +53,10 @@ export async function assertLineEquals( * * @export * @param {Page} page - a Playwright page - * @param {string} path the file to check + * @param {string} path - the file to check * @param {number} start - the start of the desired line range (0-based) * @param {number} end - the end of the desired line range (1-based) - * @param {string} expected the expected content + * @param {string} expected - the expected content * @param {boolean} [cached=true] - whether to use `app.vault.cachedRead` */ export async function assertLinesEqual( @@ -81,6 +81,23 @@ const getFile = (file: string): TFile => { return f; } +/** + * asserts all lines in the given range match a regex. + * + * @export + * @param {Page} page - a Playwright page + * @param {string} path - the file to check + * @param {number} start - the start of the desired line range (0-based) + * @param {number} end - the end of the desired line range (1-based) + * @param {RegExp} regex - the regex to test against + * @param {boolean} [cached=true] - whether to use `app.vault.cachedRead` + */ +export async function assertLinesMatch(page: Page, path: string, start: number, end: number, regex: RegExp, cached: boolean = true) { + const fileContent = await readFile(page, path, cached); + const lines = fileContent.split("\n").slice(start, end); + expect(lines.every(l => regex.test(l))).toEqual(true); +} + /** * reads the file at `path` and returns its contents * @@ -88,7 +105,7 @@ const getFile = (file: string): TFile => { * @param {Page} page - a Playwright page * @param {string} path the file to read * @param {boolean} [cached=true] - whether to use `app.vault.cachedRead` - * @return {*} {Promise} the file's contents + * @return {Promise} the file's contents */ export async function readFile( page: Page, @@ -115,7 +132,7 @@ export async function readFile( * @param {Page} page - a Playwright page * @param {((a: App, args?: A) => T | Promise)} callback - the function to execute * @param {A} [args] - optional arguments to pass to the callback - * @return {*} {Promise} a promise containing `callback`'s return value (if any) + * @return {Promise} a promise containing `callback`'s return value (if any) */ export async function doWithApp( page: Page,