Skip to content

Commit

Permalink
LevelManager & CharacterHealth Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
llafuente committed Nov 9, 2016
1 parent 0191369 commit c14afa7
Show file tree
Hide file tree
Showing 8 changed files with 458 additions and 76 deletions.
284 changes: 284 additions & 0 deletions Assets/Editor/Test/CharacterHealthTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
using System;
using System.Collections.Generic;
using System.Threading;
using NUnit.Framework;
using UnityEngine;

using UnityPlatformer;

namespace UnityPlatformer {
[TestFixture]
[Category("Character")]
class CharacterHealthTest {
Character character;
CharacterHealth health;
PlatformerCollider2D col2d;
bool onHealCalled = false;
bool onDamageCalled = false;
bool onImmunityCalled = false;
bool onMaxHealthCalled = false;
bool onInjuredCalled = false;
bool onHurtCalled = false;
bool onDeathCalled = false;
bool onGameOverCalled = false;
bool onInvulnerabilityStartCalled = false;
bool onInvulnerabilityEndCalled = false;
bool onRespawnCalled = false;

Configuration config;
UpdateManager umgr;

void FixtureCreateHealth() {
Configuration.ClearInstance();
UpdateManager.ClearInstance();
System.GC.Collect();

var obj = new GameObject();
config = obj.AddComponent<Configuration>();
Assert.NotNull(config);
umgr = obj.AddComponent<UpdateManager>();
Assert.NotNull(umgr);


var objx = new GameObject();
character = objx.AddComponent<Character>();
Assert.NotNull(character);
health = objx.GetComponent<CharacterHealth>();
health.OnEnable();
Assert.NotNull(health);
col2d = objx.GetComponent<PlatformerCollider2D>();
Assert.NotNull(col2d);

health.onHeal += () => { onHealCalled = true; };
health.onDamage += () => { onDamageCalled = true; };
health.onImmunity += () => { onImmunityCalled = true; };
health.onMaxHealth += () => { onMaxHealthCalled = true; };
health.onInjured += (Damage dt, CharacterHealth to) => { onInjuredCalled = true; };
health.onHurt += (Damage dt, CharacterHealth to) => { onHurtCalled = true; };
health.onDeath += () => { onDeathCalled = true; };
health.onGameOver += () => { onGameOverCalled = true; };
health.onInvulnerabilityStart += () => { onInvulnerabilityStartCalled = true; };
health.onInvulnerabilityEnd += () => { onInvulnerabilityEndCalled = true; };
health.onRespawn += () => { onRespawnCalled = true; };

ResetCallbacks();
}

void ResetCallbacks() {
onHealCalled = false;
onDamageCalled = false;
onImmunityCalled = false;
onMaxHealthCalled = false;
onInjuredCalled = false;
onHurtCalled = false;
onDeathCalled = false;
onGameOverCalled = false;
onInvulnerabilityStartCalled = false;
onInvulnerabilityEndCalled = false;
onRespawnCalled = false;
}

[Test]
public void DamageTest() {
FixtureCreateHealth();

health.startingHealth = 2;
health.maxHealth = 2;

health.Start();
Assert.That(health.lives, Is.EqualTo(1));
Assert.That(health.health, Is.EqualTo(2));
Assert.That(health.IsInvulnerable(), Is.EqualTo(false));

Assert.That(onHealCalled, Is.EqualTo(true));
Assert.That(onDamageCalled, Is.EqualTo(false));
Assert.That(onImmunityCalled, Is.EqualTo(false));
Assert.That(onMaxHealthCalled, Is.EqualTo(true));
Assert.That(onInjuredCalled, Is.EqualTo(false));
Assert.That(onHurtCalled, Is.EqualTo(false));
Assert.That(onDeathCalled, Is.EqualTo(false));
Assert.That(onGameOverCalled, Is.EqualTo(false));
Assert.That(onInvulnerabilityStartCalled, Is.EqualTo(false));
Assert.That(onRespawnCalled, Is.EqualTo(false));

ResetCallbacks();

health.Damage(1, null);

Assert.That(onHealCalled, Is.EqualTo(false));
Assert.That(onDamageCalled, Is.EqualTo(true));
Assert.That(onImmunityCalled, Is.EqualTo(false));
Assert.That(onMaxHealthCalled, Is.EqualTo(false));
Assert.That(onInjuredCalled, Is.EqualTo(true));
Assert.That(onHurtCalled, Is.EqualTo(false));
Assert.That(onDeathCalled, Is.EqualTo(false));
Assert.That(onGameOverCalled, Is.EqualTo(false));
Assert.That(onInvulnerabilityStartCalled, Is.EqualTo(true));
Assert.That(onRespawnCalled, Is.EqualTo(false));

// after recieve damage time to be invulnerable
Assert.That(health.IsInvulnerable(), Is.EqualTo(true));

ResetCallbacks();

// TEST add time, wait until invulnerability ends
health.PlatformerUpdate(0.25f);
Assert.That(health.IsInvulnerable(), Is.EqualTo(true));
health.PlatformerUpdate(2);
Assert.That(health.IsInvulnerable(), Is.EqualTo(false));
Assert.That(onInvulnerabilityEndCalled, Is.EqualTo(true));

ResetCallbacks();

health.immunity = DamageType.Water;

// TEST invulnerable to recieved damage
var obj3 = new GameObject();
var damage = obj3.AddComponent<Damage>();
var character2 = obj3.AddComponent<Character>();
Assert.NotNull(damage);
damage.type = DamageType.Water;
damage.causer = character2.GetComponent<CharacterHealth>();

health.Damage(damage);
Assert.That(onHealCalled, Is.EqualTo(false));
Assert.That(onDamageCalled, Is.EqualTo(true));
Assert.That(onImmunityCalled, Is.EqualTo(true));
Assert.That(onMaxHealthCalled, Is.EqualTo(false));
Assert.That(onInjuredCalled, Is.EqualTo(false));
Assert.That(onHurtCalled, Is.EqualTo(false));
Assert.That(onDeathCalled, Is.EqualTo(false));
Assert.That(onGameOverCalled, Is.EqualTo(false));
Assert.That(onInvulnerabilityStartCalled, Is.EqualTo(false));
Assert.That(onRespawnCalled, Is.EqualTo(false));

ResetCallbacks();
/// damage again, kill character

health.Damage(1, null);
Assert.That(onHealCalled, Is.EqualTo(false));
Assert.That(onDamageCalled, Is.EqualTo(true));
Assert.That(onImmunityCalled, Is.EqualTo(false));
Assert.That(onMaxHealthCalled, Is.EqualTo(false));
Assert.That(onInjuredCalled, Is.EqualTo(true));
Assert.That(onHurtCalled, Is.EqualTo(false));
Assert.That(onDeathCalled, Is.EqualTo(true));
Assert.That(onGameOverCalled, Is.EqualTo(true));
Assert.That(onInvulnerabilityStartCalled, Is.EqualTo(false));
Assert.That(onRespawnCalled, Is.EqualTo(false));
}

[Test]
public void DamageUpdateManagerKillTest() {
FixtureCreateHealth();

health.startingLives = 2;
health.maxLives = 2;
health.startingHealth = 2;
health.maxHealth = 2;

health.Start();
Assert.That(health.lives, Is.EqualTo(2));
Assert.That(health.health, Is.EqualTo(2));
Assert.That(health.IsInvulnerable(), Is.EqualTo(false));

Assert.That(onHealCalled, Is.EqualTo(true));
Assert.That(onDamageCalled, Is.EqualTo(false));
Assert.That(onImmunityCalled, Is.EqualTo(false));
Assert.That(onMaxHealthCalled, Is.EqualTo(true));
Assert.That(onInjuredCalled, Is.EqualTo(false));
Assert.That(onHurtCalled, Is.EqualTo(false));
Assert.That(onDeathCalled, Is.EqualTo(false));
Assert.That(onGameOverCalled, Is.EqualTo(false));
Assert.That(onInvulnerabilityStartCalled, Is.EqualTo(false));
Assert.That(onRespawnCalled, Is.EqualTo(false));

ResetCallbacks();

health.Damage(1, null);

Assert.That(onHealCalled, Is.EqualTo(false));
Assert.That(onDamageCalled, Is.EqualTo(true));
Assert.That(onImmunityCalled, Is.EqualTo(false));
Assert.That(onMaxHealthCalled, Is.EqualTo(false));
Assert.That(onInjuredCalled, Is.EqualTo(true));
Assert.That(onHurtCalled, Is.EqualTo(false));
Assert.That(onDeathCalled, Is.EqualTo(false));
Assert.That(onGameOverCalled, Is.EqualTo(false));
Assert.That(onInvulnerabilityStartCalled, Is.EqualTo(true));
Assert.That(onRespawnCalled, Is.EqualTo(false));

// after recieve damage time to be invulnerable
Assert.That(health.IsInvulnerable(), Is.EqualTo(true));

ResetCallbacks();

Time.fixedDeltaTime = 0.25f;
// TEST add time, wait until invulnerability ends
umgr.FixedUpdate();
Assert.That(health.IsInvulnerable(), Is.EqualTo(true));
Time.fixedDeltaTime = 2f;
umgr.FixedUpdate();
Assert.That(health.IsInvulnerable(), Is.EqualTo(false));
Assert.That(onInvulnerabilityEndCalled, Is.EqualTo(true));

ResetCallbacks();

health.immunity = DamageType.Water;

// TEST invulnerable to recieved damage
var obj3 = new GameObject();
var damage = obj3.AddComponent<Damage>();
var character2 = obj3.AddComponent<Character>();
Assert.NotNull(damage);
damage.type = DamageType.Water;
damage.causer = character2.GetComponent<CharacterHealth>();

health.Damage(damage);
Assert.That(onHealCalled, Is.EqualTo(false));
Assert.That(onDamageCalled, Is.EqualTo(true));
Assert.That(onImmunityCalled, Is.EqualTo(true));
Assert.That(onMaxHealthCalled, Is.EqualTo(false));
Assert.That(onInjuredCalled, Is.EqualTo(false));
Assert.That(onHurtCalled, Is.EqualTo(false));
Assert.That(onDeathCalled, Is.EqualTo(false));
Assert.That(onGameOverCalled, Is.EqualTo(false));
Assert.That(onInvulnerabilityStartCalled, Is.EqualTo(false));
Assert.That(onRespawnCalled, Is.EqualTo(false));

ResetCallbacks();

/// damage again, kill character

health.Damage(1, null);
Assert.That(onHealCalled, Is.EqualTo(true));
Assert.That(onDamageCalled, Is.EqualTo(true));
Assert.That(onImmunityCalled, Is.EqualTo(false));
Assert.That(onMaxHealthCalled, Is.EqualTo(true));
Assert.That(onInjuredCalled, Is.EqualTo(true));
Assert.That(onHurtCalled, Is.EqualTo(false));
Assert.That(onDeathCalled, Is.EqualTo(true));
Assert.That(onGameOverCalled, Is.EqualTo(false));
Assert.That(onInvulnerabilityStartCalled, Is.EqualTo(false));
Assert.That(onRespawnCalled, Is.EqualTo(true));

ResetCallbacks();

Assert.That(health.lives, Is.EqualTo(1));
Assert.That(health.health, Is.EqualTo(2));

// kill it!
health.Kill();
Assert.That(onHealCalled, Is.EqualTo(false));
Assert.That(onDamageCalled, Is.EqualTo(false));
Assert.That(onImmunityCalled, Is.EqualTo(false));
Assert.That(onMaxHealthCalled, Is.EqualTo(false));
Assert.That(onInjuredCalled, Is.EqualTo(false));
Assert.That(onHurtCalled, Is.EqualTo(false));
Assert.That(onDeathCalled, Is.EqualTo(true));
Assert.That(onGameOverCalled, Is.EqualTo(true));
Assert.That(onInvulnerabilityStartCalled, Is.EqualTo(false));
Assert.That(onRespawnCalled, Is.EqualTo(false));
}
}
}
12 changes: 12 additions & 0 deletions Assets/Editor/Test/CharacterHealthTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 38 additions & 33 deletions Assets/Editor/Test/LevelManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,53 @@

