Skip to content

Commit

Permalink
~Better automated testing
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Jul 19, 2023
1 parent e9e5fa2 commit fface03
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 122 deletions.
22 changes: 22 additions & 0 deletions test/bnfs/bnf.bnf
@@ -0,0 +1,22 @@
program ::= %w* def+ ;
w ::= comment | " " | "\t" | "\n" | "\r\n" ;
comment ::= "#" !"\n"* "\n" ;

name ::= letter ( letter | digit | "_" )* ;
letter ::= "a"->"z" | "A"->"Z" ;
digit ::= "0"->"9" ;
hex ::= "0"->"9" | "a"->"f" | "A"->"F" ;

constant ::= %"\"" frag* %"\"" ;
frag ::= escape | byte | !"\""+ ;
escape ::= %"\\" !"" ;
byte ::= %"x" ...(hex hex) ;

def ::= ...name %(w+ "::=" w*) expr %(w* ";" w*) ;

expr ::= expr_arg %w* ( ...expr_infix? %w* expr_arg %w* )* ;
expr_arg ::= expr_prefix ( constant | expr_brackets | ...name ) ...expr_suffix? ;
expr_prefix ::= ..."%"? ..."..."? ..."!"? ;
expr_infix ::= "->" | "|" ;
expr_suffix ::= "*" | "?" | "+" ;
expr_brackets ::= %( "(" w* ) expr %( w* ")" ) ;
126 changes: 100 additions & 26 deletions test/index.js
@@ -1,37 +1,111 @@
import { execSync } from "child_process";
import * as path from "path";
import { execSync } from "node:child_process";
import { pathToFileURL } from "node:url";
import path from "node:path";
import chalk from "chalk";
import fs from "node:fs";

const cwd = path.dirname(process.argv[1]);
let tests = {
"BNF compilations": ['node ../bin/cli.js ./bnfs/', { cwd }],
"LOLcode": ['node ./lolcode.js', { cwd }],
"Double Parse": ['node double-parse.js', { cwd }],
// "Uniview Parse": ['node ./uv/index.js', { cwd }],
};
import * as Shared from "./bnfs/shared.js";

let failed = false;
const cwd = path.dirname(process.argv[1]);

