diff --git a/models/round_result.js b/models/round_result.js new file mode 100644 index 0000000..ea010ab --- /dev/null +++ b/models/round_result.js @@ -0,0 +1,58 @@ +"use strict"; + +import { Round, Team } from "./round.js"; + +export default class RoundResult { + /** How many points the round was worth. + * @type {number} + */ + #points; + /** Who wan the round. + * @type {Team} + */ + #winner; + + constructor(value, winner) { + if (typeof value === "number" + && (winner === Team.We || winner === Team.They)) + { + this.#points = value; + this.#winner = winner; + } else if (typeof value === "object" && winner === undefined) { + if (!("points" in value)) + throw new TypeError("missing points in deserialization object"); + if (typeof value.points !== "number") + throw new TypeEror("points in deserialization object must be number"); + this.#points = value.points; + + if (!("winner" in value)) + throw new TypeError("missing winner in deserialization object"); + if (value.winner !== Team.We && value.winner !== Team.They) + throw new TypeError("winner in deserialization object not real team"); + this.#winner = value.winner; + } else { + throw new TypeError("unknown form for RoundResult constructor"); + } + } + + /** Get the points the round was worth. */ + get points() { + return this.#points; + } + + /** Get the winner of the round. + * + * @returns {Team} The winner of the round. + */ + get winner() { + return this.#winner; + } + + /** Export needed data for JSON serialization. */ + toJSON() { + return { + points: this.#points, + winner: this.#winner, + }; + } +} diff --git a/models/round_result.test.js b/models/round_result.test.js new file mode 100644 index 0000000..5d2ad60 --- /dev/null +++ b/models/round_result.test.js @@ -0,0 +1,52 @@ +"use strict"; + +import { Team } from "./round.js"; +import RoundResult from "./round_result.js"; + +QUnit.module("models", function() { + QUnit.module("RoundResult", function() { + QUnit.test("regular construction", function(assert) { + let rr = new RoundResult(2, Team.We); + assert.strictEqual(rr.points, 2, "correct points"); + assert.strictEqual(rr.winner, Team.We, "correct winner"); + }); + + QUnit.test("serialization", function(assert) { + let rr = new RoundResult(3, Team.They); + assert.deepEqual( + rr.toJSON(), + { + points: 3, + winner: Team.They, + }, + "correct serialization object", + ); + }); + + QUnit.test("deserialization", function(assert) { + let rr = new RoundResult({ + points: 4, + winner: Team.We, + }); + assert.strictEqual(rr.points, 4, "correct points"); + assert.strictEqual(rr.winner, Team.We, "correct winner"); + }); + + QUnit.test("invalid deserialization", function(assert) { + let deso = {}; + assert.throws(function() { new RoundResult(deso); }, "no points"); + + deso.points = "5"; + assert.throws(function() { new RoundResult(deso); }, "string points"); + + deso.points = 5; + assert.throws(function() { new RoundResult(deso); }, "no winner"); + + deso.winner = "Team.They"; + assert.throws(function() { new RoundResult(deso); }, "string winner"); + + deso.winner = Team.They; + new RoundResult(deso); + }); + }); +}); diff --git a/test.html b/test.html index 8aceb3d..ace4f27 100644 --- a/test.html +++ b/test.html @@ -12,6 +12,7 @@
+