implement model for round results
This commit is contained in:
parent
df588af0c6
commit
13971d2073
58
models/round_result.js
Normal file
58
models/round_result.js
Normal file
@ -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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
52
models/round_result.test.js
Normal file
52
models/round_result.test.js
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -12,6 +12,7 @@
|
|||||||
<div id="qunit-fixture"></div>
|
<div id="qunit-fixture"></div>
|
||||||
<script src="vendored/qunit-2.25.0.js"></script>
|
<script src="vendored/qunit-2.25.0.js"></script>
|
||||||
<script src="models/round.test.js" type="module"></script>
|
<script src="models/round.test.js" type="module"></script>
|
||||||
|
<script src="models/round_result.test.js" type="module"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
Loading…
Reference in New Issue
Block a user