Skip to content

Commit

Permalink
First run of all runtime tests
Browse files Browse the repository at this point in the history
Test Suites: 1 failed, 1 passed, 2 total
Tests:       76 failed, 367 passed, 443 total
Snapshots:   0 total
Time:        417.649 s
  • Loading branch information
mike-lischke committed Jan 27, 2024
1 parent 6370cc8 commit 0a7bcc9
Show file tree
Hide file tree
Showing 35 changed files with 417 additions and 485 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
{
"type": "node",
"request": "launch",
"name": "Run api.spec.ts",
"name": "Run runtime.spec.ts",
"runtimeExecutable": "node",
"runtimeArgs": [
"--experimental-vm-modules",
"--no-warnings",
"--loader",
"ts-node/esm",
"${workspaceRoot}/node_modules/.bin/jest",
"api.spec.ts",
"runtime.spec.ts",
"--no-coverage",
"--runInBand"
],
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ function main(argv: string[]): void {
ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);
<else>
stream.fill();
for(let i=0; i\<stream.tokens.length; i++) {
console.log(stream.tokens[i].toString());
const tokens = stream.getTokens();
for(let i=0; i\<tokens.length; i++) {
console.log(tokens[i].toString());
}
<if(showDFA)>
process.stdout.write(lexer.interpreter.decisionToDFA[Lexer.DEFAULT_MODE].toLexerString());
Expand Down
12 changes: 12 additions & 0 deletions runtime-testsuite/resources/helpers/package.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "module",
"devDependencies": {
"antlr4ng": "2.0.8",
"antlr4ng-cli": "1.0.7",
"ts-node": "10.9.2"
},
"scripts": {
"generate": "antlr4ng -Xexact-output-dir",
"install-deps": "npm i"
}
}
10 changes: 10 additions & 0 deletions runtime-testsuite/resources/helpers/tsconfig.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "Node16",
"moduleResolution": "Node16",
},
"ts-node": {
"esm": true
}
}
12 changes: 12 additions & 0 deletions runtime-testsuite/runtime.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) Mike Lischke. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { RuntimeTests } from "./src/RuntimeTests.js";
import { TestNG } from "./utils/TestNG.js";

describe("RuntimeTests", () => {
const testNG = new TestNG();
testNG.run(RuntimeTests);
});
11 changes: 5 additions & 6 deletions runtime-testsuite/src/CustomDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import { RuntimeTestDescriptor } from "./RuntimeTestDescriptor.js";
import { GrammarType } from "./GrammarType.js";
import { PredictionMode } from "antlr4ng";
import { padZero } from "../temp.js";

export class CustomDescriptors {
Expand Down Expand Up @@ -50,7 +49,7 @@ export class CustomDescriptors {
"lexer grammar L;\n" +
"T: ~'\\n'+;\n" +
"SEPARATOR: '\\n';",
null, false, false, false, PredictionMode.LL, true, null, CustomDescriptors.path);
null, false, false, false, "LL", true, null, CustomDescriptors.path);
}

private static getLineSeparatorCrLfDescriptor(): RuntimeTestDescriptor {
Expand All @@ -71,7 +70,7 @@ export class CustomDescriptors {
"lexer grammar L;\n" +
"T: ~'\\r'+;\n" +
"SEPARATOR: '\\r\\n';",
null, false, false, false, PredictionMode.LL, true, null, CustomDescriptors.path);
null, false, false, false, "LL", true, null, CustomDescriptors.path);
}

private static getLargeLexerDescriptor(): RuntimeTestDescriptor {
Expand All @@ -98,7 +97,7 @@ export class CustomDescriptors {
"",
grammarName,
grammar.toString(),
null, false, false, false, PredictionMode.LL, true, null, CustomDescriptors.path);
null, false, false, false, "LL", true, null, CustomDescriptors.path);
}

