Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Jun 21, 2024
1 parent aa5d358 commit c24382a
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/commands/prot2pept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ The input should have either one protein sequence per line or contain a FASTA fo
*/
async run() {
this.parseArguments();
const pattern = new RegExp(this.program.opts().pattern, "g");

let pattern;
try {
pattern = new RegExp(this.program.opts().pattern, "g");
} catch (e) {
this.program.error(`Your pattern was invalid: ${(e as Error).message}`);
//process.exit(1);
}

let fasta = false;
let protein = [];
Expand Down
124 changes: 124 additions & 0 deletions tests/commands/prot2pept.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { Prot2pept } from '../../lib/commands/prot2pept';
import { jest } from '@jest/globals';
import * as mock from 'mock-stdin';

let output: string[];
let error: string[];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const writeSpy = jest
.spyOn(process.stdout, "write")
.mockImplementation((data: unknown) => { output.push(data as string); return true; });
const errorSpy = jest
.spyOn(process.stderr, "write")
.mockImplementation((data: unknown) => { error.push(data as string); return true; });

beforeEach(() => {
output = [];
error = [];
});

test('test single line input 1', async () => {
const stdin = mock.stdin();

const command = new Prot2pept();
const run = command.run();

stdin.send("AALTERAALTERPAALTER\n");
stdin.end();

await run;

expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output.join("").trimEnd()).toBe("AALTER\nAALTERPAALTER");
});

test('test single line input 2', async () => {
const stdin = mock.stdin();

const command = new Prot2pept();
const run = command.run();

stdin.send("KRKPR\n");
stdin.end();

await run;

expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output.join("").trimEnd()).toBe("K\nR\nKPR");
});

test('test multi line input', async () => {
const stdin = mock.stdin();

const command = new Prot2pept();
const run = command.run();

stdin.send("AALTERAALTERPAALTER\n");
stdin.send("AALTERAA\n");
stdin.end();

await run;

expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output.join("").trimEnd()).toBe("AALTER\nAALTERPAALTER\nAALTER\nAA");
});

test('test fasta input 1', async () => {
const stdin = mock.stdin();

const command = new Prot2pept();
const run = command.run();

stdin.send(">AKA\nAALTERAALTERPAALTER\n");
stdin.end();

await run;

expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output.join("").trimEnd()).toBe(">AKA\nAALTER\nAALTERPAALTER");
});

test('test fasta input 2', async () => {
const stdin = mock.stdin();

const command = new Prot2pept();
const run = command.run();

stdin.send(">AKA\nAAL\nT\nERAALTER\nP\nAALTER\n");
stdin.end();

await run;

expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output.join("").trimEnd()).toBe(">AKA\nAALTER\nAALTERPAALTER");
});

test('test fasta input 3', async () => {
const stdin = mock.stdin();

const command = new Prot2pept();
const run = command.run();

stdin.send(">AKA\nAAL\nT\n>\nERAALTER\nP\nAALTER");
stdin.end();

await run;

expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output.join("").trimEnd()).toBe(">AKA\nAALT\n>\nER\nAALTERPAALTER");
});

test('test custom pattern', async () => {
const stdin = mock.stdin();

const command = new Prot2pept({ args: ["--pattern", "([KR])([^A])"] });
const run = command.run();

stdin.send("AALTERAALTERPAALTER\n");
stdin.end();

await run;

expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output.join("").trimEnd()).toBe("AALTERAALTER\nPAALTER");
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"module": "NodeNext",
"strict": true,
"resolveJsonModule": true,
"target": "esnext"
}
}

0 comments on commit c24382a

Please sign in to comment.