Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/tests/test_Health.js
- This file was added.
Engine.LoadHelperScript("tests/ValueModification.js"); | |||||
Engine.LoadHelperScript("tests/Sound.js"); | |||||
Engine.LoadComponentScript("interfaces/DeathDamage.js"); | |||||
Engine.LoadComponentScript("interfaces/Health.js"); | |||||
Engine.LoadComponentScript("Health.js"); | |||||
const entity_id = 5; | |||||
const template = { | |||||
"Max": 50, | |||||
"RegenRate": 0, | |||||
"IdleRegenRate": 0, | |||||
"DeathType": "corpse", | |||||
bb: trailing comma | |||||
wraitiiAuthorUnsubmitted Done Inline ActionsI can never remember if we like those or not. wraitii: I can never remember if we like those or not. | |||||
} | |||||
bbUnsubmitted Not Done Inline Actionssemi bb: semi | |||||
let cmpHealth = ConstructComponent(entity_id, "Health", template); | |||||
bbUnsubmitted Not Done Inline ActionsMaybe (surely) a bit out of scope: one could test the regeneration function too bb: Maybe (surely) a bit out of scope: one could test the regeneration function too
Sounds more… | |||||
AddMock(entity_id, IID_DeathDamage, { | |||||
"CauseDeathDamage": () => {}, | |||||
bbUnsubmitted Not Done Inline Actionsmore trailing comma's bb: more trailing comma's | |||||
}); | |||||
AddMock(entity_id, IID_Position, { | |||||
"IsInWorld": () => true, | |||||
"GetPosition": () => ({ "x" : 0, "z": 0 }), | |||||
"GetRotation": () => ({ "x" : 0, "y": 0, "z": 0 }), | |||||
}); | |||||
AddMock(entity_id, IID_Ownership, { | |||||
"GetOwner": () => 1, | |||||
}); | |||||
AddMock(entity_id, IID_Visual, { | |||||
"GetActorSeed": () => 1, | |||||
}); | |||||
AddMock(SYSTEM_ENTITY, IID_TemplateManager, { | |||||
"GetCurrentTemplateName": () => "test", | |||||
}); | |||||
let injured_flag = false; | |||||
AddMock(SYSTEM_ENTITY, IID_RangeManager, { | |||||
"SetEntityFlag": (ent, flag, value) => injured_flag = value, | |||||
}); | |||||
//////////////////////////////////////////// | |||||
//////////////////////////////////////////// | |||||
TS_ASSERT_EQUALS(cmpHealth.GetHitpoints(), 50); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetMaxHitpoints(), 50); | |||||
TS_ASSERT_EQUALS(cmpHealth.IsInjured(), false); | |||||
let change = cmpHealth.Reduce(25); | |||||
TS_ASSERT_EQUALS(injured_flag, true); | |||||
TS_ASSERT_EQUALS(change.killed, false); | |||||
TS_ASSERT_EQUALS(change.change, -25); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetHitpoints(), 25); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetMaxHitpoints(), 50); | |||||
TS_ASSERT_EQUALS(cmpHealth.IsInjured(), true); | |||||
bbUnsubmitted Not Done Inline ActionsOne could check for unhealability maybe too bb: One could check for unhealability maybe too | |||||
change = cmpHealth.Increase(25); | |||||
TS_ASSERT_EQUALS(injured_flag, false); | |||||
TS_ASSERT_EQUALS(change.new, 50); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetHitpoints(), 50); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetMaxHitpoints(), 50); | |||||
TS_ASSERT_EQUALS(cmpHealth.IsInjured(), false); | |||||
// Check death. | |||||
const corpse_id = entity_id + 1; | |||||
let corpse_entity; | |||||
Engine.AddLocalEntity = function(template) { | |||||
corpse_entity = template; | |||||
AddMock(corpse_id, IID_Position, { | |||||
"JumpTo": () => {}, | |||||
"SetYRotation": () => {}, | |||||
"SetXZRotation": () => {}, | |||||
}); | |||||
AddMock(corpse_id, IID_Ownership, { | |||||
"SetOwner": () => {}, | |||||
}); | |||||
AddMock(corpse_id, IID_Visual, { | |||||
"SetActorSeed": () => {}, | |||||
"SelectAnimation": () => {}, | |||||
}); | |||||
return corpse_id; | |||||
}; | |||||
change = cmpHealth.Reduce(50); | |||||
bbUnsubmitted Not Done Inline ActionsOne could consider testing something >max too bb: One could consider testing something >max too | |||||
StanUnsubmitted Not Done Inline ActionsOn could also test OnValueModification(msg) and maybe Update actors by sending fake messages. Stan: On could also test OnValueModification(msg) and maybe Update actors by sending fake messages. | |||||
// Assert we create a corpse with the proper template. | |||||
TS_ASSERT_EQUALS(corpse_entity, "corpse|test"); | |||||
// Check that we are not marked as injured. | |||||
TS_ASSERT_EQUALS(injured_flag, false); | |||||
TS_ASSERT_EQUALS(change.killed, true); | |||||
TS_ASSERT_EQUALS(change.change, -50); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetHitpoints(), 0); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetMaxHitpoints(), 50); | |||||
TS_ASSERT_EQUALS(cmpHealth.IsInjured(), false); | |||||
// Check that we can't be revived once dead. | |||||
change = cmpHealth.Increase(25); | |||||
TS_ASSERT_EQUALS(change.new, 0); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetHitpoints(), 0); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetMaxHitpoints(), 50); | |||||
TS_ASSERT_EQUALS(cmpHealth.IsInjured(), false); | |||||
// Check that we can't die twice. | |||||
change = cmpHealth.Reduce(50); | |||||
TS_ASSERT_EQUALS(change.killed, false); | |||||
TS_ASSERT_EQUALS(change.change, 0); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetHitpoints(), 0); | |||||
TS_ASSERT_EQUALS(cmpHealth.GetMaxHitpoints(), 50); | |||||
TS_ASSERT_EQUALS(cmpHealth.IsInjured(), false); |
Wildfire Games · Phabricator
trailing comma