Files
Battle-for-Tismo/tests/data_test.js
T

97 lines
3.7 KiB
JavaScript

import { TestCase } from "./framework/test_case.js";
import {
CIVILISATIONS,
PROTO_UNITS,
BUILDINGS,
GOVERNMENTS,
TECHNOLOGIES,
TRAINABLE_UNIT_IDS,
protoUnitById,
buildingById,
governmentById,
technologyById,
effectValue,
} from "../shared/data.js";
export class DataTest extends TestCase {
test_lookups_return_the_matching_entry() {
this.assertEqual(protoUnitById("modern_infantry"), PROTO_UNITS.find((u) => u.id === "modern_infantry"));
this.assertEqual(buildingById("market"), BUILDINGS.find((b) => b.id === "market"));
this.assertEqual(governmentById("republic"), GOVERNMENTS[0]);
this.assertEqual(technologyById("scientific_method"), TECHNOLOGIES[0]);
}
test_unknown_lookups_return_null() {
this.assertNull(protoUnitById("nope"));
this.assertNull(buildingById("nope"));
this.assertNull(governmentById("nope"));
this.assertNull(technologyById("nope"));
}
test_every_civilisation_is_well_formed() {
this.assertGreaterOrEqual(CIVILISATIONS.length, 2);
for (const civ of CIVILISATIONS) {
this.assertTrue(civ.id.length > 0, "id");
this.assertTrue(civ.name.length > 0, "name");
this.assertTrue(civ.primaryColour.startsWith("#"), `primary colour of ${civ.id}`);
this.assertTrue(civ.flag.endsWith(".png"), `flag of ${civ.id}`);
this.assertNotEmpty(civ.cityNames, `city names of ${civ.id}`);
}
}
test_trainable_units_exist_in_the_catalogue() {
this.assertNotEmpty(TRAINABLE_UNIT_IDS);
for (const id of TRAINABLE_UNIT_IDS) {
this.assertNotNull(protoUnitById(id), `trainable unit ${id}`);
}
}
test_artillery_is_expensive_slow_and_long_ranged() {
const infantry = protoUnitById("modern_infantry");
const artillery = protoUnitById("artillery");
this.assertNotNull(artillery, "artillery exists");
this.assertTrue(artillery.military, "artillery is military");
this.assertGreater(artillery.cost, infantry.cost * 2, "much more expensive");
this.assertGreater(artillery.upkeep, infantry.upkeep, "costlier to keep");
this.assertLess(artillery.speed, infantry.speed, "slower than infantry");
this.assertLess(artillery.defense, infantry.defense / 4, "nearly no defense");
this.assertGreater(artillery.attack, infantry.attack, "hits harder");
this.assertGreater(artillery.range, infantry.range, "outranges infantry");
this.assertEqual(artillery.range, 2);
this.assertTrue(TRAINABLE_UNIT_IDS.includes("artillery"), "artillery is trainable");
this.assertTrue(artillery.icon.endsWith(".svg"));
}
test_buildings_and_governments_carry_effects() {
for (const building of BUILDINGS) {
this.assertNotEmpty(building.effects, `${building.id} effects`);
this.assertGreater(building.costAlpha, 1, `${building.id} cost growth`);
for (const effect of building.effects) {
this.assertTrue(effect.stat.length > 0);
this.assertGreater(effect.baseValue, 0);
}
}
for (const government of GOVERNMENTS) {
this.assertNotEmpty(government.effects, `${government.id} effects`);
}
}
test_technologies_form_a_tree() {
const ids = new Set(TECHNOLOGIES.map((t) => t.id));
for (const tech of TECHNOLOGIES) {
this.assertGreater(tech.cost, 0, `${tech.id} cost`);
this.assertNotEmpty(tech.effects, `${tech.id} effects`);
for (const prerequisite of tech.prerequisites) {
this.assertTrue(ids.has(prerequisite), `${tech.id} prerequisite ${prerequisite} exists`);
}
}
}
test_effect_value_scales_with_level() {
const effect = { baseValue: 10, exponent: 1 };
this.assertApprox(effectValue(effect, 1), 10);
this.assertApprox(effectValue(effect, 3), 30);
this.assertEqual(effectValue(effect, 0), 0);
}
}