Skip to content

Commit

Permalink
Prepped the token tests and rounded out the library tests. Prepared t…
Browse files Browse the repository at this point in the history
…o make all of the module but the root and hud unit-testable.
  • Loading branch information
lupestro committed Feb 12, 2024
1 parent 64f255a commit 351b897
Show file tree
Hide file tree
Showing 8 changed files with 656 additions and 393 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ module.exports = {
quench: "readable",
},
overrides: [
{
files: ["test/*.mjs"],
globals: {
it: "readable",
describe: "readable",
beforeEach: "readable",
afterEach: "readable",
},
},
{
files: [".eslintrc.js", "webpack.config.js"],
parserOptions: {
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ jobs:
- name: Build All
run: |
pnpm install
pnpm lint
pnpm build
pnpm test
# Create a zip file with all files required by the module to add to the release
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@babel/core": "^7.23.9",
"@babel/preset-env": "^7.23.9",
"@babel/eslint-parser": "7.23.10",
"@babel/plugin-syntax-dynamic-import":"^7.8.3",
"babel-loader": "^9.1.3",
"babel-preset-es2015": "^6.24.1",
"c8":"^9.1.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/topology.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class DefaultLightTopology {
quantityField = "quantity";
constructor(/*quantityField*/) {}
actorHasLightSource(actor, lightSource) {
return lightSource.name === "Self";
return !!(lightSource && lightSource.name === "Self");
}
getImage(/*actor, lightSource*/) {
return DEFAULT_IMAGE_URL;
Expand Down
712 changes: 458 additions & 254 deletions test/library-tests.mjs

Large diffs are not rendered by default.

171 changes: 91 additions & 80 deletions test/test-stubs.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from 'assert';
import assert from "assert";

// These stubs provide all (and only) the features we use from Foundry objects

Expand All @@ -7,40 +7,49 @@ export class MockItem {
img;
system;
constructor(name, quantity) {
this.name = name;
this.img = `${name}.png`;
if (typeof quantity === "undefined") {
this.name = name;
this.img = `${name}.png`;
if (typeof quantity === "undefined") {
this.system = {};
} else {
this.system = {quantity: quantity};
}
} else {
this.system = { quantity: quantity };
}
}
update(props) {
for (let prop in props) {
let subprops = prop.split('.');
for (let prop in props) {
let subprops = prop.split(".");
let where = this;
for (let i = 0; i < subprops.length - 1; i++) {
where = where[subprops[i]];
where = where[subprops[i]];
}
where[subprops[subprops.length - 1]] = props[prop];
}
};
};
}
}
}

export class MockActor {
constructor(id, name, items, protoBright, protoDim) {
this.system = {id: id};
this.system = { id: id };
this.name = name;
this.items = items;
this.prototypeToken = {
light: {
bright: protoBright,
dim: protoDim
}
}
dim: protoDim,
},
};
// Data for stubs of functions that only appear in actors from GURPS topology
this.findEquipmentByNameMethod;
this.updateEqtCountDataMethod;
}
findEquipmentByName(name) {
return this.findEquipmentByNameMethod(name);
}
};

updateEqtCount(key, value) {
return this.updateEqtCountDataMethod(key, value);
}
}

export class MockToken {
name;
system;
Expand All @@ -59,87 +68,89 @@ export class MockToken {
}
}
getFlag(modname, flagname) {
assert.equal(modname, 'torch');
assert.equal(modname, "torch");
return this.flags[flagname];
};
}
setFlag(modname, flagname, value) {
assert.equal(modname, 'torch');
assert.equal(modname, "torch");
this.flags[flagname] = value;
};
}
update(props) {
for (let prop in props) {
let subprops = prop.split('.');
let subprops = prop.split(".");
let where = this;
for (let i = 0; i < subprops.length - 1; i++) {
where = where[subprops[i]];
where = where[subprops[i]];
}
where[subprops[subprops.length - 1]] = props[prop];
}
};
};

export class MockGame {
constructor (system, actors, isGM, settings) {
this.actorData = actors;
this.settingsData = settings;
this.user = { isGM: isGM };
this.system = { id: system };
this.actors = {
get : (id) => this.getActor(id)
}
this.settings = {
get : (modname, settingName) => this.getSetting(modname, settingName)
}
};
getSetting(modname, settingName) {
assert.equal(modname, 'torch');
let value = this.settingsData[settingName];
assert.notEqual(value, undefined, `Value for setting ${settingName} is undefined`);
return value;
};
getActor(id) {
return this.actorData.find ( (actor) => actor.system.id === id );
}
};
}

export class MockScene {
tokens = [];
constructor(tokens) {
if (tokens) {
this.tokens = tokens;
}
}
randomNumericString() {
return Math.floor(Math.random() * 1000000).toString(16);
tokens;
constructor(system, tokens, gridSize) {
this.tokens = tokens;
this.grid = { size: gridSize };
}
createEmbeddedDocuments (tokens) {
const newTokens = [];
// Give 'em all ids
createEmbeddedDocuments(type, tokens, options) {
assert.equal(type, "Token");
assert.deepEqual(options, { temporary: false, renderSheet: false });
for (const token of tokens) {
if (token.id) {
newTokens.push(Object.assign(token, {}));
} else {
newTokens.push(Object.assign(token, {id: this.randomNumericString()}));
// Add any tokens not already present in the collection
if (!this.tokens.find((aToken) => aToken.id === token.id)) {
this.tokens.push(token);
}
}
this.tokens.concat(newTokens);
}
getEmbeddedDocument(type, id) {
assert.equal(type, 'Token');
let token = this.tokens.find( (token) => token.id === id);
assert.notEqual(token, undefined);
return token;
getEmbeddedDocument(type, token) {
assert.equal(type, "Token");
return this.tokens.find((aToken) => aToken.id === token.id);
}
deleteEmbeddedDocuments(tokens) {
for (const token of tokens) {
assert.ok(this.tokens.find( (token) => token.id === id));
}
const remaining = [];
deleteEmbeddedDocuments(type, tokens) {
assert.equal(type, "Token");
let updatedTokens = [];
for (const token of this.tokens) {
if (!this.tokens.find( (token) => token.id === id)) {
remaining.push(token);
// Keep all tokens not appearing on the supplied list
if (!tokens.find((aToken) => aToken.id === token.id)) {
updatedTokens.push(token);
}
}
this.tokens = remaining;
this.tokens = updatedTokens;
}
}

export class MockGame {
constructor(system, actors, isGM, settings) {
this.actorData = actors;
this.settingsData = settings;
this.user = { isGM: isGM };
this.system = { id: system };
this.actors = {
get: (id) => this.getActor(id),
};
this.settings = {
get: (modname, settingName) => this.getSetting(modname, settingName),
};
this.expectedRequest;
this.socket = {
emit: (name, request) => {
assert.strictEqual(name, "module.torch");
assert.strictDeepEqual(request, this.expectedRequest);
},
};
}
getSetting(modname, settingName) {
assert.equal(modname, "torch");
let value = this.settingsData[settingName];
assert.notEqual(
value,
undefined,
`Value for setting ${settingName} is undefined`,
);
return value;
}
getActor(id) {
return this.actorData.find((actor) => actor.system.id === id);
}
}
Loading

0 comments on commit 351b897

Please sign in to comment.