for (let key in tests) {
let test = tests[key];
console.log(key);

test[1].stdio = [null, null, null];

let res;
function CompileBNFs(){
console.log("Compiling BNFs");
try {
res = execSync(test[0], test[1]);
console.log(" Passed\n");
execSync("node ../bin/cli.js ./bnfs/", { cwd })
} catch (e) {
console.error(" "+
e.stderr
.toString()
.replace(/\n+/g, " \n")
);

console.log(' Failed\n');
failed = true;
console.error(e.stderr);
console.error("Failed to compile BNFs");
process.exit(1);
}
}




async function SampleTests() {
const syntaxes = fs.readdirSync(`${cwd}/bnfs`)
.filter(x => x.endsWith(".bnf"))
.map(x => x.slice(0, -4));


console.log("Automated Sample Tests");
let failure = false;
for (const lang of syntaxes) {
console.log(" ", lang);
const dir = `${cwd}/samples/${lang}`;
if (!fs.existsSync(dir)) continue;

const files = fs.readdirSync(dir);
if (files.length === 0) continue;

const syntaxURL = pathToFileURL(`${cwd}/bnfs/${lang}.js`);
const Parser = await import(syntaxURL);

if (!Parser.Parse_Program) {
continue;
}

for (const file of files) {
const data = fs.readFileSync(`${dir}/${file}`, "utf8");
try {
const syntax = Parser.Parse_Program(data, false);
if (syntax instanceof Shared.ParseError) throw syntax;
if (syntax.isPartial) throw new Error("Partial Match");

console.log(` ${chalk.green("PASS")} ${file}`);
} catch (e) {
console.log(` ${chalk.red("FAIL")} ${file}`);
failure = true;
}
}
}

if (failure) process.exit(1);
}


process.exit(failed ? 1 : 0);



function ManualTests () {
let tests = {
// "Double Parse": ['node double-parse.js', { cwd }],
// "Uniview Parse": ['node ./uv/index.js', { cwd }],
};

let failed = false;

for (let key in tests) {
let test = tests[key];
console.log(key);

test[1].stdio = [null, null, null];

let res;
try {
res = execSync(test[0], test[1]);
console.log(" Passed\n");
} catch (e) {
console.error(" "+
e.stderr
.toString()
.replace(/\n+/g, " \n")
);

console.log(' Failed\n');
failed = true;
}
}

process.exit(failed ? 1 : 0);
}


async function Main() {
CompileBNFs();
await SampleTests();
ManualTests();
}
Main();
96 changes: 0 additions & 96 deletions test/lolcode.js

This file was deleted.

22 changes: 22 additions & 0 deletions test/samples/bnf/self.txt
@@ -0,0 +1,22 @@
program ::= %w* def+ ;
w ::= comment | " " | "\t" | "\n" | "\r\n" ;
comment ::= "#" !"\n"* "\n" ;

name ::= letter ( letter | digit | "_" )* ;
letter ::= "a"->"z" | "A"->"Z" ;
digit ::= "0"->"9" ;
hex ::= "0"->"9" | "a"->"f" | "A"->"F" ;

constant ::= %"\"" frag* %"\"" ;
frag ::= escape | byte | !"\""+ ;
escape ::= %"\\" !"" ;
byte ::= %"x" ...(hex hex) ;

def ::= ...name %(w+ "::=" w*) expr %(w* ";" w*) ;

expr ::= expr_arg %w* ( ...expr_infix? %w* expr_arg %w* )* ;
expr_arg ::= expr_prefix ( constant | expr_brackets | ...name ) ...expr_suffix? ;
expr_prefix ::= ..."%"? ..."..."? ..."!"? ;
expr_infix ::= "->" | "|" ;
expr_suffix ::= "*" | "?" | "+" ;
expr_brackets ::= %( "(" w* ) expr %( w* ")" ) ;
9 changes: 9 additions & 0 deletions test/samples/lolcode/comments.txt
@@ -0,0 +1,9 @@
HAI
VISIBLE "U SEE THIS"
BTW VISIBLE "U SEE NOTHING"
OBTW
VISIBLE "U SEE NOTHIN"
VISIBLE "U STIL SEE NOTHIN"
TLDR
VISIBLE "U SEE THIS"
KTHXBYE
3 changes: 3 additions & 0 deletions test/samples/lolcode/declare.txt
@@ -0,0 +1,3 @@
HAI
I HAZ A n
KTHXBYE
3 changes: 3 additions & 0 deletions test/samples/lolcode/hello-world.txt
@@ -0,0 +1,3 @@
HAI
VISIBLE "HAI WORLD!"
KTHXBYE
10 changes: 10 additions & 0 deletions test/samples/lolcode/if-branch.txt
@@ -0,0 +1,10 @@
HAI
GIMMEH number
I HAZ A even ITZ MOD OF number AN 2
O RLY? even
YA RLY
VISIBLE "ITZ EVEN"
NO WAI
VISIBLE "NOZ EVEN"
OIC
KTHXBYE
3 changes: 3 additions & 0 deletions test/samples/lolcode/stdin.txt
@@ -0,0 +1,3 @@
HAI
I HAZ A number ITZ 3
KTHXBYE
7 changes: 7 additions & 0 deletions test/samples/lolcode/while-loop.txt
@@ -0,0 +1,7 @@
HAI
GIMMEH number
IM IN YR loop WILE WIN
number R SUM OF number AN 1
VISIBLE number
IM OUTTA YR loop
KTHXBYE

0 comments on commit fface03

Please sign in to comment.