Commit fd591f03 authored by Romain Deneau's avatar Romain Deneau

Ajout du TypeScript

parent fd69fc8f
......@@ -56,3 +56,27 @@ BUILD SUCCESSFUL in 2s
4 passing
```
### TypeScript
- Aller dans le répertoire `typescript`
- Ouvrir une console
- Installer les packages npm : `npm i` ou `yarn`
- Le moteur des tests unitaires est [jest](https://facebook.github.io/jest/) combiné avec [ts-jest](https://github.com/kulshekhar/ts-jest) pour supporter les fichiers TypeScript :
- Exécuter la commande `npm test` en console pour lancer les tests en one shot. Vérifier l'obtention de 4 tests passants comme dans la sortie ci-dessous.
- Utiliser ensuite `npm run tdd` en mode watch : les tests sont lancés à chaque enregistrement d'une modification dans un fichier.
```txt
PASS src\videostore.spec.ts
VideoStore
✔️ test single NewRelease statement
✔️ test dual NewRelease statement
✔️ test single children statement
✔️ test multiple regular statement
Test Suites: 1 passed, 1 total
Tests: 4 passed, 4 total
Snapshots: 0 total
Time: 2.37s
Ran all test suites.
```
{
"name": "videostore",
"version": "1.0.0",
"description": "Videostore kata - uncle bob style",
"author": "Soat",
"license": "ISC",
"main": "index.js",
"scripts": {
"test": "jest",
"tdd": "jest --watch"
},
"devDependencies": {
"@types/jest": "^22.1.1",
"jest": "^22.1.4",
"ts-jest": "^22.0.3",
"tslint": "^5.9.1",
"typescript": "^2.7.1"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
}
import { Movie } from './movie';
import { Rental } from './rental';
export class Customer {
private _rentals: Rental[] = [];
constructor(readonly name: string) {
}
addRental(rental: Rental) {
this._rentals.push(rental);
}
statement(): string {
let frequentRenterPoints = 0;
let totalAmount = 0;
let result = 'Rental Record for ' + this.name + '\n';
for (let each of this._rentals) {
let thisAmount = 0;
// dtermines the amount for each line
switch (each.movie.priceCode) {
case Movie.REGULAR:
thisAmount += 2;
if (each.daysRented > 2) {
thisAmount = thisAmount + (each.daysRented - 2) * 1.5;
}
break;
case Movie.NEW_RELEASE:
thisAmount = thisAmount + each.daysRented * 3;
break;
case Movie.CHILDREN:
thisAmount += 1.5;
if (each.daysRented > 3) {
thisAmount = thisAmount + (each.daysRented - 3) * 1.5;
}
break;
}
frequentRenterPoints++;
if (each.movie.priceCode == Movie.NEW_RELEASE && each.daysRented > 1) {
frequentRenterPoints++;
}
result += '\t' + each.movie.title + '\t' + thisAmount.toFixed(1) + '\n';
totalAmount += thisAmount;
}
result += 'You owed ' + totalAmount.toFixed(1) + '\n';
result += 'You earned ' + frequentRenterPoints + ' frequent renter points \n';
return result;
}
}
\ No newline at end of file
export class Movie {
static readonly REGULAR = 0;
static readonly NEW_RELEASE = 1;
static readonly CHILDREN = 2;
constructor (
readonly title: string,
public priceCode: number,
) {}
}
\ No newline at end of file
import { Movie } from "./movie";
export class Rental {
public constructor(
readonly movie: Movie,
readonly daysRented: number,
) {}
}
\ No newline at end of file
import { Customer } from './customer';
import { Movie } from './movie';
import { Rental } from './rental';
describe('VideoStore', () => {
let customer: Customer;
beforeEach(() => {
customer = new Customer('Fred');
});
it('test single NewRelease statement', () => {
customer.addRental(new Rental(new Movie('The Cell', Movie.NEW_RELEASE), 3));
expect(customer.statement())
.toBe('Rental Record for Fred\n\tThe Cell\t9.0\nYou owed 9.0\nYou earned 2 frequent renter points \n');
});
it('test dual NewRelease statement', () => {
customer.addRental(new Rental(new Movie('The Cell', Movie.NEW_RELEASE), 3));
customer.addRental(new Rental(new Movie('The Tigger Movie', Movie.NEW_RELEASE), 3));
expect(customer.statement())
.toBe('Rental Record for Fred\n\tThe Cell\t9.0\n\tThe Tigger Movie\t9.0\nYou owed 18.0\nYou earned 4 frequent renter points \n');
});
it('test single children statement', () => {
customer.addRental(new Rental(new Movie('The Tigger Movie', Movie.CHILDREN), 3));
expect(customer.statement())
.toBe('Rental Record for Fred\n\tThe Tigger Movie\t1.5\nYou owed 1.5\nYou earned 1 frequent renter points \n',);
});
it('test multiple regular statement', () => {
customer.addRental(new Rental(new Movie('Plan 9 from Outer Space', Movie.REGULAR), 1));
customer.addRental(new Rental(new Movie('8 1/2', Movie.REGULAR), 2));
customer.addRental(new Rental(new Movie('Eraserhead', Movie.REGULAR), 3));
expect(customer.statement())
.toBe('Rental Record for Fred\n\tPlan 9 from Outer Space\t2.0\n\t8 1/2\t2.0\n\tEraserhead\t3.5\nYou owed 7.5\nYou earned 3 frequent renter points \n');
});
});
{
"compilerOptions": {
"module": "commonjs",
"strict": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": [
"es2015", "es2016", "es2017", "esnext"
]
},
"include": [
"src/**/*"
]
}
\ No newline at end of file
{
"extends": "tslint:latest",
"rulesDirectory": "built/local/tslint/rules",
"rules": {
"array-type": [true, "array"],
"ban-types": {
"options": [
["Object", "Avoid using the `Object` type. Did you mean `object`?"],
["Function", "Avoid using the `Function` type. Prefer a specific function type, like `() => void`, or use `ts.AnyFunction`."],
["Boolean", "Avoid using the `Boolean` type. Did you mean `boolean`?"],
["Number", "Avoid using the `Number` type. Did you mean `number`?"],
["String", "Avoid using the `String` type. Did you mean `string`?"]
]
},
"boolean-trivia": true,
"class-name": true,
"comment-format": [true,
"check-space"
],
"curly":[true, "ignore-same-line"],
"debug-assert": true,
"indent": [true,
"spaces"
],
"interface-name": [true, "never-prefix"],
"interface-over-type-literal": true,
"jsdoc-format": true,
"linebreak-style": [true, "CRLF"],
"next-line": [true,
"check-catch",
"check-else"
],
"no-bom": true,
"no-double-space": true,
"no-in-operator": true,
"no-increment-decrement": true,
"no-inferrable-types": true,
"no-internal-module": true,
"no-null-keyword": true,
"no-switch-case-fall-through": true,
"no-trailing-whitespace": [true, "ignore-template-strings"],
"no-type-assertion-whitespace": true,
"no-var-keyword": true,
"object-literal-shorthand": true,
"object-literal-surrounding-space": true,
"one-line": [true,
"check-open-brace",
"check-whitespace"
],
"prefer-const": true,
"quotemark": [true,
"double",
"avoid-escape"
],
"semicolon": [true, "always", "ignore-bound-class-methods"],
"space-within-parens": true,
"triple-equals": true,
"type-operator-spacing": true,
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
},
{
"call-signature": "onespace",
"index-signature": "onespace",
"parameter": "onespace",
"property-declaration": "onespace",
"variable-declaration": "onespace"
}
],
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-module",
"check-separator",
"check-type"
],
"no-implicit-dependencies": [true, "dev"],
"object-literal-key-quotes": [true, "consistent-as-needed"],
"variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore"],
"arrow-parens": false,
"arrow-return-shorthand": false,
"forin": false,
"member-access": false,
"no-conditional-assignment": false,
"no-console": false,
"no-debugger": false,
"no-empty-interface": false,
"no-eval": false,
"no-object-literal-type-assertion": false,
"no-shadowed-variable": false,
"no-submodule-imports": false,
"no-unnecessary-initializer": false,
"no-var-requires": false,
"ordered-imports": false,
"prefer-conditional-expression": false,
"radix": false,
"trailing-comma": false,
"align": false,
"eofline": false,
"max-line-length": false,
"no-consecutive-blank-lines": false,
"space-before-function-paren": false,
"ban-comma-operator": false,
"max-classes-per-file": false,
"member-ordering": false,
"no-angle-bracket-type-assertion": false,
"no-bitwise": false,
"no-namespace": false,
"no-reference": false,
"object-literal-sort-keys": false,
"one-variable-per-declaration": false
}
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment