From cede87461fd096a45a195a3e5fc16939dad5a684 Mon Sep 17 00:00:00 2001 From: bbod Date: Wed, 11 Sep 2019 22:35:35 -0600 Subject: [PATCH] Have one last unit test --- .../.idea.Tello Drone/.idea/contentModel.xml | 5 +- Tello Drone/IUdpClientWrapperFactory.cs | 2 - Tello Drone/MissionCommander.cs | 13 +- Tello Drone/MissionList.cs | 6 - Tello Drone/Missions/Mission2.cs | 5 +- Tello Drone/Missions/Mission3.cs | 1 + Tello Drone/TestNameAttributes.cs | 17 -- Tello Drone/UdpClientWrapper.cs | 2 + Tello Drone/UdpClientWrapperFactory.cs | 2 - TelloDroneUnitTests/ConsoleLoggerTests.cs | 5 +- TelloDroneUnitTests/DroneCommandsTests.cs | 6 +- TelloDroneUnitTests/DroneFactoryTests.cs | 2 +- TelloDroneUnitTests/DroneTests.cs | 152 ++++++++++++++++++ TelloDroneUnitTests/MissionCommanderTests.cs | 44 +++++ TelloDroneUnitTests/MissionListTests.cs | 30 ++++ .../UdpClientWrapperFactoryTests.cs | 24 +++ 16 files changed, 269 insertions(+), 47 deletions(-) delete mode 100644 Tello Drone/TestNameAttributes.cs create mode 100644 TelloDroneUnitTests/DroneTests.cs create mode 100644 TelloDroneUnitTests/MissionCommanderTests.cs create mode 100644 TelloDroneUnitTests/MissionListTests.cs create mode 100644 TelloDroneUnitTests/UdpClientWrapperFactoryTests.cs diff --git a/.idea/.idea.Tello Drone/.idea/contentModel.xml b/.idea/.idea.Tello Drone/.idea/contentModel.xml index aab3155..1a946cb 100644 --- a/.idea/.idea.Tello Drone/.idea/contentModel.xml +++ b/.idea/.idea.Tello Drone/.idea/contentModel.xml @@ -46,9 +46,7 @@ - - @@ -58,6 +56,9 @@ + + + diff --git a/Tello Drone/IUdpClientWrapperFactory.cs b/Tello Drone/IUdpClientWrapperFactory.cs index 097c1cb..1dc5f9d 100644 --- a/Tello Drone/IUdpClientWrapperFactory.cs +++ b/Tello Drone/IUdpClientWrapperFactory.cs @@ -1,5 +1,3 @@ -using System.Dynamic; - namespace Tello_Drone { public interface IUdpClientWrapperFactory diff --git a/Tello Drone/MissionCommander.cs b/Tello Drone/MissionCommander.cs index 02275bc..c8ecc68 100644 --- a/Tello Drone/MissionCommander.cs +++ b/Tello Drone/MissionCommander.cs @@ -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() diff --git a/Tello Drone/MissionList.cs b/Tello Drone/MissionList.cs index 871f5ac..b30b43a 100644 --- a/Tello Drone/MissionList.cs +++ b/Tello Drone/MissionList.cs @@ -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 diff --git a/Tello Drone/Missions/Mission2.cs b/Tello Drone/Missions/Mission2.cs index b7c783d..daab864 100644 --- a/Tello Drone/Missions/Mission2.cs +++ b/Tello Drone/Missions/Mission2.cs @@ -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); } } diff --git a/Tello Drone/Missions/Mission3.cs b/Tello Drone/Missions/Mission3.cs index 24462a3..807ce4d 100644 --- a/Tello Drone/Missions/Mission3.cs +++ b/Tello Drone/Missions/Mission3.cs @@ -9,6 +9,7 @@ namespace Tello_Drone.Missions drone.BackFlip(); drone.Left(20); drone.Right(20); + drone.Reverse(30); } } } \ No newline at end of file diff --git a/Tello Drone/TestNameAttributes.cs b/Tello Drone/TestNameAttributes.cs deleted file mode 100644 index 94b1173..0000000 --- a/Tello Drone/TestNameAttributes.cs +++ /dev/null @@ -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; } - } -} \ No newline at end of file diff --git a/Tello Drone/UdpClientWrapper.cs b/Tello Drone/UdpClientWrapper.cs index 252358f..4094fbf 100644 --- a/Tello Drone/UdpClientWrapper.cs +++ b/Tello Drone/UdpClientWrapper.cs @@ -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 diff --git a/Tello Drone/UdpClientWrapperFactory.cs b/Tello Drone/UdpClientWrapperFactory.cs index 12be098..17e6d32 100644 --- a/Tello Drone/UdpClientWrapperFactory.cs +++ b/Tello Drone/UdpClientWrapperFactory.cs @@ -1,5 +1,3 @@ -using System.Dynamic; - namespace Tello_Drone { public class UdpClientWrapperFactory : IUdpClientWrapperFactory diff --git a/TelloDroneUnitTests/ConsoleLoggerTests.cs b/TelloDroneUnitTests/ConsoleLoggerTests.cs index 5e677a0..e93245e 100644 --- a/TelloDroneUnitTests/ConsoleLoggerTests.cs +++ b/TelloDroneUnitTests/ConsoleLoggerTests.cs @@ -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 { diff --git a/TelloDroneUnitTests/DroneCommandsTests.cs b/TelloDroneUnitTests/DroneCommandsTests.cs index 4807e49..237c572 100644 --- a/TelloDroneUnitTests/DroneCommandsTests.cs +++ b/TelloDroneUnitTests/DroneCommandsTests.cs @@ -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 { diff --git a/TelloDroneUnitTests/DroneFactoryTests.cs b/TelloDroneUnitTests/DroneFactoryTests.cs index 2dfb3e5..4563fc0 100644 --- a/TelloDroneUnitTests/DroneFactoryTests.cs +++ b/TelloDroneUnitTests/DroneFactoryTests.cs @@ -2,7 +2,7 @@ using Moq; using NUnit.Framework; using Tello_Drone; -namespace Tests +namespace TelloDroneUnitTests { public class DroneFactoryTests { diff --git a/TelloDroneUnitTests/DroneTests.cs b/TelloDroneUnitTests/DroneTests.cs new file mode 100644 index 0000000..c7a2051 --- /dev/null +++ b/TelloDroneUnitTests/DroneTests.cs @@ -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 _mockDroneCommands; + [SetUp] + public void SetUp() + { + _mockDroneCommands = new Mock(); + } + + [Test] + public void ValidForwardTest() + { + var drone = new Drone(_mockDroneCommands.Object); + drone.Forward(It.IsAny()); + _mockDroneCommands.Verify(x => x.Forward(It.IsAny()), Times.Once); + } + + [Test] + public void ValidReverseTest() + { + var drone = new Drone(_mockDroneCommands.Object); + drone.Reverse(It.IsAny()); + _mockDroneCommands.Verify(x => x.Reverse(It.IsAny()), Times.Once); + } + + [Test] + public void ValidUpTest() + { + var drone = new Drone(_mockDroneCommands.Object); + drone.Up(It.IsAny()); + _mockDroneCommands.Verify(x => x.Up(It.IsAny()), Times.Once); + } + + + [Test] + public void ValidDownTest() + { + var drone = new Drone(_mockDroneCommands.Object); + drone.Down(It.IsAny()); + _mockDroneCommands.Verify(x => x.Down(It.IsAny()), Times.Once); + } + + [Test] + public void ValidLeftTest() + { + var drone = new Drone(_mockDroneCommands.Object); + drone.Left(It.IsAny()); + _mockDroneCommands.Verify(x => x.Left(It.IsAny()), Times.Once); + } + + [Test] + public void ValidRightTest() + { + var drone = new Drone(_mockDroneCommands.Object); + drone.Right(It.IsAny()); + _mockDroneCommands.Verify(x => x.Right(It.IsAny()), 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()); + _mockDroneCommands.Verify(x => x.RotateClockWise(It.IsAny()), Times.Once); + } + + } +} \ No newline at end of file diff --git a/TelloDroneUnitTests/MissionCommanderTests.cs b/TelloDroneUnitTests/MissionCommanderTests.cs new file mode 100644 index 0000000..6ac2104 --- /dev/null +++ b/TelloDroneUnitTests/MissionCommanderTests.cs @@ -0,0 +1,44 @@ +using System.Threading.Tasks; +using Moq; +using NUnit.Framework; +using Tello_Drone; + +namespace TelloDroneUnitTests +{ + public class MissionCommanderTests + { + private Mock _mockDroneFactory; + private Mock _mockMission; + private Mock _mockDrone; + private Mock _mockDroneCommands; + + [SetUp] + public void SetUp() + { + _mockDroneFactory = new Mock(); + _mockMission = new Mock(); + _mockDroneCommands = new Mock(); + _mockDrone = new Mock(_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()), 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()), Times.Never); + } + } +} \ No newline at end of file diff --git a/TelloDroneUnitTests/MissionListTests.cs b/TelloDroneUnitTests/MissionListTests.cs new file mode 100644 index 0000000..3f8aba9 --- /dev/null +++ b/TelloDroneUnitTests/MissionListTests.cs @@ -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); + } + } + } +} \ No newline at end of file diff --git a/TelloDroneUnitTests/UdpClientWrapperFactoryTests.cs b/TelloDroneUnitTests/UdpClientWrapperFactoryTests.cs new file mode 100644 index 0000000..d3b1375 --- /dev/null +++ b/TelloDroneUnitTests/UdpClientWrapperFactoryTests.cs @@ -0,0 +1,24 @@ +using System.Threading; +using Moq; +using NUnit.Framework; +using Tello_Drone; + +namespace TelloDroneUnitTests +{ + public class UdpClientWrapperFactoryTests + { + private Mock _mockConsoleLogger; + + [SetUp] + public void SetUp() + { + _mockConsoleLogger = new Mock(); + } + + [Test] + public void CreateTest() + { + Assert.IsInstanceOf(typeof(UdpClientWrapper), new UdpClientWrapperFactory(_mockConsoleLogger.Object).Create()); + } + } +} \ No newline at end of file