From a7c73080a7b7268929a515e1e8de66986bfe769f Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 14 Sep 2023 17:28:42 +0200 Subject: [PATCH] improve tests --- .../domain/carpool-path-creator.service.ts | 27 ++++++++++++------- .../value-objects/point.value-object.ts | 3 +++ .../unit/core/point.value-object.spec.ts | 16 +++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/modules/ad/core/domain/carpool-path-creator.service.ts b/src/modules/ad/core/domain/carpool-path-creator.service.ts index 9af2fd9..aa615a2 100644 --- a/src/modules/ad/core/domain/carpool-path-creator.service.ts +++ b/src/modules/ad/core/domain/carpool-path-creator.service.ts @@ -10,12 +10,11 @@ export class CarpoolPathCreator { private readonly passengerWaypoints: Point[], ) {} - public createCarpoolPath = (): WayStep[] => { - const passengerWaysteps: WayStep[] = this._createPassengerWaysteps(); - if (this.driverWaypoints.length == 2) { - } - return passengerWaysteps; - }; + public createCarpoolPath = (): WayStep[] => + this._createMixedWaysteps( + this._createDriverWaysteps(), + this._createPassengerWaysteps(), + ); private _createDriverWaysteps = (): WayStep[] => this.driverWaypoints.map( @@ -48,7 +47,7 @@ export class CarpoolPathCreator { }); if ( this.driverWaypoints.filter((driverWaypoint: Point) => - this._isSamePoint(driverWaypoint, passengerWaypoint), + passengerWaypoint.isSame(driverWaypoint), ).length > 0 ) { waystep.actors.push( @@ -64,8 +63,18 @@ export class CarpoolPathCreator { return waysteps; }; - private _isSamePoint = (point1: Point, point2: Point): boolean => - point1.lon === point2.lon && point1.lat === point2.lat; + private _createMixedWaysteps = ( + driverWaysteps: WayStep[], + passengerWaysteps: WayStep[], + ): WayStep[] => + driverWaysteps.length == 2 + ? [driverWaysteps[0], ...passengerWaysteps, driverWaysteps[1]] + : this._createComplexMixedWaysteps(driverWaysteps, passengerWaysteps); + + private _createComplexMixedWaysteps = ( + driverWaysteps: WayStep[], + passengerWaysteps: WayStep[], + ): WayStep[] => []; private _getTarget = (index: number, waypoints: Point[]): Target => index == 0 diff --git a/src/modules/ad/core/domain/value-objects/point.value-object.ts b/src/modules/ad/core/domain/value-objects/point.value-object.ts index 2047ead..54a2677 100644 --- a/src/modules/ad/core/domain/value-objects/point.value-object.ts +++ b/src/modules/ad/core/domain/value-objects/point.value-object.ts @@ -22,6 +22,9 @@ export class Point extends ValueObject { return this.props.lat; } + isSame = (point: this): boolean => + point.lon == this.lon && point.lat == this.lat; + protected validate(props: PointProps): void { if (props.lon > 180 || props.lon < -180) throw new ArgumentOutOfRangeException('lon must be between -180 and 180'); diff --git a/src/modules/ad/tests/unit/core/point.value-object.spec.ts b/src/modules/ad/tests/unit/core/point.value-object.spec.ts index b6980e2..fb9943b 100644 --- a/src/modules/ad/tests/unit/core/point.value-object.spec.ts +++ b/src/modules/ad/tests/unit/core/point.value-object.spec.ts @@ -10,6 +10,22 @@ describe('Point value object', () => { expect(pointVO.lat).toBe(48.689445); expect(pointVO.lon).toBe(6.17651); }); + it('should check if two points are identical', () => { + const pointVO = new Point({ + lat: 48.689445, + lon: 6.17651, + }); + const identicalPointVO = new Point({ + lat: 48.689445, + lon: 6.17651, + }); + const differentPointVO = new Point({ + lat: 48.689446, + lon: 6.17651, + }); + expect(pointVO.isSame(identicalPointVO)).toBeTruthy(); + expect(pointVO.isSame(differentPointVO)).toBeFalsy(); + }); it('should throw an exception if longitude is invalid', () => { try { new Point({