This commit is contained in:
2025-01-17 13:10:42 +01:00
commit 4536213c91
15115 changed files with 1442174 additions and 0 deletions

View File

@@ -0,0 +1,266 @@
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
public class AsteroidTests
{
GameObject asteroidPrefab;
[SetUp]
public void Setup()
{
GameManager.InitializeTestingEnvironment(false, false, false, false, false);
asteroidPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().asteroidPrefab;
}
void ClearScene()
{
Transform[] objects = Object.FindObjectsByType<Transform>(FindObjectsSortMode.None);
foreach (Transform obj in objects)
{
if (obj != null)
Object.DestroyImmediate(obj.gameObject);
}
}
[Test]
public void _01_AsteroidPrefabExists()
{
Assert.NotNull(asteroidPrefab);
}
[Test]
public void _02_AsteroidPrefabCanBeInstantiated()
{
ClearScene();
GameObject asteroid = (GameObject)Object.Instantiate(asteroidPrefab);
asteroid.name = "Asteroid";
Assert.NotNull(GameObject.Find("Asteroid"));
}
[Test]
public void _03_AsteroidPrefabHasRequiredComponentTransform()
{
Assert.IsNotNull(asteroidPrefab.GetComponent<Transform>());
}
[Test]
public void _04_AsteroidPrefabHasRequiredComponentCollider()
{
Assert.IsNotNull(asteroidPrefab.GetComponent<CircleCollider2D>());
}
[Test]
public void _05_AsteroidPrefabHasRequiredComponentControllerScript()
{
Assert.IsNotNull(asteroidPrefab.GetComponent<AsteroidController>());
Assert.IsNotNull(asteroidPrefab.GetComponent<AsteroidController>().asteroidExplosion); // Checking if script component has required references
}
[UnityTest]
public IEnumerator _06_AsteroidGameobjectIsDestroyedOnSplit()
{
ClearScene();
GameObject asteroid = (GameObject)Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
asteroid.GetComponent<AsteroidController>().Split();
yield return null;
Assert.IsTrue(asteroid == null, "Base asteroid was not destroyed on Split");
}
[Test]
public void _07_AsteroidSplitCountCanBeChanged()
{
ClearScene();
AsteroidController asteroid = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity).GetComponent<AsteroidController>();
asteroid.SetSplitCount(2);
Assert.IsTrue(asteroid.GetSplitCount() == 2);
}
[UnityTest]
public IEnumerator _08_AsteroidCanSplitIntoTwo()
{
ClearScene();
GameObject asteroid = (GameObject)Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
asteroid.GetComponent<AsteroidController>().Split(); // Split base asteroid
yield return null;
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None); // Find all asteroids in the scene
Assert.IsTrue(asteroids.Length == 2); // There should be 2 asteroids in the scene now
}
[UnityTest]
public IEnumerator _09_AsteroidCanSplitTwice()
{
ClearScene();
GameObject asteroid = (GameObject)Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
asteroid.GetComponent<AsteroidController>().Split(); // Split base asteroid
yield return null;
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None); // Find all asteroids in the scene
foreach(AsteroidController childAsteroid in asteroids)
childAsteroid.Split(); // Split found asteroids
yield return null;
asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None); // Find all asteroids in the scene
Assert.IsTrue(asteroids.Length == 4); // There should be 4 asteroids in the scene now
}
[UnityTest]
// It takes three hits to destroy an asteroid from base size, after 3 hits the asteroid should not split anymore
public IEnumerator _10_AsteroidCannotSplitThreeTimes()
{
ClearScene();
GameObject asteroid = (GameObject)Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
asteroid.GetComponent<AsteroidController>().Split(); // Split base asteroid
yield return null;
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None); // Find all asteroids in the scene
foreach (AsteroidController childAsteroid in asteroids)
childAsteroid.Split(); // Split found asteroids
yield return null;
asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None); // Find all asteroids in the scene
foreach (AsteroidController childAsteroid in asteroids)
childAsteroid.Split(); // Split found asteroids
yield return null;
asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None); // Find all asteroids in the scene
Assert.IsTrue(asteroids.Length == 0); // There should be no asteroids left in the scene
}
[UnityTest]
// Splitting the asteroid should spawn 2 asteroids at half scale of the split asteroid
public IEnumerator _11_AsteroidScaleIsCutInHalfOnSplit()
{
ClearScene();
GameObject asteroid = (GameObject)Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
asteroid.GetComponent<AsteroidController>().Split(); // Split base asteroid
yield return null;
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None); // Find all asteroids in the scene
foreach (AsteroidController childAsteroid in asteroids)
Assert.IsTrue(childAsteroid.transform.localScale == new Vector3(0.5f, 0.5f, 0.5f));
}
[Test]
public void _12_AsteroidCanMove()
{
ClearScene();
AsteroidController asteroid = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity).GetComponent<AsteroidController>();
asteroid.Move();
Assert.IsTrue(asteroid.transform.position != Vector3.zero);
}
[Test]
public void _13_AsteroidDirectionCanBeChanged()
{
ClearScene();
AsteroidController asteroid = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity).GetComponent<AsteroidController>();
asteroid.SetDirection(Vector2.up);
Assert.IsTrue(asteroid.GetDirection() == Vector2.up);
}
[UnityTest]
public IEnumerator _14_AsteroidMovesAccordingToItsDirectionVector()
{
ClearScene();
AsteroidController asteroid = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity).GetComponent<AsteroidController>();
asteroid.SetDirection(Vector2.up);
Assert.IsTrue(asteroid.GetDirection() == Vector2.up);
float t = 0.5f;
while (t > 0.0f)
{
t -= Time.deltaTime;
yield return null;
}
Assert.IsTrue(asteroid.transform.position.x == 0.0f && asteroid.transform.position.y > 0.0f);
}
[UnityTest]
public IEnumerator _15_AsteroidIsDestroyedWhenOffscreen()
{
ClearScene();
AsteroidController asteroid = Object.Instantiate(asteroidPrefab, Vector3.right * 100, Quaternion.identity).GetComponent<AsteroidController>();
yield return null;
Assert.IsTrue(asteroid == null, "Asteroid was not destroyed when off screen");
}
[Test]
public void _16_AsteroidPrefabHasRequiredVisual()
{
Transform visualChild = asteroidPrefab.transform.GetChild(0);
Assert.IsTrue(visualChild.name == "Visual");
Assert.IsNotNull(visualChild);
Assert.IsNotNull(visualChild.GetComponent<MeshRenderer>());
Assert.IsNotNull(visualChild.GetComponent<MeshRenderer>().sharedMaterials[0]);
Assert.IsNotNull(visualChild.GetComponent<MeshFilter>());
Assert.IsNotNull(visualChild.GetComponent<MeshFilter>().sharedMesh);
}
[Test]
public void _17_AsteroidPrefabHasRequiredComponentRigidbody()
{
Assert.IsNotNull(asteroidPrefab.GetComponent<Rigidbody2D>());
Assert.IsTrue(asteroidPrefab.GetComponent<Rigidbody2D>().isKinematic);
Assert.IsTrue(asteroidPrefab.GetComponent<Rigidbody2D>().collisionDetectionMode == CollisionDetectionMode2D.Continuous);
Assert.IsTrue(asteroidPrefab.GetComponent<Rigidbody2D>().interpolation == RigidbodyInterpolation2D.Interpolate);
}
[UnityTest]
public IEnumerator _18_AsteroidStartsWithARandomRotation()
{
ClearScene();
AsteroidController asteroid1 = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity).GetComponent<AsteroidController>();
AsteroidController asteroid2 = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity).GetComponent<AsteroidController>();
AsteroidController asteroid3 = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity).GetComponent<AsteroidController>();
yield return null;
Assert.IsTrue(asteroid1.transform.rotation != asteroid2.transform.rotation && asteroid1.transform.rotation != asteroid3.transform.rotation && asteroid2.transform.rotation != asteroid3.transform.rotation);
}
[UnityTest]
public IEnumerator _19_AsteroidSpawnsExplosionWhenDestroyed()
{
ClearScene();
GameObject asteroid = (GameObject)Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
asteroid.GetComponent<AsteroidController>().Split();
yield return null;
Assert.IsTrue(asteroid == null);
GameObject explosion = GameObject.Find("ExplosionRegular(Clone)");
Assert.NotNull(explosion);
}
[UnityTest]
public IEnumerator _20_AsteroidDoesntMoveDuringPause()
{
ClearScene();
AsteroidController asteroid = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity).GetComponent<AsteroidController>();
asteroid.SetDirection(Vector2.up);
Vector3 startPosition = asteroid.transform.position;
GameManager.IsPaused = true;
for(int i = 0; i < 20; i++)
yield return null;
Assert.IsTrue(asteroid.transform.position == startPosition);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6ab6f846b4ad547d6b568e2c987b74c7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
{
"name": "Unity.TestTools.CodeCoverage.Sample.Asteroids.Tests",
"references": [
"Unity.TestTools.CodeCoverage.Sample.Asteroids",
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d397668ee21414d44b387471611d12a8
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,64 @@
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
#endif
public class CameraTests
{
GameObject cameraPrefab;
LoadSceneParameters loadSceneParameters;
#if UNITY_EDITOR
string asteroidsScenePath;
#endif
[SetUp]
public void Setup()
{
GameManager.InitializeTestingEnvironment(true, true, true, false, false);
loadSceneParameters = new LoadSceneParameters(LoadSceneMode.Single, LocalPhysicsMode.None);
Object asteroidsScene = ((GameObject)Resources.Load("TestsReferences")).GetComponent<TestsReferences>().asteroidsScene;
#if UNITY_EDITOR
asteroidsScenePath = AssetDatabase.GetAssetPath(asteroidsScene);
#endif
cameraPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().cameraPrefab;
}
[Test]
public void _01_CameraPrefabExists()
{
Assert.NotNull(cameraPrefab);
}
[Test]
public void _02_CameraPrefabHasRequiredComponents()
{
Assert.IsTrue(cameraPrefab.GetComponent<Camera>().clearFlags == CameraClearFlags.Skybox);
Assert.IsTrue(cameraPrefab.GetComponent<Camera>().orthographic);
}
[UnityTest]
public IEnumerator _03_CameraExistsInScene()
{
#if UNITY_EDITOR
EditorSceneManager.LoadSceneInPlayMode(asteroidsScenePath, loadSceneParameters);
yield return null;
Assert.IsTrue(Object.FindAnyObjectByType<Camera>().name == "Camera");
#else
yield return null;
Assert.Pass();
#endif
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8950a0381a02c27488d31f5dba9570c7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,105 @@
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
public class FXTests
{
GameObject spaceshipPrefab;
GameObject asteroidPrefab;
GameObject spaceshipDebrisPrefab;
GameObject explosionPrefab;
[SetUp]
public void Setup()
{
GameManager.InitializeTestingEnvironment(false, false, true, false, false);
spaceshipPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().spaceshipPrefab;
asteroidPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().asteroidPrefab;
spaceshipDebrisPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().spaceshipDebrisPrefab;
explosionPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().explosionPrefab;
}
void ClearScene()
{
Transform[] objects = Object.FindObjectsByType<Transform>(FindObjectsSortMode.None);
foreach (Transform obj in objects)
{
if (obj != null)
Object.DestroyImmediate(obj.gameObject);
}
}
[Test]
public void _01_FXPrefabsExist()
{
Assert.NotNull(spaceshipDebrisPrefab);
Assert.NotNull(spaceshipDebrisPrefab.GetComponent<DebrisController>());
Assert.NotNull(explosionPrefab);
}
[UnityTest]
public IEnumerator _02_DebrisFragmentsHaveVelocityOnStart()
{
ClearScene();
GameObject debris = Object.Instantiate(spaceshipDebrisPrefab, Vector3.zero, Quaternion.identity);
yield return null;
foreach(Rigidbody2D fragment in debris.GetComponentsInChildren<Rigidbody2D>())
{
Assert.IsTrue(fragment.velocity != Vector2.zero);
}
}
[UnityTest]
public IEnumerator _03_DebrisFragmentsAreDestroyedAfterSeconds()
{
ClearScene();
GameObject debris = Object.Instantiate(spaceshipDebrisPrefab, Vector3.zero, Quaternion.identity);
yield return new WaitForSeconds(1.0f); // Debris should be destroyed after 1 sec
Assert.IsTrue(debris == null);
Assert.IsTrue(Object.FindObjectsByType<Rigidbody2D>(FindObjectsSortMode.None).Length == 0);
}
[UnityTest]
public IEnumerator _04_ExplosionParticlesAreSpawnedWithDebris()
{
ClearScene();
GameObject debris = Object.Instantiate(spaceshipDebrisPrefab, Vector3.zero, Quaternion.identity);
yield return null;
ParticleSystem explosion = Object.FindAnyObjectByType<ParticleSystem>();
Assert.IsTrue(explosion != null);
yield return new WaitForSeconds(explosion.main.duration);
Assert.IsTrue(explosion == null);
}
[UnityTest]
public IEnumerator _05_ExplosionParticlesActivateChildParticlesAndSubEmmitersSuccessfully()
{
ClearScene();
ParticleSystem explosion = Object.Instantiate(spaceshipDebrisPrefab.GetComponent<DebrisController>().explosionParticles, Vector3.zero, Quaternion.identity).GetComponent<ParticleSystem>();
Assert.IsTrue(explosion != null);
Assert.IsTrue(explosion.transform.GetChild(0).GetComponent<ParticleSystem>() != null);
Assert.IsTrue(explosion.transform.GetChild(1).GetComponent<ParticleSystem>() != null);
Assert.IsTrue(explosion.transform.GetChild(2).GetComponent<ParticleSystem>() != null);
yield return null;
Assert.IsTrue(explosion.particleCount == 50); // Main explosion PS burst emits 50 particles on the first frame
Assert.IsTrue(explosion.transform.GetChild(0).GetComponent<ParticleSystem>().particleCount == 1); // Glow emits 1 particle on the first frame
Assert.IsTrue(explosion.transform.GetChild(1).GetComponent<ParticleSystem>().particleCount == 2); // Shockwave emits 2 particles on the first frame
Assert.IsTrue(explosion.subEmitters.GetSubEmitterSystem(0) != null); // Subemitter exists, indexOutOfRange if subemitter cannot be found
yield return new WaitForSeconds(0.2f);
Assert.IsTrue(explosion.subEmitters.GetSubEmitterSystem(0).particleCount > 0); // Subemitter emits particles over time
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c134ae620ad267341a2ee7b06ea72051
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,275 @@
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
#endif
public class GameManagerTests
{
GameObject gameManagerPrefab;
GameObject asteroidPrefab;
GameObject cameraPrefab;
LoadSceneParameters loadSceneParameters;
#if UNITY_EDITOR
string asteroidsScenePath;
#endif
[SetUp]
public void Setup()
{
loadSceneParameters = new LoadSceneParameters(LoadSceneMode.Single, LocalPhysicsMode.None);
Object asteroidsScene = ((GameObject)Resources.Load("TestsReferences")).GetComponent<TestsReferences>().asteroidsScene;
#if UNITY_EDITOR
asteroidsScenePath = AssetDatabase.GetAssetPath(asteroidsScene);
#endif
gameManagerPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().gameManagerPrefab;
asteroidPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().asteroidPrefab;
cameraPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().cameraPrefab;
}
void ClearScene()
{
Transform[] objects = Object.FindObjectsByType<Transform>(FindObjectsSortMode.None);
foreach (Transform obj in objects)
{
if (obj != null)
Object.DestroyImmediate(obj.gameObject);
}
}
[Test]
public void _01_GameManagerPrefabExists()
{
Assert.NotNull(gameManagerPrefab);
}
[Test]
public void _02_GameManagerPrefabHasRequiredComponentScript()
{
Assert.IsNotNull(gameManagerPrefab.GetComponent<GameManager>());
}
[UnityTest]
public IEnumerator _03_GameManagerExistsInScene()
{
#if UNITY_EDITOR
EditorSceneManager.LoadSceneInPlayMode(asteroidsScenePath, loadSceneParameters);
yield return null;
Assert.NotNull(Object.FindAnyObjectByType<GameManager>());
#else
yield return null;
Assert.Pass();
#endif
}
[UnityTest]
public IEnumerator _04_GameManagerCanSpawnSpaceshipOnLoad()
{
ClearScene();
Object.Instantiate(gameManagerPrefab).GetComponent<GameManager>();
GameManager.InitializeTestingEnvironment(true, false, false, false, false);
yield return null;
SpaceshipController spaceship = Object.FindAnyObjectByType<SpaceshipController>();
Assert.IsTrue(spaceship != null);
}
[UnityTest]
public IEnumerator _05_GameManagerRespawnsSpaceshipAfterItHasBeenDestroyed()
{
ClearScene();
Object.Instantiate(gameManagerPrefab).GetComponent<GameManager>();
GameManager.InitializeTestingEnvironment(true, false, false, false, false);
yield return null;
GameObject asteroid = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
SpaceshipController spaceship = Object.FindAnyObjectByType<SpaceshipController>();
spaceship.transform.position = Vector2.right * 10;
asteroid.transform.position = spaceship.transform.position;
yield return new WaitForSeconds(0.5f);
Assert.IsTrue(spaceship == null);
yield return new WaitForSeconds(GameManager.SPACESHIP_RESPAWN_DELAY);
spaceship = Object.FindAnyObjectByType<SpaceshipController>();
Assert.IsTrue(spaceship != null);
}
[UnityTest]
public IEnumerator _06_GameManagerDoesNotRespawnShipAfterThreeDeaths()
{
ClearScene();
GameManager gameManager = Object.Instantiate(gameManagerPrefab).GetComponent<GameManager>();
GameManager.InitializeTestingEnvironment(true, false, false, false, false);
Object.DestroyImmediate(Object.FindAnyObjectByType<SpaceshipController>());
gameManager.deaths = 2;
gameManager.RespawnShip(0.0f);
yield return new WaitForSeconds(GameManager.SPACESHIP_RESPAWN_DELAY);
SpaceshipController spaceship = Object.FindAnyObjectByType<SpaceshipController>();
Assert.IsTrue(spaceship != null);
Object.DestroyImmediate(spaceship);
gameManager.RespawnShip(0.0f);
yield return new WaitForSeconds(GameManager.SPACESHIP_RESPAWN_DELAY);
spaceship = Object.FindAnyObjectByType<SpaceshipController>();
Assert.IsTrue(spaceship == null);
}
[UnityTest]
public IEnumerator _07_GameManagerSpawnsAsteroids()
{
ClearScene();
Object.Instantiate(gameManagerPrefab);
GameManager.InitializeTestingEnvironment(false, true, false, true, false);
yield return null;
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None);
Assert.IsTrue(asteroids.Length > 0);
}
[UnityTest]
public IEnumerator _08_GameManagerSpawnsAsteroidsOverTime()
{
ClearScene();
GameManager gameManager = Object.Instantiate(gameManagerPrefab).GetComponent<GameManager>();
GameManager.InitializeTestingEnvironment(false, true, false, true, false);
yield return new WaitForSeconds(gameManager.asteroidSpawnDelay + 0.5f);
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None);
Assert.IsTrue(asteroids.Length > 1);
}
[Test]
public void _09_GameManagerOnlySpawnsAsteroidsOffscreen()
{
ClearScene();
GameManager gameManager = Object.Instantiate(gameManagerPrefab).GetComponent<GameManager>();
GameManager.InitializeTestingEnvironment(false, false, false, false, false);
Object.Instantiate(cameraPrefab);
for (int i = 0; i < 100; i++)
gameManager.SpawnAsteroids();
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None);
foreach (AsteroidController roid in asteroids)
{
Vector2 positionOnCamera = Camera.main.WorldToViewportPoint(roid.transform.position);
Assert.IsTrue(((positionOnCamera.x >= -0.1f && positionOnCamera.x <= 1.1f) && (positionOnCamera.y <= -0.05f || positionOnCamera.y >= 1.05f)) ||
((positionOnCamera.x <= -0.05f || positionOnCamera.x >= 1.05f) && (positionOnCamera.y >= -0.1f && positionOnCamera.y <= 1.1f)));
}
}
[Test]
public void _10_GameManagerSpawnsRandomSizeAsteroids()
{
ClearScene();
GameManager gameManager = Object.Instantiate(gameManagerPrefab).GetComponent<GameManager>();
GameManager.InitializeTestingEnvironment(false, false, false, false, false);
bool small = false;
bool medium = false;
bool big = false;
for (int i = 0; i < 100; i++)
gameManager.SpawnAsteroids();
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None);
foreach (AsteroidController roid in asteroids)
{
if (roid.GetSplitCount() == 2)
small = true;
else if (roid.GetSplitCount() == 1)
medium = true;
else if (roid.GetSplitCount() == 0)
big = true;
if (small && medium && big)
break;
}
Assert.IsTrue(small && medium && big);
}
[UnityTest]
public IEnumerator _11_GameManagerScoreIsIncreasedAfterAsteroidsAreDestroyed()
{
ClearScene();
Object.Instantiate(gameManagerPrefab);
GameManager.InitializeTestingEnvironment(false, false, false, false, false);
yield return null;
AsteroidController asteroid = Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity).GetComponent<AsteroidController>();
int score = GameManager.score;
asteroid.Split();
Assert.IsTrue(score != GameManager.score);
Assert.IsTrue(GameManager.score == 1000);
yield return null;
score = GameManager.score;
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None);
foreach(AsteroidController ast in asteroids)
ast.Split();
Assert.IsTrue(score != GameManager.score);
Assert.IsTrue(GameManager.score == 2000);
yield return null;
score = GameManager.score;
asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None);
foreach (AsteroidController ast in asteroids)
ast.Split();
Assert.IsTrue(score != GameManager.score);
Assert.IsTrue(GameManager.score == 3000);
}
[UnityTest]
public IEnumerator _12_ReachingCertainScoreChangesSpaceshipWeapons()
{
ClearScene();
Object.Instantiate(gameManagerPrefab);
GameManager.InitializeTestingEnvironment(true, false, false, false, false);
yield return null;
for (int i = 0; i < 8; i++)
GameManager.AddToScore(0);
SpaceshipController spaceship = Object.FindAnyObjectByType<SpaceshipController>();
Assert.IsTrue(spaceship != null);
// Weapon changes to laser upon reaching 8000 points
Assert.IsTrue(spaceship.currentWeapon == SpaceshipController.Weapon.Laser);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d4264f527d5b24e81811c2bfca2f7a68
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,302 @@
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
public class SpaceshipTests {
GameObject spaceshipPrefab;
GameObject asteroidPrefab;
GameObject cameraPrefab;
[SetUp]
public void Setup()
{
GameManager.InitializeTestingEnvironment(false, false, true, false, false);
spaceshipPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().spaceshipPrefab;
asteroidPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().asteroidPrefab;
cameraPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().cameraPrefab;
}
void ClearScene()
{
Transform[] objects = Object.FindObjectsByType<Transform>(FindObjectsSortMode.None);
foreach (Transform obj in objects)
{
if (obj != null)
Object.DestroyImmediate(obj.gameObject);
}
}
[Test]
public void _01_SpaceshipPrefabExists() {
Assert.NotNull(spaceshipPrefab);
}
[Test]
public void _02_SpaceshipPrefabCanBeInstantiated()
{
ClearScene();
GameObject spaceship = (GameObject)Object.Instantiate(spaceshipPrefab);
spaceship.name = "Spaceship";
Assert.NotNull(GameObject.Find("Spaceship"));
}
[Test]
public void _03_SpaceshipPrefabHasRequiredComponentTransform()
{
Assert.IsNotNull(spaceshipPrefab.GetComponent<Transform>());
}
[Test]
public void _04_SpaceshipPrefabHasRequiredComponentCollider()
{
Assert.IsNotNull(spaceshipPrefab.GetComponent<PolygonCollider2D>());
}
[Test]
public void _05_SpaceshipPrefabHasRequiredComponentControllerScript()
{
Assert.IsNotNull(spaceshipPrefab.GetComponent<SpaceshipController>());
// Checking if script component has required references
Assert.IsNotNull(spaceshipPrefab.GetComponent<SpaceshipController>().spaceshipDebris);
Assert.IsNotNull(spaceshipPrefab.GetComponent<SpaceshipController>().weaponList);
}
[Test]
public void _06_SpaceshipPrefabHasRequiredVisual()
{
Transform visualChild = spaceshipPrefab.transform.GetChild(0);
Assert.IsTrue(visualChild.name == "Visual");
Assert.IsNotNull(visualChild);
Assert.IsNotNull(visualChild.GetComponent<MeshRenderer>());
Assert.IsNotNull(visualChild.GetComponent<MeshRenderer>().sharedMaterials[0]);
Assert.IsNotNull(visualChild.GetComponent<MeshRenderer>().sharedMaterials[1]);
Assert.IsNotNull(visualChild.GetComponent<MeshFilter>());
Assert.IsNotNull(visualChild.GetComponent<MeshFilter>().sharedMesh);
}
[Test]
public void _07_SpaceshipPrefabHasRequiredComponentRigidbody()
{
Assert.IsNotNull(spaceshipPrefab.GetComponent<Rigidbody2D>());
Assert.IsTrue(spaceshipPrefab.GetComponent<Rigidbody2D>().isKinematic);
Assert.IsTrue(spaceshipPrefab.GetComponent<Rigidbody2D>().collisionDetectionMode == CollisionDetectionMode2D.Continuous);
Assert.IsTrue(spaceshipPrefab.GetComponent<Rigidbody2D>().interpolation == RigidbodyInterpolation2D.Interpolate);
}
[UnityTest]
public IEnumerator _08_SpaceshipIsDestroyedOnCollisionWithAsteroid()
{
ClearScene();
GameObject spaceship = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.identity);
Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
yield return new WaitForFixedUpdate();
yield return null;
Assert.IsTrue(spaceship == null);
}
[UnityTest]
public IEnumerator _09_SpaceshipTriggersAsteroidSplit()
{
ClearScene();
Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.identity);
Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
yield return new WaitForFixedUpdate();
yield return null;
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None);
Assert.IsTrue(asteroids.Length > 1);
}
[Test]
public void _10_SpaceshipCanMove()
{
ClearScene();
SpaceshipController spaceship = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.identity).GetComponent<SpaceshipController>();
spaceship.direction = Vector2.up;
spaceship.Move();
Assert.IsTrue(spaceship.transform.position != Vector3.zero);
}
[UnityTest]
public IEnumerator _11_SpaceshipRotationCanBeChanged()
{
ClearScene();
SpaceshipController spaceship = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.identity).GetComponent<SpaceshipController>();
spaceship.transform.eulerAngles = new Vector3(0.0f, 0.0f, 180.0f);
float startingRotation = spaceship.transform.eulerAngles.z;
spaceship.Turn(1.0f); // Turn right
yield return null;
Assert.IsTrue(spaceship.transform.eulerAngles.z < startingRotation);
startingRotation = spaceship.transform.eulerAngles.z;
spaceship.Turn(-1.0f); // Turn left
yield return null;
Assert.IsTrue(spaceship.transform.eulerAngles.z > startingRotation);
}
[Test]
public void _12_SpaceshipMovesAccordingToItsDirectionVector()
{
ClearScene();
SpaceshipController spaceship = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.Euler(0.0f, 0.0f, 0.0f)).GetComponent<SpaceshipController>();
spaceship.Thrust(1.0f);
spaceship.Move();
Assert.IsTrue(spaceship.transform.position.y >= 0.0f && spaceship.transform.position.x == 0.0f);
}
[UnityTest]
public IEnumerator _13_SpaceshipIsWarpedToTheOtherSideOfTheScreenAfterMovingOffscreen()
{
ClearScene();
Object.Instantiate(cameraPrefab);
SpaceshipController spaceship = Object.Instantiate(spaceshipPrefab, Vector2.right * 100.0f, Quaternion.identity).GetComponent<SpaceshipController>();
yield return null;
Assert.IsTrue(spaceship.transform.position.x < 0.0f);
spaceship.transform.position = Vector2.left * 100.0f;
yield return null;
Assert.IsTrue(spaceship.transform.position.x > 0.0f);
spaceship.transform.position = Vector2.up * 100.0f;
yield return null;
Assert.IsTrue(spaceship.transform.position.y < 0.0f);
spaceship.transform.position = Vector2.down * 100.0f;
yield return null;
Assert.IsTrue(spaceship.transform.position.y > 0.0f);
}
[Test]
public void _14_SpaceshipCanFireProjectiles()
{
ClearScene();
SpaceshipController spaceship = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.Euler(0.0f, 0.0f, 0.0f)).GetComponent<SpaceshipController>();
spaceship.Shoot();
ProjectileController projectile = Object.FindAnyObjectByType<ProjectileController>();
Assert.IsTrue(projectile != null);
}
[UnityTest]
public IEnumerator _15_SpaceshipSpawnsDebrisWhenDestroyed()
{
ClearScene();
GameObject spaceship = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.identity);
Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
yield return new WaitForFixedUpdate();
yield return null;
Assert.IsTrue(spaceship == null);
DebrisController[] objects = Object.FindObjectsByType<DebrisController>(FindObjectsSortMode.None);
Assert.IsTrue(objects.Length > 0);
}
[UnityTest]
public IEnumerator _16_SpaceshipEngineEmitsParticles()
{
ClearScene();
GameObject spaceship = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.identity);
Assert.IsTrue(spaceship.transform.GetChild(0).GetChild(0) != null);
Assert.IsTrue(spaceship.transform.GetChild(0).GetChild(0).GetComponent<EngineTrail>() != null);
ParticleSystem ps = spaceship.transform.GetChild(0).GetChild(0).GetComponent<ParticleSystem>();
Assert.IsTrue(ps != null);
Assert.IsTrue(ps.particleCount == 0);
yield return null;
Assert.IsTrue(ps.particleCount > 0);
}
[UnityTest]
public IEnumerator _17_SpaceshipEngineParticlesAreClearedAfterWarp()
{
ClearScene();
Object.Instantiate(cameraPrefab);
GameObject spaceship = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.identity);
ParticleSystem ps = spaceship.transform.GetChild(0).GetChild(0).GetComponent<ParticleSystem>();
yield return null; // Wait for particles to spawn
spaceship.transform.position = Vector2.left * 100.0f;
yield return null;
Assert.IsTrue(ps.particleCount == 0);
}
[UnityTest]
public IEnumerator _18_SpaceshipDoesntMoveDuringPause()
{
ClearScene();
SpaceshipController spaceship = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.identity).GetComponent<SpaceshipController>();
spaceship.direction = Vector2.up;
Vector3 startPosition = spaceship.transform.position;
GameManager.IsPaused = true;
for (int i = 0; i < 20; i++)
yield return null;
Assert.IsTrue(spaceship.transform.position == startPosition);
}
[Test]
public void _19_SpaceshipPrefabHasRequiredComponentAnimator()
{
Assert.IsNotNull(spaceshipPrefab.transform.GetChild(0).GetComponent<Animator>());
Assert.IsNotNull(spaceshipPrefab.transform.GetChild(0).GetComponent<Animator>().runtimeAnimatorController != null);
Assert.IsTrue(spaceshipPrefab.transform.GetChild(0).GetComponent<Animator>().runtimeAnimatorController.name == "ShipAnimator");
Assert.IsTrue(spaceshipPrefab.transform.GetChild(0).GetComponent<Animator>().cullingMode == AnimatorCullingMode.AlwaysAnimate);
Assert.IsTrue(spaceshipPrefab.transform.GetChild(0).GetComponent<Animator>().updateMode == AnimatorUpdateMode.Normal);
}
[UnityTest]
public IEnumerator _20_SpaceshipAnimatorPlaysSpawnAnimationOnSpawn()
{
ClearScene();
Animator animator = Object.Instantiate(spaceshipPrefab, Vector3.zero, Quaternion.identity).transform.GetChild(0).GetComponent<Animator>();
yield return null;
Assert.IsNotNull(animator);
Assert.IsTrue(animator.GetCurrentAnimatorStateInfo(0).IsName("SpaceshipSpawn"));
while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1.0f)
{
yield return null;
}
Assert.IsTrue(animator.transform.localScale == Vector3.one);
}
[Test]
public void _21_SpaceshipWeaponListContainsData()
{
WeaponList weaponList = spaceshipPrefab.GetComponent<SpaceshipController>().weaponList;
Assert.IsNotNull(weaponList);
Assert.IsTrue(weaponList.weapons.Count == 2);
foreach(WeaponList.Weapon weapon in weaponList.weapons)
{
Assert.IsTrue(weapon.weaponName.Length != 0);
Assert.IsNotNull(weapon.weaponPrefab);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8c44e1f2b99a94dd4ad55e123afc4544
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
using UnityEngine;
public class TestsReferences : MonoBehaviour
{
#region SCENES
public Object asteroidsScene;
#endregion
#region CORE
public GameObject spaceshipPrefab;
public GameObject cameraPrefab;
public GameObject asteroidPrefab;
public GameObject gameManagerPrefab;
#endregion
#region WEAPONS
public GameObject projectilePrefab;
public GameObject laserPrefab;
#endregion
#region UI
public GameObject inGameMenuPrefab;
#endregion
#region FX
public GameObject spaceshipDebrisPrefab;
public GameObject explosionPrefab;
#endregion
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 442ac454dde377b499a5106b58789677
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,243 @@
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
#endif
public class UserInterfaceTests
{
GameObject gameManagerPrefab;
GameObject inGameMenuPrefab;
LoadSceneParameters loadSceneParameters;
#if UNITY_EDITOR
string asteroidsScenePath;
#endif
[SetUp]
public void Setup()
{
GameManager.InitializeTestingEnvironment(true, false, false, false, false);
loadSceneParameters = new LoadSceneParameters(LoadSceneMode.Single, LocalPhysicsMode.None);
Object asteroidsScene = ((GameObject)Resources.Load("TestsReferences")).GetComponent<TestsReferences>().asteroidsScene;
#if UNITY_EDITOR
asteroidsScenePath = AssetDatabase.GetAssetPath(asteroidsScene);
#endif
gameManagerPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().gameManagerPrefab;
inGameMenuPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().inGameMenuPrefab;
}
void ClearScene()
{
Transform[] objects = Object.FindObjectsByType<RectTransform>(FindObjectsSortMode.None);
foreach (Transform obj in objects)
{
if (obj != null)
Object.DestroyImmediate(obj.gameObject);
}
}
[UnityTest]
public IEnumerator _01_InGameMenuExistsInScene()
{
#if UNITY_EDITOR
EditorSceneManager.LoadSceneInPlayMode(asteroidsScenePath, loadSceneParameters);
yield return null;
Assert.NotNull(Object.FindAnyObjectByType<InGameMenuController>());
#else
yield return null;
Assert.Pass();
#endif
}
[UnityTest]
public IEnumerator _02_InGameMenuHasCorrectUIElements()
{
ClearScene();
GameObject inGameMenu = Object.Instantiate(inGameMenuPrefab, Vector3.zero, Quaternion.identity);
yield return null;
Assert.NotNull(inGameMenu);
Canvas canvas = inGameMenu.GetComponent<Canvas>();
Assert.NotNull(canvas);
Assert.NotNull(canvas.GetComponent<InGameMenuController>());
Assert.NotNull(canvas.transform.GetChild(0));
Assert.IsFalse(canvas.transform.GetChild(0).gameObject.activeInHierarchy);
Assert.NotNull(canvas.transform.GetChild(1));
Assert.IsTrue(canvas.transform.GetChild(1).gameObject.activeInHierarchy);
Assert.NotNull(canvas.transform.GetChild(2));
Assert.IsTrue(canvas.transform.GetChild(2).gameObject.activeInHierarchy);
}
[UnityTest]
public IEnumerator _03_InGameMenuContainsButtons()
{
ClearScene();
GameObject inGameMenu = Object.Instantiate(inGameMenuPrefab, Vector3.zero, Quaternion.identity);
yield return null;
Transform container = inGameMenu.transform.GetChild(0);
Assert.NotNull(container.GetComponent<VerticalLayoutGroup>());
// InGameMenu has the game title
Assert.NotNull(container.GetChild(1).GetComponent<Image>());
Assert.IsTrue(container.GetChild(1).GetComponent<Image>().sprite != null);
// Pause menu has a resume button
Assert.NotNull(container.GetChild(1).GetComponent<Button>());
Assert.IsTrue(container.GetChild(1).name == "ResumeButton");
Assert.IsTrue(container.GetChild(1).GetComponent<Button>().onClick.GetPersistentMethodName(0) == "ChangeMenuState");
}
[UnityTest]
public IEnumerator _04_PauseMenuCanBeEnabledAndDisabled()
{
ClearScene();
GameObject inGameMenu = Object.Instantiate(inGameMenuPrefab, Vector3.zero, Quaternion.identity);
yield return null;
InGameMenuController menuController = inGameMenu.GetComponent<InGameMenuController>();
Assert.IsTrue(menuController.transform.GetChild(0).name == "PauseMenu");
menuController.ChangeMenuState(true);
Assert.IsTrue(menuController.transform.GetChild(0).gameObject.activeInHierarchy);
Assert.IsTrue(menuController.transform.GetChild(1).gameObject.activeInHierarchy);
menuController.ChangeMenuState(false);
Assert.IsFalse(menuController.transform.GetChild(0).gameObject.activeInHierarchy);
Assert.IsTrue(menuController.transform.GetChild(1).gameObject.activeInHierarchy);
}
[UnityTest]
public IEnumerator _05_PauseMenuChangesGameManagerGameState()
{
#if UNITY_EDITOR
EditorSceneManager.LoadSceneInPlayMode(asteroidsScenePath, loadSceneParameters);
yield return null;
InGameMenuController menuController = GameObject.Find("InGameMenu").GetComponent<InGameMenuController>();
menuController.ChangeMenuState(true);
Assert.IsTrue(GameManager.IsPaused);
Assert.IsTrue(Time.timeScale == 0.0f);
menuController.ChangeMenuState(false);
Assert.IsFalse(GameManager.IsPaused);
Assert.IsTrue(Time.timeScale == 1.0f);
#else
yield return null;
Assert.Pass();
#endif
}
[UnityTest]
public IEnumerator _06_InGameScoreCounterChangesWhenScoreChanges()
{
#if UNITY_EDITOR
EditorSceneManager.LoadSceneInPlayMode(asteroidsScenePath, loadSceneParameters);
yield return null;
Assert.NotNull(GameObject.Find("InGameMenu"));
Canvas canvas = GameObject.Find("InGameMenu").GetComponent<Canvas>();
Assert.NotNull(canvas.transform.GetChild(1));
Text[] numbers = canvas.transform.GetChild(1).GetComponentsInChildren<Text>();
Assert.IsTrue(numbers.Length == 7);
GameManager.AddToScore(0);
yield return new WaitUntil(() => numbers[3].text == "1");
Assert.IsTrue(numbers[0].text == "0" && numbers[1].text == "0" && numbers[2].text == "0" && numbers[3].text == "1" && numbers[4].text == "0" && numbers[5].text == "0" && numbers[6].text == "0");
#else
yield return null;
Assert.Pass();
#endif
}
[UnityTest]
public IEnumerator _07_InGameMenuLivesControllerPlaysAnimationsWhenLifeCountIsChanged()
{
ClearScene();
GameObject inGameMenu = Object.Instantiate(inGameMenuPrefab, Vector3.zero, Quaternion.identity);
Transform livesTransform = inGameMenu.transform.GetChild(2);
Animator[] lives = livesTransform.GetComponentsInChildren<Animator>();
Assert.IsTrue(lives.Length == 3);
yield return null;
livesTransform.GetComponent<LifeCounter>().SetLives(2);
yield return null;
int i;
// Going from 3 to 2 lives, therefore 1 "remove" animation should play
for (i = 0; i < lives.Length; i++)
{
if (i >= 2)
{
Assert.IsTrue(lives[i].enabled == true);
Assert.IsTrue(lives[i].GetCurrentAnimatorStateInfo(0).IsName("RemoveLife"));
}
else
{
Assert.IsTrue(lives[i].enabled == false);
}
}
yield return new WaitForSeconds(0.5f);
for (i = 0; i < lives.Length; i++)
Assert.IsTrue(lives[i].enabled == false);
livesTransform.GetComponent<LifeCounter>().SetLives(0);
yield return null;
// Going from 2 to 0 lives, therefore 2 "remove" animations should play
for (i = 0; i < lives.Length; i++)
{
if (i < 2)
{
Assert.IsTrue(lives[i].enabled == true);
Assert.IsTrue(lives[i].GetCurrentAnimatorStateInfo(0).IsName("RemoveLife"));
}
else
{
Assert.IsTrue(lives[i].enabled == false);
}
}
yield return new WaitForSeconds(0.5f);
for (i = 0; i < lives.Length; i++)
Assert.IsTrue(lives[i].enabled == false);
livesTransform.GetComponent<LifeCounter>().SetLives(3);
yield return null;
// Going from 0 to 3 lives, therefore 3 "recover" animations should play
for (i = 0; i < lives.Length; i++)
{
Assert.IsTrue(lives[i].enabled == true);
Assert.IsTrue(lives[i].GetCurrentAnimatorStateInfo(0).IsName("RecoverLife"));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 02f764eff5fa8bd4b802f392c705b9a9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,238 @@
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using UnityEditor;
public class WeaponTests
{
GameObject projectilePrefab;
GameObject laserPrefab;
GameObject asteroidPrefab;
GameObject spaceshipPrefab;
[SetUp]
public void Setup()
{
GameManager.InitializeTestingEnvironment(false, false, false, false, false);
spaceshipPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().spaceshipPrefab;
asteroidPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().asteroidPrefab;
projectilePrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().projectilePrefab;
laserPrefab = ((GameObject)Resources.Load("TestsReferences", typeof(GameObject))).GetComponent<TestsReferences>().laserPrefab;
}
void ClearScene()
{
Transform[] objects = Object.FindObjectsByType<Transform>(FindObjectsSortMode.None);
foreach (Transform obj in objects)
{
if(obj != null)
Object.DestroyImmediate(obj.gameObject);
}
}
/*
// Uncomment the code from line 35 up to line 237, run the tests again and compare the code coverage results
[Test]
public void _01_ProjectilePrefabExists()
{
Assert.NotNull(projectilePrefab);
}
[Test]
public void _02_ProjectilePrefabCanBeInstantiated()
{
ClearScene();
GameObject projectile = (GameObject)Object.Instantiate(projectilePrefab);
projectile.name = "Projectile";
Assert.NotNull(GameObject.Find("Projectile"));
}
[Test]
public void _03_ProjectilePrefabHasRequiredComponentTransform()
{
Assert.IsNotNull(projectilePrefab.GetComponent<Transform>());
}
[Test]
public void _04_ProjectilePrefabHasRequiredComponentCollider()
{
Assert.IsNotNull(projectilePrefab.GetComponent<BoxCollider2D>());
Assert.IsTrue(projectilePrefab.GetComponent<BoxCollider2D>().size == new Vector2(0.2f, 0.2f));
}
[Test]
public void _05_ProjectilePrefabHasRequiredComponentControllerScript()
{
Assert.IsNotNull(projectilePrefab.GetComponent<ProjectileController>());
}
[Test]
public void _06_ProjectilePrefabHasRequiredVisual()
{
Transform visualChild = projectilePrefab.transform.GetChild(0);
Assert.IsTrue(visualChild.name == "Visual");
Assert.IsNotNull(visualChild);
Assert.IsNotNull(visualChild.GetComponent<MeshRenderer>());
Assert.IsNotNull(visualChild.GetComponent<MeshRenderer>().sharedMaterials[0]);
Assert.IsNotNull(visualChild.GetComponent<MeshFilter>());
Assert.IsNotNull(visualChild.GetComponent<MeshFilter>().sharedMesh);
}
[Test]
public void _07_ProjectileCanMove()
{
ClearScene();
ProjectileController projectile = Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity).GetComponent<ProjectileController>();
projectile.Move();
Assert.IsTrue(projectile.transform.position != Vector3.zero);
}
[Test]
public void _08_ProjectileDirectionCanBeChanged()
{
ClearScene();
ProjectileController projectile = Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity).GetComponent<ProjectileController>();
projectile.SetDirection(Vector2.up);
Assert.IsTrue(projectile.GetDirection() == Vector2.up);
}
[UnityTest]
public IEnumerator _09_ProjectileMovesAccordingToItsDirectionVector()
{
ClearScene();
ProjectileController projectile = Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity).GetComponent<ProjectileController>();
projectile.SetDirection(Vector2.up);
Assert.IsTrue(projectile.GetDirection() == Vector2.up);
float t = 0.5f;
while (t > 0.0f)
{
t -= Time.deltaTime;
yield return null;
}
Assert.IsTrue(projectile.transform.position.x == 0.0f && projectile.transform.position.y > 0.0f); //check if projectile moves according to given trajectory along the Y axis
}
[UnityTest]
public IEnumerator _10_ProjectileRotatesAccordingToItsDirectionVector()
{
ClearScene();
ProjectileController projectile = Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity).GetComponent<ProjectileController>();
projectile.SetDirection(new Vector2(0.714f, -0.156f).normalized);
yield return null;
Assert.IsTrue((Vector2)projectile.transform.up == projectile.GetDirection());
}
[UnityTest]
public IEnumerator _11_ProjectileIsDestroyedWhenOffsceen()
{
ClearScene();
ProjectileController projectile = Object.Instantiate(projectilePrefab, Vector3.right * 100, Quaternion.identity).GetComponent<ProjectileController>();
yield return null;
Assert.IsTrue(projectile == null);
}
[Test]
public void _12_ProjectilePrefabHasRequiredComponentRigidbody()
{
Assert.IsNotNull(projectilePrefab.GetComponent<Rigidbody2D>());
Assert.IsTrue(projectilePrefab.GetComponent<Rigidbody2D>().isKinematic);
Assert.IsTrue(projectilePrefab.GetComponent<Rigidbody2D>().collisionDetectionMode == CollisionDetectionMode2D.Continuous);
Assert.IsTrue(projectilePrefab.GetComponent<Rigidbody2D>().interpolation == RigidbodyInterpolation2D.Interpolate);
}
[UnityTest]
public IEnumerator _13_ProjectileIsDestroyedOnCollisionWithAsteroid()
{
ClearScene();
GameObject projectile = Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity);
Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
yield return new WaitForFixedUpdate();
yield return null;
Assert.IsTrue(projectile == null);
}
[UnityTest]
public IEnumerator _14_ProjectileIgnoresCollisionWithSpaceship()
{
ClearScene();
GameObject projectile = Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity);
Object.Instantiate(spaceshipPrefab, Vector2.zero, Quaternion.identity);
yield return new WaitForFixedUpdate();
yield return null;
Assert.IsTrue(projectile != null);
}
[UnityTest]
public IEnumerator _15_ProjectileTriggersAsteroidSplit()
{
ClearScene();
Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity);
Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
yield return new WaitForFixedUpdate();
yield return null;
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None);
Assert.IsTrue(asteroids.Length > 1);
}
[UnityTest]
public IEnumerator _16_ProjectilesCannotSplitTheSameAsteroidTwice()
{
ClearScene();
Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity);
Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity);
Object.Instantiate(asteroidPrefab, Vector3.zero, Quaternion.identity);
yield return new WaitForFixedUpdate();
yield return null;
AsteroidController[] asteroids = Object.FindObjectsByType<AsteroidController>(FindObjectsSortMode.None);
Assert.IsTrue(asteroids.Length == 2);
}
[UnityTest]
public IEnumerator _17_ProjectilesDontMoveDuringPause()
{
ClearScene();
ProjectileController projectile = Object.Instantiate(projectilePrefab, Vector3.zero, Quaternion.identity).GetComponent<ProjectileController>();
projectile.SetDirection(Vector2.up);
Vector3 startPosition = projectile.transform.position;
GameManager.IsPaused = true;
for (int i = 0; i < 10; i++)
yield return null;
Assert.IsTrue(projectile.transform.position == startPosition);
}
[UnityTest]
public IEnumerator _18_LaserFiresSuccessfully()
{
// ClearScene();
// SpaceshipController spaceship = Object.Instantiate(spaceshipPrefab).GetComponent<SpaceshipController>();
// spaceship.currentWeapon = SpaceshipController.Weapon.Laser;
// spaceship.Shoot();
yield return null;
// LaserController laser = Object.FindAnyObjectByType<LaserController>();
// Assert.NotNull(laser);
}
*/
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a84ac4720880c4da591130ed39f030d9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: