Compare commits
4 Commits
97522013f3
...
1ab49d8b08
Author | SHA1 | Date | |
---|---|---|---|
1ab49d8b08 | |||
98edde4a8f | |||
e61ce661f5 | |||
b47e3608b5 |
29
src/exercises/TP1/AgeValidation/index.test.ts
Normal file
29
src/exercises/TP1/AgeValidation/index.test.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import { AgeValidation } from "./index";
|
||||||
|
|
||||||
|
describe("Test AgeValidation", () => {
|
||||||
|
const ageValidator = new AgeValidation();
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ input: "30", expected: 30 },
|
||||||
|
{ input: "25", expected: 25 },
|
||||||
|
{ input: "50", expected: 50 },
|
||||||
|
])(
|
||||||
|
"should validate age %s correctly",
|
||||||
|
({ input, expected }) => {
|
||||||
|
const result = ageValidator.validateAge(input);
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
console.log(`Test with input = ${input}, expected = ${expected}, result = ${result}`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ input: "17", expectedErrorMsg: "L'âge doit être compris entre 18 et 130 ans" },
|
||||||
|
{ input: "131", expectedErrorMsg: "L'âge doit être compris entre 18 et 130 ans" },
|
||||||
|
{ input: "abc", expectedErrorMsg: "L'âge doit représenter un nombre entier positif" },
|
||||||
|
])(
|
||||||
|
"should throw a validation exception for invalid age %s",
|
||||||
|
({ input, expectedErrorMsg }) => {
|
||||||
|
expect(() => ageValidator.validateAdult(input)).toThrowError(expectedErrorMsg);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
40
src/exercises/TP1/AgeValidation/index.ts
Normal file
40
src/exercises/TP1/AgeValidation/index.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
export class AgeValidation {
|
||||||
|
validateAge(input: string): number {
|
||||||
|
try {
|
||||||
|
const age = this.validateNumber(input);
|
||||||
|
if (!Number.isInteger(age) || age < 0) {
|
||||||
|
throw new ValidationException("L'âge doit représenter un nombre entier positif");
|
||||||
|
}
|
||||||
|
return age;
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof ValidationException) {
|
||||||
|
throw new ValidationException("L'âge doit représenter un nombre entier positif");
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
validateAdult(input: string): number {
|
||||||
|
const age = this.validateAge(input);
|
||||||
|
if (age < 18 || age > 130) {
|
||||||
|
throw new ValidationException("L'âge doit être compris entre 18 et 130 ans");
|
||||||
|
}
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
private validateNumber(input: string): number {
|
||||||
|
const num = parseFloat(input);
|
||||||
|
if (isNaN(num)) {
|
||||||
|
throw new ValidationException('La valeur doit représenter un nombre');
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ValidationException extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message);
|
||||||
|
this.name = 'ValidationException';
|
||||||
|
}
|
||||||
|
}
|
57
src/exercises/TP1/NumberValidation/index.test.ts
Normal file
57
src/exercises/TP1/NumberValidation/index.test.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import { NumberValidation } from "./index";
|
||||||
|
|
||||||
|
describe("Test NumberValidation", () => {
|
||||||
|
const numberValidator = new NumberValidation();
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ input: "123", expected: 123 },
|
||||||
|
{ input: "123.45", expected: 123.45 },
|
||||||
|
{ input: "-123", expected: -123 }
|
||||||
|
])(
|
||||||
|
"should validate number %s correctly",
|
||||||
|
({ input, expected }) => {
|
||||||
|
const result = numberValidator.validateNumber(input);
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
console.log(`Test with input = ${input}, expected = ${expected}, result = ${result}`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ input: "123", expected: 123 },
|
||||||
|
{ input: "-123", expected: -123 }
|
||||||
|
])(
|
||||||
|
"should validate integer %s correctly",
|
||||||
|
({ input, expected }) => {
|
||||||
|
const result = numberValidator.validateInteger(input);
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
console.log(`Test with input = ${input}, expected = ${expected}, result = ${result}`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ input: "123", expected: 123 },
|
||||||
|
{ input: "123.45", expected: 123.45 }
|
||||||
|
])(
|
||||||
|
"should validate positive number %s correctly",
|
||||||
|
({ input, expected }) => {
|
||||||
|
const result = numberValidator.validatePositive(input);
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
console.log(`Test with input = ${input}, expected = ${expected}, result = ${result}`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
it("should throw a validation exception when input is not a number", () => {
|
||||||
|
const input = "abc";
|
||||||
|
expect(() => numberValidator.validateNumber(input)).toThrowError("La valeur doit représenter un nombre");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw a validation exception when input is not an integer", () => {
|
||||||
|
const input = "123.45";
|
||||||
|
expect(() => numberValidator.validateInteger(input)).toThrowError("La valeur doit représenter un nombre entier");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw a validation exception when input is not a positive number", () => {
|
||||||
|
const input = "-123";
|
||||||
|
expect(() => numberValidator.validatePositive(input)).toThrowError("La valeur doit représenter un nombre positif");
|
||||||
|
});
|
||||||
|
});
|
32
src/exercises/TP1/NumberValidation/index.ts
Normal file
32
src/exercises/TP1/NumberValidation/index.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
export class ValidationException extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message);
|
||||||
|
this.name = 'ValidationException';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NumberValidation {
|
||||||
|
validateNumber(input: string): number {
|
||||||
|
const num = parseFloat(input);
|
||||||
|
if (isNaN(num)) {
|
||||||
|
throw new ValidationException('La valeur doit représenter un nombre');
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
validateInteger(input: string): number {
|
||||||
|
const num = this.validateNumber(input);
|
||||||
|
if (!Number.isInteger(num)) {
|
||||||
|
throw new ValidationException('La valeur doit représenter un nombre entier');
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
validatePositive(input: string): number {
|
||||||
|
const num = this.validateNumber(input);
|
||||||
|
if (num <= 0) {
|
||||||
|
throw new ValidationException('La valeur doit représenter un nombre positif');
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
}
|
35
src/exercises/TP1/PhoneValidation/index.test.ts
Normal file
35
src/exercises/TP1/PhoneValidation/index.test.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { PhoneValidation } from "./index";
|
||||||
|
|
||||||
|
describe("Test PhoneValidation", () => {
|
||||||
|
const phoneValidator = new PhoneValidation();
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ input: "0612345678", expected: "0612345678" },
|
||||||
|
{ input: "07.12.34.56.78", expected: "07.12.34.56.78" },
|
||||||
|
{ input: "04 12 34 56 78", expected: "04 12 34 56 78" },
|
||||||
|
{ input: "08/12/34/56/78", expected: "08/12/34/56/78" }
|
||||||
|
])(
|
||||||
|
"should validate phone %s correctly",
|
||||||
|
({ input, expected }) => {
|
||||||
|
const result = phoneValidator.validatePhone(input);
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
console.log(`Test with input = ${input}, expected = ${expected}, result = ${result}`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{ input: "", expectedErrorMsg: "Le numéro ne peut pas être vide" },
|
||||||
|
{ input: "123", expectedErrorMsg: "Le numéro doit contenir exactement 10 chiffres" },
|
||||||
|
{ input: "061234567", expectedErrorMsg: "Le numéro doit contenir exactement 10 chiffres" },
|
||||||
|
{ input: "0912345678", expectedErrorMsg: "Le numéro doit commencer par 04, 06, 07 ou 08" },
|
||||||
|
{ input: "06.123.45.6.78", expectedErrorMsg: "Le numéro doit être composé de 5 paires de deux chiffres séparés par des points, des espaces ou des slashs" },
|
||||||
|
{ input: "06/12/34/56", expectedErrorMsg: "Le numéro doit contenir exactement 10 chiffres" },
|
||||||
|
{ input: "0312345678", expectedErrorMsg: "Le numéro doit commencer par 04, 06, 07 ou 08" },
|
||||||
|
{ input: "05.12.34.56.78", expectedErrorMsg: "Le numéro doit commencer par 04, 06, 07 ou 08" }
|
||||||
|
])(
|
||||||
|
"should throw a validation exception for invalid phone %s",
|
||||||
|
({ input, expectedErrorMsg }) => {
|
||||||
|
expect(() => phoneValidator.validatePhone(input)).toThrowError(expectedErrorMsg);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
45
src/exercises/TP1/PhoneValidation/index.ts
Normal file
45
src/exercises/TP1/PhoneValidation/index.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
export class PhoneValidation {
|
||||||
|
validatePhone(input: string): string {
|
||||||
|
if (input.length === 0) {
|
||||||
|
throw new ValidationException("Le numéro ne peut pas être vide");
|
||||||
|
}
|
||||||
|
|
||||||
|
// digit checker
|
||||||
|
const digitsOnly = input.replace(/[ ./]/g, '');
|
||||||
|
if (!/^\d{10}$/.test(digitsOnly)) {
|
||||||
|
throw new ValidationException("Le numéro doit contenir exactement 10 chiffres");
|
||||||
|
}
|
||||||
|
|
||||||
|
// check number
|
||||||
|
if (!/^(04|06|07|08)/.test(digitsOnly)) {
|
||||||
|
throw new ValidationException("Le numéro doit commencer par 04, 06, 07 ou 08");
|
||||||
|
}
|
||||||
|
|
||||||
|
// separator is '.' or '/' or ' ' and identiques
|
||||||
|
const separators = input.match(/[ ./]/g);
|
||||||
|
if (separators) {
|
||||||
|
if (separators.length !== 4) {
|
||||||
|
throw new ValidationException("Le numéro doit comporter 5 paires de chiffres séparés par des points, des espaces ou des slashs, mais pas en même temps");
|
||||||
|
}
|
||||||
|
|
||||||
|
const separator = separators[0];
|
||||||
|
if (!/^[ .\/]$/.test(separator)) {
|
||||||
|
throw new ValidationException("Les séparateurs acceptés sont seulement des points, des espaces ou des slashs");
|
||||||
|
}
|
||||||
|
|
||||||
|
const parts = input.split(separator);
|
||||||
|
if (parts.length !== 5 || parts.some(part => part.length !== 2 || !/^\d{2}$/.test(part))) {
|
||||||
|
throw new ValidationException("Le numéro doit être composé de 5 paires de deux chiffres séparés par des points, des espaces ou des slashs");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ValidationException extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message);
|
||||||
|
this.name = 'ValidationException';
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,3 @@
|
|||||||
async function start() {
|
|
||||||
console.log("Application starts...");
|
|
||||||
|
|
||||||
console.log("Application ends...");
|
|
||||||
}
|
|
||||||
|
|
||||||
start();
|
|
||||||
export function name(foo : number) : number {
|
export function name(foo : number) : number {
|
||||||
return foo + 1;
|
return foo + 1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user