mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-06 01:56:40 +00:00
raise for negative or null id
This commit is contained in:
parent
da3258aa21
commit
19192a3ab6
3 changed files with 16 additions and 6 deletions
2
Makefile
2
Makefile
|
|
@ -2,4 +2,4 @@
|
|||
run:
|
||||
deno run --watch --unstable src/index.ts
|
||||
tests:
|
||||
deno test
|
||||
deno test --coverage --unstable
|
||||
|
|
@ -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");
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
Loading…
Reference in a new issue