0ad/binaries/data/mods/public/simulation/helpers/tests/test_Attacking.js
wraitii 6b29cbac10 Add simple tests for Attacking.
Forgot to 'svn add' in 38b2e37a61

This was SVN commit r23013.
2019-09-29 07:03:09 +00:00

79 lines
2.7 KiB
JavaScript
Executable file

Engine.LoadHelperScript("Attacking.js");
Engine.LoadComponentScript("interfaces/Attack.js");
Engine.LoadComponentScript("interfaces/Capturable.js");
Engine.LoadComponentScript("interfaces/Health.js");
Engine.LoadComponentScript("interfaces/Promotion.js");
// Unit tests for the Attacking helper.
// TODO: Some of it is tested in components/test_Damage.js, which should be spliced and moved.
class testHandleAttackEffects {
constructor() {
this.resultString = "";
this.TESTED_ENTITY_ID = 5;
this.attackData = {
"Damage": "Uniquely Hashed Value",
"Capture": "Something Else Entirely",
};
}
/**
* This tests that we inflict multiple effect types.
*/
testMultipleEffects() {
AddMock(this.TESTED_ENTITY_ID, IID_Health, {
"TakeDamage": x => { this.resultString += x; },
});
AddMock(this.TESTED_ENTITY_ID, IID_Capturable, {
"Capture": x => { this.resultString += x; },
});
Attacking.HandleAttackEffects("Test", this.attackData, this.TESTED_ENTITY_ID, INVALID_ENTITY, INVALID_PLAYER);
TS_ASSERT(this.resultString.indexOf(this.attackData.Damage) !== -1);
TS_ASSERT(this.resultString.indexOf(this.attackData.Capture) !== -1);
}
/**
* This tests that we correctly handle effect types if one is not received.
*/
testSkippedEffect() {
AddMock(this.TESTED_ENTITY_ID, IID_Capturable, {
"Capture": x => { this.resultString += x; },
});
Attacking.HandleAttackEffects("Test", this.attackData, this.TESTED_ENTITY_ID, INVALID_ENTITY, INVALID_PLAYER);
TS_ASSERT(this.resultString.indexOf(this.attackData.Damage) === -1);
TS_ASSERT(this.resultString.indexOf(this.attackData.Capture) !== -1);
this.resultString = "";
DeleteMock(this.TESTED_ENTITY_ID, IID_Capturable);
AddMock(this.TESTED_ENTITY_ID, IID_Health, {
"TakeDamage": x => { this.resultString += x; },
});
Attacking.HandleAttackEffects("Test", this.attackData, this.TESTED_ENTITY_ID, INVALID_ENTITY, INVALID_PLAYER);
TS_ASSERT(this.resultString.indexOf(this.attackData.Damage) !== -1);
TS_ASSERT(this.resultString.indexOf(this.attackData.Capture) === -1);
}
/**
* Regression test that bonus multiplier is handled correctly.
*/
testBonusMultiplier() {
AddMock(this.TESTED_ENTITY_ID, IID_Health, {
"TakeDamage": (_, __, ___, mult) => { TS_ASSERT_EQUALS(mult, 2); },
});
AddMock(this.TESTED_ENTITY_ID, IID_Capturable, {
"Capture": (_, __, ___, mult) => { TS_ASSERT_EQUALS(mult, 2); },
});
Attacking.HandleAttackEffects("Test", this.attackData, this.TESTED_ENTITY_ID, INVALID_ENTITY, INVALID_PLAYER, 2);
}
}
new testHandleAttackEffects().testMultipleEffects();
new testHandleAttackEffects().testSkippedEffect();
new testHandleAttackEffects().testBonusMultiplier();