Skip to content

Commit

Permalink
2023-09-05T13:50:42.263Z
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Sep 5, 2023
1 parent fae1434 commit d9c1fca
Show file tree
Hide file tree
Showing 251 changed files with 135 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/030-classes/110-setters.problem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { expect, it } from "vitest";

class CanvasNode {
#x;
#y;

constructor(position?: { x: number; y: number }) {
this.#x = position?.x ?? 0;
this.#y = position?.y ?? 0;
}

get position() {
return {
x: this.#x,
y: this.#y,
};
}

move(x: number, y: number) {
this.#x = x;
this.#y = y;
}
}

it("Should be able to move", () => {
const canvasNode = new CanvasNode();

expect(canvasNode.position).toEqual({ x: 0, y: 0 });

canvasNode.move(10, 20);

expect(canvasNode.position).toEqual({ x: 10, y: 20 });
});

it("Should be able to receive an initial position", () => {
const canvasNode = new CanvasNode({
x: 10,
y: 20,
});

expect(canvasNode.position).toEqual({ x: 10, y: 20 });
});

it("Should not be able to access x and y from the outside", () => {
const canvasNode = new CanvasNode();

expect(
// @ts-expect-error
canvasNode.#x,
).toEqual(undefined);
expect(
// @ts-expect-error
canvasNode.#y,
).toEqual(undefined);
});

it("Should let you set the position", () => {
const canvasNode = new CanvasNode();

expect(canvasNode.position).toEqual({ x: 0, y: 0 });

canvasNode.position = { x: 10, y: 20 };

expect(canvasNode.position).toEqual({ x: 10, y: 20 });
});
70 changes: 70 additions & 0 deletions src/030-classes/110-setters.solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { expect, it } from "vitest";

class CanvasNode {
#x;
#y;

constructor(position?: { x: number; y: number }) {
this.#x = position?.x ?? 0;
this.#y = position?.y ?? 0;
}

get position() {
return {
x: this.#x,
y: this.#y,
};
}

set position(pos) {
this.#x = pos.x;
this.#y = pos.y;
}

move(x: number, y: number) {
this.#x = x;
this.#y = y;
}
}

it("Should be able to move", () => {
const canvasNode = new CanvasNode();

expect(canvasNode.position).toEqual({ x: 0, y: 0 });

canvasNode.move(10, 20);

expect(canvasNode.position).toEqual({ x: 10, y: 20 });
});

it("Should be able to receive an initial position", () => {
const canvasNode = new CanvasNode({
x: 10,
y: 20,
});

expect(canvasNode.position).toEqual({ x: 10, y: 20 });
});

it("Should not be able to access x and y from the outside", () => {
const canvasNode = new CanvasNode();

expect(
// @ts-expect-error
canvasNode.#x,
).toEqual(undefined);
expect(
// @ts-expect-error
canvasNode.#y,
).toEqual(undefined);
});

it("Should let you set the position", () => {
const canvasNode = new CanvasNode();

expect(canvasNode.position).toEqual({ x: 0, y: 0 });

canvasNode.position = { x: 10, y: 20 };

expect(canvasNode.position).toEqual({ x: 10, y: 20 });
});
File renamed without changes.
File renamed without changes.

0 comments on commit d9c1fca

Please sign in to comment.