1
0
Fork 0

Added unit testing for SubMunitionMissile

This commit is contained in:
Michaël Lemaire 2015-03-12 01:00:00 +01:00
parent af82ffff7a
commit 488b059913
2 changed files with 136 additions and 9 deletions

View file

@ -3,16 +3,42 @@ module SpaceTac.Game {
// unit testing utilities
export class TestTools {
// Set a ship action points, adding/updating an equipment if needed
static setShipAP(ship: Ship, points: number, recovery: number) {
var powers = ship.listEquipment(SlotType.Power);
var equipment: Equipment;
if (powers.length === 0) {
equipment = (new Equipments.BasicPowerCore()).generateFixed(0);
ship.addSlot(SlotType.Power).attach(equipment);
} else {
equipment = powers[0];
// Create a battle between two fleets, with a fixed play order (owned ships, then enemy ships)
static createBattle(own_ships: number, enemy_ships: number): Battle {
var fleet1 = new Fleet();
var fleet2 = new Fleet();
while (own_ships--) {
fleet1.addShip(new Ship());
}
while (enemy_ships--) {
fleet2.addShip(new Ship());
}
var battle = new Battle(fleet1, fleet2);
battle.play_order = fleet1.ships.concat(fleet2.ships);
battle.advanceToNextShip();
return battle;
}
// Get or add an equipment of a given slot type
static getOrGenEquipment(ship: Ship, slot: SlotType, template: LootTemplate): Equipment {
var equipped = ship.listEquipment(slot);
var equipment: Equipment;
if (equipped.length === 0) {
equipment = template.generateFixed(0);
ship.addSlot(slot).attach(equipment);
} else {
equipment = equipped[0];
}
return equipment;
}
// Set a ship action points, adding/updating an equipment if needed
static setShipAP(ship: Ship, points: number, recovery: number = 0): void {
var equipment = this.getOrGenEquipment(ship, SlotType.Power, new Equipments.BasicPowerCore());
equipment.permanent_effects.forEach((effect: BaseEffect) => {
if (effect.code === "attrmax") {
@ -32,5 +58,31 @@ module SpaceTac.Game {
ship.ap_current.set(points);
ship.ap_recover.set(recovery);
}
// Set a ship hull and shield points, adding/updating an equipment if needed
static setShipHP(ship: Ship, hull_points: number, shield_points: number): void {
var armor = TestTools.getOrGenEquipment(ship, SlotType.Armor, new Equipments.IronHull());
var shield = TestTools.getOrGenEquipment(ship, SlotType.Shield, new Equipments.BasicForceField());
armor.permanent_effects.forEach((effect: BaseEffect) => {
if (effect.code === "attrmax") {
var meffect = <AttributeMaxEffect>effect;
if (meffect.attrcode === AttributeCode.Hull) {
meffect.value = hull_points;
}
}
});
shield.permanent_effects.forEach((effect: BaseEffect) => {
if (effect.code === "attrmax") {
var meffect = <AttributeMaxEffect>effect;
if (meffect.attrcode === AttributeCode.Shield) {
meffect.value = shield_points;
}
}
});
ship.updateAttributes();
ship.restoreHealth();
}
}
}

View file

@ -0,0 +1,75 @@
/// <reference path="../../../definitions/jasmine.d.ts"/>
/// <reference path="../SubMunitionMissile.ts"/>
module SpaceTac.Game.Specs {
"use strict";
describe("SubMunitionMissile", () => {
it("hits several targets in circle", () => {
var battle = TestTools.createBattle(1, 2);
var ship = battle.fleets[0].ships[0];
ship.setArenaPosition(0, 0);
TestTools.setShipAP(ship, 100);
TestTools.setShipHP(ship, 50, 30);
var enemy1 = battle.fleets[1].ships[0];
enemy1.setArenaPosition(0, 1);
TestTools.setShipHP(enemy1, 50, 30);
var enemy2 = battle.fleets[1].ships[1];
enemy2.setArenaPosition(0, 2);
TestTools.setShipHP(enemy2, 50, 30);
var template = new Equipments.SubMunitionMissile();
var equipment = template.generateFixed(0);
equipment.distance = 5;
equipment.blast = 1.5;
(<DamageEffect>equipment.target_effects[0]).value = 20;
var checkHP = (h1: number, s1: number, h2: number, s2: number, h3: number, s3: number): void => {
expect(ship.hull.current).toBe(h1);
expect(ship.shield.current).toBe(s1);
expect(enemy1.hull.current).toBe(h2);
expect(enemy1.shield.current).toBe(s2);
expect(enemy2.hull.current).toBe(h3);
expect(enemy2.shield.current).toBe(s3);
};
checkHP(50, 30, 50, 30, 50, 30);
battle.log.clear();
battle.log.addFilter("attr");
// Fire at a ship
var t = Target.newFromShip(enemy1);
expect(equipment.action.canBeUsed(battle, ship)).toBe(true);
equipment.action.apply(battle, ship, t);
checkHP(50, 10, 50, 10, 50, 10);
expect(battle.log.events.length).toBe(4);
expect(battle.log.events[0]).toEqual(new FireEvent(ship, equipment, t));
expect(battle.log.events[1]).toEqual(new DamageEvent(ship, 0, 20));
expect(battle.log.events[2]).toEqual(new DamageEvent(enemy1, 0, 20));
expect(battle.log.events[3]).toEqual(new DamageEvent(enemy2, 0, 20));
battle.log.clear();
// Fire in space
t = Target.newFromLocation(0, 2.4);
expect(equipment.action.canBeUsed(battle, ship)).toBe(true);
equipment.action.apply(battle, ship, t);
checkHP(50, 10, 40, 0, 40, 0);
expect(battle.log.events.length).toBe(3);
expect(battle.log.events[0]).toEqual(new FireEvent(ship, equipment, t));
expect(battle.log.events[1]).toEqual(new DamageEvent(enemy1, 10, 10));
expect(battle.log.events[2]).toEqual(new DamageEvent(enemy2, 10, 10));
battle.log.clear();
// Fire far away
t = Target.newFromLocation(0, 5);
expect(equipment.action.canBeUsed(battle, ship)).toBe(true);
equipment.action.apply(battle, ship, t);
checkHP(50, 10, 40, 0, 40, 0);
expect(battle.log.events.length).toBe(1);
expect(battle.log.events[0]).toEqual(new FireEvent(ship, equipment, t));
});
});
}