First automated test with a simple bit of fuzzing and some simple test files.

master
Zed A. Shaw 3 years ago
parent c905af1214
commit f872546c46
  1. 1724
      package-lock.json
  2. 6
      package.json
  3. 8
      src/buttons.js
  4. 60
      tests/basic_tests.js
  5. 9
      tests/garbag1.btn
  6. 9
      tests/test2.btn

1724
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -8,7 +8,8 @@
"test": "tests"
},
"scripts": {
"test": "ava test tests"
"test": "ava test tests",
"test-watch": "nodemon --exec npx ava tests"
},
"repository": {
"type": "git",
@ -21,6 +22,7 @@
"author": "Zed A. Shaw",
"license": "AGPL-3.0-or-later",
"dependencies": {
"ava": "^4.2.0"
"ava": "^4.2.0",
"nodemon": "^2.0.15"
}
}

@ -412,9 +412,11 @@ export class ButtonMachine {
if(this.running) {
let [op, data] = this.cur_op;
let op_func = this[`op_${op}`];
this.assert(op_func !== undefined, `Invalid operation ${op}`);
op_func.call(this, data);
this.tick++;
if(this.assert(op_func !== undefined, `Invalid operation ${op}`)) {
op_func.call(this, data);
this.tick++;
}
}
}

@ -1,26 +1,38 @@
import test from "ava";
import fs from "fs";
import { ButtonMachine } from '../src/buttons.js';
let code = [
['PUSH', -10],
['PUSH', 1],
['ADD'],
['POKE', 1],
['JNZ', 1],
['STOR', 'IX'],
['POP'],
['PEEK', 1],
['JNZ', 6],
];
let machine = new ButtonMachine();
machine.load(code);
const ops = ButtonMachine.operations();
const registers = machine.register_names();
machine.run();
console.log("STACK TOP", machine.stack_top);
console.log("REGISTERS", registers);
console.log("STACK", machine.stack);
console.log("RAM", machine.ram);
const run_code = (source) => {
const machine = new ButtonMachine();
const code = machine.parse(source.toString());
machine.load(code);
machine.run();
return machine;
}
test("parse and run file", t => {
const test_src = [
["./tests/basic.btn", 74],
["./tests/test2.btn", 0]
]
for(let [source, expected] of test_src) {
console.log("TESTING FILE", source, "EXPECTING", expected);
const code = fs.readFileSync(source);
const machine = run_code(code);
t.is(machine.stack_top, expected);
console.log("STACK TOP", machine.stack_top);
}
});
test("fuzz the parser", t => {
const code = fs.readFileSync("./tests/garbag1.btn");
const machine = run_code(code);
t.is(machine.halted, true);
t.is(machine.error_line, 0);
});

@ -0,0 +1,9 @@
asdlkj
sadf
sad
fas
f
sadfg
asdflkhja sdflk;jas
sad'dfkjlsadflkjsadfjlsad

@ -0,0 +1,9 @@
PUSH -10
PUSH 1
ADD
POKE 1
JNZ 1
STOR IX
POP
PEEK 1
JNZ 6
Loading…
Cancel
Save