raise for negative or null id

This commit is contained in:
Ruidy 2021-02-19 12:24:52 +01:00
parent da3258aa21
commit 19192a3ab6
3 changed files with 16 additions and 6 deletions

View file

@ -2,4 +2,4 @@
run:
deno run --watch --unstable src/index.ts
tests:
deno test
deno test --coverage --unstable

View file

@ -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");
});

View file

@ -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());