using UnityPlatformer;

[TestFixture]
[Category("LevelManager")]
class LevelManagerTest {
[Test]
void CreateLevelManagerTest() {
var obj = new GameObject();
var lmgr = obj.AddComponent<LevelManager>();
Assert.NotNull(lmgr);
namespace UnityPlatformer {
[TestFixture]
[Category("LevelManager")]
class LevelManagerTest {
[Test]
public void CreateLevelManagerTest() {
var obj = new GameObject();
var lmgr = obj.AddComponent<LevelManager>();
Assert.NotNull(lmgr);

lmgr.levels = new LevelData[3];
// 0 - main menu
lmgr.levels[0].unlock = new int[1];
lmgr.levels[0].unlock[0] = 1;
lmgr.levels[0].startLocked = false;
lmgr.levels[0].isMenu = true;
lmgr.levels = new LevelData[3];
// 0 - main menu
lmgr.levels[0] = new LevelData();
lmgr.levels[0].unlock = new int[1];
lmgr.levels[0].unlock[0] = 1;
lmgr.levels[0].startLocked = false;
lmgr.levels[0].isMenu = true;

// 1-1 - level
lmgr.levels[1].unlock = new int[1];
lmgr.levels[1].unlock[0] = 2;
lmgr.levels[1].startLocked = true;
// 1-1 - level
lmgr.levels[1] = new LevelData();
lmgr.levels[1].unlock = new int[1];
lmgr.levels[1].unlock[0] = 2;
lmgr.levels[1].startLocked = true;

// 1-2 - level
lmgr.levels[2].unlock = null;
lmgr.levels[2].startLocked = true;
// 1-2 - level
lmgr.levels[2] = new LevelData();
lmgr.levels[2].unlock = null;
lmgr.levels[2].startLocked = true;

lmgr.Start(); // startLocked -> locked
Assert.That(lmgr.levels[0].locked, Is.EqualTo(false));
Assert.That(lmgr.levels[1].locked, Is.EqualTo(true));
Assert.That(lmgr.levels[2].locked, Is.EqualTo(true));
lmgr.Start(); // startLocked -> locked
Assert.That(lmgr.levels[0].locked, Is.EqualTo(false));
Assert.That(lmgr.levels[1].locked, Is.EqualTo(true));
Assert.That(lmgr.levels[2].locked, Is.EqualTo(true));


Assert.That(lmgr.currentLevel.sceneId, Is.EqualTo(0));
Assert.That(lmgr.currentLevel.sceneId, Is.EqualTo(0));

lmgr.LevelCleared(); // nice hack to start :)
Assert.That(lmgr.currentLevel.sceneId, Is.EqualTo(1));
Assert.That(lmgr.currentLevel.locked, Is.EqualTo(false));
lmgr.LevelCleared(); // nice hack to start :)
Assert.That(lmgr.currentLevel.sceneId, Is.EqualTo(1));
Assert.That(lmgr.currentLevel.locked, Is.EqualTo(false));

lmgr.LevelCleared();
Assert.That(lmgr.currentLevel.sceneId, Is.EqualTo(2));
Assert.That(lmgr.currentLevel.locked, Is.EqualTo(false));
lmgr.LevelCleared();
Assert.That(lmgr.currentLevel.sceneId, Is.EqualTo(2));
Assert.That(lmgr.currentLevel.locked, Is.EqualTo(false));

Assert.Pass();
Assert.Pass();
}
}
}
/*
Expand Down
Loading

0 comments on commit c14afa7

Please sign in to comment.