< Summary

Class:DroneGame.Tests.TestGrid
Assembly:Tests
File(s):/github/workspace/Assets/Tests/TestGrid.cs
Covered lines:61
Uncovered lines:0
Coverable lines:61
Total lines:119
Line coverage:100% (61 of 61)
Covered branches:0
Total branches:0
Covered methods:4
Total methods:4
Method coverage:100% (4 of 4)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GlobalSetup()0%220100%
TestGridCreation()0%66096.3%
TestShortestPath()0%44096.67%
TestDroneMove()0%550100%

File(s)

/github/workspace/Assets/Tests/TestGrid.cs

#LineLine coverage
 1using NUnit.Framework;
 2
 3using UnityEngine;
 4using UnityEngine.TestTools;
 5
 6using System.Collections.Generic;
 7using System.Diagnostics;
 8using System.Collections;
 9
 10namespace DroneGame.Tests
 11{
 12  public class TestGrid
 13  {
 14    Grid _grid;
 15
 16    [OneTimeSetUp]
 17    public void GlobalSetup()
 118    {
 119      var gridPrefab = Resources.Load<Grid>("Grid") ?? throw new("Resource Grid Not found");
 120      _grid = Object.Instantiate(gridPrefab);
 121    }
 22
 23    [UnityTest]
 24    public IEnumerator TestGridCreation()
 125    {
 126      while(_grid.parsed == null) yield return null;
 27
 13128      foreach (var currTile in _grid.parsed)
 6429      {
 6430        var currCoordinate = currTile.Key;
 6431        var tileInGrid = _grid[currCoordinate];
 32
 6433        Assert.True(char.IsLetter(currCoordinate[0]));
 6434        Assert.True(char.IsNumber(currCoordinate[1]));
 35
 6436        var neighbors = tileInGrid.neighbors;
 37
 6438        Assert.IsNotEmpty(neighbors);
 6439        Assert.False(neighbors.ContainsKey(currCoordinate));
 40
 64041        foreach (var neighbor in currTile.Value)
 22442        {
 22443          var neighborCoordinate = neighbor.Key;
 44
 22445          Assert.Contains(neighborCoordinate, neighbors.Keys);
 46
 47          // no need to test for the time because the parser automatically raises an error if it were to be invalid
 22448          Assert.True(char.IsLetter(neighborCoordinate[0]));
 22449          Assert.True(char.IsNumber(neighborCoordinate[1]));
 22450        }
 6451      }
 152    }
 53
 54    [UnityTest]
 55    public IEnumerator TestShortestPath()
 156    {
 157      while(_grid.parsed == null) yield return null;
 58
 159      var A1ToA1 = new List<string>() { "A1" };
 160      var A1ToA3 = new List<string>() { "A1", "A2", "A3" };
 161      var A1ToA4 = new List<string>() { "A1", "A2", "A3", "A4" };
 62
 63      // Where we going, we don't need diagonals
 64      // At first, I tried with diagonals, but my own code reminded that there were no diagonals in the API
 165      var A1ToH8 = new List<string>(){"A1", "B1", "C1", "C2", "C3", "D3", "E3",
 66                                      "F3", "F4", "F5", "F6", "G6", "G7", "H7",
 67                                      "H8"};
 68
 69      // Nothing is done, A1 needs to be returned immediately
 170      var calculatedPath = _grid.GetShortestPath("A1", "A1").path;
 171      Assert.AreEqual(A1ToA1, calculatedPath);
 72
 173      var watch = Stopwatch.StartNew();
 74      // Now the algorithm needs to work.
 175      calculatedPath = _grid.GetShortestPath("A1", "A3").path;
 176      Assert.AreEqual(A1ToA3, calculatedPath);
 177      watch.Stop();
 78
 179      var firstRunTime = watch.ElapsedMilliseconds;
 80
 181      watch = Stopwatch.StartNew();
 82      // This one must be executed faster than the first time because of cache
 183      calculatedPath = _grid.GetShortestPath("A1", "A3").path;
 184      Assert.AreEqual(A1ToA3, calculatedPath);
 185      watch.Stop();
 186      Assert.Less(watch.ElapsedMilliseconds, firstRunTime);
 87
 188      calculatedPath = _grid.GetShortestPath("A1", "A4").path;
 189      Assert.AreEqual(A1ToA4, calculatedPath);
 90
 191      calculatedPath = _grid.GetShortestPath("A1", "H8").path;
 192      Assert.AreEqual(A1ToH8, calculatedPath);
 93
 194      calculatedPath = _grid.GetShortestPath(new string[] { "A1", "H8" }).path;
 195      Assert.AreEqual(A1ToH8, calculatedPath);
 96
 197      calculatedPath = _grid.GetShortestPath(new string[] { "A1", "F5", "H8" }).path;
 198      Assert.AreEqual(A1ToH8, calculatedPath);
 99
 1100      calculatedPath = _grid.GetShortestPath(new string[] { "A1", "F5", "G6", "H8" }).path;
 1101      Assert.AreEqual(A1ToH8, calculatedPath);
 1102    }
 103
 104    [UnityTest]
 105    public IEnumerator TestDroneMove()
 1106    {
 12207107      while(_grid.parsed == null) yield return null;
 108
 1109      var dronePrefab = Resources.Load<Drone>("Drone/Drone") ?? throw new("Resource Drone Not found");
 1110      var drone = Object.Instantiate(dronePrefab);
 111
 112
 1113      var first = _grid["A1"];
 1114      var second = _grid["A2"];
 115
 1116      drone.StartCoroutine(drone.FollowPath(new(){first, second}));
 1117    }
 118  }
 119}