Have one last unit test

This commit is contained in:
2019-09-11 22:35:35 -06:00
parent 6e42363daf
commit cede87461f
16 changed files with 269 additions and 47 deletions

View File

@@ -46,9 +46,7 @@
</e>
</e>
</e>
<e p="Program.cs" t="Include" />
<e p="Tello Drone.csproj" t="IncludeRecursive" />
<e p="TestNameAttributes.cs" t="Include" />
<e p="UdpClientWrapper.cs" t="Include" />
<e p="UdpClientWrapperFactory.cs" t="Include" />
</e>
@@ -58,6 +56,9 @@
<e p="ConsoleLoggerTests.cs" t="Include" />
<e p="DroneCommandsTests.cs" t="Include" />
<e p="DroneFactoryTests.cs" t="Include" />
<e p="DroneTests.cs" t="Include" />
<e p="MissionCommanderTests.cs" t="Include" />
<e p="MissionListTests.cs" t="Include" />
<e p="obj" t="ExcludeRecursive">
<e p="Debug" t="Include">
<e p="netcoreapp2.2" t="Include">

View File

@@ -1,5 +1,3 @@
using System.Dynamic;
namespace Tello_Drone
{
public interface IUdpClientWrapperFactory

View File

@@ -1,7 +1,3 @@
using System;
using System.Linq;
using System.Security.Authentication.ExtendedProtection;
namespace Tello_Drone
{
public class MissionCommander : IMissionCommander
@@ -15,12 +11,13 @@ namespace Tello_Drone
public void RunMission(IMissions mission)
{
SetUpDrone(3);
if(!SetUpDrone(3))
return;
mission.Run(_drone);
TearDownDrone();
}
private void SetUpDrone(int retryCount)
private bool SetUpDrone(int retryCount)
{
var inCommandMode = false;
while (inCommandMode != true && retryCount > 0)
@@ -28,7 +25,11 @@ namespace Tello_Drone
inCommandMode = _drone.Command();
retryCount--;
}
if (!inCommandMode)
return false;
_drone.TakeOff();
return true;
}
private void TearDownDrone()

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using FastExpressionCompiler.LightExpression;
using ImTools;
using Tello_Drone.Missions;
namespace Tello_Drone

View File

@@ -1,11 +1,12 @@
namespace Tello_Drone.Missions
{
[TestNameAttributes("Mission 2")]
public class Mission2: IMissions
{
public void Run(Drone drone)
{
drone.Forward(45);
drone.BackFlip();
drone.Reverse(45);
}
}

View File

@@ -9,6 +9,7 @@ namespace Tello_Drone.Missions
drone.BackFlip();
drone.Left(20);
drone.Right(20);
drone.Reverse(30);
}
}
}

View File

@@ -1,17 +0,0 @@
using System;
using System.Dynamic;
using System.Security.Cryptography;
namespace Tello_Drone
{
[AttributeUsage(AttributeTargets.Class)]
public class TestNameAttributes : Attribute
{
public TestNameAttributes(string name)
{
Name = name;
}
public string Name { get; }
}
}

View File

