From f7dcf0e5b2c0a8c51f1940b38f6acaad3f41bd00 Mon Sep 17 00:00:00 2001 From: Bart Mesuere Date: Wed, 31 Jul 2024 15:01:59 +0200 Subject: [PATCH] add basic command runner tests --- lib/commands/unipept/unipept_subcommand.ts | 2 +- .../unipept/unipept_subcommand.test.ts | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/commands/unipept/unipept_subcommand.test.ts diff --git a/lib/commands/unipept/unipept_subcommand.ts b/lib/commands/unipept/unipept_subcommand.ts index 3ebecee6..a9e6e593 100644 --- a/lib/commands/unipept/unipept_subcommand.ts +++ b/lib/commands/unipept/unipept_subcommand.ts @@ -149,6 +149,6 @@ export abstract class UnipeptSubcommand { } private globToRegex(glob: string): RegExp { - return new RegExp(glob.replace(/\*/g, ".*")); + return new RegExp(`^${glob.replace(/\*/g, ".*")}$`); } } diff --git a/tests/commands/unipept/unipept_subcommand.test.ts b/tests/commands/unipept/unipept_subcommand.test.ts new file mode 100644 index 00000000..de816a36 --- /dev/null +++ b/tests/commands/unipept/unipept_subcommand.test.ts @@ -0,0 +1,58 @@ +import { Interface } from 'readline'; +import { Pept2lca } from '../../../lib/commands/unipept/pept2lca'; + +test('test command setup', () => { + const command = new Pept2lca(); + expect(command.name).toBe("pept2lca"); + expect(command.user_agent).toMatch(/^unipept-cli/); + expect(command.command.name()).toBe("pept2lca"); +}); + +test('test correct host', () => { + const command = new Pept2lca(); + + expect(command.host).toBe("https://api.unipept.ugent.be"); + expect(command["getHost"]()).toBe("https://api.unipept.ugent.be"); + + command.options.host = "https://optionshost"; + expect(command["getHost"]()).toBe("https://optionshost"); + + command.options.host = "http://optionshost"; + expect(command["getHost"]()).toBe("http://optionshost"); + + command.options.host = "optionshost"; + expect(command["getHost"]()).toBe("http://optionshost"); +}); + +test('test correct inputIterator', async () => { + const command = new Pept2lca(); + + // should be stdin + let input = command["getInputIterator"]([]) as Interface; + expect(input).toBeInstanceOf(Interface); + input.close(); + + // should be a (non-existant) file and error + input = command["getInputIterator"]([], "filename") as Interface; + input.on("error", (e) => { + expect(e.toString()).toMatch(/no such file/); + }); + + // should be array + const inputArray = command["getInputIterator"](["A", "B"]); + expect(inputArray).toBeInstanceOf(Array); +}); + +test('test selected fields parsing', () => { + const command = new Pept2lca(); + + command.options.select = ["a,b,c"]; + expect(command["getSelectedFields"]()).toStrictEqual([/^a$/, /^b$/, /^c$/]); +}); + +test('test selected fields with wildcards', () => { + const command = new Pept2lca(); + + command.options.select = ["taxon*,name"]; + expect(command["getSelectedFields"]()).toStrictEqual([/^taxon.*$/, /^name$/]); +});