private static getAtnStatesSizeMoreThan65535Descriptor(): RuntimeTestDescriptor {
Expand Down Expand Up @@ -146,7 +145,7 @@ export class CustomDescriptors {
"",
grammarName,
grammar.toString(),
null, false, false, false, PredictionMode.LL, true,
null, false, false, false, "LL", true,
["CSharp", "Python3", "Go", "PHP", "Swift", "JavaScript", "TypeScript", "Dart"],
CustomDescriptors.path);
}
Expand Down Expand Up @@ -193,6 +192,6 @@ export class CustomDescriptors {
"r",
"P",
grammar,
null, false, false, false, PredictionMode.LL, true, null, CustomDescriptors.path);
null, false, false, false, "LL", true, null, CustomDescriptors.path);
}
}
14 changes: 13 additions & 1 deletion runtime-testsuite/src/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class FileUtils {

public static deleteDirectory(f: string): void {
const stat = fs.statSync(f);
if (stat.isDirectory() && !stat.isSymbolicLink) {
if (stat.isDirectory() && !stat.isSymbolicLink()) {
fs.rmdirSync(f, { recursive: true });
} else {
fs.unlinkSync(f);
Expand All @@ -86,4 +86,16 @@ export class FileUtils {
return false;
}
}

public static copyFileOrFolder(from: string, to: string): void {
const stat = fs.statSync(from);
if (stat.isDirectory()) {
fs.mkdirSync(to, { recursive: true });
fs.readdirSync(from).forEach((f) => {
FileUtils.copyFileOrFolder(path.join(from, f), path.join(to, f));
});
} else {
fs.copyFileSync(from, to);
}
}
}
122 changes: 55 additions & 67 deletions runtime-testsuite/src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
* can be found in the LICENSE.txt file in the project root.
*/

/* eslint-disable jsdoc/require-returns, jsdoc/require-param */

import path from "path";
import { execSync } from "child_process";

import { FileUtils } from "./FileUtils.js";
import { ErrorQueue } from "./ErrorQueue.js";
Expand All @@ -13,83 +16,68 @@ import { DefaultToolListener, Tool } from "../temp.js";
export class Generator {

/** Run ANTLR on stuff in workdir and error queue back */
public static antlrOnString(workdir: string,
targetName: string | null,
grammarFileName: string,
defaultListener: boolean, extraOptions: string[]): ErrorQueue;
/** Write a grammar to tmpdir and run antlr */
public static antlrOnString(workdir: string,
targetName: string | null,
grammarFileName: string,
grammarStr: string,
defaultListener: boolean, extraOptions: string[]): ErrorQueue;
public static antlrOnString(...args: unknown[]): ErrorQueue {
switch (args.length) {
case 5: {
const [workdir, targetName, grammarFileName, defaultListener, extraOptions] =
args as [string, string | null, string, boolean, string[]];

const options = [...extraOptions];
if (targetName !== null) {
options.push("-Dlanguage=" + targetName);
}

if (!options.includes("-o")) {
options.push("-o");
options.push(workdir);
}
public static generateANTLRFilesInWorkDir(workdir: string, targetName: string | null,
grammarFileName: string, defaultListener: boolean, extraOptions: string[]): ErrorQueue {
const options = [...extraOptions];
if (targetName !== null) {
options.push("-Dlanguage=" + targetName);
} else {
options.push("-Dlanguage=TypeScript");
}

if (!options.includes("-lib")) {
options.push("-lib");
options.push(workdir);
}
if (!options.includes("-encoding")) {
options.push("-encoding");
options.push("UTF-8");
}
options.push(path.join(workdir, grammarFileName));
if (!options.includes("-o")) {
options.push("-o");
options.push(workdir);
}

const antlr = new Tool(options);
const errorQueue = new ErrorQueue(antlr);
antlr.addListener(errorQueue);
if (defaultListener) {
antlr.addListener(new DefaultToolListener(antlr));
}
antlr.processGrammarsOnCommandLine();
if (!options.includes("-lib")) {
options.push("-lib");
options.push(workdir);
}
if (!options.includes("-encoding")) {
options.push("-encoding");
options.push("UTF-8");
}
options.push(path.join(workdir, grammarFileName));

const errors: string[] = [];
const antlr = new Tool(options);
const errorQueue = new ErrorQueue(antlr);
antlr.addListener(errorQueue);
if (defaultListener) {
antlr.addListener(new DefaultToolListener(antlr));
}
//antlr.processGrammarsOnCommandLine();

if (!defaultListener && errorQueue.errors.length > 0) {
for (const msg of errorQueue.errors) {
const msgST = antlr.errMgr.getMessageTemplate(msg);
errors.push(msgST!.render());
}
}
const errors: string[] = [];

/*if (!defaultListener && errorQueue.warnings.length > 0) {
for (let i = 0; i < errorQueue.warnings.length; i++) {
const msg = errorQueue.warnings[i];
// antlrToolErrors.append(msg); warnings are hushed
}
}*/
if (!defaultListener && errorQueue.errors.length > 0) {
for (const msg of errorQueue.errors) {
const msgST = antlr.errMgr.getMessageTemplate(msg);
errors.push(msgST!.render());
}
}

return errorQueue;
/*if (!defaultListener && errorQueue.warnings.length > 0) {
for (let i = 0; i < errorQueue.warnings.length; i++) {
const msg = errorQueue.warnings[i];
// antlrToolErrors.append(msg); warnings are hushed
}
}*/

case 6: {
const [workdir, targetName, grammarFileName, grammarStr, defaultListener, extraOptions] =
args as [string, string, string, string, boolean, string[]];
// Generate test parsers, lexers and listeners.
/*execSync(`npm run generate -- ${this.targetPath}/*.g4 -o ${this.targetPath}`,
{ encoding: "utf-8", cwd: this.targetPath });*/
execSync(`npm run generate -- ${options.join(" ")}`, { encoding: "utf-8", cwd: workdir });

FileUtils.mkdir(workdir);
FileUtils.writeFile(workdir, grammarFileName, grammarStr);
return errorQueue;
}

return Generator.antlrOnString(workdir, targetName, grammarFileName, defaultListener, extraOptions);
}
public static generateANTLRFilesInTempDir(workdir: string, targetName: string | null,
grammarFileName: string, grammarStr: string, defaultListener: boolean, extraOptions: string[]): ErrorQueue {
FileUtils.mkdir(workdir);
FileUtils.writeFile(workdir, grammarFileName, grammarStr);

default: {
throw new Error("Invalid number of arguments");
}
}
return Generator.generateANTLRFilesInWorkDir(workdir, targetName, grammarFileName, defaultListener,
extraOptions);
}

}
27 changes: 14 additions & 13 deletions runtime-testsuite/src/Processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export class Processor {

public readonly args: string[];
public readonly workingDirectory: string;
public readonly environmentVariables: Map<string, string>;
public readonly environmentVariables: Map<string, string | undefined>;
public readonly throwOnNonZeroErrorCode: boolean;

public constructor(args: string[], workingDirectory: string, environmentVariables: Map<string, string>,
public constructor(args: string[], workingDirectory: string, environmentVariables: Map<string, string | undefined>,
throwOnNonZeroErrorCode: boolean) {
this.args = args;
this.workingDirectory = workingDirectory;
Expand All @@ -28,7 +28,7 @@ export class Processor {
}

public static run(args: string[], workingDirectory: string,
environmentVariables?: Map<string, string>): Promise<ProcessorResult> {
environmentVariables?: Map<string, string | undefined>): Promise<ProcessorResult> {
environmentVariables ??= new Map<string, string>();

return new Processor(args, workingDirectory, environmentVariables, true).start();
Expand All @@ -40,35 +40,36 @@ export class Processor {
console.log("RUNNING " + this.args + " in " + this.workingDirectory);
}

const spawnOptions: ProcessEnvOptions = { cwd: this.workingDirectory };
if (this.environmentVariables.size > 0) {
const spawnOptions: ProcessEnvOptions = { cwd: this.workingDirectory, env: {} };
/*if (this.environmentVariables.size > 0) {
spawnOptions.env = {};
this.environmentVariables.forEach((value, key) => {
spawnOptions.env![key] = value;
});
}
const java = spawn("java", this.args, spawnOptions);
}*/
const node = spawn("node", this.args, spawnOptions);

const out: string[] = [];
const err: string[] = [];

java.stderr.on("data", (data: Buffer) => {
node.stderr.on("data", (data: Buffer) => {
err.push(data.toString());
reject(err.join(""));
});

java.stdout.on("data", (data: Buffer) => {
node.stdout.on("data", (data: Buffer) => {
out.push(data.toString());
});

java.on("exit", (code: number | null, signal: string | null) => {
node.on("exit", (code: number | null, signal: string | null) => {
const output = out.join("");
const errors = err.join("");
if (this.throwOnNonZeroErrorCode && java.exitCode !== 0) {
throw new Error("Exit code " + java.exitCode + " with output:\n" +
if (this.throwOnNonZeroErrorCode && node.exitCode !== 0) {
throw new Error("Exit code " + node.exitCode + " with output:\n" +
RuntimeTestUtils.joinLines(output, errors));
}

resolve(new ProcessorResult(java.exitCode, output, errors));
resolve(new ProcessorResult(node.exitCode, output, errors));
});
});
}
Expand Down
5 changes: 2 additions & 3 deletions runtime-testsuite/src/RunOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import { Stage } from "./Stage.js";
import { PredictionMode } from "antlr4ng";

export class RunOptions {
public readonly grammarFileName: string;
Expand All @@ -25,14 +24,14 @@ export class RunOptions {
public readonly showDFA: boolean;
public readonly endStage: Stage;
public readonly superClass: string | null;
public readonly predictionMode: PredictionMode;
public readonly predictionMode: string;
public readonly buildParseTree: boolean;

public constructor(grammarFileName: string, grammarStr: string, parserName: string | null, lexerName: string | null,
useListener: boolean, useVisitor: boolean, startRuleName: string | null,
input: string, profile: boolean, showDiagnosticErrors: boolean,
traceATN: boolean, showDFA: boolean, endStage: Stage,
language: string, superClass: string | null, predictionMode: PredictionMode, buildParseTree: boolean) {
language: string, superClass: string | null, predictionMode: string, buildParseTree: boolean) {
this.grammarFileName = grammarFileName;
this.grammarStr = grammarStr;
this.parserName = parserName;
Expand Down
Loading

0 comments on commit 0a7bcc9

Please sign in to comment.