@@ -1,8 +1,10 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
[assembly: InternalsVisibleTo("TelloDroneUnitTests")]
namespace Tello_Drone
{
internal class UdpClientWrapper : IUdpClientWrapper

View File

@@ -1,5 +1,3 @@
using System.Dynamic;
namespace Tello_Drone
{
public class UdpClientWrapperFactory : IUdpClientWrapperFactory

View File

@@ -1,10 +1,7 @@
using System;
using System.Linq.Expressions;
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace Tests
namespace TelloDroneUnitTests
{
public class ConsoleLoggerTests
{

View File

@@ -1,12 +1,8 @@
using System;
using System.Net.Sockets;
using ImTools;
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace Tests
namespace TelloDroneUnitTests
{
public class DroneCommandsTests
{

View File

@@ -2,7 +2,7 @@ using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace Tests
namespace TelloDroneUnitTests
{
public class DroneFactoryTests
{

View File

@@ -0,0 +1,152 @@
using System;
using System.Security.Cryptography.X509Certificates;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace TelloDroneUnitTests
{
public class DroneTests
{
private Mock<IDroneCommands> _mockDroneCommands;
[SetUp]
public void SetUp()
{
_mockDroneCommands = new Mock<IDroneCommands>();
}
[Test]
public void ValidForwardTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.Forward(It.IsAny<int>());
_mockDroneCommands.Verify(x => x.Forward(It.IsAny<int>()), Times.Once);
}
[Test]
public void ValidReverseTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.Reverse(It.IsAny<int>());
_mockDroneCommands.Verify(x => x.Reverse(It.IsAny<int>()), Times.Once);
}
[Test]
public void ValidUpTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.Up(It.IsAny<int>());
_mockDroneCommands.Verify(x => x.Up(It.IsAny<int>()), Times.Once);
}
[Test]
public void ValidDownTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.Down(It.IsAny<int>());
_mockDroneCommands.Verify(x => x.Down(It.IsAny<int>()), Times.Once);
}
[Test]
public void ValidLeftTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.Left(It.IsAny<int>());
_mockDroneCommands.Verify(x => x.Left(It.IsAny<int>()), Times.Once);
}
[Test]
public void ValidRightTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.Right(It.IsAny<int>());
_mockDroneCommands.Verify(x => x.Right(It.IsAny<int>()), Times.Once);
}
[Test]
public void ValidFrontFlipTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.FrontFlip();
_mockDroneCommands.Verify(x => x.FrontFlip(), Times.Once);
}
[Test]
public void ValidBackFlipTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.BackFlip();
_mockDroneCommands.Verify(x => x.BackFlip(), Times.Once);
}
[Test]
public void ValidLeftFlipTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.LeftFlip();
_mockDroneCommands.Verify(x => x.LeftFlip(), Times.Once);
}
[Test]
public void ValidRightFlipTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.RightFlip();
_mockDroneCommands.Verify(x => x.RightFlip(), Times.Once);
}
[Test]
public void CommandTest()
{
_mockDroneCommands.Setup(x => x.Command()).Returns(true);
var drone = new Drone(_mockDroneCommands.Object);
Assert.AreEqual(true, drone.Command());
_mockDroneCommands.Verify(x => x.Command(), Times.Once);
}
[Test]
public void CommandReturnsFalse()
{
_mockDroneCommands.Setup(x => x.Command()).Returns(false);
var drone = new Drone(_mockDroneCommands.Object);
Assert.AreEqual(false, drone.Command());
_mockDroneCommands.Verify(x => x.Command(), Times.Once);
}
[Test]
public void LandTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.Land();
_mockDroneCommands.Verify(x => x.Land(), Times.Once);
}
[Test]
public void TakeOffTest()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.TakeOff();
_mockDroneCommands.Verify(x => x.TakeOff(), Times.Once);
}
[Test]
public void InitialTakeOffTest()
{
_mockDroneCommands.Setup(x => x.InitialTakeOff()).Returns(true);
var drone = new Drone(_mockDroneCommands.Object);
Assert.AreEqual(true, drone.InitialTakeOff());
_mockDroneCommands.Verify(x => x.InitialTakeOff(), Times.Once);
}
[Test]
public void RotateClockWise()
{
var drone = new Drone(_mockDroneCommands.Object);
drone.RotateClockWise(It.IsAny<int>());
_mockDroneCommands.Verify(x => x.RotateClockWise(It.IsAny<int>()), Times.Once);
}
}
}

View File

@@ -0,0 +1,44 @@
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace TelloDroneUnitTests
{
public class MissionCommanderTests
{
private Mock<IDroneFactory> _mockDroneFactory;
private Mock<IMissions> _mockMission;
private Mock<Drone> _mockDrone;
private Mock<IDroneCommands> _mockDroneCommands;
[SetUp]
public void SetUp()
{
_mockDroneFactory = new Mock<IDroneFactory>();
_mockMission = new Mock<IMissions>();
_mockDroneCommands = new Mock<IDroneCommands>();
_mockDrone = new Mock<Drone>(_mockDroneCommands.Object);
}
[Test]
public void RunMissionTest()
{
_mockDroneCommands.Setup(x => x.Command()).Returns(true);
_mockDroneFactory.SetupGet(x => x.CreateDrone).Returns(_mockDrone.Object);
var missionCommander = new MissionCommander(_mockDroneFactory.Object);
missionCommander.RunMission(_mockMission.Object);
_mockMission.Verify(x => x.Run(It.IsAny<Drone>()), Times.Once);
}
[Test]
public void RunMissionSetupFailed()
{
_mockDroneCommands.Setup(x => x.Command()).Returns(false);
_mockDroneFactory.SetupGet(x => x.CreateDrone).Returns(_mockDrone.Object);
var missionCommander = new MissionCommander(_mockDroneFactory.Object);
missionCommander.RunMission(_mockMission.Object);
_mockMission.Verify(x => x.Run(It.IsAny<Drone>()), Times.Never);
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace TelloDroneUnitTests
{
public class MissionListTests
{
[SetUp]
public void SetUp()
{
}
[Test]
public void MissionListTest()
{
var missionList = new MissionList();
for (int i = 0; i < missionList.GetMissionList().Count; i++)
{
Assert.AreEqual(typeof(string), missionList.GetMissionList()[i].Item1.GetType());
Assert.IsInstanceOf(typeof(IMissions), missionList.GetMissionList()[i].Item2);
}
}
}
}

View File

@@ -0,0 +1,24 @@
using System.Threading;
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace TelloDroneUnitTests
{
public class UdpClientWrapperFactoryTests
{
private Mock<IConsoleLogger> _mockConsoleLogger;
[SetUp]
public void SetUp()
{
_mockConsoleLogger = new Mock<IConsoleLogger>();
}
[Test]
public void CreateTest()
{
Assert.IsInstanceOf(typeof(UdpClientWrapper), new UdpClientWrapperFactory(_mockConsoleLogger.Object).Create());
}
}
}