From 8980c3c6b52ea6c3a3cabb69d179e3641ced811f Mon Sep 17 00:00:00 2001 From: Colin Date: Sat, 13 Jul 2019 20:02:06 +0100 Subject: [PATCH] More unit testing. Added check for to be able to support setting the alignment mode --- .../TelescopeUnitTests.cs | 79 ++++++++++++++++++- Meade.net.Telescope/Telescope.cs | 27 ++++--- Meade.net/Wrapper/SharedResourcesWrapper.cs | 3 + 3 files changed, 99 insertions(+), 10 deletions(-) diff --git a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs index a3868db..4b4ab51 100644 --- a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs +++ b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs @@ -9,6 +9,7 @@ using ASCOM.Meade.net.Wrapper; using ASCOM.Utilities.Interfaces; using Moq; using NUnit.Framework; +using NotImplementedException = ASCOM.NotImplementedException; namespace Meade.net.Telescope.UnitTests { @@ -39,6 +40,7 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Setup(x => x.SendString(":GZ#")).Returns("DDD*MM’SS"); _sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497).Returns(() => "AUTOSTAR"); _sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497_31EE).Returns(() => "31Ee"); + _sharedResourcesWrapperMock.Setup(x => x.AUTOSTAR497_43EG) .Returns(() => "43Eg"); _sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny())).Callback(action => { action(); }); @@ -300,7 +302,7 @@ namespace Meade.net.Telescope.UnitTests [TestCase("AUTOSTAR", "30Ab", false)] [TestCase("AUTOSTAR","31Ee", true)] - [TestCase("AUTOSTAR", "41Aa", true)] + [TestCase("AUTOSTAR", "43Eg", true)] [TestCase("AUTOSTAR II", "", false)] public void IsNewPulseGuidingSupported_ThenIsSupported_ThenReturnsTrue(string productName, string firmware, bool isSupported) { @@ -429,5 +431,80 @@ namespace Meade.net.Telescope.UnitTests Assert.That(name, Is.EqualTo(expectedName)); } + + [Test] + public void AlignmentMode_Get_WhenNotConnected_ThrowsException() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + + var exception = Assert.Throws(() => { var actualResult = _telescope.AlignmentMode; }); + Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: AlignmentMode Get")); + } + + + [TestCase("A", AlignmentModes.algAltAz)] + [TestCase("P", AlignmentModes.algPolar)] + [TestCase("G", AlignmentModes.algGermanPolar)] + public void AlignmentMode_Get_WhenScopeInAltAz_ReturnsAltAz(string telescopeMode, AlignmentModes alignmentMode) + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + _telescope.Connected = true; + + const char ack = (char)6; + _sharedResourcesWrapperMock.Setup(x => x.SendChar(ack.ToString())).Returns(telescopeMode); + + var actualResult = _telescope.AlignmentMode; + + Assert.That(actualResult, Is.EqualTo(alignmentMode)); + } + + [Test] + public void AlignmentMode_Get_WhenUnknownAlignmentMode_ThrowsException() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + _telescope.Connected = true; + + Assert.Throws(() => { var actualResult = _telescope.AlignmentMode; }); + } + + [Test] + public void AlignmentMode_Set_WhenNotConnected_ThrowsException() + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => _sharedResourcesWrapperMock.Object.AUTOSTAR497_31EE); + + var exception = Assert.Throws(() => { _telescope.AlignmentMode = AlignmentModes.algAltAz; }); + Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: AlignmentMode Set")); + } + + [TestCase("AUTOSTAR", "43Eg", AlignmentModes.algAltAz, ":AA#")] + [TestCase("AUTOSTAR", "43Eg", AlignmentModes.algPolar, ":AP#")] + [TestCase("AUTOSTAR", "43Eg", AlignmentModes.algGermanPolar, ":AP#")] + public void AlignmentMode_Set_WhenConnected_ThenSendsExpectedCommand(string productName, string firmware, AlignmentModes alignmentMode, string expectedCommand) + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(productName); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(firmware); + _telescope.Connected = true; + + _telescope.AlignmentMode = alignmentMode; + + _sharedResourcesWrapperMock.Verify( x => x.SendBlind(expectedCommand), Times.Once); + } + + [TestCase("AUTOSTAR", "43Ef")] + public void AlignmentMode_Set_WhenAutostarFirmwareToLow_ThenThrowsException(string productName, string firmware ) + { + _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(productName); + _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(firmware); + _telescope.Connected = true; + + var excpetion = Assert.Throws(() => { _telescope.AlignmentMode = AlignmentModes.algAltAz; }); + + Assert.That(excpetion.Property, Is.EqualTo("AlignmentMode")); + Assert.That(excpetion.AccessorSet, Is.True); + } } } diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index 3dab40a..b95c53a 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -482,14 +482,17 @@ namespace ASCOM.Meade.net //L If scope in Land Mode //P If scope in Polar Mode - //todo implement GW Command - //var alignmentString = SerialPort.CommandTerminated(":GW#", "#"); - //:GW# Get Scope Alignment Status - //Returns: # - // where: - //mount: A - AzEl mounted, P - Equatorially mounted, G - german mounted equatorial - //tracking: T - tracking, N - not tracking - //alignment: 0 - needs alignment, 1 - one star aligned, 2 - two star aligned, 3 - three star aligned. + //todo implement GW Command - Supported in Autostar 43Eg and above + //if FirmwareIsGreaterThan(_sharedResourcesWrapper.AUTOSTAR497_43EG) + //{ + //var alignmentString = SerialPort.CommandTerminated(":GW#", "#"); + //:GW# Get Scope Alignment Status + //Returns: # + // where: + //mount: A - AzEl mounted, P - Equatorially mounted, G - german mounted equatorial + //tracking: T - tracking, N - not tracking + //alignment: 0 - needs alignment, 1 - one star aligned, 2 - two star aligned, 3 - three star aligned. + //} AlignmentModes alignmentMode; switch (alignmentString) @@ -515,7 +518,13 @@ namespace ASCOM.Meade.net { CheckConnected("AlignmentMode Set"); - switch (value) + //todo tidy this up into a better solution that means can :GW#, :AL#, :AA#, & :AP# and checked for Autostar properly + if (!FirmwareIsGreaterThan(_sharedResourcesWrapper.AUTOSTAR497_43EG)) + throw new PropertyNotImplementedException("AlignmentMode",true ); + + //todo make this only try with Autostar 43Eg and above. + + switch (value) { case AlignmentModes.algAltAz: _sharedResourcesWrapper.SendBlind(":AA#"); diff --git a/Meade.net/Wrapper/SharedResourcesWrapper.cs b/Meade.net/Wrapper/SharedResourcesWrapper.cs index 90caa72..a7f662c 100644 --- a/Meade.net/Wrapper/SharedResourcesWrapper.cs +++ b/Meade.net/Wrapper/SharedResourcesWrapper.cs @@ -10,6 +10,8 @@ namespace ASCOM.Meade.net.Wrapper string AUTOSTAR497 { get; } string AUTOSTAR497_31EE { get; } + string AUTOSTAR497_43EG { get;} + void Connect(string deviceId); void Disconnect(string deviceId); @@ -38,6 +40,7 @@ namespace ASCOM.Meade.net.Wrapper public string AUTOSTAR497 => "Autostar"; public string AUTOSTAR497_31EE => "31Ee"; + public string AUTOSTAR497_43EG => "43Eg"; #endregion