mirror of
https://github.com/rjNemo/deno_hello
synced 2026-06-12 12:56:38 +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:
|
run:
|
||||||
deno run --watch --unstable src/index.ts
|
deno run --watch --unstable src/index.ts
|
||||||
tests:
|
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 {
|
||||||
import { sayHello } from "./index.ts";
|
assertStrictEquals,
|
||||||
|
assertThrows,
|
||||||
|
} from "https://deno.land/std@0.87.0/testing/asserts.ts";
|
||||||
|
import { sayHello, ValidationError } from "./index.ts";
|
||||||
|
|
||||||
Deno.test("Hello test", () => {
|
Deno.test("Hello test", () => {
|
||||||
const actual = sayHello();
|
const actual = sayHello();
|
||||||
|
|
@ -7,10 +10,15 @@ Deno.test("Hello test", () => {
|
||||||
assertStrictEquals(actual, expected);
|
assertStrictEquals(actual, expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("Random Hello", () => {
|
Deno.test("Specific Hello", () => {
|
||||||
const id = 5;
|
const id = 5;
|
||||||
const actual = sayHello(id);
|
const actual = sayHello(id);
|
||||||
const expected = "Bonjour le monde";
|
const expected = "Bonjour le monde";
|
||||||
|
|
||||||
assertStrictEquals(actual, expected);
|
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
|
* Display the iconic Hello, World
|
||||||
*/
|
*/
|
||||||
export const sayHello = (id?: number) => {
|
export const sayHello = (id?: number) => {
|
||||||
|
if (id && id < 1) {
|
||||||
|
throw new ValidationError(`Invalid index: ${id}`);
|
||||||
|
}
|
||||||
return (id === 5) ? "Bonjour le monde" : "Hello, World!";
|
return (id === 5) ? "Bonjour le monde" : "Hello, World!";
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(sayHello());
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue