From 4b65c946d2c4cbb27868cfd92c0d799460656e76 Mon Sep 17 00:00:00 2001 From: Colin Date: Fri, 19 Jul 2019 12:49:56 +0100 Subject: [PATCH] Unit testing --- .../TelescopeUnitTests.cs | 123 +++++++++++++++++- Meade.net.Telescope/Telescope.cs | 9 +- 2 files changed, 128 insertions(+), 4 deletions(-) diff --git a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs index e975e26..f1c0fb6 100644 --- a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs +++ b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs @@ -45,6 +45,8 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny())).Callback(action => { action(); }); _sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny>())).Returns>( (func) => func()); + _sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny>())).Returns>((func) => func()); + _sharedResourcesWrapperMock.Setup(x => x.ReadProfile()).Returns(_profileProperties); @@ -1102,7 +1104,6 @@ namespace Meade.net.Telescope.UnitTests _telescope.Connected = true; _sharedResourcesWrapperMock.Setup(x => x.SendString(":GR#")).Returns(telescopeRaResult); - _utilMock.Setup(x => x.HMSToHours(telescopeRaResult)).Returns(hmsResult); var result = _telescope.RightAscension; @@ -2263,5 +2264,125 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Verify(x => x.SendChar(":MS#"), Times.Once); _utilMock.Verify(x => x.WaitForMilliseconds(It.IsAny()), Times.Exactly(iterations)); } + + [Test] + public void Azimuth_WhenNotConnected_ThenThrowsException() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + + var exception = Assert.Throws(() => { var result = _telescope.Azimuth; }); + Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: Azimuth Get")); + } + + [Test] + public void Azimuth_WhenConnected_ThenReturnsTelescopeAzumith() + { + var expectedAzimuth = 200; + + var telescopeLongitude = "350"; + var telescopeLongitudeValue = 350; + + var telescopeLatitude = "HH:MM:SS"; + var telescopeLatitudeValue = 1.2; + + var mockHourAngle = 3; + + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + _telescope.Connected = true; + + _sharedResourcesWrapperMock.Setup(x => x.SendString(":GC#")).Returns("10/15/20"); + _sharedResourcesWrapperMock.Setup(x => x.SendString(":GL#")).Returns("20:15:10"); + _sharedResourcesWrapperMock.Setup(x => x.SendString(":GG#")).Returns("-1.0"); + + _sharedResourcesWrapperMock.Setup(x => x.SendString(":Gg#")).Returns(telescopeLongitude); + _utilMock.Setup(x => x.DMSToDegrees(telescopeLongitude)).Returns(telescopeLongitudeValue); + + _sharedResourcesWrapperMock.Setup(x => x.SendString(":GR#")).Returns(telescopeLatitude); + _utilMock.Setup(x => x.HMSToHours(telescopeLatitude)).Returns(telescopeLatitudeValue); + + _astroMathsMock.Setup(x => x.RightAscensionToHourAngle(It.IsAny(), It.IsAny(), It.IsAny())).Returns(mockHourAngle); + + _astroMathsMock.Setup(x => x.ConvertEqToHoz(mockHourAngle, It.IsAny(), It.IsAny())).Returns( new HorizonCoordinates{ Altitude = 45, Azimuth = expectedAzimuth }); + + var result = _telescope.Azimuth; + + Assert.That(result,Is.EqualTo(expectedAzimuth)); + } + + [Test] + public void Altitude_WhenNotConnected_ThenThrowsException() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + + var exception = Assert.Throws(() => { var result = _telescope.Altitude; }); + Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: Altitude Get")); + } + + [Test] + public void Altitude_WhenConnected_ThenReturnsTelescopeAltitude() + { + var expectedAltitude = 45; + + var telescopeLongitude = "350"; + var telescopeLongitudeValue = 350; + + var telescopeLatitude = "HH:MM:SS"; + var telescopeLatitudeValue = 1.2; + + var mockHourAngle = 3; + + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + _telescope.Connected = true; + + _sharedResourcesWrapperMock.Setup(x => x.SendString(":GC#")).Returns("10/15/20"); + _sharedResourcesWrapperMock.Setup(x => x.SendString(":GL#")).Returns("20:15:10"); + _sharedResourcesWrapperMock.Setup(x => x.SendString(":GG#")).Returns("-1.0"); + + _sharedResourcesWrapperMock.Setup(x => x.SendString(":Gg#")).Returns(telescopeLongitude); + _utilMock.Setup(x => x.DMSToDegrees(telescopeLongitude)).Returns(telescopeLongitudeValue); + + _sharedResourcesWrapperMock.Setup(x => x.SendString(":GR#")).Returns(telescopeLatitude); + _utilMock.Setup(x => x.HMSToHours(telescopeLatitude)).Returns(telescopeLatitudeValue); + + _astroMathsMock.Setup(x => x.RightAscensionToHourAngle(It.IsAny(), It.IsAny(), It.IsAny())).Returns(mockHourAngle); + + _astroMathsMock.Setup(x => x.ConvertEqToHoz(mockHourAngle, It.IsAny(), It.IsAny())).Returns(new HorizonCoordinates { Altitude = expectedAltitude, Azimuth = 200 }); + + var result = _telescope.Altitude; + + Assert.That(result, Is.EqualTo(expectedAltitude)); + } + + [Test] + public void AbortSlew_WhenNotConnected_ThenThrowsException() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + + var exception = Assert.Throws(() => { _telescope.AbortSlew(); }); + Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: AbortSlew")); + } + + [Test] + public void AbortSlew_WhenConnected_ThenSendsStopSlewingToTelescope() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + _telescope.Connected = true; + + _telescope.AbortSlew(); + + _sharedResourcesWrapperMock.Verify( x => x.SendBlind(":Q#"),Times.Once); + + var isSloSlewing = _telescope.Slewing; + + Assert.That(isSloSlewing, Is.False); + _sharedResourcesWrapperMock.Verify( x => x.SendString(":D#"), Times.Once); + } } } +; \ No newline at end of file diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index a95943e..d9d187c 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -464,6 +464,9 @@ namespace ASCOM.Meade.net _sharedResourcesWrapper.SendBlind(":Q#"); //:Q# Halt all current slewing //Returns:Nothing + + _movingPrimary = false; + _movingSecondary = false; } public AlignmentModes AlignmentMode @@ -552,7 +555,7 @@ namespace ASCOM.Meade.net { get { - CheckConnected("Altitude get"); + CheckConnected("Altitude Get"); var altAz = CalcAltAzFromTelescopeEqData(); LogMessage("Altitude", $"{altAz.Altitude}"); @@ -642,7 +645,7 @@ namespace ASCOM.Meade.net { get { - CheckConnected("Azimuth get"); + CheckConnected("Azimuth Get"); //var result = _sharedResourcesWrapper.SendString(":GZ#"); //:GZ# Get telescope azimuth @@ -1261,7 +1264,7 @@ namespace ASCOM.Meade.net var longitude = _sharedResourcesWrapper.SendString(":Gg#"); //:Gg# Get Current Site Longitude - //Returns: sDDD* MM# + //Returns: sDDD*MM# //The current site Longitude. East Longitudes are expressed as negative double siteLongitude = _utilities.DMSToDegrees(longitude);