Skip to content

Commit

Permalink
feat(options): Implements debug and showMinor game options
Browse files Browse the repository at this point in the history
  • Loading branch information
jcowman2 committed Sep 7, 2018
1 parent d0d0dde commit 12515ee
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/config/instance-options.ts
Expand Up @@ -14,7 +14,7 @@ const OPTION_OVERRIDES_PROXY_HANDLER = {
receiver: object
) {
throw new RegalError(
"Cannot modify the properties of the instance overrides."
"Cannot modify the properties of the InstanceOption option overrides."
);
}
};
Expand Down
13 changes: 13 additions & 0 deletions src/output.ts
Expand Up @@ -99,6 +99,19 @@ export class InstanceOutput {
line: string,
lineType: OutputLineType = OutputLineType.NORMAL
) {
switch (lineType) {
case OutputLineType.DEBUG:
if (!this.game.options.debug) {
return;
}
break;
case OutputLineType.MINOR:
if (!this.game.options.showMinor) {
return;
}
break;
}

const outputLine = {
data: line,
id: ++this._lineCount,
Expand Down
162 changes: 162 additions & 0 deletions test/config.test.ts
@@ -0,0 +1,162 @@
import { expect } from "chai";
import "mocha";

import GameInstance from "../src/game-instance";
import { RegalError } from "../src/error";
import { OPTION_KEYS, DEFAULT_GAME_OPTIONS } from "../src/config";
import { OutputLineType } from "../src/output";

describe("Config", function() {
describe("Game Options", function() {
it("Test defaults", function() {
const myGame = new GameInstance();
OPTION_KEYS.forEach(key => {
expect(myGame.options[key]).to.deep.equal(
DEFAULT_GAME_OPTIONS[key]
);
});
});

describe("GameOption Validation", function() {
it("GameOptions.debug VALID", function() {
const myGame = new GameInstance({ debug: true });
expect(myGame.options.overrides).to.deep.equal({
debug: true
});
expect(myGame.options.debug).to.be.true;
});

it("GameOptions.debug INVALID", function() {
expect(() => new GameInstance(<any>{ debug: 3 })).to.throw(
RegalError,
"RegalError: The option <debug> is of type <number>, must be of type <boolean>."
);
});

it("GameOptions.forbidChanges VALID: boolean", function() {
const myGame = new GameInstance({ forbidChanges: true });
expect(myGame.options.overrides).to.deep.equal({
forbidChanges: true
});
expect(myGame.options.forbidChanges).to.be.true;
});

it("GameOptions.forbidChanges VALID: empty array", function() {
const myGame = new GameInstance({ forbidChanges: [] });
expect(myGame.options.overrides).to.deep.equal({
forbidChanges: []
});
expect(myGame.options.forbidChanges).to.deep.equal([]);
});

it("GameOptions.forbidChanges VALID: valid array", function() {
const myGame = new GameInstance({
forbidChanges: ["debug", "forbidChanges"]
});
expect(myGame.options.overrides).to.deep.equal({
forbidChanges: ["debug", "forbidChanges"]
});
expect(myGame.options.forbidChanges).to.deep.equal([
"debug",
"forbidChanges"
]);
});

it("GameOptions.forbidChanges INVALID: mistype", function() {
expect(
() => new GameInstance(<any>{ forbidChanges: 3 })
).to.throw(
RegalError,
"RegalError: The option <forbidChanges> is of type <number>, must be of type <boolean> or <string[]>."
);
});

it("GameOptions.forbidChanges INVALID: illegal array", function() {
expect(
() =>
new GameInstance({ forbidChanges: ["debug", "blark"] })
).to.throw(
RegalError,
"RegalError: The option <blark> does not exist."
);
});

it("GameOptions.showMinor VALID", function() {
const myGame = new GameInstance({ showMinor: true });
expect(myGame.options.overrides).to.deep.equal({
showMinor: true
});
expect(myGame.options.showMinor).to.be.true;
});

it("GameOptions.showMinor INVALID", function() {
expect(() => new GameInstance(<any>{ showMinor: 3 })).to.throw(
RegalError,
"RegalError: The option <showMinor> is of type <number>, must be of type <boolean>."
);
});
});

describe("Option Behavior", function() {
it("DEBUG output is not printed when GameOptions.debug is set to false", function() {
const myGame = new GameInstance({ debug: false });
myGame.output.writeDebug("Hello, world!");

expect(myGame.output.lines).to.deep.equal([]);
});

it("DEBUG output is printed when GameOptions.debug is set to true", function() {
const myGame = new GameInstance({ debug: true });
myGame.output.writeDebug("Hello, world!");

expect(myGame.output.lines).to.deep.equal([
{
id: 1,
type: OutputLineType.DEBUG,
data: "Hello, world!"
}
]);
});

it("MINOR output is not printed when GameOptions.showMinor is set to false", function() {
const myGame = new GameInstance({ showMinor: false });
myGame.output.writeMinor("Hello, world!");

expect(myGame.output.lines).to.deep.equal([]);
});

it("MINOR output is printed when GameOptions.showMinor is set to true", function() {
const myGame = new GameInstance({ showMinor: true });
myGame.output.writeMinor("Hello, world!");

expect(myGame.output.lines).to.deep.equal([
{
id: 1,
type: OutputLineType.MINOR,
data: "Hello, world!"
}
]);
});
});
});

describe("InstanceOptions", function() {
it("The properties of InstanceOptions cannot be modified", function() {
const myGame = new GameInstance();
expect(() => (myGame.options.debug = true)).to.throw(
RegalError,
"Cannot modify the properties of InstanceOptions."
);
});

it("The properties of InstanceOptions.overrides cannot be modified", function() {
const myGame = new GameInstance();
expect(
() => ((<any>myGame.options.overrides).debug = true)
).to.throw(
RegalError,
"Cannot modify the properties of the InstanceOption option overrides."
);
});
});
});
93 changes: 0 additions & 93 deletions test/game-api.test.ts
Expand Up @@ -308,98 +308,5 @@ describe("Game API", function() {
);
expect(response.instance).to.be.undefined;
});

describe("Option validation checks", function() {
it("GameOptions.debug VALID", function() {
onStartCommand(game => noop);

const response = Game.postStartCommand({
debug: true
});
const options = response.instance.options;

expect(response.output.wasSuccessful).to.be.true;
expect(options.overrides).to.deep.equal({
debug: true
});
expect(options.debug).to.be.true;
});

it("GameOptions.debug INVALID", function() {
onStartCommand(game => noop);

const response = Game.postStartCommand(<any>{ foo: 3 });

expect(response.output.wasSuccessful).to.be.false;
expect(response.output.error.message).to.equal(
"RegalError: Invalid option name <foo>."
);
expect(response.instance).to.be.undefined;
});

it("GameOptions.forbidChanges VALID", function() {
onStartCommand(game => noop);

const response1 = Game.postStartCommand({
forbidChanges: true
});
const options1 = response1.instance.options;

expect(response1.output.wasSuccessful).to.be.true;
expect(options1.overrides).to.deep.equal({
forbidChanges: true
});
expect(options1.forbidChanges).to.be.true;

const response2 = Game.postStartCommand({
forbidChanges: []
});
const options2 = response2.instance.options;

expect(response2.output.wasSuccessful).to.be.true;
expect(options2.overrides).to.deep.equal({
forbidChanges: []
});
expect(options2.forbidChanges).to.deep.equal([]);

const response3 = Game.postStartCommand({
forbidChanges: ["debug", "forbidChanges"]
});
const options3 = response3.instance.options;

expect(response3.output.wasSuccessful).to.be.true;
expect(options3.overrides).to.deep.equal({
forbidChanges: ["debug", "forbidChanges"]
});
expect(options3.forbidChanges).to.deep.equal([
"debug",
"forbidChanges"
]);
});

it("GameOptions.forbidChanges INVALID", function() {
onStartCommand(game => noop);

const response1 = Game.postStartCommand(<any>{
forbidChanges: 5
});

expect(response1.output.wasSuccessful).to.be.false;
expect(response1.output.error.message).to.equal(
"RegalError: The option <forbidChanges> is of type <number>, must be of type <boolean> or <string[]>."
);
expect(response1.instance).to.be.undefined;

const response2 = Game.postStartCommand({
forbidChanges: ["blark"]
});

expect(response2.output.wasSuccessful).to.be.false;
expect(response2.output.error.message).to.equal(
"RegalError: The option <blark> does not exist."
);
expect(response2.instance).to.be.undefined;
});
});
});
});
11 changes: 11 additions & 0 deletions test/game-instance.test.ts
Expand Up @@ -24,4 +24,15 @@ describe("GameInstance", function() {

expect(former).to.not.equal(current);
});

