From 6a4d9f3aa6b7c2320c81cb6b326dd81da356655b Mon Sep 17 00:00:00 2001 From: Lukas Rapp Date: Tue, 14 Jun 2022 13:40:22 +0200 Subject: [PATCH] Added some more WaveManager tests --- Assets/Scripts/WaveManager.cs | 9 ++-- .../Tests/Editmode/MinionConstellationTest.cs | 28 +++++++++++ .../Editmode/MinionConstellationTest.cs.meta | 11 +++++ ...eManagerTest.cs => ParameterGrowthTest.cs} | 21 +-------- ...st.cs.meta => ParameterGrowthTest.cs.meta} | 0 Assets/Tests/Editmode/ReportDeathTest.cs | 47 +++++++++++++++++++ Assets/Tests/Editmode/ReportDeathTest.cs.meta | 11 +++++ Assets/Tests/Editmode/SpawnRobotTest.cs | 33 +++++++++++++ Assets/Tests/Editmode/SpawnRobotTest.cs.meta | 11 +++++ 9 files changed, 148 insertions(+), 23 deletions(-) create mode 100644 Assets/Tests/Editmode/MinionConstellationTest.cs create mode 100644 Assets/Tests/Editmode/MinionConstellationTest.cs.meta rename Assets/Tests/Editmode/{WaveManagerTest.cs => ParameterGrowthTest.cs} (66%) rename Assets/Tests/Editmode/{WaveManagerTest.cs.meta => ParameterGrowthTest.cs.meta} (100%) create mode 100644 Assets/Tests/Editmode/ReportDeathTest.cs create mode 100644 Assets/Tests/Editmode/ReportDeathTest.cs.meta create mode 100644 Assets/Tests/Editmode/SpawnRobotTest.cs create mode 100644 Assets/Tests/Editmode/SpawnRobotTest.cs.meta diff --git a/Assets/Scripts/WaveManager.cs b/Assets/Scripts/WaveManager.cs index aadf51d..604477d 100644 --- a/Assets/Scripts/WaveManager.cs +++ b/Assets/Scripts/WaveManager.cs @@ -29,7 +29,7 @@ public class WaveManager : MonoBehaviour public float cooldown; public float duration; public float strength; - private List minions; + public List minions; public void Start() { @@ -56,7 +56,6 @@ IEnumerator Manage() //before proceeding to the next wave until they are all dead. if (minions.Count > 0) { - // Debug.Log("There are still minions."); yield return new WaitUntil(() => (minions.Count == 0)); } @@ -124,7 +123,7 @@ public void ReportDeath(GameObject entity) // Remove the entity and drop reward GenerateDrop(entity.transform.position); - Destroy(entity); + DestroyImmediate(entity); minions.Remove(entity); } } @@ -139,7 +138,7 @@ private void GenerateDrop(Vector3 position) } } - void Spawn(Attacker attacker, Vector3 center) + public void Spawn(Attacker attacker, Vector3 center) { //Generate normalized vector from random angle and stretch it to match specified range. float angle = UnityEngine.Random.Range(0F, (float)(Math.PI * 2)); @@ -192,8 +191,10 @@ public List> GenerateSpawnPattern() float quota = strength; //Debug.Log("Quota: " + quota); + Debug.Log("a"); List> output = new List>(); while (quota > 0) { + Debug.Log("Quota: " + quota); Attacker selected = options[pivot]; float baseStrength = selected.GetStrength(); float scaledStrength = baseStrength * selected.GetStrengthScale(); diff --git a/Assets/Tests/Editmode/MinionConstellationTest.cs b/Assets/Tests/Editmode/MinionConstellationTest.cs new file mode 100644 index 0000000..e2a5c0c --- /dev/null +++ b/Assets/Tests/Editmode/MinionConstellationTest.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using NUnit.Framework; +using Robot; +using UnityEngine; +using UnityEngine.TestTools; + +public class MinionConstellationTest +{ + [Test] + public void TestMinionConstellation() + { + var manager = GameObject + .FindGameObjectsWithTag("WaveManager")[0] + .GetComponent(); + manager.Start(); + + List> pattern = manager.GenerateSpawnPattern(); + int firstRobotCount = 0; + foreach (Tuple entry in pattern) { + Assert.IsTrue(entry.Item2.strength == 10); + firstRobotCount++; + } + + Assert.IsTrue(firstRobotCount == 10); + } +} diff --git a/Assets/Tests/Editmode/MinionConstellationTest.cs.meta b/Assets/Tests/Editmode/MinionConstellationTest.cs.meta new file mode 100644 index 0000000..dbac00f --- /dev/null +++ b/Assets/Tests/Editmode/MinionConstellationTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a67187c0e842fb2e18b4556d7c6403a1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tests/Editmode/WaveManagerTest.cs b/Assets/Tests/Editmode/ParameterGrowthTest.cs similarity index 66% rename from Assets/Tests/Editmode/WaveManagerTest.cs rename to Assets/Tests/Editmode/ParameterGrowthTest.cs index 612c440..d454e18 100644 --- a/Assets/Tests/Editmode/WaveManagerTest.cs +++ b/Assets/Tests/Editmode/ParameterGrowthTest.cs @@ -9,26 +9,8 @@ using UnityEngine.TestTools; using UnityEngine.UIElements; -public class WaveManagerTest +public class ParameterGrowthTest { - [Test] - public void TestMinionConstellation() - { - var manager = GameObject - .FindGameObjectsWithTag("WaveManager")[0] - .GetComponent(); - manager.Start(); - - List> pattern = manager.GenerateSpawnPattern(); - int firstRobotCount = 0; - foreach (Tuple entry in pattern) { - Assert.IsTrue(entry.Item2.strength == 10); - firstRobotCount++; - } - - Assert.IsTrue(firstRobotCount == 10); - } - [Test] public void TestParameterGrowth() { @@ -42,6 +24,7 @@ public void TestParameterGrowth() manager.cooldown = 1F; manager.duration = 5F; manager.strength = 100F; + manager.wave = 1; for (int i = 0; i < 3; i++) { Assert.IsTrue(Mathf.Abs(manager.cooldown - cooldowns[i]) < eta); diff --git a/Assets/Tests/Editmode/WaveManagerTest.cs.meta b/Assets/Tests/Editmode/ParameterGrowthTest.cs.meta similarity index 100% rename from Assets/Tests/Editmode/WaveManagerTest.cs.meta rename to Assets/Tests/Editmode/ParameterGrowthTest.cs.meta diff --git a/Assets/Tests/Editmode/ReportDeathTest.cs b/Assets/Tests/Editmode/ReportDeathTest.cs new file mode 100644 index 0000000..7b9f4e4 --- /dev/null +++ b/Assets/Tests/Editmode/ReportDeathTest.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using NUnit.Framework; +using Robot; +using UnityEngine; +using UnityEngine.TestTools; + +public class ReportDeathTest +{ + [Test] + public void TestDeathAlreadyReported() + { + Statics.Initialize(); + var manager = GameObject + .FindGameObjectsWithTag("WaveManager")[0] + .GetComponent(); + manager.Start(); + + // Simulate spawning + var spawnVector = new Vector3(0F, 0F, 0F); + var firstAttacker = manager.attackers[0]; + manager.Spawn(firstAttacker, spawnVector); + + // Take first minion + var firstMinion = manager.minions[0]; + var strength = (int) firstMinion.GetComponent().GetStrength(); + + // Report death first time + var sizeBefore = manager.minions.Count; + manager.ReportDeath(firstMinion); + var currentSize = manager.minions.Count; + + // Assert functioning first report + Assert.IsTrue(currentSize < sizeBefore); + Assert.IsTrue(Statics.GetScoreModel().GetScore() == strength); + + // Report death as second time (should be ignored) + sizeBefore = currentSize; + manager.ReportDeath(firstMinion); + currentSize = manager.minions.Count; + + // Assert ignored death report + Assert.IsTrue(currentSize == sizeBefore); + Assert.IsTrue(Statics.GetScoreModel().GetScore() == strength); + } +} diff --git a/Assets/Tests/Editmode/ReportDeathTest.cs.meta b/Assets/Tests/Editmode/ReportDeathTest.cs.meta new file mode 100644 index 0000000..df821a6 --- /dev/null +++ b/Assets/Tests/Editmode/ReportDeathTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2401cb5f704d72e5da7f3df17888c658 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tests/Editmode/SpawnRobotTest.cs b/Assets/Tests/Editmode/SpawnRobotTest.cs new file mode 100644 index 0000000..13e401a --- /dev/null +++ b/Assets/Tests/Editmode/SpawnRobotTest.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using NUnit.Framework; +using Robot; +using UnityEngine; +using UnityEngine.TestTools; + +public class SpawnRobotTest +{ + [Test] + public void TestSpawnRobot() + { + var manager = GameObject + .FindGameObjectsWithTag("WaveManager")[0] + .GetComponent(); + manager.Start(); + + // Spawn attacker + var spawnVector = new Vector3(0F, 0F, 0F); + var firstAttacker = manager.attackers[0]; + manager.Spawn(firstAttacker, spawnVector); + + // Retrieve instance + var attackerInstance = manager.minions[0]; + var difVector = attackerInstance.transform.position - spawnVector; + var distance = difVector.magnitude; + + // Check if in valid range + Assert.IsTrue(distance >= WaveManager.MIN_SPAWN_RADIUS && + distance <= WaveManager.MAX_SPAWN_RADIUS); + } +} diff --git a/Assets/Tests/Editmode/SpawnRobotTest.cs.meta b/Assets/Tests/Editmode/SpawnRobotTest.cs.meta new file mode 100644 index 0000000..139e7d6 --- /dev/null +++ b/Assets/Tests/Editmode/SpawnRobotTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 694249134ab0ba041ac1ed2f4316ecae +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: