From 19192a3ab6c9bce8cbbb7c7115b3a7aa6da098d5 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 19 Feb 2021 12:24:52 +0100 Subject: [PATCH] raise for negative or null id --- Makefile | 2 +- src/index.test.ts | 14 +++++++++++--- src/index.ts | 6 ++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 3fcc48b..8060b47 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,4 @@ run: deno run --watch --unstable src/index.ts tests: - deno test \ No newline at end of file + deno test --coverage --unstable \ No newline at end of file diff --git a/src/index.test.ts b/src/index.test.ts index 4111cac..66c6250 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,5 +1,8 @@ -import { assertStrictEquals } from "https://deno.land/std@0.87.0/testing/asserts.ts"; -import { sayHello } from "./index.ts"; +import { + assertStrictEquals, + assertThrows, +} from "https://deno.land/std@0.87.0/testing/asserts.ts"; +import { sayHello, ValidationError } from "./index.ts"; Deno.test("Hello test", () => { const actual = sayHello(); @@ -7,10 +10,15 @@ Deno.test("Hello test", () => { assertStrictEquals(actual, expected); }); -Deno.test("Random Hello", () => { +Deno.test("Specific Hello", () => { const id = 5; const actual = sayHello(id); const expected = "Bonjour le monde"; assertStrictEquals(actual, expected); }); + +Deno.test("Hello fails for non strictly positive values", () => { + const id = -1; + assertThrows(() => sayHello(id), ValidationError, "Invalid index: -1"); +}); diff --git a/src/index.ts b/src/index.ts index b7bf741..2b1c5a6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,10 @@ +export class ValidationError extends Error {} /** * Display the iconic Hello, World */ export const sayHello = (id?: number) => { + if (id && id < 1) { + throw new ValidationError(`Invalid index: ${id}`); + } return (id === 5) ? "Bonjour le monde" : "Hello, World!"; }; - -console.log(sayHello());