More Unit Tests

This commit is contained in:
2019-09-09 22:16:51 -06:00
parent 52a566eaf4
commit d99bce4d14
12 changed files with 296 additions and 6 deletions

View File

@@ -7,6 +7,9 @@
<e p="FastExpressionCompiler.cs" t="Include" />
<e p="ImTools.cs" t="Include" />
</e>
<e p="$USER_HOME$/.nuget/packages/nunit3testadapter/3.11.0/build/netcoreapp1.0/nunit.engine.netstandard.dll" t="Include" />
<e p="$USER_HOME$/.nuget/packages/nunit3testadapter/3.11.0/build/netcoreapp1.0/NUnit3.TestAdapter.dll" t="Include" />
<e p="$USER_HOME$/.nuget/packages/nunit3testadapter/3.11.0/build/netcoreapp1.0/NUnit3.TestAdapter.pdb" t="Include" />
<e p="$USER_HOME$/Library/Caches/Rider2019.2/extResources" t="IncludeRecursive" />
<e p="$USER_HOME$/Library/Caches/Rider2019.2/resharper-host/local/Transient/ReSharperHost/v192/SolutionCaches/_Tello Drone.1285879164.00" t="ExcludeRecursive" />
<e p="$PROJECT_DIR$" t="IncludeFlat">
@@ -26,6 +29,7 @@
<e p="IMissions.cs" t="Include" />
<e p="IModule.cs" t="Include" />
<e p="IUdpClientWrapper.cs" t="Include" />
<e p="IUdpClientWrapperFactory.cs" t="Include" />
<e p="Missions.cs" t="Include" />
<e p="obj" t="ExcludeRecursive">
<e p="Debug" t="Include">
@@ -37,8 +41,24 @@
<e p="Program.cs" t="Include" />
<e p="Tello Drone.csproj" t="IncludeRecursive" />
<e p="UdpClientWrapper.cs" t="Include" />
<e p="UdpClientWrapperFactory.cs" t="Include" />
</e>
<e p="Tello Drone.sln" t="IncludeFlat" />
<e p="TelloDroneUnitTests" t="IncludeRecursive">
<e p="bin" t="ExcludeRecursive" />
<e p="ConsoleLoggerTests.cs" t="Include" />
<e p="DroneCommandsTests.cs" t="Include" />
<e p="DroneFactoryTests.cs" t="Include" />
<e p="obj" t="ExcludeRecursive">
<e p="Debug" t="Include">
<e p="netcoreapp2.2" t="Include">
<e p="TelloDroneUnitTests.AssemblyInfo.cs" t="Include" />
<e p="TelloDroneUnitTests.Program.cs" t="Include" />
</e>
</e>
</e>
<e p="TelloDroneUnitTests.csproj" t="IncludeRecursive" />
</e>
</e>
</component>
</project>

View File

@@ -2,6 +2,9 @@
<module type="RIDER_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$USER_HOME$/.nuget/packages/dryioc/4.0.6/contentFiles/cs/netstandard2.0/DryIoc" />
<content url="file://$USER_HOME$/.nuget/packages/nunit3testadapter/3.11.0/build/netcoreapp1.0/NUnit3.TestAdapter.dll" />
<content url="file://$USER_HOME$/.nuget/packages/nunit3testadapter/3.11.0/build/netcoreapp1.0/NUnit3.TestAdapter.pdb" />
<content url="file://$USER_HOME$/.nuget/packages/nunit3testadapter/3.11.0/build/netcoreapp1.0/nunit.engine.netstandard.dll" />
<content url="file://$MODULE_DIR$/../.." />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

@@ -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

View File

@@ -11,5 +11,6 @@ namespace Tello_Drone
LogReceived?.Invoke(message);
Console.WriteLine(message);
}
}
}

View File

@@ -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)

View File

@@ -0,0 +1,9 @@
using System.Dynamic;
namespace Tello_Drone
{
public interface IUdpClientWrapperFactory
{
IUdpClientWrapper Create();
}
}

View File

@@ -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();
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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<IUdpClientWrapperFactory> _mockUdpClientFactory;
private Mock<IUdpClientWrapper> _mockUdpClientWrapper;
[SetUp]
public void Setup()
{
_mockUdpClientFactory = new Mock<IUdpClientWrapperFactory>();
_mockUdpClientWrapper = new Mock<IUdpClientWrapper>();
}
[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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), Times.Once);
}
[Test]
public void CommandStateTest()
{
_mockUdpClientWrapper.Setup(x => x.TrySend(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>())).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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), 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<int>(), It.IsAny<int>()), Times.Once);
}
}
}

View File

@@ -0,0 +1,26 @@
using Moq;
using NUnit.Framework;
using Tello_Drone;
namespace Tests
{
public class DroneFactoryTests
{
private Mock<IDroneCommands> _mockDroneCommands;
[SetUp]
public void Setup()
{
_mockDroneCommands = new Mock<IDroneCommands>();
}
[Test]
public void CreateDroneTests()
{
var droneFactory = new DroneFactory(_mockDroneCommands.Object);
var drone = droneFactory.CreateDrone;
Assert.AreEqual(typeof(Drone), drone.GetType());
}
}
}

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.13.0" />
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tello Drone\Tello Drone.csproj" />
</ItemGroup>
</Project>