aboutsummaryrefslogtreecommitdiffgithub
path: root/novice/simulator/cli-debugger.test.ts
blob: 630c814b28da0e5de120dd3454e94edec3ad4202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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/);
            });
        });
    });
});