aboutsummaryrefslogtreecommitdiffgithub
diff options
context:
space:
mode:
authorAustin Adams <git@austinjadams.com>2019-01-13 14:36:20 -0500
committerAustin Adams <git@austinjadams.com>2019-01-13 14:36:20 -0500
commit911ce30fcf960afccf1497ef46b4bfce40621a71 (patch)
tree65af8bf8f5aa8648bdb78cad00f9e27d6f70769c
parentd971a11e2a499fb672dd555a25499102de50b055 (diff)
downloadnovice-911ce30fcf960afccf1497ef46b4bfce40621a71.tar.gz
novice-911ce30fcf960afccf1497ef46b4bfce40621a71.tar.xz
Update tests for new Assembler, argparse
-rw-r--r--novice/assembler/assembler.test.ts4
-rw-r--r--novice/assembler/index.ts2
-rw-r--r--novice/cli.test.ts110
3 files changed, 51 insertions, 65 deletions
diff --git a/novice/assembler/assembler.test.ts b/novice/assembler/assembler.test.ts
index 24172ab..0b059ab 100644
--- a/novice/assembler/assembler.test.ts
+++ b/novice/assembler/assembler.test.ts
@@ -1,5 +1,5 @@
import { Readable } from 'stream';
-import { getParser, getIsa, Assembler } from '.';
+import { getConfig, Assembler } from '.';
describe('assembler', () => {
describe('parse(fp)', () => {
@@ -9,7 +9,7 @@ describe('assembler', () => {
beforeEach(() => {
fp = new Readable();
// Test the complx parser and lc3 isa for now
- assembler = new Assembler(getParser("complx"), getIsa("lc3"));
+ assembler = new Assembler(getConfig('lc3'));
});
it('parses trivial program', () => {
diff --git a/novice/assembler/index.ts b/novice/assembler/index.ts
index 3555dc2..49e5094 100644
--- a/novice/assembler/index.ts
+++ b/novice/assembler/index.ts
@@ -59,4 +59,4 @@ function getConfig(configName: string): AssemblerConfig {
};
}
-export { Assembler, getParser, getGenerator, getIsa, getOpSpec, getSerializer, getConfig };
+export { Assembler, AssemblerConfig, getParser, getGenerator, getIsa, getOpSpec, getSerializer, getConfig };
diff --git a/novice/cli.test.ts b/novice/cli.test.ts
index 154a321..721c43a 100644
--- a/novice/cli.test.ts
+++ b/novice/cli.test.ts
@@ -1,12 +1,11 @@
import { Readable, Writable } from 'stream';
import main from './cli';
-import { Parser } from './assembler/parsers';
// Mocks
jest.mock('fs');
import * as fs from 'fs';
jest.mock('./assembler');
-import { Assembler, getParser, getIsa } from './assembler';
+import { Assembler, AssemblerConfig, getConfig, getParser } from './assembler';
describe('cli', () => {
let stdout: Writable, stderr: Writable;
@@ -31,32 +30,25 @@ describe('cli', () => {
// @ts-ignore
fs.createReadStream.mockReset();
// @ts-ignore
+ fs.createWriteStream.mockReset();
+ // @ts-ignore
Assembler.mockReset();
// @ts-ignore
- getParser.mockReset();
+ getConfig.mockReset();
// @ts-ignore
- getIsa.mockReset();
- });
-
- it('prints usage with no args', () => {
- return main([], stdout, stderr).then(exitCode => {
- expect(exitCode).toEqual(1);
- expect(stdoutActual).toEqual('');
- expect(stderrActual).toContain('usage');
- });
+ getParser.mockReset();
});
- describe('asm-pass1 subcommand', () => {
- it('prints usage with missing args', () => {
- return main(['asm-pass1'], stdout, stderr).then(exitCode => {
- expect(exitCode).toEqual(1);
- expect(stdoutActual).toEqual('');
- expect(stderrActual).toContain('usage');
- });
- });
+ describe('asm subcommand', () => {
+ let mockInFp: Readable;
+ let mockOutFp: Writable;
+ let mockAssembleTo: (inFp: Readable, outFp: Writable) => Promise<void>;
+ let mockConfig: AssemblerConfig;
- it('parses asm file', () => {
- let mockFp = {
+ beforeEach(() => {
+ // @ts-ignore
+ mockInFp = {
+ // @ts-ignore
on(event: string, handler: () => void) {
if (event === 'readable') {
handler();
@@ -64,53 +56,63 @@ describe('cli', () => {
}
};
// @ts-ignore
- fs.createReadStream.mockReturnValue(mockFp);
+ fs.createReadStream.mockReturnValue(mockInFp);
- let json = {kush: 'coma'};
- let mockParse = jest.fn((fp: Readable) => Promise.resolve(json));
// @ts-ignore
- Assembler.mockImplementation((parser: Parser) => {
- return { parse: mockParse };
- });
+ mockOutFp = {
+ cork: () => {},
+ uncork: () => {},
+ end: () => {},
+ };
// @ts-ignore
- let mockParser: Parser = {thanku: 'next'};
+ fs.createWriteStream.mockReturnValue(mockOutFp);
+
// @ts-ignore
- getParser.mockReturnValue(mockParser);
+ mockAssembleTo = jest.fn((inFp: Readable, outFp: Writable) => Promise.resolve());
// @ts-ignore
- let mockIsa: Isa = {its: 'gucci'};
+ Assembler.mockImplementation((cfg: AssemblerConfig) => {
+ return { assembleTo: mockAssembleTo };
+ });
// @ts-ignore
- getIsa.mockReturnValue(mockIsa);
+ mockConfig = {
+ // @ts-ignore
+ serializer: {
+ fileExt: () => 'star',
+ },
+ };
+ // @ts-ignore
+ getConfig.mockReturnValue(mockConfig);
+ });
- return main(['asm-pass1', 'pizza', 'roll', 'patrick.asm'],
+ it('parses asm file', () => {
+ return main(['asm', '-c', 'bread', 'patrick.asm'],
stdout, stderr).then(exitCode => {
// @ts-ignore
expect(fs.createReadStream.mock.calls).toEqual([['patrick.asm']]);
// @ts-ignore
- expect(getParser.mock.calls).toEqual([['pizza']]);
+ expect(fs.createWriteStream.mock.calls).toEqual([['patrick.star']]);
// @ts-ignore
- expect(getIsa.mock.calls).toEqual([['roll']]);
+ expect(getConfig.mock.calls).toEqual([['bread']]);
// @ts-ignore
- expect(Assembler.mock.calls).toEqual([[mockParser, mockIsa]]);
- expect(mockParse.mock.calls).toEqual([[mockFp]]);
+ expect(Assembler.mock.calls).toEqual([[mockConfig]]);
+ // @ts-ignore
+ expect(mockAssembleTo.mock.calls).toEqual([[mockInFp, mockOutFp]]);
expect(exitCode).toEqual(0);
- expect(stdoutActual).toEqual(JSON.stringify(json));
+ expect(stdoutActual).toEqual('');
expect(stderrActual).toEqual('');
});
});
it('handles nonexistent file', () => {
- let mockFp = {
- on(event: string, handler: (reason: any) => void) {
- if (event === 'error') {
- handler(new Error('wow bad'));
- }
+ // @ts-ignore
+ mockInFp.on = (event: string, handler: (reason: any) => void) => {
+ if (event === 'error') {
+ handler(new Error('wow bad'));
}
};
- // @ts-ignore
- fs.createReadStream.mockReturnValue(mockFp);
- return main(['asm-pass1', 'michael', 'lin', 'sanjay.asm'],
+ return main(['asm', 'sanjay.asm'],
stdout, stderr).then(exitCode => {
// @ts-ignore
expect(fs.createReadStream.mock.calls).toEqual([['sanjay.asm']]);
@@ -123,22 +125,6 @@ describe('cli', () => {
});
describe('tablegen subcommand', () => {
- it('prints usage with no args', () => {
- return main(['tablegen'], stdout, stderr).then(exitCode => {
- expect(exitCode).toEqual(1);
- expect(stdoutActual).toEqual('');
- expect(stderrActual).toContain('usage');
- });
- });
-
- it('prints usage with extra args', () => {
- return main(['tablegen', 'big', 'daddy'], stdout, stderr).then(exitCode => {
- expect(exitCode).toEqual(1);
- expect(stdoutActual).toEqual('');
- expect(stderrActual).toContain('usage');
- });
- });
-
it('generates table', () => {
const json = {massive: 'banana'};
const mockGenTable = jest.fn(() => json);