File size: 8,620 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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.FindObjectsOfType<Transform>();
        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.FindObjectsOfType<AsteroidController>();
        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.FindObjectsOfType<AsteroidController>();
        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.FindObjectOfType<LaserController>();
        // Assert.NotNull(laser);
    }
*/
}