From 78e0b75c5da14699c7861eedbfef52073bebbc30 Mon Sep 17 00:00:00 2001 From: Dirk Riehle Date: Mon, 2 Dec 2024 19:55:33 +0100 Subject: [PATCH] Updated to B06 --- src/adap-b02/names/StringArrayName.ts | 2 +- src/adap-b02/names/StringName.ts | 2 +- src/adap-b03/names/StringArrayName.ts | 2 +- src/adap-b03/names/StringName.ts | 2 +- src/adap-b04/common/AssertionDispatcher.ts | 32 --------- src/adap-b04/common/Exception.ts | 16 +---- .../common/IllegalArgumentException.ts | 8 +-- src/adap-b04/common/InvalidStateException.ts | 8 +-- src/adap-b04/common/MethodFailedException.ts | 8 +-- .../coordinates/AbstractCoordinate.ts | 67 ++++--------------- src/adap-b04/coordinates/Coordinate.ts | 1 - src/adap-b04/files/Directory.ts | 8 ++- src/adap-b04/files/Node.ts | 6 +- src/adap-b04/names/StringArrayName.ts | 2 +- src/adap-b04/names/StringName.ts | 2 +- src/adap-b05/common/AssertionDispatcher.ts | 32 --------- src/adap-b05/common/Exception.ts | 18 +---- .../common/IllegalArgumentException.ts | 8 +-- src/adap-b05/common/InvalidStateException.ts | 8 +-- src/adap-b05/common/MethodFailedException.ts | 8 +-- .../common/ServiceFailureException.ts | 2 +- src/adap-b05/{errors1 => errors}/Errors.ts | 0 src/adap-b05/files/Directory.ts | 8 ++- src/adap-b05/files/Node.ts | 17 +---- src/adap-b05/files/RootNode.ts | 7 -- src/adap-b05/names/StringArrayName.ts | 2 +- src/adap-b05/names/StringName.ts | 2 +- src/adap-b06/common/Cloneable.ts | 4 +- src/adap-b06/common/Equality.ts | 3 +- src/adap-b06/common/Exception.ts | 19 ++++-- .../common/IllegalArgumentException.ts | 15 ++--- src/adap-b06/common/InvalidStateException.ts | 14 ++-- src/adap-b06/common/MethodFailedException.ts | 14 ++-- src/adap-b06/common/Printable.ts | 29 +++++--- .../common/ServiceFailureException.ts | 17 +++++ .../coordinates/AbstractCoordinate.ts | 58 ++++++---------- .../coordinates/CartesianCoordinate.ts | 12 +++- src/adap-b06/coordinates/Coordinate.ts | 3 +- src/adap-b06/coordinates/PolarCoordinate.ts | 12 +++- src/adap-b06/examples/Functions.ts | 3 + src/adap-b06/examples/PostalAddress.ts | 17 +++++ src/adap-b06/examples/QuantityUnit.ts | 55 +++++++++++++++ src/adap-b06/examples/Ranges.ts | 46 +++++++++++++ src/adap-b06/names/README.txt | 1 + test/adap-b04/common/Exceptions.test.ts | 12 ++-- test/adap-b05/files/Files.test.ts | 34 +++++----- test/adap-b06/coordinates/Coordinates.test.ts | 26 +++++++ 47 files changed, 339 insertions(+), 333 deletions(-) delete mode 100644 src/adap-b04/common/AssertionDispatcher.ts delete mode 100644 src/adap-b05/common/AssertionDispatcher.ts rename src/adap-b05/{errors1 => errors}/Errors.ts (100%) create mode 100644 src/adap-b06/common/ServiceFailureException.ts create mode 100644 src/adap-b06/examples/Functions.ts create mode 100644 src/adap-b06/examples/PostalAddress.ts create mode 100644 src/adap-b06/examples/QuantityUnit.ts create mode 100644 src/adap-b06/examples/Ranges.ts create mode 100644 src/adap-b06/names/README.txt create mode 100644 test/adap-b06/coordinates/Coordinates.test.ts diff --git a/src/adap-b02/names/StringArrayName.ts b/src/adap-b02/names/StringArrayName.ts index 3d216645..1f2be55d 100644 --- a/src/adap-b02/names/StringArrayName.ts +++ b/src/adap-b02/names/StringArrayName.ts @@ -6,7 +6,7 @@ export class StringArrayName implements Name { protected delimiter: string = DEFAULT_DELIMITER; protected components: string[] = []; - constructor(other: string[], delimiter?: string) { + constructor(source: string[], delimiter?: string) { throw new Error("needs implementation or deletion"); } diff --git a/src/adap-b02/names/StringName.ts b/src/adap-b02/names/StringName.ts index 1009a702..d38380ef 100644 --- a/src/adap-b02/names/StringName.ts +++ b/src/adap-b02/names/StringName.ts @@ -7,7 +7,7 @@ export class StringName implements Name { protected name: string = ""; protected noComponents: number = 0; - constructor(other: string, delimiter?: string) { + constructor(source: string, delimiter?: string) { throw new Error("needs implementation or deletion"); } diff --git a/src/adap-b03/names/StringArrayName.ts b/src/adap-b03/names/StringArrayName.ts index da67f37c..c15af367 100644 --- a/src/adap-b03/names/StringArrayName.ts +++ b/src/adap-b03/names/StringArrayName.ts @@ -6,7 +6,7 @@ export class StringArrayName extends AbstractName { protected components: string[] = []; - constructor(other: string[], delimiter?: string) { + constructor(source: string[], delimiter?: string) { super(); throw new Error("needs implementation or deletion"); } diff --git a/src/adap-b03/names/StringName.ts b/src/adap-b03/names/StringName.ts index 8049e4af..c323f5dc 100644 --- a/src/adap-b03/names/StringName.ts +++ b/src/adap-b03/names/StringName.ts @@ -7,7 +7,7 @@ export class StringName extends AbstractName { protected name: string = ""; protected noComponents: number = 0; - constructor(other: string, delimiter?: string) { + constructor(source: string, delimiter?: string) { super(); throw new Error("needs implementation or deletion"); } diff --git a/src/adap-b04/common/AssertionDispatcher.ts b/src/adap-b04/common/AssertionDispatcher.ts deleted file mode 100644 index bfe4fcda..00000000 --- a/src/adap-b04/common/AssertionDispatcher.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { IllegalArgumentException } from "./IllegalArgumentException"; -import { InvalidStateException } from "./InvalidStateException"; -import { MethodFailedException } from "./MethodFailedException"; - -export enum ExceptionType { - PRECONDITION = 0, - CLASS_INVARIANT = 1, - POSTCONDITION = 2 -}; - -export class AssertionDispatcher { - - protected static DISPATCHER = new AssertionDispatcher(); - - public static dispatch(key: ExceptionType, condition: boolean, message?: string) { - this.DISPATCHER.doDispatch(key, condition, message); - } - - protected methods: { [key in ExceptionType]: (c: boolean, m?: string) => void }; - - constructor() { - this.methods = { - [ExceptionType.PRECONDITION]: IllegalArgumentException.assertCondition, - [ExceptionType.CLASS_INVARIANT]: InvalidStateException.assertCondition, - [ExceptionType.POSTCONDITION]: MethodFailedException.assertCondition - } - } - - protected doDispatch(key: ExceptionType, condition: boolean, message?: string) { - this.methods[key](condition, message); - } -} \ No newline at end of file diff --git a/src/adap-b04/common/Exception.ts b/src/adap-b04/common/Exception.ts index 3e2cd15d..29f72dc2 100644 --- a/src/adap-b04/common/Exception.ts +++ b/src/adap-b04/common/Exception.ts @@ -7,10 +7,6 @@ export abstract class Exception extends Error { protected trigger: Exception | null = null; - static isNullOrUndefined(o: Object | null) { - return (o == undefined) || (o == null); - } - constructor(m: string, t?: Exception) { super(m); @@ -24,18 +20,8 @@ export abstract class Exception extends Error { } public getTrigger(): Exception { - this.assertHasTrigger(); // should be InvalidStateException + // @todo check if trigger is null return this.trigger as Exception; } - protected assertHasTrigger(): void { - if (!this.hasTrigger()) { - throw new (class extends Exception { - constructor(t: Exception) { - super("exception had no trigger", t); - } - })(this); - } - } - } \ No newline at end of file diff --git a/src/adap-b04/common/IllegalArgumentException.ts b/src/adap-b04/common/IllegalArgumentException.ts index bf7b2990..9179ce6a 100644 --- a/src/adap-b04/common/IllegalArgumentException.ts +++ b/src/adap-b04/common/IllegalArgumentException.ts @@ -7,11 +7,7 @@ import { InvalidStateException } from "./InvalidStateException"; */ export class IllegalArgumentException extends Exception { - static assertIsNotNullOrUndefined(o: Object | null, m: string = "null or undefined", t?: Exception): void { - this.assertCondition(!this.isNullOrUndefined(o), m); - } - - static assertCondition(c: boolean, m: string = "illegal argument", t?: Exception): void { + public static assert(c: boolean, m: string = "illegal argument", t?: Exception): void { if (!c) throw new IllegalArgumentException(m, t); } @@ -20,7 +16,7 @@ export class IllegalArgumentException extends Exception { } public getTrigger(): Exception { - InvalidStateException.assertCondition(this.hasTrigger()); + InvalidStateException.assert(this.hasTrigger()); return super.getTrigger(); } diff --git a/src/adap-b04/common/InvalidStateException.ts b/src/adap-b04/common/InvalidStateException.ts index 58dae00d..bfad4338 100644 --- a/src/adap-b04/common/InvalidStateException.ts +++ b/src/adap-b04/common/InvalidStateException.ts @@ -5,12 +5,8 @@ import { Exception } from "./Exception"; * In other words, a class invariant failed. */ export class InvalidStateException extends Exception { - - static assertIsNotNullOrUndefined(o: Object | null, m: string = "null or undefined", t?: Exception): void { - this.assertCondition(!this.isNullOrUndefined(o), m, t); - } - - static assertCondition(c: boolean, m: string = "invalid state", t?: Exception): void { + + public static assert(c: boolean, m: string = "invalid state", t?: Exception): void { if (!c) throw new InvalidStateException(m, t); } diff --git a/src/adap-b04/common/MethodFailedException.ts b/src/adap-b04/common/MethodFailedException.ts index e365a5f0..365a693c 100644 --- a/src/adap-b04/common/MethodFailedException.ts +++ b/src/adap-b04/common/MethodFailedException.ts @@ -5,12 +5,8 @@ import { Exception } from "./Exception"; * In other words, a postcondition failed. */ export class MethodFailedException extends Exception { - - static assertIsNotNullOrUndefined(o: Object | null, m: string = "null or undefined", t?: Exception): void { - this.assertCondition(!this.isNullOrUndefined(o), m, t); - } - - static assertCondition(c: boolean, m: string = "method failed", t?: Exception): void { + + public static assert(c: boolean, m: string = "method failed", t?: Exception): void { if (!c) throw new MethodFailedException(m, t); } diff --git a/src/adap-b04/coordinates/AbstractCoordinate.ts b/src/adap-b04/coordinates/AbstractCoordinate.ts index ecb4a56a..0532a4d9 100644 --- a/src/adap-b04/coordinates/AbstractCoordinate.ts +++ b/src/adap-b04/coordinates/AbstractCoordinate.ts @@ -1,7 +1,7 @@ -import { ExceptionType } from "../common/AssertionDispatcher"; -import { AssertionDispatcher } from "../common/AssertionDispatcher"; import { IllegalArgumentException } from "../common/IllegalArgumentException"; +import { InvalidStateException } from "../common/InvalidStateException"; import { MethodFailedException } from "../common/MethodFailedException"; + import { Coordinate } from "./Coordinate"; export abstract class AbstractCoordinate implements Coordinate { @@ -17,8 +17,6 @@ export abstract class AbstractCoordinate implements Coordinate { } public isEqual(other: Coordinate): boolean { - this.assertIsNotNullOrUndefinedAsPrecondition(other); - return (this.doGetX() == other.getX()) && (this.doGetY() == other.getY()); } @@ -42,8 +40,6 @@ export abstract class AbstractCoordinate implements Coordinate { protected abstract doGetX(): number; public setX(x: number): void { - this.assertIsNotNullOrUndefinedAsPrecondition(x); - this.doSetX(x); } @@ -56,16 +52,12 @@ export abstract class AbstractCoordinate implements Coordinate { protected abstract doGetY(): number; public setY(y: number): void { - this.assertIsNotNullOrUndefinedAsPrecondition(y); - this.doSetY(y); } protected abstract doSetY(y: number): void; public calcStraightLineDistance(other: Coordinate): number { - this.assertIsNotNullOrUndefinedAsPrecondition(other); - let deltaX: number = Math.abs(other.getX() - this.doGetX()); let deltaY: number = Math.abs(other.getY() - this.doGetY()); return Math.hypot(deltaX, deltaY); @@ -78,9 +70,7 @@ export abstract class AbstractCoordinate implements Coordinate { protected abstract doGetR(): number; public setR(r: number): void { - this.assertIsNotNullOrUndefinedAsPrecondition(r); - this.assertIsValidRAsPrecondition(r); - + IllegalArgumentException.assert(this.isValidR(r)); this.doSetR(r); } @@ -97,74 +87,41 @@ export abstract class AbstractCoordinate implements Coordinate { * @param phi Angle of vector */ public setPhi(phi: number): void { - this.assertIsNotNullOrUndefinedAsPrecondition(phi); - this.assertIsValidPhiAsPrecondition(phi); + IllegalArgumentException.assert(this.isValidPhi(phi)); this.doSetPhi(phi); const newPhi: number = this.doGetPhi(); - this.assertIsValidPhiAsClassInvariant(newPhi); + InvalidStateException.assert(this.isValidPhi(newPhi)); - MethodFailedException.assertCondition(newPhi == phi); + MethodFailedException.assert(newPhi == phi); } protected abstract doSetPhi(phi: number): void; public calcGreatCircleDistance(other: Coordinate): number { - this.assertIsNotNullOrUndefinedAsPrecondition(other); - let lowerR = Math.min(this.getR(), other.getR()); let deltaPhi = Math.abs(other.getPhi() - this.getPhi()); return lowerR * deltaPhi; } public multiplyWith(other: Coordinate): void { - this.assertIsNotNullOrUndefinedAsPrecondition(other); - let newR = this.getR() * other.getR(); let newPhi = this.getPhi() + other.getPhi(); this.setR(newR); this.setPhi(newPhi); } - protected assertIsNotNullOrUndefinedAsPrecondition(other: Object): void { - this.assertIsNotNullOrUndefined(other, ExceptionType.PRECONDITION); - } - - protected assertIsNotNullOrUndefined(other: Object, et: ExceptionType): void { - let condition: boolean = !IllegalArgumentException.isNullOrUndefined(other); - AssertionDispatcher.dispatch(et, condition, "null or undefined value"); - } - - protected assertIsValidRAsPrecondition(r: number): void { - this.assertIsValidR(r, ExceptionType.PRECONDITION); - } - - protected assertIsValidR(r: number, et: ExceptionType): void { - let condition: boolean = (r >= 0); - AssertionDispatcher.dispatch(et, condition, "invalid r value"); - } - - protected assertIsValidPhiAsPrecondition(phi: number): void { - this.assertIsValidPhi(phi, ExceptionType.PRECONDITION); - } - - protected assertIsValidPhiAsClassInvariant(phi: number): void { - this.assertIsValidPhi(phi, ExceptionType.CLASS_INVARIANT); - } - - protected assertIsValidPhi(phi: number, et: ExceptionType): void { - let condition: boolean = (phi < 0) || (phi >= 2*Math.PI); - AssertionDispatcher.dispatch(et, condition, "invalid phi value"); + protected isValidR(r: number): boolean { + return r >= 0; } - protected assertIsValidDelCharAsPrecondition(d: string): void { - this.assertIsValidDelChar(d, ExceptionType.PRECONDITION); + protected isValidPhi(phi: number): boolean { + return (phi >= 0) && (phi < 2*Math.PI); } - protected assertIsValidDelChar(d: string, et: ExceptionType): void { - let condition: boolean = (d.length == 1); - AssertionDispatcher.dispatch(et, condition, "invalid delimiter character"); + protected isValidDelChar(d: string): boolean { + return d.length == 1; } } \ No newline at end of file diff --git a/src/adap-b04/coordinates/Coordinate.ts b/src/adap-b04/coordinates/Coordinate.ts index c7eba49e..f97f52bc 100644 --- a/src/adap-b04/coordinates/Coordinate.ts +++ b/src/adap-b04/coordinates/Coordinate.ts @@ -1,6 +1,5 @@ import { Equality } from "../common/Equality"; import { Cloneable } from "../common/Cloneable"; -import { Printable } from "../common/Printable"; /** * A coordinate (here) is a point in a two-dimensional coordinate system. diff --git a/src/adap-b04/files/Directory.ts b/src/adap-b04/files/Directory.ts index 3ff937f7..4926be59 100644 --- a/src/adap-b04/files/Directory.ts +++ b/src/adap-b04/files/Directory.ts @@ -8,11 +8,15 @@ export class Directory extends Node { super(bn, pn); } - public add(cn: Node): void { + public hasChildNode(cn: Node): boolean { + return this.childNodes.has(cn); + } + + public addChildNode(cn: Node): void { this.childNodes.add(cn); } - public remove(cn: Node): void { + public removeChildNode(cn: Node): void { this.childNodes.delete(cn); // Yikes! Should have been called remove } diff --git a/src/adap-b04/files/Node.ts b/src/adap-b04/files/Node.ts index af26b451..d6ef9c6b 100644 --- a/src/adap-b04/files/Node.ts +++ b/src/adap-b04/files/Node.ts @@ -14,12 +14,12 @@ export class Node { protected initialize(pn: Directory): void { this.parentNode = pn; - this.parentNode.add(this); + this.parentNode.addChildNode(this); } public move(to: Directory): void { - this.parentNode.remove(this); - to.add(this); + this.parentNode.removeChildNode(this); + to.addChildNode(this); this.parentNode = to; } diff --git a/src/adap-b04/names/StringArrayName.ts b/src/adap-b04/names/StringArrayName.ts index da67f37c..c15af367 100644 --- a/src/adap-b04/names/StringArrayName.ts +++ b/src/adap-b04/names/StringArrayName.ts @@ -6,7 +6,7 @@ export class StringArrayName extends AbstractName { protected components: string[] = []; - constructor(other: string[], delimiter?: string) { + constructor(source: string[], delimiter?: string) { super(); throw new Error("needs implementation or deletion"); } diff --git a/src/adap-b04/names/StringName.ts b/src/adap-b04/names/StringName.ts index 8049e4af..c323f5dc 100644 --- a/src/adap-b04/names/StringName.ts +++ b/src/adap-b04/names/StringName.ts @@ -7,7 +7,7 @@ export class StringName extends AbstractName { protected name: string = ""; protected noComponents: number = 0; - constructor(other: string, delimiter?: string) { + constructor(source: string, delimiter?: string) { super(); throw new Error("needs implementation or deletion"); } diff --git a/src/adap-b05/common/AssertionDispatcher.ts b/src/adap-b05/common/AssertionDispatcher.ts deleted file mode 100644 index bfe4fcda..00000000 --- a/src/adap-b05/common/AssertionDispatcher.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { IllegalArgumentException } from "./IllegalArgumentException"; -import { InvalidStateException } from "./InvalidStateException"; -import { MethodFailedException } from "./MethodFailedException"; - -export enum ExceptionType { - PRECONDITION = 0, - CLASS_INVARIANT = 1, - POSTCONDITION = 2 -}; - -export class AssertionDispatcher { - - protected static DISPATCHER = new AssertionDispatcher(); - - public static dispatch(key: ExceptionType, condition: boolean, message?: string) { - this.DISPATCHER.doDispatch(key, condition, message); - } - - protected methods: { [key in ExceptionType]: (c: boolean, m?: string) => void }; - - constructor() { - this.methods = { - [ExceptionType.PRECONDITION]: IllegalArgumentException.assertCondition, - [ExceptionType.CLASS_INVARIANT]: InvalidStateException.assertCondition, - [ExceptionType.POSTCONDITION]: MethodFailedException.assertCondition - } - } - - protected doDispatch(key: ExceptionType, condition: boolean, message?: string) { - this.methods[key](condition, message); - } -} \ No newline at end of file diff --git a/src/adap-b05/common/Exception.ts b/src/adap-b05/common/Exception.ts index 3e2cd15d..07f9bae6 100644 --- a/src/adap-b05/common/Exception.ts +++ b/src/adap-b05/common/Exception.ts @@ -1,5 +1,3 @@ -import { InvalidStateException } from "./InvalidStateException"; - /** * Root class for exceptions in ADAP examples */ @@ -7,10 +5,6 @@ export abstract class Exception extends Error { protected trigger: Exception | null = null; - static isNullOrUndefined(o: Object | null) { - return (o == undefined) || (o == null); - } - constructor(m: string, t?: Exception) { super(m); @@ -24,18 +18,8 @@ export abstract class Exception extends Error { } public getTrigger(): Exception { - this.assertHasTrigger(); // should be InvalidStateException + // @todo check if trigger is null return this.trigger as Exception; } - protected assertHasTrigger(): void { - if (!this.hasTrigger()) { - throw new (class extends Exception { - constructor(t: Exception) { - super("exception had no trigger", t); - } - })(this); - } - } - } \ No newline at end of file diff --git a/src/adap-b05/common/IllegalArgumentException.ts b/src/adap-b05/common/IllegalArgumentException.ts index bf7b2990..9179ce6a 100644 --- a/src/adap-b05/common/IllegalArgumentException.ts +++ b/src/adap-b05/common/IllegalArgumentException.ts @@ -7,11 +7,7 @@ import { InvalidStateException } from "./InvalidStateException"; */ export class IllegalArgumentException extends Exception { - static assertIsNotNullOrUndefined(o: Object | null, m: string = "null or undefined", t?: Exception): void { - this.assertCondition(!this.isNullOrUndefined(o), m); - } - - static assertCondition(c: boolean, m: string = "illegal argument", t?: Exception): void { + public static assert(c: boolean, m: string = "illegal argument", t?: Exception): void { if (!c) throw new IllegalArgumentException(m, t); } @@ -20,7 +16,7 @@ export class IllegalArgumentException extends Exception { } public getTrigger(): Exception { - InvalidStateException.assertCondition(this.hasTrigger()); + InvalidStateException.assert(this.hasTrigger()); return super.getTrigger(); } diff --git a/src/adap-b05/common/InvalidStateException.ts b/src/adap-b05/common/InvalidStateException.ts index 58dae00d..bfad4338 100644 --- a/src/adap-b05/common/InvalidStateException.ts +++ b/src/adap-b05/common/InvalidStateException.ts @@ -5,12 +5,8 @@ import { Exception } from "./Exception"; * In other words, a class invariant failed. */ export class InvalidStateException extends Exception { - - static assertIsNotNullOrUndefined(o: Object | null, m: string = "null or undefined", t?: Exception): void { - this.assertCondition(!this.isNullOrUndefined(o), m, t); - } - - static assertCondition(c: boolean, m: string = "invalid state", t?: Exception): void { + + public static assert(c: boolean, m: string = "invalid state", t?: Exception): void { if (!c) throw new InvalidStateException(m, t); } diff --git a/src/adap-b05/common/MethodFailedException.ts b/src/adap-b05/common/MethodFailedException.ts index e365a5f0..365a693c 100644 --- a/src/adap-b05/common/MethodFailedException.ts +++ b/src/adap-b05/common/MethodFailedException.ts @@ -5,12 +5,8 @@ import { Exception } from "./Exception"; * In other words, a postcondition failed. */ export class MethodFailedException extends Exception { - - static assertIsNotNullOrUndefined(o: Object | null, m: string = "null or undefined", t?: Exception): void { - this.assertCondition(!this.isNullOrUndefined(o), m, t); - } - - static assertCondition(c: boolean, m: string = "method failed", t?: Exception): void { + + public static assert(c: boolean, m: string = "method failed", t?: Exception): void { if (!c) throw new MethodFailedException(m, t); } diff --git a/src/adap-b05/common/ServiceFailureException.ts b/src/adap-b05/common/ServiceFailureException.ts index cdda39dc..1ce5fdd7 100644 --- a/src/adap-b05/common/ServiceFailureException.ts +++ b/src/adap-b05/common/ServiceFailureException.ts @@ -6,7 +6,7 @@ import { Exception } from "./Exception"; */ export class ServiceFailureException extends Exception { - static assertCondition(c: boolean, m: string = "service failed", t?: Exception): void { + public static assert(c: boolean, m: string = "service failed", t?: Exception): void { if (!c) throw new ServiceFailureException(m, t); } diff --git a/src/adap-b05/errors1/Errors.ts b/src/adap-b05/errors/Errors.ts similarity index 100% rename from src/adap-b05/errors1/Errors.ts rename to src/adap-b05/errors/Errors.ts diff --git a/src/adap-b05/files/Directory.ts b/src/adap-b05/files/Directory.ts index 3ff937f7..4926be59 100644 --- a/src/adap-b05/files/Directory.ts +++ b/src/adap-b05/files/Directory.ts @@ -8,11 +8,15 @@ export class Directory extends Node { super(bn, pn); } - public add(cn: Node): void { + public hasChildNode(cn: Node): boolean { + return this.childNodes.has(cn); + } + + public addChildNode(cn: Node): void { this.childNodes.add(cn); } - public remove(cn: Node): void { + public removeChildNode(cn: Node): void { this.childNodes.delete(cn); // Yikes! Should have been called remove } diff --git a/src/adap-b05/files/Node.ts b/src/adap-b05/files/Node.ts index 02d32229..1f858a71 100644 --- a/src/adap-b05/files/Node.ts +++ b/src/adap-b05/files/Node.ts @@ -1,4 +1,3 @@ -import { ExceptionType, AssertionDispatcher } from "../common/AssertionDispatcher"; import { IllegalArgumentException } from "../common/IllegalArgumentException"; import { InvalidStateException } from "../common/InvalidStateException"; @@ -18,12 +17,12 @@ export class Node { protected initialize(pn: Directory): void { this.parentNode = pn; - this.parentNode.add(this); + this.parentNode.addChildNode(this); } public move(to: Directory): void { - this.parentNode.remove(this); - to.add(this); + this.parentNode.removeChildNode(this); + to.addChildNode(this); this.parentNode = to; } @@ -61,14 +60,4 @@ export class Node { throw new Error("needs implementation or deletion"); } - protected assertClassInvariants(): void { - const bn: string = this.doGetBaseName(); - this.assertIsValidBaseName(bn, ExceptionType.CLASS_INVARIANT); - } - - protected assertIsValidBaseName(bn: string, et: ExceptionType): void { - const condition: boolean = (bn != ""); - AssertionDispatcher.dispatch(et, condition, "invalid base name"); - } - } diff --git a/src/adap-b05/files/RootNode.ts b/src/adap-b05/files/RootNode.ts index f671dbc3..996b1b69 100644 --- a/src/adap-b05/files/RootNode.ts +++ b/src/adap-b05/files/RootNode.ts @@ -1,5 +1,3 @@ -import { ExceptionType, AssertionDispatcher } from "../common/AssertionDispatcher"; - import { Name } from "../names/Name"; import { StringName } from "../names/StringName"; import { Directory } from "./Directory"; @@ -32,9 +30,4 @@ export class RootNode extends Directory { // null operation } - protected assertIsValidBaseName(bn: string, et: ExceptionType): void { - const condition: boolean = (bn == ""); // Root must have "" as base name - AssertionDispatcher.dispatch(et, condition, "invalid base name"); - } - } \ No newline at end of file diff --git a/src/adap-b05/names/StringArrayName.ts b/src/adap-b05/names/StringArrayName.ts index da67f37c..c15af367 100644 --- a/src/adap-b05/names/StringArrayName.ts +++ b/src/adap-b05/names/StringArrayName.ts @@ -6,7 +6,7 @@ export class StringArrayName extends AbstractName { protected components: string[] = []; - constructor(other: string[], delimiter?: string) { + constructor(source: string[], delimiter?: string) { super(); throw new Error("needs implementation or deletion"); } diff --git a/src/adap-b05/names/StringName.ts b/src/adap-b05/names/StringName.ts index 8049e4af..c323f5dc 100644 --- a/src/adap-b05/names/StringName.ts +++ b/src/adap-b05/names/StringName.ts @@ -7,7 +7,7 @@ export class StringName extends AbstractName { protected name: string = ""; protected noComponents: number = 0; - constructor(other: string, delimiter?: string) { + constructor(source: string, delimiter?: string) { super(); throw new Error("needs implementation or deletion"); } diff --git a/src/adap-b06/common/Cloneable.ts b/src/adap-b06/common/Cloneable.ts index c34600e1..768202f3 100644 --- a/src/adap-b06/common/Cloneable.ts +++ b/src/adap-b06/common/Cloneable.ts @@ -3,6 +3,6 @@ export interface Cloneable { /** * Returns shallow copy (clone) of this object */ - clone(): Object; // @todo Use object - + clone(): Object; // @todo Clarify use of Objects vs object + } \ No newline at end of file diff --git a/src/adap-b06/common/Equality.ts b/src/adap-b06/common/Equality.ts index 5428e269..87e986bd 100644 --- a/src/adap-b06/common/Equality.ts +++ b/src/adap-b06/common/Equality.ts @@ -4,10 +4,11 @@ export interface Equality { * Returns true if other object is of equal value to this one * @param other Object to compare with */ - isEqual(other: Object): boolean; // @todo How to ensure this works with vitest and Object.is? + isEqual(other: Object): boolean; /** * Returns hashcode for this object, respecting equality contract */ getHashCode(): number; + } diff --git a/src/adap-b06/common/Exception.ts b/src/adap-b06/common/Exception.ts index 93cf6d7c..07f9bae6 100644 --- a/src/adap-b06/common/Exception.ts +++ b/src/adap-b06/common/Exception.ts @@ -3,12 +3,23 @@ */ export abstract class Exception extends Error { - static isNullOrUndefined(o: Object | null) { - return (o == undefined) || (o == null); - } + protected trigger: Exception | null = null; - constructor(m: string) { + constructor(m: string, t?: Exception) { super(m); + + if (t != undefined) { + this.trigger = t; + } + } + + public hasTrigger(): boolean { + return this.trigger != null; + } + + public getTrigger(): Exception { + // @todo check if trigger is null + return this.trigger as Exception; } } \ No newline at end of file diff --git a/src/adap-b06/common/IllegalArgumentException.ts b/src/adap-b06/common/IllegalArgumentException.ts index a347b864..e9a8881c 100644 --- a/src/adap-b06/common/IllegalArgumentException.ts +++ b/src/adap-b06/common/IllegalArgumentException.ts @@ -1,4 +1,5 @@ import { Exception } from "./Exception"; +import { InvalidStateException } from "./InvalidStateException"; /** * An IllegalArgumentException signals an invalid argument. @@ -6,16 +7,12 @@ import { Exception } from "./Exception"; */ export class IllegalArgumentException extends Exception { - static assertIsNotNullOrUndefined(o: Object | null, exMsg: string = "null or undefined"): void { - this.assertCondition(this.isNullOrUndefined(o), exMsg); + public static assert(c: boolean, m: string = "illegal argument", t?: Exception): void { + if (!c) throw new IllegalArgumentException(m, t); } - static assertCondition(cond: boolean, exMsg: string): void { - if (!cond) throw new IllegalArgumentException(exMsg); + constructor(m: string, t?: Exception) { + super(m, t); } - - constructor(m: string) { - super(m); - } - + } diff --git a/src/adap-b06/common/InvalidStateException.ts b/src/adap-b06/common/InvalidStateException.ts index e4139ce7..bfad4338 100644 --- a/src/adap-b06/common/InvalidStateException.ts +++ b/src/adap-b06/common/InvalidStateException.ts @@ -5,17 +5,13 @@ import { Exception } from "./Exception"; * In other words, a class invariant failed. */ export class InvalidStateException extends Exception { - - static assertNotNullOrUndefined(o: Object | null, exMsg: string = "null or undefined"): void { - this.assertCondition(this.isNullOrUndefined(o), exMsg); - } - - static assertCondition(cond: boolean, exMsg: string): void { - if (!cond) throw new InvalidStateException(exMsg); + + public static assert(c: boolean, m: string = "invalid state", t?: Exception): void { + if (!c) throw new InvalidStateException(m, t); } - constructor(m: string) { - super(m); + constructor(m: string, t?: Exception) { + super(m, t); } } diff --git a/src/adap-b06/common/MethodFailedException.ts b/src/adap-b06/common/MethodFailedException.ts index 2e4228e4..365a693c 100644 --- a/src/adap-b06/common/MethodFailedException.ts +++ b/src/adap-b06/common/MethodFailedException.ts @@ -5,17 +5,13 @@ import { Exception } from "./Exception"; * In other words, a postcondition failed. */ export class MethodFailedException extends Exception { - - static assertNotNullOrUndefined(o: Object | null, exMsg: string = "null or undefined"): void { - this.assertCondition(this.isNullOrUndefined(o), exMsg); - } - - static assertCondition(cond: boolean, exMsg: string): void { - if (!cond) throw new MethodFailedException(exMsg); + + public static assert(c: boolean, m: string = "method failed", t?: Exception): void { + if (!c) throw new MethodFailedException(m, t); } - constructor(m: string) { - super(m); + constructor(m: string, t?: Exception) { + super(m, t); } } diff --git a/src/adap-b06/common/Printable.ts b/src/adap-b06/common/Printable.ts index 4dd378c8..604c07b0 100644 --- a/src/adap-b06/common/Printable.ts +++ b/src/adap-b06/common/Printable.ts @@ -1,14 +1,27 @@ +export const DEFAULT_DELIMITER: string = '.'; +export const ESCAPE_CHARACTER = '\\'; + export interface Printable { - /** - * Returns human-readable representation of this object - * Expects that delimiter is a single character + /** + * Returns a human-readable representation of the Name instance using user-set control characters + * Control characters are not escaped (creating a human-readable string) + * Users can vary the delimiter character to be used + * The delimiter character must be a single character */ - asString(delChar?: string): string; + asString(delimiter?: string): string; - /** - * Returns machine-readable representation of this object - */ - asDataString(): string; + /** + * Returns a machine-readable representation of Name instance using default control characters + * Machine-readable means that from a data string, a Name can be parsed back in + * The control characters in the data string are the default characters + * Different fields of the object are separated by the delimiter character + */ + asDataString(): string; + + /** + * Returns delimiter char; must be a single character + */ + getDelimiterCharacter(): string; } \ No newline at end of file diff --git a/src/adap-b06/common/ServiceFailureException.ts b/src/adap-b06/common/ServiceFailureException.ts new file mode 100644 index 00000000..1ce5fdd7 --- /dev/null +++ b/src/adap-b06/common/ServiceFailureException.ts @@ -0,0 +1,17 @@ +import { Exception } from "./Exception"; + +/** + * A ServiceFailureException signals that a service failed to provide its service. + * ServiceFailureExceptions must be checked for by the client after the service call. + */ +export class ServiceFailureException extends Exception { + + public static assert(c: boolean, m: string = "service failed", t?: Exception): void { + if (!c) throw new ServiceFailureException(m, t); + } + + constructor(m: string, t?: Exception) { + super(m, t); + } + +} diff --git a/src/adap-b06/coordinates/AbstractCoordinate.ts b/src/adap-b06/coordinates/AbstractCoordinate.ts index d8ac9845..3478d639 100644 --- a/src/adap-b06/coordinates/AbstractCoordinate.ts +++ b/src/adap-b06/coordinates/AbstractCoordinate.ts @@ -1,12 +1,17 @@ import { IllegalArgumentException } from "../common/IllegalArgumentException"; + import { Coordinate } from "./Coordinate"; -import { PolarCoordinate } from "./PolarCoordinate"; export abstract class AbstractCoordinate implements Coordinate { - public asString(delChar: string) { - this.assertIsValidDelChar(delChar); + public clone(): Coordinate { + return this.doCreate(this.doGetX(), this.doGetY()); + } + + protected abstract doCreate(x: number, y: number): Coordinate; + public asString(delChar: string) { + IllegalArgumentException.assert(this.isValidDelChar(delChar)); return this.doGetX() + delChar + this.doGetY(); } @@ -14,31 +19,23 @@ export abstract class AbstractCoordinate implements Coordinate { return this.asDataString(); } - public asDataString(): string { - return this.asString("#"); - } + public abstract asDataString(): string; public isEqual(other: Coordinate): boolean { - this.assertIsNotNullOrUndefined(other); - return (this.doGetX() == other.getX()) && (this.doGetY() == other.getY()); } public getHashCode(): number { let hashCode: number = 0; const s: string = this.asDataString(); - for (let i = 0; i < s.length; i++) { - let c = s.charCodeAt(i); + for (let i: number = 0; i < s.length; i++) { + let c: number = s.charCodeAt(i); hashCode = (hashCode << 5) - hashCode + c; hashCode |= 0; } return hashCode; } - public clone(): Coordinate { - return { ...this }; - } - abstract getOrigin(): Coordinate; public getX(): number { @@ -48,8 +45,6 @@ export abstract class AbstractCoordinate implements Coordinate { protected abstract doGetX(): number; public setX(x: number): Coordinate { - this.assertIsNotNullOrUndefined(x); - return this.doSetX(x); } @@ -62,16 +57,12 @@ export abstract class AbstractCoordinate implements Coordinate { protected abstract doGetY(): number; public setY(y: number): Coordinate { - this.assertIsNotNullOrUndefined(y); - return this.doSetY(y); } protected abstract doSetY(y: number): Coordinate; public calcStraightLineDistance(other: Coordinate): number { - this.assertIsNotNullOrUndefined(other); - let deltaX: number = Math.abs(other.getX() - this.doGetX()); let deltaY: number = Math.abs(other.getY() - this.doGetY()); return Math.hypot(deltaX, deltaY); @@ -84,8 +75,6 @@ export abstract class AbstractCoordinate implements Coordinate { protected abstract doGetR(): number; public setR(r: number): Coordinate { - this.assertIsNotNullOrUndefined(r); - return this.doSetR(r); } @@ -98,43 +87,34 @@ export abstract class AbstractCoordinate implements Coordinate { protected abstract doGetPhi(): number; public setPhi(phi: number): Coordinate { - this.assertIsNotNullOrUndefined(phi); - this.assertIsValidPhi(phi); - + IllegalArgumentException.assert(this.isValidPhi(phi)); return this.doSetPhi(phi); } protected abstract doSetPhi(phi: number): Coordinate; public calcGreatCircleDistance(other: Coordinate): number { - this.assertIsNotNullOrUndefined(other); - let lowerR = Math.min(this.getR(), other.getR()); let deltaPhi = Math.abs(other.getPhi() - this.getPhi()); return lowerR * deltaPhi; } public multiplyWith(other: Coordinate): Coordinate { - this.assertIsNotNullOrUndefined(other); - let newR = this.getR() * other.getR(); let newPhi = this.getPhi() + other.getPhi(); - return new PolarCoordinate(newR, newPhi); + return this.doCreate(newR, newPhi); } - protected assertIsNotNullOrUndefined(other: Object): void { - let condition: boolean = !IllegalArgumentException.isNullOrUndefined(other); - IllegalArgumentException.assertCondition(condition, "null or undefined argument"); + protected isValidR(r: number): boolean { + return r >= 0; } - protected assertIsValidPhi(phi: number): void { - let condition: boolean = (phi < 0) || (phi >= 2*Math.PI); - IllegalArgumentException.assertCondition(condition, "invalid phi value"); + protected isValidPhi(phi: number): boolean { + return (phi >= 0) && (phi < 2*Math.PI); } - protected assertIsValidDelChar(d: string) { - let condition: boolean = (d.length == 1); - IllegalArgumentException.assertCondition(condition, "invalid delimiter character"); + protected isValidDelChar(d: string): boolean { + return d.length == 1; } } \ No newline at end of file diff --git a/src/adap-b06/coordinates/CartesianCoordinate.ts b/src/adap-b06/coordinates/CartesianCoordinate.ts index fc961ebd..6acd2c54 100644 --- a/src/adap-b06/coordinates/CartesianCoordinate.ts +++ b/src/adap-b06/coordinates/CartesianCoordinate.ts @@ -14,14 +14,22 @@ export class CartesianCoordinate extends AbstractCoordinate { protected initialize(x?: number, y?: number): void { if (x != undefined) { - this.doSetX(x); + this.x = x; } if (y != undefined) { - this.doSetY(y); + this.y = y; } } + protected doCreate(x: number, y: number): Coordinate { + return new CartesianCoordinate(x, y); + } + + public asDataString(): string { + return this.doGetX() + '#' + this.doGetY(); + } + public getOrigin(): Coordinate { return new CartesianCoordinate(0, 0); } diff --git a/src/adap-b06/coordinates/Coordinate.ts b/src/adap-b06/coordinates/Coordinate.ts index df980094..86ed5bce 100644 --- a/src/adap-b06/coordinates/Coordinate.ts +++ b/src/adap-b06/coordinates/Coordinate.ts @@ -1,12 +1,11 @@ import { Equality } from "../common/Equality"; import { Cloneable } from "../common/Cloneable"; -import { Printable } from "../common/Printable"; /** * A coordinate (here) is a point in a two-dimensional coordinate system. * The coordinate system may be cartesian or polar; coordinates should be interchangeable. */ -export interface Coordinate extends Equality, Cloneable, Printable { +export interface Coordinate extends Cloneable, Equality { /** * Returns the origin of the coordinate system diff --git a/src/adap-b06/coordinates/PolarCoordinate.ts b/src/adap-b06/coordinates/PolarCoordinate.ts index abddbb1c..ad5f5add 100644 --- a/src/adap-b06/coordinates/PolarCoordinate.ts +++ b/src/adap-b06/coordinates/PolarCoordinate.ts @@ -14,14 +14,22 @@ export class PolarCoordinate extends AbstractCoordinate { protected initialize(r?: number, phi?: number): void { if (r != undefined) { - this.setR(r); + this.r = r; } if (phi != undefined) { - this.setPhi(phi); + this.phi = phi; } } + protected doCreate(r: number, phi: number): Coordinate { + return new PolarCoordinate(r, phi); + } + + public asDataString(): string { + return this.doGetR() + '#' + this.doGetPhi(); + } + public getOrigin(): Coordinate { return new PolarCoordinate(0, 0); } diff --git a/src/adap-b06/examples/Functions.ts b/src/adap-b06/examples/Functions.ts new file mode 100644 index 00000000..7be8c65e --- /dev/null +++ b/src/adap-b06/examples/Functions.ts @@ -0,0 +1,3 @@ +export function calculateDuration1(distance: number, speed: number): number { + return distance / speed; +} \ No newline at end of file diff --git a/src/adap-b06/examples/PostalAddress.ts b/src/adap-b06/examples/PostalAddress.ts new file mode 100644 index 00000000..0a4200a4 --- /dev/null +++ b/src/adap-b06/examples/PostalAddress.ts @@ -0,0 +1,17 @@ +export class PostalAddress { + + protected street: string; + protected city: string; + protected state: string; + protected postalCode: string; + protected country: string; + + constructor(st: string, city: string, state: string, pc: string, country: string) { + this.street = st; + this.city = city; + this.state = state; + this.postalCode = pc; + this.country = country; + } + +} \ No newline at end of file diff --git a/src/adap-b06/examples/QuantityUnit.ts b/src/adap-b06/examples/QuantityUnit.ts new file mode 100644 index 00000000..27c02279 --- /dev/null +++ b/src/adap-b06/examples/QuantityUnit.ts @@ -0,0 +1,55 @@ +import { IllegalArgumentException } from "../common/IllegalArgumentException"; + +export class QuantityUnit { + + protected value: number; + protected unit: SIUnit; + + constructor(value: number, unit: SIUnit) { + this.value = value; + this.unit = unit; + } + +} + +export enum BaseUnit { + m = 0, + kg = 1, + s = 2, + A = 3, + K = 4, + cd = 5, + mol = 6 +} + +export class SIUnit { + + protected exponents: number[] = [0, 0, 0, 0, 0, 0, 0]; + + constructor(exponents: number[]) { + this.exponents = [...exponents]; + } + + public isEqual(other: SIUnit): boolean { + return this.exponents.every((v, i) => v === other.exponents[i]); + } + + public add(other: SIUnit): SIUnit { + IllegalArgumentException.assert(!other.isEqual(this)); + return new SIUnit(this.exponents); + } + + public multiply(other: SIUnit): SIUnit { + let result: number[] = [0, 0, 0, 0, 0, 0, 0]; + for (let i = 0; i < this.exponents.length; i++) { + result[1] = this.exponents[i] + other.exponents[i]; + }; + return new SIUnit(result); + } + +} + +export function calculateDuration2(distance: number, speed: number): QuantityUnit { + IllegalArgumentException.assert(speed != 0); + return new QuantityUnit(distance / speed, new SIUnit([0, 0, 1, 0, 0, 0, 0])); +} diff --git a/src/adap-b06/examples/Ranges.ts b/src/adap-b06/examples/Ranges.ts new file mode 100644 index 00000000..7ef259fa --- /dev/null +++ b/src/adap-b06/examples/Ranges.ts @@ -0,0 +1,46 @@ +export class RangeBound { + + protected value: T; + protected inclusive: boolean; + + constructor(value: T, inclusive: boolean) { + this.value = value; + this.inclusive = inclusive; + } + + public getValue(): T { + return this.value; + } + + public isInclusive(): boolean { + return this.inclusive; + } + +} + +export class Range { + + protected lowerBound: RangeBound; + protected upperBound: RangeBound; + + constructor(lowerBound: RangeBound, upperBound: RangeBound) { + this.lowerBound = lowerBound; + this.upperBound = upperBound; + } + + public includes(value: T): boolean { + let lowerValue = this.lowerBound.getValue(); + let upperValue = this.upperBound.getValue(); + return true; // @todo + } + + + public getLowerBound(): RangeBound { + return this.lowerBound; + } + + public getUpperBound(): RangeBound { + return this.upperBound; + } + +} diff --git a/src/adap-b06/names/README.txt b/src/adap-b06/names/README.txt new file mode 100644 index 00000000..316d0d62 --- /dev/null +++ b/src/adap-b06/names/README.txt @@ -0,0 +1 @@ +Your homework goes into this folder \ No newline at end of file diff --git a/test/adap-b04/common/Exceptions.test.ts b/test/adap-b04/common/Exceptions.test.ts index b7d3a26b..c9c2681c 100644 --- a/test/adap-b04/common/Exceptions.test.ts +++ b/test/adap-b04/common/Exceptions.test.ts @@ -8,13 +8,13 @@ describe("Asserting not null or undefined", () => { it("test asserIsNotNullOrUndefined", async () => { const m: string = "null or undefined"; - IllegalArgumentException.assertIsNotNullOrUndefined("hurray!"); - expect(() => IllegalArgumentException.assertIsNotNullOrUndefined(null)).toThrow(new IllegalArgumentException(m)); + IllegalArgumentException.assert("hurray!" != null); + expect(() => IllegalArgumentException.assert(false, m)).toThrow(new IllegalArgumentException(m)); - MethodFailedException.assertIsNotNullOrUndefined("hurray!"); - expect(() => MethodFailedException.assertIsNotNullOrUndefined(null)).toThrow(new MethodFailedException(m)); + MethodFailedException.assert("hurray!" != null); + expect(() => MethodFailedException.assert(false, m)).toThrow(new MethodFailedException(m)); - InvalidStateException.assertIsNotNullOrUndefined("hurray!"); - expect(() => InvalidStateException.assertIsNotNullOrUndefined(null)).toThrow(new InvalidStateException(m)); + InvalidStateException.assert("hurray!" != null); + expect(() => InvalidStateException.assert(false, m)).toThrow(new InvalidStateException(m)); }); }); diff --git a/test/adap-b05/files/Files.test.ts b/test/adap-b05/files/Files.test.ts index 28231336..5292f057 100644 --- a/test/adap-b05/files/Files.test.ts +++ b/test/adap-b05/files/Files.test.ts @@ -1,5 +1,9 @@ import { describe, it, expect } from "vitest"; +import { Exception } from "../../../src/adap-b05/common/Exception"; +import { InvalidStateException } from "../../../src/adap-b05/common/InvalidStateException"; +import { ServiceFailureException } from "../../../src/adap-b05/common/ServiceFailureException"; + import { StringName } from "../../../src/adap-b05/names/StringName"; import { Node } from "../../../src/adap-b05/files/Node"; @@ -29,9 +33,9 @@ function createFileSystem(): RootNode { describe("Basic naming test", () => { it("test name checking", () => { - // let fs: RootNode = createFileSystem(); + let fs: RootNode = createFileSystem(); // let ls: Node = [...fs.findNodes("ls")][0]; - // expect(ls.getFullName().isEqual(new StringName("/usr/bin/ls", '/'))); + // expect(ls.getFullName().asString()).toBe(new StringName("/usr/bin/ls", '/')); }); }); @@ -56,18 +60,18 @@ function createBuggySetup(): RootNode { describe("Buggy setup test", () => { it("test finding files", () => { - // let threwException: boolean = false; - // try { - // let fs: RootNode = createBuggySetup(); - // fs.findNodes("ls"); - // } catch(er) { - // threwException = true; - // let ex: Exception = er as Exception; - // expect(ex instanceof ServiceFailureException); - // expect(ex.hasTrigger()); - // let tx: Exception = ex.getTrigger(); - // expect(tx instanceof InvalidStateException); - // } - // expect(threwException); + let threwException: boolean = false; + try { + let fs: RootNode = createBuggySetup(); + fs.findNodes("ls"); + } catch(er) { + threwException = true; + // let ex: Exception = er as Exception; + // expect(ex).toBeInstanceOf(ServiceFailureException); + // expect(ex.hasTrigger()).toBe(true); + // let tx: Exception = ex.getTrigger(); + // expect(tx).toBeInstanceOf(InvalidStateException); + } + expect(threwException).toBe(true); }); }); diff --git a/test/adap-b06/coordinates/Coordinates.test.ts b/test/adap-b06/coordinates/Coordinates.test.ts new file mode 100644 index 00000000..c57a1b96 --- /dev/null +++ b/test/adap-b06/coordinates/Coordinates.test.ts @@ -0,0 +1,26 @@ +import { describe, it, expect } from "vitest"; + +import { Coordinate } from "../../../src/adap-b06/coordinates/Coordinate"; +import { CartesianCoordinate } from "../../../src/adap-b06/coordinates/CartesianCoordinate"; +import { PolarCoordinate } from "../../../src/adap-b06/coordinates/PolarCoordinate"; + +describe("Equality test", () => { + it("test isEqual", () => { + let c1: Coordinate = new CartesianCoordinate(0, 0); + let p1: Coordinate = new PolarCoordinate(0, 0); + expect(c1.isEqual(p1)).toBe(true); + expect(c1.getHashCode() == p1.getHashCode()).toBe(true); + + c1 = c1.setX(2); + p1 = p1.setR(2); + expect(c1.isEqual(p1)).toBe(true); + expect(c1.getHashCode() == p1.getHashCode()).toBe(true); + + c1 = c1.setY(1); + p1 = p1.setPhi(Math.PI / 2); + expect(c1.isEqual(p1)).toBe(false); + expect(c1.getHashCode() == p1.getHashCode()).toBe(false); + }); +}); + +