Lots more unit tests, and some refactoring to make the code follow SOLID better.

This commit is contained in:
2019-07-13 19:27:51 +01:00
parent 64b949551e
commit bfde58c6af
2 changed files with 128 additions and 10 deletions
@@ -1,6 +1,8 @@
using System; using System;
using System.Globalization;
using ASCOM; using ASCOM;
using ASCOM.Astrometry.AstroUtils; using ASCOM.Astrometry.AstroUtils;
using ASCOM.DeviceInterface;
using ASCOM.Meade.net; using ASCOM.Meade.net;
using ASCOM.Meade.net.AstroMaths; using ASCOM.Meade.net.AstroMaths;
using ASCOM.Meade.net.Wrapper; using ASCOM.Meade.net.Wrapper;
@@ -34,7 +36,7 @@ namespace Meade.net.Telescope.UnitTests
_astroUtilsMock = new Mock<IAstroUtils>(); _astroUtilsMock = new Mock<IAstroUtils>();
_sharedResourcesWrapperMock = new Mock<ISharedResourcesWrapper>(); _sharedResourcesWrapperMock = new Mock<ISharedResourcesWrapper>();
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GZ#")).Returns("DDD*MMSS#"); _sharedResourcesWrapperMock.Setup(x => x.SendString(":GZ#")).Returns("DDD*MMSS");
_sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497).Returns(() => "AUTOSTAR"); _sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497).Returns(() => "AUTOSTAR");
_sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497_31EE).Returns(() => "31Ee"); _sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497_31EE).Returns(() => "31Ee");
@@ -309,5 +311,123 @@ namespace Meade.net.Telescope.UnitTests
Assert.That(result, Is.EqualTo(isSupported)); Assert.That(result, Is.EqualTo(isSupported));
} }
[Test]
public void SetLongFormatFalse_WhenTelescopeReturnsShortFormat_ThenDoesNothing()
{
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GZ#")).Returns("DDD*MM");
_telescope.SetLongFormat(false);
_sharedResourcesWrapperMock.Verify(x => x.SendBlind(":U#"),Times.Never);
}
[Test]
public void SetLongFormatFalse_WhenTelescopeReturnsLongFormat_ThenTogglesPrecision()
{
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GZ#")).Returns("DDD*MMSS");
_telescope.SetLongFormat(false);
_sharedResourcesWrapperMock.Verify(x => x.SendBlind(":U#"), Times.Once);
}
[Test]
public void SetLongFormatTrue_WhenTelescopeReturnsLongFormat_ThenDoesNothing()
{
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GZ#")).Returns("DDD*MMSS");
_telescope.SetLongFormat(true);
_sharedResourcesWrapperMock.Verify(x => x.SendBlind(":U#"), Times.Never);
}
[Test]
public void SetLongFormatTrue_WhenTelescopeReturnsShortFormat_ThenTogglesPrecision()
{
_sharedResourcesWrapperMock.Setup(x => x.SendString(":GZ#")).Returns("DDD*MM");
_telescope.SetLongFormat(true);
_sharedResourcesWrapperMock.Verify(x => x.SendBlind(":U#"), Times.Once);
}
[Test]
public void SelectSite_WhenNewSiteToLow_ThenThrowsException()
{
var site = 0;
var result = Assert.Throws<ArgumentOutOfRangeException>(() => { _telescope.SelectSite(site); });
Assert.That(result.Message, Is.EqualTo($"Site cannot be lower than 1\r\nParameter name: site\r\nActual value was {site}."));
}
[Test]
public void SelectSite_WhenNewSiteToHigh_ThenThrowsException()
{
var site = 5;
var result = Assert.Throws<ArgumentOutOfRangeException>(() => { _telescope.SelectSite(site); });
Assert.That(result.Message, Is.EqualTo($"Site cannot be higher than 4\r\nParameter name: site\r\nActual value was {site}."));
}
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
[TestCase(4)]
public void SelectSite_WhenNewSiteToHigh_ThenThrowsException(int site)
{
_telescope.SelectSite(site);
_sharedResourcesWrapperMock.Verify(x => x.SendBlind($":W{site}#"), Times.Once);
}
[Test]
public void Description_Get()
{
var expectedDescription = "Meade Generic";
var description = _telescope.Description;
Assert.That(description, Is.EqualTo(expectedDescription));
}
[Test]
public void DriverVersion_Get()
{
Version version = System.Reflection.Assembly.GetAssembly(typeof(ASCOM.Meade.net.Telescope)).GetName().Version;
string exptectedDriverInfo = $"{version.Major}.{version.Minor}.{version.Revision}.{version.Build}";
var driverVersion = _telescope.DriverVersion;
Assert.That(driverVersion, Is.EqualTo(exptectedDriverInfo));
}
[Test]
public void DriverInfo_Get()
{
Version version = System.Reflection.Assembly.GetAssembly(typeof(ASCOM.Meade.net.Telescope)).GetName().Version;
string exptectedDriverInfo = $"{_telescope.Description} .net driver. Version: {_telescope.DriverVersion}";
var driverInfo = _telescope.DriverInfo;
Assert.That(driverInfo, Is.EqualTo(exptectedDriverInfo));
}
[Test]
public void InterfaceVersion_Get()
{
var interfaceVersion = _telescope.InterfaceVersion;
Assert.That(interfaceVersion, Is.EqualTo(3));
Assert.That(_telescope, Is.AssignableTo<ITelescopeV3>());
}
[Test]
public void Name_Get()
{
string expectedName = "Meade Generic";
var name = _telescope.Name;
Assert.That(name, Is.EqualTo(expectedName));
}
} }
} }
+7 -9
View File
@@ -356,7 +356,7 @@ namespace ASCOM.Meade.net
{ {
var result = _sharedResourcesWrapper.SendString(":GZ#"); var result = _sharedResourcesWrapper.SendString(":GZ#");
//:GZ# Get telescope azimuth //:GZ# Get telescope azimuth
//Returns: DDD*MM#T or DDD*MMSS# //Returns: DDD*MM# or DDD*MMSS#
//The current telescope Azimuth depending on the selected precision. //The current telescope Azimuth depending on the selected precision.
bool isLongFormat = result.Length > 6; bool isLongFormat = result.Length > 6;
@@ -373,12 +373,13 @@ namespace ASCOM.Meade.net
}); });
} }
private void SelectSite(int site) //todo hook this up to a custom action
public void SelectSite(int site)
{ {
if (site < 1) if (site < 1)
throw new ArgumentOutOfRangeException("site cannot be lower than 1"); throw new ArgumentOutOfRangeException("site",site,"Site cannot be lower than 1");
else if (site > 4) else if (site > 4)
throw new ArgumentOutOfRangeException("site cannot be higher than 4"); throw new ArgumentOutOfRangeException("site", site, "Site cannot be higher than 4");
_sharedResourcesWrapper.SendBlind($":W{site}#"); _sharedResourcesWrapper.SendBlind($":W{site}#");
//:W<n># //:W<n>#
@@ -400,10 +401,8 @@ namespace ASCOM.Meade.net
{ {
get get
{ {
Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
// TODO customise this driver description // TODO customise this driver description
string driverInfo = "Meade Generic .net driver. Version: " + String.Format(CultureInfo.InvariantCulture, "{0}.{1}", version.Major, string driverInfo = $"{Description} .net driver. Version: {DriverVersion}";
version.Minor);
LogMessage("DriverInfo Get", driverInfo); LogMessage("DriverInfo Get", driverInfo);
return driverInfo; return driverInfo;
} }
@@ -414,8 +413,7 @@ namespace ASCOM.Meade.net
get get
{ {
Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
string driverVersion = string driverVersion = $"{version.Major}.{version.Minor}.{version.Revision}.{version.Build}";
String.Format(CultureInfo.InvariantCulture, "{0}.{1}", version.Major, version.Minor);
LogMessage("DriverVersion Get", driverVersion); LogMessage("DriverVersion Get", driverVersion);
return driverVersion; return driverVersion;
} }