it("Cycling a game instance copies its options", function() {
const former = new GameInstance({
debug: true,
forbidChanges: ["debug"]
});
const current = former.cycle();

expect(former.options).to.not.equal(current.options);
expect(current.options).to.deep.equal(former.options);
});
});
6 changes: 3 additions & 3 deletions test/output.test.ts
Expand Up @@ -21,7 +21,7 @@ describe("Output", function() {
});

it("InstanceOutput.writeLine accepts an optional OutputLineType", function() {
const myGame = new GameInstance();
const myGame = new GameInstance({ debug: true });

myGame.output.writeLine("Hello, world!", OutputLineType.DEBUG);

Expand Down Expand Up @@ -111,7 +111,7 @@ describe("Output", function() {
});

it("InstanceOutput.writeDebug writes DEBUG lines", function() {
const myGame = new GameInstance();
const myGame = new GameInstance({ debug: true });

myGame.output.writeDebug("Line 1", "Line 2");

Expand Down Expand Up @@ -144,7 +144,7 @@ describe("Output", function() {
});

it("Multiple line types with InstanceOutput", function() {
const myGame = new GameInstance();
const myGame = new GameInstance({ debug: true });

myGame.output.writeDebug("Room loaded.");
myGame.output.writeTitle("West of House");
Expand Down

0 comments on commit 12515ee

Please sign in to comment.