Skip to content

Commit

Permalink
write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Jun 19, 2024
1 parent 6791e0c commit f825d53
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/commands/peptfilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ The input should have one peptide per line. FASTA headers are preserved in the o

async run() {
this.parseArguments();
console.log(this.program.opts())
const minLen = this.program.opts().minLen;
const maxlen = this.program.opts().maxLen;
const minLen = this.program.opts().minlen;
const maxlen = this.program.opts().maxlen;
const lacks = this.program.opts().lacks || [];
const contains = this.program.opts().contains || [];

Expand All @@ -33,6 +32,7 @@ The input should have one peptide per line. FASTA headers are preserved in the o
process.stdout.write(line + "\n");
continue;
}

if (Peptfilter.checkLength(line, minLen, maxlen) && Peptfilter.checkLacks(line, lacks) && Peptfilter.checkContains(line, contains)) {
process.stdout.write(line + "\n");
}
Expand Down
98 changes: 98 additions & 0 deletions tests/commands/peptfilter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { Peptfilter } from '../../lib/commands/peptfilter';
import * as mock from 'mock-stdin';

let output: string[];
let error: string[];
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 length filter', async () => {
// min length
expect(Peptfilter.checkLength('AALER', 4, 10)).toBe(true);
expect(Peptfilter.checkLength('AALER', 5, 10)).toBe(true);
expect(Peptfilter.checkLength('AALER', 6, 10)).toBe(false);

// max length
expect(Peptfilter.checkLength('AALER', 1, 4)).toBe(false);
expect(Peptfilter.checkLength('AALER', 1, 5)).toBe(true);
expect(Peptfilter.checkLength('AALER', 1, 6)).toBe(true);
});

test('test lacks filter', async () => {
expect(Peptfilter.checkLacks('AALER', ''.split(""))).toBe(true);
expect(Peptfilter.checkLacks('AALER', 'BCD'.split(""))).toBe(true);
expect(Peptfilter.checkLacks('AALER', 'A'.split(""))).toBe(false);
expect(Peptfilter.checkLacks('AALER', 'AE'.split(""))).toBe(false);
});

test('test contains filter', async () => {
expect(Peptfilter.checkContains('AALER', ''.split(""))).toBe(true);
expect(Peptfilter.checkContains('AALER', 'A'.split(""))).toBe(true);
expect(Peptfilter.checkContains('AALER', 'AE'.split(""))).toBe(true);
expect(Peptfilter.checkContains('AALER', 'BCD'.split(""))).toBe(false);
expect(Peptfilter.checkContains('AALER', 'AB'.split(""))).toBe(false);
});

test('test default filter from stdin', async () => {
const stdin = mock.stdin();

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

stdin.send("AAAA\n");
stdin.send("AAAAA\n");
stdin.end();

await run;

expect(writeSpy).toHaveBeenCalledTimes(1);
expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output.length).toBe(1);
});

test('test if it passes fasta from stdin', async () => {
const stdin = mock.stdin();

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

stdin.send(">AA\n");
stdin.send("AAA\n");
stdin.end();

await run;

expect(writeSpy).toHaveBeenCalledTimes(1);
expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output[0]).toBe(">AA\n");
});

test('test complex example from stdin', async () => {
const stdin = mock.stdin();

const command = new Peptfilter({ args: ["--minlen", "4", "--maxlen", "10", "--lacks", "B", "--contains", "A"] });
const run = command.run();

stdin.send("A\n");
stdin.send("AAAAAAAAAAA\n");
stdin.send("AAAAB\n");
stdin.send("BBBBB\n");
stdin.send("CCCCC\n");
stdin.send("CCCCCA\n");
stdin.end();

await run;

expect(writeSpy).toHaveBeenCalledTimes(1);
expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output[0]).toBe("CCCCCA\n");
});

0 comments on commit f825d53

Please sign in to comment.