modaq
Version:
Quiz Bowl Reader using TypeScript, React, and MobX
305 lines • 19.2 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const GameFormats = __importStar(require("src/state/GameFormats"));
const PacketState = __importStar(require("src/state/PacketState"));
const PacketState_1 = require("src/state/PacketState");
require("src/parser/IFormattedText");
require("src/state/IGameFormat");
const noPowersGameFormat = Object.assign(Object.assign({}, GameFormats.UndefinedGameFormat), { powerMarkers: [], pointsForPowers: [] });
const powersGameFormat = Object.assign(Object.assign({}, GameFormats.UndefinedGameFormat), { powers: [{ marker: "(*)", points: 15 }] });
const superpowersGameFormat = Object.assign(Object.assign({}, GameFormats.UndefinedGameFormat), { powers: [
{ marker: "(+)", points: 20 },
{ marker: "(*)", points: 15 },
] });
describe("PacketStateTests", () => {
describe("formattedQuestionText", () => {
// Most of these tests are handled by FormattedTextParserTests, so just test that it's hooked up to it and that
// we include the end character
it("formattedQuestionText has end marker", () => {
const tossup = new PacketState_1.Tossup("This is my question", "Answer");
const formattedText = tossup.getWords(noPowersGameFormat).map((word) => word.word);
chai_1.expect(formattedText.length).to.equal(5);
chai_1.expect(formattedText[0].length).to.equal(1);
chai_1.expect(formattedText[0][0].text).to.equal("This");
chai_1.expect(formattedText[4].length).to.equal(1);
chai_1.expect(formattedText[4][0].text).to.equal("■END■");
});
it("formattedQuestionText has formatting", () => {
const tossup = new PacketState_1.Tossup("<b>This is</b> my question", "Answer");
const formattedText = tossup.getWords(noPowersGameFormat).map((word) => word.word);
chai_1.expect(formattedText.length).to.be.greaterThan(1);
chai_1.expect(formattedText[0].length).to.equal(1);
const formattedWord = formattedText[0][0];
chai_1.expect(formattedWord.text).to.equal("This");
chai_1.expect(formattedWord.bolded).to.be.true;
chai_1.expect(formattedWord.emphasized).to.be.false;
chai_1.expect(formattedWord.underlined).to.be.false;
chai_1.expect(formattedWord.pronunciation).to.be.false;
});
it("formattedQuestionText has pronunciation guide", () => {
const tossup = new PacketState_1.Tossup('My question ("QWEST-shun") is this.', "Answer");
const formattedText = tossup.getWords(noPowersGameFormat).map((word) => word.word);
chai_1.expect(formattedText.length).to.be.greaterThan(1);
chai_1.expect(formattedText[2].length).to.equal(1);
const formattedWord = formattedText[2][0];
chai_1.expect(formattedWord.text).to.equal('("QWEST-shun")');
chai_1.expect(formattedWord.bolded).to.be.false;
chai_1.expect(formattedWord.emphasized).to.be.false;
chai_1.expect(formattedWord.underlined).to.be.false;
chai_1.expect(formattedWord.pronunciation).to.be.true;
});
it("formattedQuestionText has power marker", () => {
const tossup = new PacketState_1.Tossup("The power marker (*) is here.", "Answer");
const formattedText = tossup.getWords(powersGameFormat).map((word) => word.word);
chai_1.expect(formattedText.length).to.be.greaterThan(1);
chai_1.expect(formattedText[3].length).to.equal(1);
const formattedWord = formattedText[3][0];
chai_1.expect(formattedWord.text).to.equal("(*)");
chai_1.expect(formattedWord.bolded).to.be.false;
chai_1.expect(formattedWord.emphasized).to.be.false;
chai_1.expect(formattedWord.underlined).to.be.false;
chai_1.expect(formattedWord.pronunciation).to.be.false;
});
it("formattedQuestionText has power marker with punctuation after it", () => {
const format = Object.assign(Object.assign({}, powersGameFormat), { pronunciationGuideMarkers: ["(", ")"] });
const tossup = new PacketState_1.Tossup("The power marker (*), I think.", "Answer");
const formattedText = tossup.getWords(format).map((word) => word.word);
chai_1.expect(formattedText.length).to.be.greaterThan(1);
chai_1.expect(formattedText[3].length).to.equal(2);
const formattedFirstPart = formattedText[3][0];
chai_1.expect(formattedFirstPart.text).to.equal("(*)");
chai_1.expect(formattedFirstPart.bolded).to.be.false;
chai_1.expect(formattedFirstPart.emphasized).to.be.false;
chai_1.expect(formattedFirstPart.underlined).to.be.false;
chai_1.expect(formattedFirstPart.pronunciation).to.be.false;
const formattedSecondWord = formattedText[3][1];
chai_1.expect(formattedSecondWord.text).to.equal(",");
chai_1.expect(formattedSecondWord.bolded).to.be.false;
chai_1.expect(formattedSecondWord.emphasized).to.be.false;
chai_1.expect(formattedSecondWord.underlined).to.be.false;
chai_1.expect(formattedSecondWord.pronunciation).to.be.false;
});
});
// Need tests for getBonusWords?
describe("getBonusWords", () => {
it("No pronunciation guide in format", () => {
const formattedText = PacketState.getBonusWords("<b>This is</b> my bonus part", Object.assign(Object.assign({}, GameFormats.ACFGameFormat), { pronunciationGuideMarkers: undefined }));
chai_1.expect(formattedText.length).to.equal(2);
const firstSegment = formattedText[0];
chai_1.expect(firstSegment.text).to.equal("This is");
chai_1.expect(firstSegment.pronunciation).to.be.false;
chai_1.expect(firstSegment.bolded).to.be.true;
const secondSegment = formattedText[1];
chai_1.expect(secondSegment.text).to.equal(" my bonus part");
chai_1.expect(secondSegment.pronunciation).to.be.false;
chai_1.expect(secondSegment.bolded).to.be.false;
});
it("With pronunciation guide", () => {
const formattedText = PacketState.getBonusWords('<b>This is</b> my bonus ("BONE-us") part', GameFormats.ACFGameFormat);
chai_1.expect(formattedText.length).to.equal(4);
chai_1.expect(formattedText[0].text).to.equal("This is");
chai_1.expect(formattedText[0].pronunciation).to.be.false;
chai_1.expect(formattedText[0].bolded).to.be.true;
chai_1.expect(formattedText[1].text).to.equal(" my bonus ");
chai_1.expect(formattedText[1].pronunciation).to.be.false;
chai_1.expect(formattedText[1].bolded).to.be.false;
chai_1.expect(formattedText[2].text).to.equal('("BONE-us")');
chai_1.expect(formattedText[2].pronunciation).to.be.true;
chai_1.expect(formattedText[2].bolded).to.be.false;
chai_1.expect(formattedText[3].text).to.equal(" part");
chai_1.expect(formattedText[3].pronunciation).to.be.false;
chai_1.expect(formattedText[3].bolded).to.be.false;
});
it("With multiple pronunciation guide", () => {
const formattedText = PacketState.getBonusWords('<u>Another</u> ("an-OTH-er") bonus ("BONE-us") part', GameFormats.ACFGameFormat);
chai_1.expect(formattedText.length).to.equal(6);
chai_1.expect(formattedText[0].text).to.equal("Another");
chai_1.expect(formattedText[0].pronunciation).to.be.false;
chai_1.expect(formattedText[0].underlined).to.be.true;
chai_1.expect(formattedText[1].text).to.equal(" ");
chai_1.expect(formattedText[1].pronunciation).to.be.false;
chai_1.expect(formattedText[1].underlined).to.be.false;
chai_1.expect(formattedText.slice(2).map((text) => text.underlined)).to.not.contain(true);
chai_1.expect(formattedText[2].text).to.equal('("an-OTH-er")');
chai_1.expect(formattedText[2].pronunciation).to.be.true;
chai_1.expect(formattedText[3].text).to.equal(" bonus ");
chai_1.expect(formattedText[3].pronunciation).to.be.false;
chai_1.expect(formattedText[4].text).to.equal('("BONE-us")');
chai_1.expect(formattedText[4].pronunciation).to.be.true;
chai_1.expect(formattedText[5].text).to.equal(" part");
chai_1.expect(formattedText[5].pronunciation).to.be.false;
});
it("No pronunication guide, but defined in format", () => {
const formattedText = PacketState.getBonusWords("<b>This is</b> my bonus part", GameFormats.ACFGameFormat);
chai_1.expect(formattedText.length).to.equal(2);
chai_1.expect(formattedText.map((text) => text.pronunciation)).to.not.contain(true);
chai_1.expect(formattedText[0].text).to.equal("This is");
chai_1.expect(formattedText[0].bolded).to.be.true;
chai_1.expect(formattedText[1].text).to.equal(" my bonus part");
chai_1.expect(formattedText[1].bolded).to.be.false;
});
});
describe("getPointsAtPosition", () => {
it("No powers", () => {
const tossup = new PacketState_1.Tossup("This is my question", "Answer");
const points = tossup.getPointsAtPosition(noPowersGameFormat, 2);
chai_1.expect(points).to.equal(10);
});
it("Negative value", () => {
const tossup = new PacketState_1.Tossup("This is my question", "Answer");
const points = tossup.getPointsAtPosition(noPowersGameFormat, -1);
// Go with the default if we get a nonsense value
chai_1.expect(points).to.equal(10);
});
it("At last word", () => {
const tossup = new PacketState_1.Tossup("This is my question", "Answer");
const points = tossup.getPointsAtPosition(noPowersGameFormat, tossup.question.length - 1);
chai_1.expect(points).to.equal(10);
});
it("At end", () => {
const tossup = new PacketState_1.Tossup("This is my question", "Answer");
const points = tossup.getPointsAtPosition(noPowersGameFormat, tossup.question.length);
chai_1.expect(points).to.equal(10);
});
it("In power", () => {
const tossup = new PacketState_1.Tossup("This is my (*) question", "Answer");
const points = tossup.getPointsAtPosition(powersGameFormat, 2);
chai_1.expect(points).to.equal(15);
});
it("Out of power", () => {
const tossup = new PacketState_1.Tossup("This is my (*) question", "Answer");
const points = tossup.getPointsAtPosition(powersGameFormat, 3);
chai_1.expect(points).to.equal(10);
});
it("In superpower", () => {
const tossup = new PacketState_1.Tossup("This is (+) my (*) question", "Answer");
const points = tossup.getPointsAtPosition(superpowersGameFormat, 1);
chai_1.expect(points).to.equal(20);
});
it("In power but not superpower", () => {
const tossup = new PacketState_1.Tossup("This is (+) my (*) question", "Answer");
const points = tossup.getPointsAtPosition(superpowersGameFormat, 2);
chai_1.expect(points).to.equal(15);
});
it("Out of power and superpower", () => {
const tossup = new PacketState_1.Tossup("This is (+) my (*) question", "Answer");
const points = tossup.getPointsAtPosition(superpowersGameFormat, 3);
chai_1.expect(points).to.equal(10);
});
it("In power with pronunciation guide", () => {
const gameFormat = Object.assign(Object.assign({}, powersGameFormat), { pronunciationGuideMarkers: ["(", ")"] });
const tossup = new PacketState_1.Tossup('This question ("quest-shun") is my (*) question', "Answer");
const points = tossup.getPointsAtPosition(gameFormat, 3);
chai_1.expect(points).to.equal(15);
});
it("In power with multiple pronunciation guides", () => {
const gameFormat = Object.assign(Object.assign({}, powersGameFormat), { pronunciationGuideMarkers: ["(", ")"] });
const tossup = new PacketState_1.Tossup('This question ("quest-shun") is my ("mai") (*) question', "Answer");
const points = tossup.getPointsAtPosition(gameFormat, 3);
chai_1.expect(points).to.equal(15);
});
it("In power with pronunciation guide followed by a period", () => {
const gameFormat = Object.assign(Object.assign({}, powersGameFormat), { pronunciationGuideMarkers: ["(", ")"] });
const tossup = new PacketState_1.Tossup('This is a question ("quest-shun"). It is my (*) question', "Answer");
const points = tossup.getPointsAtPosition(gameFormat, 4);
chai_1.expect(points).to.equal(15);
});
it("Out of power with pronunciation guide", () => {
const gameFormat = Object.assign(Object.assign({}, powersGameFormat), { pronunciationGuideMarkers: ["(", ")"] });
const tossup = new PacketState_1.Tossup('This question ("quest-shun") is my (*) question', "Answer");
const points = tossup.getPointsAtPosition(gameFormat, 4);
chai_1.expect(points).to.equal(10);
});
it("Out of power with multiple pronunciation guides", () => {
const gameFormat = Object.assign(Object.assign({}, powersGameFormat), { pronunciationGuideMarkers: ["(", ")"] });
const tossup = new PacketState_1.Tossup('This question ("quest-shun") is my (mai) (*) question', "Answer");
const points = tossup.getPointsAtPosition(gameFormat, 4);
chai_1.expect(points).to.equal(10);
});
it("In power with power at last word", () => {
const tossup = new PacketState_1.Tossup("This is my question (*)", "Answer");
const points = tossup.getPointsAtPosition(powersGameFormat, 3);
chai_1.expect(points).to.equal(15);
});
it("In superpower with superpower at last word", () => {
const tossup = new PacketState_1.Tossup("This is my question (+)", "Answer");
const points = tossup.getPointsAtPosition(superpowersGameFormat, 3);
chai_1.expect(points).to.equal(20);
});
it("In power with no superpower in question", () => {
const tossup = new PacketState_1.Tossup("This is my (*) question", "Answer");
const points = tossup.getPointsAtPosition(superpowersGameFormat, 2);
chai_1.expect(points).to.equal(15);
});
it("In power with punctuation after power marker", () => {
const tossup = new PacketState_1.Tossup("This is my (*), question", "Answer");
const points = tossup.getPointsAtPosition(powersGameFormat, 2);
chai_1.expect(points).to.equal(15);
const pointsAfter = tossup.getPointsAtPosition(powersGameFormat, 3);
chai_1.expect(pointsAfter).to.equal(10);
});
// Tossups include a special character to mark the end of the question, which is after the last word in the
// question, so the last index will be one greater than the number of words in the question.
it("Wrong before the last word (no powers)", () => {
const tossup = new PacketState_1.Tossup("This is my question", "Answer");
const points = tossup.getPointsAtPosition(noPowersGameFormat, 3, false);
chai_1.expect(points).to.equal(-5);
});
it("Wrong at the last word (no powers)", () => {
const tossup = new PacketState_1.Tossup("This is my question", "Answer");
const points = tossup.getPointsAtPosition(noPowersGameFormat, 4, false);
chai_1.expect(points).to.equal(0);
});
it("Wrong before the last word (pronunciation guide)", () => {
const tossup = new PacketState_1.Tossup('This is my question ("qwest-shun")', "Answer");
const points = tossup.getPointsAtPosition(noPowersGameFormat, 3, false);
chai_1.expect(points).to.equal(-5);
});
it("Wrong at the last word (pronunciation guide)", () => {
const tossup = new PacketState_1.Tossup('This is my question ("qwest-shun")', "Answer");
const points = tossup.getPointsAtPosition(noPowersGameFormat, 4, false);
chai_1.expect(points).to.equal(0);
});
it("Wrong before the last word (powers)", () => {
const tossup = new PacketState_1.Tossup("This is (*) my question", "Answer");
const points = tossup.getPointsAtPosition(powersGameFormat, 3, false);
chai_1.expect(points).to.equal(-5);
});
it("Wrong at the last word (powers)", () => {
const tossup = new PacketState_1.Tossup("This is (*) my question", "Answer");
const points = tossup.getPointsAtPosition(powersGameFormat, 4, false);
chai_1.expect(points).to.equal(0);
});
it("Wrong before the last word (powers and superpowers)", () => {
const tossup = new PacketState_1.Tossup("This is (*) my question", "Answer");
const points = tossup.getPointsAtPosition(superpowersGameFormat, 3, false);
chai_1.expect(points).to.equal(-5);
});
it("Wrong at the last word (powers and superpowers)", () => {
const tossup = new PacketState_1.Tossup("This (+) is (*) my question", "Answer");
const points = tossup.getPointsAtPosition(superpowersGameFormat, 4, false);
chai_1.expect(points).to.equal(0);
});
});
});
//# sourceMappingURL=PacketStateTests.js.map