From b241c0c51fe1077e0840078ab9d6e42547b29547 Mon Sep 17 00:00:00 2001 From: worksofliam Date: Fri, 6 Dec 2024 13:50:48 -0500 Subject: [PATCH] Add parser setup functions and tests for source files Signed-off-by: worksofliam --- tests/parserSetup.ts | 11 +++++++++++ tests/sources/.gitignore | 1 + tests/sources/hello.pgm.rpgle | 9 +++++++++ tests/suite/sources.test.ts | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 tests/sources/.gitignore create mode 100644 tests/sources/hello.pgm.rpgle create mode 100644 tests/suite/sources.test.ts diff --git a/tests/parserSetup.ts b/tests/parserSetup.ts index 13a57295..a42d063d 100644 --- a/tests/parserSetup.ts +++ b/tests/parserSetup.ts @@ -55,4 +55,15 @@ export default function setupParser(): Parser { }); return parser; +} + +export function getSourcesList(): string[] { + return glob.sync(`**/*.*`, { + cwd: path.join(includeDir, `sources`), + nocase: true, + }); +} + +export function getSourcesContent(name: string) { + return readFile(path.join(includeDir, `sources`, name), { encoding: `utf-8` }); } \ No newline at end of file diff --git a/tests/sources/.gitignore b/tests/sources/.gitignore new file mode 100644 index 00000000..47207062 --- /dev/null +++ b/tests/sources/.gitignore @@ -0,0 +1 @@ +p*.* \ No newline at end of file diff --git a/tests/sources/hello.pgm.rpgle b/tests/sources/hello.pgm.rpgle new file mode 100644 index 00000000..1ad69928 --- /dev/null +++ b/tests/sources/hello.pgm.rpgle @@ -0,0 +1,9 @@ +**free + +dcl-s text char(20); + +text = 'Hello, world!'; + +dsply text; + +return; \ No newline at end of file diff --git a/tests/suite/sources.test.ts b/tests/suite/sources.test.ts new file mode 100644 index 00000000..4bc29cdb --- /dev/null +++ b/tests/suite/sources.test.ts @@ -0,0 +1,32 @@ +import setupParser, { getSourcesContent, getSourcesList } from "../parserSetup"; +import { test, expect, describe } from "vitest"; +import path from "path"; + +const parser = setupParser(); + +// The purpose of this file is to test the parser against all the sources in the sources directory to ensure it doesn't crash. + +test("Source Directory Tests", async () => { + const list = await getSourcesList(); + + for (const source of list) { + const basename = path.basename(source); + const baseContent = await getSourcesContent(source); + + // These are typing tests. Can the parser accept half documents without crashing? + + let content = ``; + + let baseContentSplitUpIntoPieces = []; + const pieceSize = Math.ceil(baseContent.length / 20); + for (let i = 0; i < baseContent.length; i += pieceSize) { + baseContentSplitUpIntoPieces.push(baseContent.slice(i, i + pieceSize)); + } + + for (let i = 0; i < baseContentSplitUpIntoPieces.length; i++) { + content += baseContentSplitUpIntoPieces[i]; + + await parser.getDocs(basename, content, {collectReferences: true, ignoreCache: true, withIncludes: true}); + } + } +});