improve tests

This commit is contained in:
sbriat 2023-09-14 17:28:42 +02:00
parent f69afc4481
commit a7c73080a7
3 changed files with 37 additions and 9 deletions

View File

@ -10,12 +10,11 @@ export class CarpoolPathCreator {
private readonly passengerWaypoints: Point[], private readonly passengerWaypoints: Point[],
) {} ) {}
public createCarpoolPath = (): WayStep[] => { public createCarpoolPath = (): WayStep[] =>
const passengerWaysteps: WayStep[] = this._createPassengerWaysteps(); this._createMixedWaysteps(
if (this.driverWaypoints.length == 2) { this._createDriverWaysteps(),
} this._createPassengerWaysteps(),
return passengerWaysteps; );
};
private _createDriverWaysteps = (): WayStep[] => private _createDriverWaysteps = (): WayStep[] =>
this.driverWaypoints.map( this.driverWaypoints.map(
@ -48,7 +47,7 @@ export class CarpoolPathCreator {
}); });
if ( if (
this.driverWaypoints.filter((driverWaypoint: Point) => this.driverWaypoints.filter((driverWaypoint: Point) =>
this._isSamePoint(driverWaypoint, passengerWaypoint), passengerWaypoint.isSame(driverWaypoint),
).length > 0 ).length > 0
) { ) {
waystep.actors.push( waystep.actors.push(
@ -64,8 +63,18 @@ export class CarpoolPathCreator {
return waysteps; return waysteps;
}; };
private _isSamePoint = (point1: Point, point2: Point): boolean => private _createMixedWaysteps = (
point1.lon === point2.lon && point1.lat === point2.lat; 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 => private _getTarget = (index: number, waypoints: Point[]): Target =>
index == 0 index == 0

View File

@ -22,6 +22,9 @@ export class Point extends ValueObject<PointProps> {
return this.props.lat; return this.props.lat;
} }
isSame = (point: this): boolean =>
point.lon == this.lon && point.lat == this.lat;
protected validate(props: PointProps): void { protected validate(props: PointProps): void {
if (props.lon > 180 || props.lon < -180) if (props.lon > 180 || props.lon < -180)
throw new ArgumentOutOfRangeException('lon must be between -180 and 180'); throw new ArgumentOutOfRangeException('lon must be between -180 and 180');

View File

@ -10,6 +10,22 @@ describe('Point value object', () => {
expect(pointVO.lat).toBe(48.689445); expect(pointVO.lat).toBe(48.689445);
expect(pointVO.lon).toBe(6.17651); 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', () => { it('should throw an exception if longitude is invalid', () => {
try { try {
new Point({ new Point({