From d99bce4d14584e1acdbfd7f313145f9739d90665 Mon Sep 17 00:00:00 2001 From: bbod Date: Mon, 9 Sep 2019 22:16:51 -0600 Subject: [PATCH] More Unit Tests --- .../.idea.Tello Drone/.idea/contentModel.xml | 20 +++ .idea/.idea.Tello Drone/riderModule.iml | 3 + Tello Drone.sln | 6 + Tello Drone/ConsoleLogger.cs | 1 + Tello Drone/DroneComands.cs | 9 +- Tello Drone/IUdpClientWrapperFactory.cs | 9 + Tello Drone/Missions.cs | 3 +- Tello Drone/UdpClientWrapperFactory.cs | 18 ++ TelloDroneUnitTests/ConsoleLoggerTests.cs | 28 +++ TelloDroneUnitTests/DroneCommandsTests.cs | 159 ++++++++++++++++++ TelloDroneUnitTests/DroneFactoryTests.cs | 26 +++ .../TelloDroneUnitTests.csproj | 20 +++ 12 files changed, 296 insertions(+), 6 deletions(-) create mode 100644 Tello Drone/IUdpClientWrapperFactory.cs create mode 100644 Tello Drone/UdpClientWrapperFactory.cs create mode 100644 TelloDroneUnitTests/ConsoleLoggerTests.cs create mode 100644 TelloDroneUnitTests/DroneCommandsTests.cs create mode 100644 TelloDroneUnitTests/DroneFactoryTests.cs create mode 100644 TelloDroneUnitTests/TelloDroneUnitTests.csproj diff --git a/.idea/.idea.Tello Drone/.idea/contentModel.xml b/.idea/.idea.Tello Drone/.idea/contentModel.xml index c7d4cba..cf6a0f4 100644 --- a/.idea/.idea.Tello Drone/.idea/contentModel.xml +++ b/.idea/.idea.Tello Drone/.idea/contentModel.xml @@ -7,6 +7,9 @@ + + + @@ -26,6 +29,7 @@ + @@ -37,8 +41,24 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.Tello Drone/riderModule.iml b/.idea/.idea.Tello Drone/riderModule.iml index 41bb8ae..55cc7cd 100644 --- a/.idea/.idea.Tello Drone/riderModule.iml +++ b/.idea/.idea.Tello Drone/riderModule.iml @@ -2,6 +2,9 @@ + + + diff --git a/Tello Drone.sln b/Tello Drone.sln index 5cd7d52..5263bbb 100644 --- a/Tello Drone.sln +++ b/Tello Drone.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tello Drone", "Tello Drone\Tello Drone.csproj", "{06798083-360C-4FF0-B96F-7011835E489B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelloDroneUnitTests", "TelloDroneUnitTests\TelloDroneUnitTests.csproj", "{C3966EBE-273E-4012-9CB4-7F8ACCFB8281}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {06798083-360C-4FF0-B96F-7011835E489B}.Debug|Any CPU.Build.0 = Debug|Any CPU {06798083-360C-4FF0-B96F-7011835E489B}.Release|Any CPU.ActiveCfg = Release|Any CPU {06798083-360C-4FF0-B96F-7011835E489B}.Release|Any CPU.Build.0 = Release|Any CPU + {C3966EBE-273E-4012-9CB4-7F8ACCFB8281}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C3966EBE-273E-4012-9CB4-7F8ACCFB8281}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C3966EBE-273E-4012-9CB4-7F8ACCFB8281}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C3966EBE-273E-4012-9CB4-7F8ACCFB8281}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/Tello Drone/ConsoleLogger.cs b/Tello Drone/ConsoleLogger.cs index 88e7b4c..77c774c 100644 --- a/Tello Drone/ConsoleLogger.cs +++ b/Tello Drone/ConsoleLogger.cs @@ -11,5 +11,6 @@ namespace Tello_Drone LogReceived?.Invoke(message); Console.WriteLine(message); } + } } \ No newline at end of file diff --git a/Tello Drone/DroneComands.cs b/Tello Drone/DroneComands.cs index 46e34d5..1bac05c 100644 --- a/Tello Drone/DroneComands.cs +++ b/Tello Drone/DroneComands.cs @@ -6,10 +6,9 @@ namespace Tello_Drone { private readonly IUdpClientWrapper _udpClient; - public DroneComands(IConsoleLogger consoleLogger) + public DroneComands(IUdpClientWrapperFactory udpClientWrapperFactory) { - var udpClient = new UdpClientWrapper(consoleLogger); - _udpClient = udpClient; + _udpClient = udpClientWrapperFactory.Create(); } public void Forward(int x) =>SendCommand($"forward {x}"); @@ -34,9 +33,9 @@ namespace Tello_Drone - private bool SendCommand(string message) + private void SendCommand(string message) { - return _udpClient.TrySend(message, 5_000, 3); + _udpClient.TrySend(message, 5_000, 3); } private bool TrySendCommand(string message) diff --git a/Tello Drone/IUdpClientWrapperFactory.cs b/Tello Drone/IUdpClientWrapperFactory.cs new file mode 100644 index 0000000..097c1cb --- /dev/null +++ b/Tello Drone/IUdpClientWrapperFactory.cs @@ -0,0 +1,9 @@ +using System.Dynamic; + +namespace Tello_Drone +{ + public interface IUdpClientWrapperFactory + { + IUdpClientWrapper Create(); + } +} \ No newline at end of file diff --git a/Tello Drone/Missions.cs b/Tello Drone/Missions.cs index b3060f0..46aead5 100644 --- a/Tello Drone/Missions.cs +++ b/Tello Drone/Missions.cs @@ -33,8 +33,9 @@ namespace Tello_Drone MissionSetup(); _drone.BackFlip(); _drone.FrontFlip(); - _drone.Reverse(55); + _drone.Reverse(300); _drone.Forward(66); + _drone.Up(20); MissionTeardown(); } diff --git a/Tello Drone/UdpClientWrapperFactory.cs b/Tello Drone/UdpClientWrapperFactory.cs new file mode 100644 index 0000000..12be098 --- /dev/null +++ b/Tello Drone/UdpClientWrapperFactory.cs @@ -0,0 +1,18 @@ +using System.Dynamic; + +namespace Tello_Drone +{ + public class UdpClientWrapperFactory : IUdpClientWrapperFactory + { + private readonly IConsoleLogger _consoleLogger; + + public UdpClientWrapperFactory(IConsoleLogger consoleLogger) + { + _consoleLogger = consoleLogger; + } + public IUdpClientWrapper Create() + { + return new UdpClientWrapper(_consoleLogger); + } + } +} \ No newline at end of file diff --git a/TelloDroneUnitTests/ConsoleLoggerTests.cs b/TelloDroneUnitTests/ConsoleLoggerTests.cs new file mode 100644 index 0000000..5e677a0 --- /dev/null +++ b/TelloDroneUnitTests/ConsoleLoggerTests.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq.Expressions; +using Moq; +using NUnit.Framework; +using Tello_Drone; + +namespace Tests +{ + public class ConsoleLoggerTests + { + + [SetUp] + public void Setup() + { + } + + [Test] + public void LogTest() + { + var correctMessageSent = false; + var console = new ConsoleLogger(); + var test = "Test"; + console.LogReceived += delegate(string s) { correctMessageSent |= s == test; }; + console.Log(test); + Assert.AreEqual(correctMessageSent, true); + } + } +} \ No newline at end of file diff --git a/TelloDroneUnitTests/DroneCommandsTests.cs b/TelloDroneUnitTests/DroneCommandsTests.cs new file mode 100644 index 0000000..4807e49 --- /dev/null +++ b/TelloDroneUnitTests/DroneCommandsTests.cs @@ -0,0 +1,159 @@ + +using System; +using System.Net.Sockets; +using ImTools; +using Moq; +using NUnit.Framework; +using Tello_Drone; + +namespace Tests +{ + public class DroneCommandsTests + { + private Mock _mockUdpClientFactory; + private Mock _mockUdpClientWrapper; + + [SetUp] + public void Setup() + { + _mockUdpClientFactory = new Mock(); + _mockUdpClientWrapper = new Mock(); + } + + [Test] + public void DownCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.Down(10); + _mockUdpClientWrapper.Verify(x => x.TrySend("down 10", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void UpCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.Up(10); + _mockUdpClientWrapper.Verify(x => x.TrySend("up 10", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void ForwardCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.Forward(10); + _mockUdpClientWrapper.Verify(x => x.TrySend("forward 10", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void ReverseCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.Reverse(10); + _mockUdpClientWrapper.Verify(x => x.TrySend("back 10", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void CommandStateTest() + { + _mockUdpClientWrapper.Setup(x => x.TrySend(It.IsAny(), It.IsAny(), It.IsAny())).Returns(true); + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.Command(); + _mockUdpClientWrapper.Verify(x => x.TrySend("command", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void LeftCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.Left(10); + _mockUdpClientWrapper.Verify(x => x.TrySend("left 10", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void RightCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.Right(10); + _mockUdpClientWrapper.Verify(x => x.TrySend("right 10", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void LeftFlipCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.LeftFlip(); + _mockUdpClientWrapper.Verify(x => x.TrySend("flip l", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void RightFlipCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.RightFlip(); + _mockUdpClientWrapper.Verify(x => x.TrySend("flip r", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void FrontFlipCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.FrontFlip(); + _mockUdpClientWrapper.Verify(x => x.TrySend("flip f", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void BackFlipCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.BackFlip(); + _mockUdpClientWrapper.Verify(x => x.TrySend("flip b", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void InitialTakeOffCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.InitialTakeOff(); + _mockUdpClientWrapper.Verify(x => x.TrySend("takeoff", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void TakeOffCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.TakeOff(); + _mockUdpClientWrapper.Verify(x => x.TrySend("takeoff", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void LandCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.Land(); + _mockUdpClientWrapper.Verify(x => x.TrySend("land", It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public void RotateClockwiseCommandTest() + { + _mockUdpClientFactory.Setup(f => f.Create()).Returns(_mockUdpClientWrapper.Object); + var droneCommands = new DroneComands(_mockUdpClientFactory.Object); + droneCommands.RotateClockWise(180); + _mockUdpClientWrapper.Verify(x => x.TrySend("cw 180", It.IsAny(), It.IsAny()), Times.Once); + } + } +} \ No newline at end of file diff --git a/TelloDroneUnitTests/DroneFactoryTests.cs b/TelloDroneUnitTests/DroneFactoryTests.cs new file mode 100644 index 0000000..2dfb3e5 --- /dev/null +++ b/TelloDroneUnitTests/DroneFactoryTests.cs @@ -0,0 +1,26 @@ +using Moq; +using NUnit.Framework; +using Tello_Drone; + +namespace Tests +{ + public class DroneFactoryTests + { + private Mock _mockDroneCommands; + + [SetUp] + public void Setup() + { + _mockDroneCommands = new Mock(); + } + + [Test] + public void CreateDroneTests() + { + var droneFactory = new DroneFactory(_mockDroneCommands.Object); + var drone = droneFactory.CreateDrone; + Assert.AreEqual(typeof(Drone), drone.GetType()); + } + +} +} \ No newline at end of file diff --git a/TelloDroneUnitTests/TelloDroneUnitTests.csproj b/TelloDroneUnitTests/TelloDroneUnitTests.csproj new file mode 100644 index 0000000..8ab9ab3 --- /dev/null +++ b/TelloDroneUnitTests/TelloDroneUnitTests.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp2.2 + + false + + + + + + + + + + + + + +