From 644891ce7609c3cee73e6c6477bccc77066fd845 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sat, 9 Mar 2024 22:52:09 +0100 Subject: [PATCH] Compiler test infrastructure --- packages/compiler/jest.config.ts | 7 ++++ packages/compiler/package.json | 4 ++- packages/compiler/src/compiler/codegen.ts | 4 +-- packages/compiler/src/compiler/grammar.ts | 4 +-- packages/compiler/src/compiler/index.ts | 6 ++-- packages/compiler/src/index.ts | 4 +-- packages/compiler/src/syntax/codewriter.ts | 8 ++--- packages/compiler/src/syntax/index.ts | 20 ++++++------ packages/compiler/src/syntax/matcher.ts | 6 ++-- packages/compiler/src/syntax/reader.ts | 6 ++-- packages/compiler/src/syntax/scanner.ts | 4 +-- packages/compiler/src/syntax/template.ts | 8 ++--- packages/compiler/src/syntax/tokens.ts | 2 +- packages/compiler/test/test-utils.ts | 38 ++++++++++++++++++++++ 14 files changed, 84 insertions(+), 37 deletions(-) create mode 100644 packages/compiler/jest.config.ts create mode 100644 packages/compiler/test/test-utils.ts diff --git a/packages/compiler/jest.config.ts b/packages/compiler/jest.config.ts new file mode 100644 index 0000000..4a09b3e --- /dev/null +++ b/packages/compiler/jest.config.ts @@ -0,0 +1,7 @@ +/// SPDX-License-Identifier: GPL-3.0-or-later +/// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones + +export default { + preset: 'ts-jest', + testEnvironment: 'node', +}; diff --git a/packages/compiler/package.json b/packages/compiler/package.json index 5342822..4476df5 100644 --- a/packages/compiler/package.json +++ b/packages/compiler/package.json @@ -18,7 +18,9 @@ "rollup": "../../node_modules/.bin/rollup -c", "rollup:watch": "../../node_modules/.bin/rollup -c -w", "clean": "rm -rf lib dist", - "veryclean": "yarn run clean && rm -rf node_modules" + "veryclean": "yarn run clean && rm -rf node_modules", + "test": "../../node_modules/.bin/jest", + "test:watch": "yarn test --watch" }, "main": "dist/syndicate-compiler.js", "module": "lib/index.js", diff --git a/packages/compiler/src/compiler/codegen.ts b/packages/compiler/src/compiler/codegen.ts index e714785..7d344d3 100644 --- a/packages/compiler/src/compiler/codegen.ts +++ b/packages/compiler/src/compiler/codegen.ts @@ -7,7 +7,7 @@ import { Items, Pattern, Templates, Substitution, TokenType, SourceMap, CodeWriter, TemplateFunction, Token, SpanIndex, match, TokenBase, getRange, Pos, -} from '../syntax/index.js'; +} from '../syntax/index'; import { SyndicateParser, SyndicateTypedParser, Identifier, @@ -17,7 +17,7 @@ import { compilePattern, SpawnStatement, -} from './grammar.js'; +} from './grammar'; export function stripShebang(items: Items): Items { if ((items.length > 0) && diff --git a/packages/compiler/src/compiler/grammar.ts b/packages/compiler/src/compiler/grammar.ts index 9e24a05..1d79902 100644 --- a/packages/compiler/src/compiler/grammar.ts +++ b/packages/compiler/src/compiler/grammar.ts @@ -9,8 +9,8 @@ import { scope, bind, seq, alt, upTo, atom, atomString, group, repeat, option, withoutSpace, map, mapm, rest, discard, value, succeed, fail, separatedOrTerminatedBy, not, -} from '../syntax/index.js'; -import * as Matcher from '../syntax/matcher.js'; +} from '../syntax/index'; +import * as Matcher from '../syntax/matcher'; //--------------------------------------------------------------------------- // AST types diff --git a/packages/compiler/src/compiler/index.ts b/packages/compiler/src/compiler/index.ts index 5126f23..64281a2 100644 --- a/packages/compiler/src/compiler/index.ts +++ b/packages/compiler/src/compiler/index.ts @@ -1,6 +1,6 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -export * as Grammar from './grammar.js'; -export * as Codegen from './codegen.js'; -export { compile, CompileOptions } from './codegen.js'; +export * as Grammar from './grammar'; +export * as Codegen from './codegen'; +export { compile, CompileOptions } from './codegen'; diff --git a/packages/compiler/src/index.ts b/packages/compiler/src/index.ts index 317867a..6172190 100644 --- a/packages/compiler/src/index.ts +++ b/packages/compiler/src/index.ts @@ -1,5 +1,5 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -export * as Syntax from './syntax/index.js'; -export * from './compiler/index.js'; +export * as Syntax from './syntax/index'; +export * from './compiler/index'; diff --git a/packages/compiler/src/syntax/codewriter.ts b/packages/compiler/src/syntax/codewriter.ts index 40c2d66..d0657b1 100644 --- a/packages/compiler/src/syntax/codewriter.ts +++ b/packages/compiler/src/syntax/codewriter.ts @@ -1,10 +1,10 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -import { Token, TokenType, Item, Items, isGroup } from './tokens.js'; -import { Pos, startPos, advancePos } from './position.js'; -import { vlqEncode } from './vlq.js'; -import { SpanInfo } from './span.js'; +import { Token, TokenType, Item, Items, isGroup } from './tokens'; +import { Pos, startPos, advancePos } from './position'; +import { vlqEncode } from './vlq'; +import { SpanInfo } from './span'; export interface SourceMap { version: 3; diff --git a/packages/compiler/src/syntax/index.ts b/packages/compiler/src/syntax/index.ts index adf02dc..2d1551e 100644 --- a/packages/compiler/src/syntax/index.ts +++ b/packages/compiler/src/syntax/index.ts @@ -1,13 +1,13 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -export * from './codewriter.js'; -export * from './list.js'; -export * from './matcher.js'; -export * from './position.js'; -export * from './reader.js'; -export * from './scanner.js'; -export * from './span.js'; -export * from './template.js'; -export * from './tokens.js'; -export * from './vlq.js'; +export * from './codewriter'; +export * from './list'; +export * from './matcher'; +export * from './position'; +export * from './reader'; +export * from './scanner'; +export * from './span'; +export * from './template'; +export * from './tokens'; +export * from './vlq'; diff --git a/packages/compiler/src/syntax/matcher.ts b/packages/compiler/src/syntax/matcher.ts index e218b3e..4e087bd 100644 --- a/packages/compiler/src/syntax/matcher.ts +++ b/packages/compiler/src/syntax/matcher.ts @@ -4,9 +4,9 @@ import { Token, TokenType, Items, Item, isGroup, isToken, isSpace, isTokenType, -} from './tokens.js'; -import { Pos, startPos } from './position.js'; -import { List, ArrayList, atEnd, notAtEnd } from './list.js'; +} from './tokens'; +import { Pos, startPos } from './position'; +import { List, ArrayList, atEnd, notAtEnd } from './list'; //--------------------------------------------------------------------------- // Patterns over Item diff --git a/packages/compiler/src/syntax/reader.ts b/packages/compiler/src/syntax/reader.ts index b91188f..9cfbdfb 100644 --- a/packages/compiler/src/syntax/reader.ts +++ b/packages/compiler/src/syntax/reader.ts @@ -1,9 +1,9 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -import { TokenType, Token, Group, GroupInProgress, Item, Items, finishGroup } from './tokens.js'; -import { Pos, startPos } from './position.js'; -import { Scanner, StringScanner } from './scanner.js'; +import { TokenType, Token, Group, GroupInProgress, Item, Items, finishGroup } from './tokens'; +import { Pos, startPos } from './position'; +import { Scanner, StringScanner } from './scanner'; function matchingParen(c: string): string | null { switch (c) { diff --git a/packages/compiler/src/syntax/scanner.ts b/packages/compiler/src/syntax/scanner.ts index 5448b5e..d990e55 100644 --- a/packages/compiler/src/syntax/scanner.ts +++ b/packages/compiler/src/syntax/scanner.ts @@ -1,8 +1,8 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -import { TokenType, Token, Item, GroupInProgress } from './tokens.js'; -import { Pos, advancePos } from './position.js'; +import { TokenType, Token, Item, GroupInProgress } from './tokens'; +import { Pos, advancePos } from './position'; export abstract class Scanner implements IterableIterator { readonly pos: Pos; diff --git a/packages/compiler/src/syntax/template.ts b/packages/compiler/src/syntax/template.ts index b139855..4eaa6b3 100644 --- a/packages/compiler/src/syntax/template.ts +++ b/packages/compiler/src/syntax/template.ts @@ -1,10 +1,10 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -import { Items } from './tokens.js'; -import { Pos, startPos } from './position.js'; -import { laxRead, LaxReadOptions } from './reader.js'; -import * as M from './matcher.js'; +import { Items } from './tokens'; +import { Pos, startPos } from './position'; +import { laxRead, LaxReadOptions } from './reader'; +import * as M from './matcher'; const substPat = M.scope((o: { pos: Pos }) => M.seq(M.atom('$'), diff --git a/packages/compiler/src/syntax/tokens.ts b/packages/compiler/src/syntax/tokens.ts index 16a2da1..69d70a6 100644 --- a/packages/compiler/src/syntax/tokens.ts +++ b/packages/compiler/src/syntax/tokens.ts @@ -1,7 +1,7 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -import { Pos } from './position.js'; +import { Pos } from './position'; export enum TokenType { SPACE, diff --git a/packages/compiler/test/test-utils.ts b/packages/compiler/test/test-utils.ts new file mode 100644 index 0000000..ef7c81e --- /dev/null +++ b/packages/compiler/test/test-utils.ts @@ -0,0 +1,38 @@ +/// SPDX-License-Identifier: GPL-3.0-or-later +/// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones + +import { is, preserves } from '@preserves/core'; + +declare global { + namespace jest { + interface Matchers { + is(expected: any): R; + toThrowFilter(f: (e: Error) => boolean): R; + } + } +} + +expect.extend({ + is(actual, expected) { + return is(actual, expected) + ? { message: () => preserves`expected ${actual} not to be Preserves.is to ${expected}`, + pass: true } + : { message: () => preserves`expected ${actual} to be Preserves.is to ${expected}`, + pass: false }; + }, + + toThrowFilter(thunk, f) { + try { + thunk(); + return { message: () => preserves`expected an exception`, pass: false }; + } catch (e) { + if (f(e)) { + return { message: () => preserves`expected an exception not matching the filter`, + pass: true }; + } else { + return { message: () => preserves`expected an exception matching the filter: ${(e as any)?.constructor?.name}`, + pass: false }; + } + } + } +});