aboutsummaryrefslogtreecommitdiffgithub
diff options
context:
space:
mode:
authorAustin Adams <git@austinjadams.com>2019-01-24 21:04:03 -0600
committerAustin Adams <git@austinjadams.com>2019-01-24 21:04:03 -0600
commit7f2a57c663d40cfb81797897ab8d9c6aff1a3103 (patch)
tree436331047c548f92d8d081ae0c316d5d75eb0f7c
parent7b08f8a759794b34de13bc31f2cee36deaa75c3b (diff)
downloadnovice-7f2a57c663d40cfb81797897ab8d9c6aff1a3103.tar.gz
novice-7f2a57c663d40cfb81797897ab8d9c6aff1a3103.tar.xz
Start on cli debugger tests
-rw-r--r--novice/cli.test.ts6
-rw-r--r--novice/simulator/cli-debugger.test.ts77
2 files changed, 81 insertions, 2 deletions
diff --git a/novice/cli.test.ts b/novice/cli.test.ts
index b5bf8da..f9257c4 100644
--- a/novice/cli.test.ts
+++ b/novice/cli.test.ts
@@ -28,13 +28,15 @@ describe('cli', () => {
}
});
stdout = new Writable({
- write(str) {
+ write(str, encoding, callback) {
stdoutActual += str;
+ if (callback) callback();
}
});
stderr = new Writable({
- write(str) {
+ write(str, encoding, callback) {
stderrActual += str;
+ if (callback) callback();
}
});
diff --git a/novice/simulator/cli-debugger.test.ts b/novice/simulator/cli-debugger.test.ts
new file mode 100644
index 0000000..630c814
--- /dev/null
+++ b/novice/simulator/cli-debugger.test.ts
@@ -0,0 +1,77 @@
+import { getIsa } from '../isa';
+import { CliDebugger } from './cli-debugger';
+import { Readable, Writable } from 'stream';
+
+jest.mock('readline');
+import * as readline from 'readline';
+
+describe('cli debugger', () => {
+ let dbg: CliDebugger;
+ let stdinActual: string, stdoutActual: string;
+ let stdin: Readable, stdout: Writable;
+ let mockInterface: readline.Interface;
+
+ beforeEach(() => {
+ stdinActual = stdoutActual = '';
+ // @ts-ignore
+ readline.createInterface.mockReturnValue(mockInterface);
+ });
+
+ beforeAll(() => {
+ stdin = new Readable({
+ read(n) {
+ n = Math.min(n, stdinActual.length);
+ if (!n) {
+ return null;
+ } else {
+ const res = Buffer.from(stdinActual.slice(0, n));
+ stdinActual = stdinActual.slice(n);
+ return res;
+ }
+ }
+ });
+ stdout = new Writable({
+ write(data, enc, cb) {
+ stdoutActual += data.toString();
+ if (cb) cb();
+ }
+ });
+
+ // @ts-ignore
+ mockInterface = {
+ //close: () => {stdin.finish(); stdout.finish();},
+ close: jest.fn(),
+ on: jest.fn(),
+ question: jest.fn(),
+ };
+ });
+
+ afterEach(() => {
+ // @ts-ignore
+ readline.createInterface.mockReset();
+ });
+
+ function runCmd(cmd: string): void {
+ // @ts-ignore
+ mockInterface.question.mockImplementationOnce((q, cb) => {
+ cb(cmd);
+ });
+ }
+
+ describe('lc3 debugging', () => {
+ beforeEach(() => {
+ dbg = new CliDebugger(getIsa('lc3'), stdin, stdout);
+ });
+
+ it('quits', () => {
+ runCmd('h');
+ runCmd('q');
+
+ return dbg.run().then(() => {
+ expect(stdoutActual).toMatch('novice debugger usage');
+ expect(stdoutActual).toMatch(/h\[elp\]\s+show this message/);
+ expect(stdoutActual).toMatch(/q\[uit\]\s+escape this foul debugger/);
+ });
+ });
+ });
+});