Skip to content

Commit

Permalink
Added B02 homework
Browse files Browse the repository at this point in the history
  • Loading branch information
friedalexuni committed Oct 28, 2024
1 parent 7d02659 commit cf711ae
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 6 deletions.
35 changes: 33 additions & 2 deletions src/adap-b02/names/Name.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
export const DEFAULT_DELIMITER: string = '.';
export const ESCAPE_CHARACTER = '\\';

/**
* A name is a sequence of string components separated by a delimiter character.
* Special characters within the string may need masking, if they are to appear verbatim.
* There are only two special characters, the delimiter character and the escape character.
* The escape character can't be set, the delimiter character can.
*
* Homogenous name examples
*
* "oss.cs.fau.de" is a name with four name components and the delimiter character '.'.
* "///" is a name with four empty components and the delimiter character '/'.
* "Oh\.\.\." is a name with one component, if the delimiter character is '.'.
*/
export interface Name {

/** Returns human-readable representation of Name instance */
asNameString(delimiter?: string): string;
/**
* 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
*/
asString(delimiter?: string): 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
*/
asDataString(): string;

isEmpty(): boolean;

getDelimiterCharacter(): string;

/** Returns number of components in Name instance */
getNoComponents(): number;

getComponent(i: number): string;

/** Assumes that new Name component c is properly masked */
setComponent(i: number, c: string): void;

/** Assumes that new Name component c is properly masked */
insert(i: number, c: string): void;

/** Assumes that new Name component c is properly masked */
append(c: string): void;

remove(i: number): void;

}
10 changes: 9 additions & 1 deletion src/adap-b02/names/StringArrayName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ export class StringArrayName implements Name {
throw new Error("needs implementation");
}

public asNameString(delimiter: string = this.delimiter): string {
public asString(delimiter: string = this.delimiter): string {
throw new Error("needs implementation");
}

public asDataString(): string {
throw new Error("needs implementation");
}

public isEmpty(): boolean {
throw new Error("needs implementation");
}

public getDelimiterCharacter(): string {
throw new Error("needs implementation");
}

public getNoComponents(): number {
throw new Error("needs implementation");
}
Expand Down
12 changes: 10 additions & 2 deletions src/adap-b02/names/StringName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ export class StringName implements Name {
protected delimiter: string = DEFAULT_DELIMITER;

protected name: string = "";
protected length: number = 0; // Number of components in Name instance
protected length: number = 0;

constructor(other: string, delimiter?: string) {
throw new Error("needs implementation");
}

public asNameString(delimiter: string = this.delimiter): string {
public asString(delimiter: string = this.delimiter): string {
throw new Error("needs implementation");
}

public asDataString(): string {
throw new Error("needs implementation");
}

public isEmpty(): boolean {
throw new Error("needs implementation");
}

public getDelimiterCharacter(): string {
throw new Error("needs implementation");
}

public getNoComponents(): number {
throw new Error("needs implementation");
}
Expand Down
59 changes: 59 additions & 0 deletions test/adap-b02/names/Name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, it, expect } from "vitest";

import { Name } from "../../../src/adap-b02/names/Name";
import { StringName } from "../../../src/adap-b02/names/StringName";
import { StringArrayName } from "../../../src/adap-b02/names/StringArrayName";

describe("Basic StringName function tests", () => {
it("test insert", () => {
let n: Name = new StringName("oss.fau.de");
n.insert(1, "cs");
expect(n.asString()).toBe("oss.cs.fau.de");
});
it("test append", () => {
let n: Name = new StringName("oss.cs.fau");
n.append("de");
expect(n.asString()).toBe("oss.cs.fau.de");
});
it("test remove", () => {
let n: Name = new StringName("oss.cs.fau.de");
n.remove(0);
expect(n.asString()).toBe("cs.fau.de");
});
});

describe("Basic StringArrayName function tests", () => {
it("test insert", () => {
let n: Name = new StringArrayName(["oss", "fau", "de"]);
n.insert(1, "cs");
expect(n.asString()).toBe("oss.cs.fau.de");
});
it("test append", () => {
let n: Name = new StringArrayName(["oss", "cs", "fau"]);
n.append("de");
expect(n.asString()).toBe("oss.cs.fau.de");
});
it("test remove", () => {
let n: Name = new StringArrayName(["oss", "cs", "fau", "de"]);
n.remove(0);
expect(n.asString()).toBe("cs.fau.de");
});
});

describe("Delimiter function tests", () => {
it("test insert", () => {
let n: Name = new StringName("oss#fau#de", '#');
n.insert(1, "cs");
expect(n.asString()).toBe("oss#cs#fau#de");
});
});

describe("Escape character extravaganza", () => {
it("test escape and delimiter boundary conditions", () => {
let n: Name = new StringName("oss.cs.fau.de", '#');
expect(n.getNoComponents()).toBe(1);
expect(n.asString()).toBe("oss.cs.fau.de");
n.append("people");
expect(n.asString()).toBe("oss.cs.fau.de#people");
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "ES2023", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
Expand Down

0 comments on commit cf711ae

Please sign in to comment.