Code for the littler Buttons the Computer used in the Turing Machine portion of the book.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
buttons-computer/tests/basic_tests.js

38 lines
869 B

import test from "ava";
import fs from "fs";
import { ButtonMachine } from '../src/buttons.js';
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);
});