Skip to content

Commit

Permalink
Merge pull request #237 from guidokessels/qb-id-validation
Browse files Browse the repository at this point in the history
Add tests to validate XWS ids
  • Loading branch information
guidokessels authored Feb 20, 2019
2 parents ecd65fa + e6d1d3b commit ec8691a
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ cache:
yarn: true
script:
- yarn run validate:json
- yarn run validate:schema
- yarn run validate:tests
2 changes: 1 addition & 1 deletion data/quick-builds/galactic-empire.json
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@
"upgrades": {
"missile": ["homingmissiles"],
"sensor": ["trajectorysimulator"],
"configuration": ["bombletgenerator"],
"device": ["bombletgenerator"],
"modification": ["ablativeplating"]
}
}
Expand Down
4 changes: 2 additions & 2 deletions data/quick-builds/scum-and-villainy.json
Original file line number Diff line number Diff line change
Expand Up @@ -752,14 +752,14 @@
"id": "tansariipointveteran",
"upgrades": {
"talent": ["crackshot"],
"missile": ["heavylasercannon"]
"cannon": ["heavylasercannon"]
}
},
{
"id": "tansariipointveteran",
"upgrades": {
"talent": ["crackshot"],
"missile": ["heavylasercannon"]
"cannon": ["heavylasercannon"]
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"license": "MIT",
"scripts": {
"validate:json": "jsonlint-cli data/**/*.json",
"validate:schema": "jest tests/**-schema.test.js",
"validate:schema:watch": "jest --watch tests/**-schema.test.js",
"validate:tests": "jest tests/**.test.js",
"validate:tests:watch": "jest --watch tests/**.test.js",
"format": "prettier --loglevel warn --write \"data/**/*.json\"",
"changelog": "git log --pretty=format:'- %s (%h)' --no-merges",
"ffg2xws": "node scripts/ffg2xws.js && prettier --write data/ffg-xws.json",
Expand Down
106 changes: 106 additions & 0 deletions tests/helpers/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const manifest = require("../../data/manifest.json");

const loadedData = {};
const mapSlotXWSToName = {
astromech: "Astromech",
cannon: "Cannon",
configuration: "Configuration",
crew: "Crew",
device: "Device",
"force-power": "Force Power",
gunner: "Gunner",
illicit: "Illicit",
missile: "Missile",
modification: "Modification",
sensor: "Sensor",
tacticalrelay: "Tactical Relay",
talent: "Talent",
tech: "Tech",
title: "Title",
torpedo: "Torpedo",
turret: "Turret"
};

const loadPilotsAndShips = () => {
const allPilots = [];
const allShips = [];

manifest.pilots.forEach(({ ships }) => {
ships.forEach(filename => {
const { pilots = [], ship } = require(`../../${filename}`);
allPilots.push(...pilots);
allShips.push(ship);
});
});

return { ships: allShips, pilots: allPilots };
};

const getPilots = () => {
if (!loadedData.pilots) {
const { pilots } = loadPilotsAndShips();
loadedData.pilots = pilots;
}
return loadedData.pilots;
};

const getPilotXWSIds = () => {
const pilots = getPilots();
return pilots.map(p => p.xws).filter(Boolean);
};

const validatePilotXWSId = id => {
const ids = getPilotXWSIds();
if (ids.indexOf(id) === -1) {
throw new Error(`Pilot xws id "${id}" does not exist`);
}
};

const loadUpgrades = () => {
const allUpgrades = [];

manifest.upgrades.forEach(filename => {
const upgrades = require(`../../${filename}`);
allUpgrades.push(...upgrades);
});

return { upgrades: allUpgrades };
};

const getUpgrades = () => {
if (!loadedData.upgrades) {
const { upgrades } = loadUpgrades();
loadedData.upgrades = upgrades;
}
return loadedData.upgrades;
};

const getUpgradesXWSIds = () => {
const upgrades = getUpgrades();
return upgrades.map(u => u.xws).filter(Boolean);
};

const getUpgradesXWSIdsForSlot = slotName => {
const slotId = mapSlotXWSToName[slotName] || slotName;
const upgrades = getUpgrades();
return upgrades
.filter(u => u.sides.some(s => s.type === slotId))
.map(u => u.xws)
.filter(Boolean);
};

const validateUpgradeXWSIdForSlot = (id, slotName) => {
const ids = getUpgradesXWSIdsForSlot(slotName);
if (ids.indexOf(id) === -1) {
throw new Error(
`Upgrade xws id "${id}" does not exist for slot "${slotName}"`
);
}
};

module.exports = {
getPilotXWSIds,
validatePilotXWSId,
getUpgradesXWSIds,
validateUpgradeXWSIdForSlot
};
28 changes: 0 additions & 28 deletions tests/quick-builds-schema.test.js

This file was deleted.

55 changes: 55 additions & 0 deletions tests/quick-builds.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const path = require("path");
const {
validatePilotXWSId,
validateUpgradeXWSIdForSlot
} = require("./helpers/data");
const { matchers } = require("jest-json-schema");
expect.extend(matchers);

const { "quick-builds": quickBuildFiles } = require("../data/manifest.json");

const quickBuildsSchema = require("./schemas/quick-build.schema.json");

describe("Quick Builds", () => {
quickBuildFiles.forEach(file => {
const contents = require(`../${file}`);

test("has a top-level `quick-builds` property", () => {
expect(contents).toHaveProperty("quick-builds");
});

// `data/quick-builds/resistance.json` -> `resistance`
const faction = path.basename(file, path.extname(file));

describe(`${faction}`, () => {
const { "quick-builds": quickBuilds } = contents;

quickBuilds.forEach((qb, i) => {
// Example: `#64 - threat 2 - deathrain`
const testName = [
`#${i}`,
`threat ${qb.threat}`,
qb.pilots.map(d => d.id).join(", ")
].join(" - ");

describe(testName, () => {
test("matches schema", () => {
expect(qb).toMatchSchema(quickBuildsSchema);
});

test("uses valid XWS ids", () => {
qb.pilots.forEach(pilot => {
const { id: pilotId, upgrades = {} } = pilot;
validatePilotXWSId(pilotId);
Object.entries(upgrades).forEach(([slot, ids]) => {
ids.forEach(upgradeId =>
validateUpgradeXWSIdForSlot(upgradeId, slot)
);
});
});
});
});
});
});
});
});
20 changes: 20 additions & 0 deletions tests/xws.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { getPilotXWSIds, getUpgradesXWSIds } = require("./helpers/data");

const checkForDuplicates = ids => {
ids.reduce((acc, id) => {
if (acc.indexOf(id) > -1) {
throw new Error(`Duplicate XWS id: "${id}"`);
}
acc.push(id);
return acc;
}, []);
};

describe("XWS", () => {
test("no duplicate pilot XWS ids", () => {
checkForDuplicates(getPilotXWSIds());
});
test("no duplicate upgrade XWS ids", () => {
checkForDuplicates(getUpgradesXWSIds());
});
});

0 comments on commit ec8691a

Please sign in to comment.