From eb3c7710d7ae583ba263daa58f5ed60d3ae81f74 Mon Sep 17 00:00:00 2001 From: Colin Date: Wed, 1 May 2019 13:20:03 +0100 Subject: [PATCH] Added unit tests for reading and writing the utcDate. Fixed a couple of defects in the code that was setting the utcDate. --- .../TelescopeControllerUnitTests.cs | 102 +++++++++++++++++- MeadeAutostar497/AscomClasses/Telescope.cs | 1 + .../Controller/TelescopeController.cs | 29 +++-- 3 files changed, 119 insertions(+), 13 deletions(-) diff --git a/MeadeAutostar497.UnitTests/TelescopeControllerUnitTests.cs b/MeadeAutostar497.UnitTests/TelescopeControllerUnitTests.cs index 9362784..a1d20b4 100644 --- a/MeadeAutostar497.UnitTests/TelescopeControllerUnitTests.cs +++ b/MeadeAutostar497.UnitTests/TelescopeControllerUnitTests.cs @@ -1,10 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO.Ports; -using ASCOM; using ASCOM.MeadeAutostar497.Controller; -using ASCOM.Utilities; using Moq; using NUnit.Framework; +using InvalidOperationException = ASCOM.InvalidOperationException; namespace MeadeAutostar497.UnitTests { @@ -38,7 +38,7 @@ namespace MeadeAutostar497.UnitTests [TearDown] public void TearDown() { - _telescopeController.Port = "COM1"; + _telescopeController.Port = "COM1"; } [Test] @@ -163,5 +163,99 @@ namespace MeadeAutostar497.UnitTests serialMock.Verify(x => x.Command("#:Q#"), Times.Once); } + + [Test] + public void SlewingReturnTrueAsExpected() + { + _isConnected = true; + + _telescopeController.Connected = true; + + serialMock.Setup(x => x.CommandTerminated(":D#", "#")).Returns("|"); + + var slewing = _telescopeController.Slewing; + + Assert.That(slewing, Is.True); + } + + [Test] + public void SlewingReturnFalseAsExpected() + { + _isConnected = true; + + _telescopeController.Connected = true; + + serialMock.Setup(x => x.CommandTerminated(":D#", "#")).Returns(string.Empty); + + var slewing = _telescopeController.Slewing; + + Assert.That(slewing, Is.False); + } + + [Test] + public void utcDate_Get_ReturnsExpectedValue() + { + DateTime expectedDate = new DateTime(2019, 04, 30, 12, 32, 24, DateTimeKind.Utc); + + var dateString = "04/30/19"; + var timeString = "12:32:24"; + + _isConnected = true; + + _telescopeController.Connected = true; + + serialMock.Setup(x => x.CommandTerminated(":GC#", "#")).Returns(dateString); + serialMock.Setup(x => x.CommandTerminated(":GL#", "#")).Returns(timeString); + + var result = _telescopeController.utcDate; + + Assert.That(result, Is.EqualTo(expectedDate)); + } + + [Test] + public void utcDate_Set_SetsTelescopeDateAndTime() + { + DateTime testDateTime = new DateTime(2019, 04, 30, 19, 53, 32, DateTimeKind.Utc); + serialMock.Setup(x => x.CommandChar($":SL{testDateTime.Hour:00}:{testDateTime.Minute:00}:{testDateTime.Second:00}#")).Returns('1'); + serialMock.Setup(x => x.CommandChar($":SC{testDateTime.Month:00}/{testDateTime.Day:00}/{testDateTime:yy}#")).Returns('1'); + + _isConnected = true; + + _telescopeController.Connected = true; + + _telescopeController.utcDate = testDateTime; + } + + [Test] + public void utcDate_Set_ThrowsExceptionWhenTimeInvalid() + { + DateTime testDateTime = new DateTime(2019, 04, 30, 19, 53, 32, DateTimeKind.Utc); + //serialMock.Setup(x => x.CommandChar($":SL{testDateTime.Hour:00}:{testDateTime.Minute:00}:{testDateTime.Second:00}#")).Returns('1'); + serialMock.Setup(x => x.CommandChar($":SC{testDateTime.Month:00}/{testDateTime.Day:00}/{testDateTime:yy}#")).Returns('1'); + + _isConnected = true; + + _telescopeController.Connected = true; + + var exception = Assert.Throws( () => {_telescopeController.utcDate = testDateTime; }); + + Assert.That( exception.Message, Is.EqualTo("Failed to set local time")); + } + + [Test] + public void utcDate_Set_ThrowsExceptionWhenDateInvalid() + { + DateTime testDateTime = new DateTime(2019, 04, 30, 19, 53, 32, DateTimeKind.Utc); + serialMock.Setup(x => x.CommandChar($":SL{testDateTime.Hour:00}:{testDateTime.Minute:00}:{testDateTime.Second:00}#")).Returns('1'); + //serialMock.Setup(x => x.CommandChar($":SC{testDateTime.Month:00}/{testDateTime.Day:00}/{testDateTime:yy}#")).Returns('1'); + + _isConnected = true; + + _telescopeController.Connected = true; + + var exception = Assert.Throws(() => { _telescopeController.utcDate = testDateTime; }); + + Assert.That(exception.Message, Is.EqualTo("Failed to set local date")); + } } } diff --git a/MeadeAutostar497/AscomClasses/Telescope.cs b/MeadeAutostar497/AscomClasses/Telescope.cs index 1574c5d..d3d2113 100644 --- a/MeadeAutostar497/AscomClasses/Telescope.cs +++ b/MeadeAutostar497/AscomClasses/Telescope.cs @@ -869,6 +869,7 @@ namespace ASCOM.MeadeAutostar497 { get { + //todo implementing this, it exists. bool tracking = true; tl.LogMessage("Tracking", "Get - " + tracking.ToString()); return tracking; diff --git a/MeadeAutostar497/Controller/TelescopeController.cs b/MeadeAutostar497/Controller/TelescopeController.cs index 33da8e6..1f620e9 100644 --- a/MeadeAutostar497/Controller/TelescopeController.cs +++ b/MeadeAutostar497/Controller/TelescopeController.cs @@ -1,7 +1,6 @@ using System; using System.IO.Ports; using System.Linq; -using ASCOM.Utilities; namespace ASCOM.MeadeAutostar497.Controller { @@ -123,7 +122,7 @@ namespace ASCOM.MeadeAutostar497.Controller int day = telescopeDate.Substring(3, 2).ToInteger(); int year = telescopeDate.Substring(6, 2).ToInteger(); - if (year < 2000) //This is a hack that will work until the end of the century + if (year < 2000) //todo fix this hack that will create a Y2K100 bug { year = year + 2000; } @@ -132,14 +131,15 @@ namespace ASCOM.MeadeAutostar497.Controller int minute = telescopeTime.Substring(3, 2).ToInteger(); int second = telescopeTime.Substring(6, 2).ToInteger(); + //Todo is this telescope local time, or real utc? var newDate = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc); return newDate; } set { - //var result = SerialCommand(":SLHH:MM:SS#", true); - var timeResult = SerialPort.CommandChar($":SL{value:hh:mm:ss}#"); + //Todo is this telescope local time, or real utc? + var timeResult = SerialPort.CommandChar($":SL{value:HH:mm:ss}#"); if (timeResult != '1') { throw new InvalidOperationException("Failed to set local time"); @@ -151,7 +151,7 @@ namespace ASCOM.MeadeAutostar497.Controller var dateResult = SerialPort.CommandChar($":SC{value:MM/dd/yy}#"); if (dateResult != '1') { - throw new InvalidOperationException("Failed to set local time"); + throw new InvalidOperationException("Failed to set local date"); } //throwing away these two strings which represent @@ -190,10 +190,10 @@ namespace ASCOM.MeadeAutostar497.Controller if (value < -90) throw new ASCOM.InvalidValueException("Latitude cannot be less than -90 degrees."); - int dd = Convert.ToInt32(Math.Floor(value)); - int mm = Convert.ToInt32(60 * (value - dd)); + int d = Convert.ToInt32(Math.Floor(value)); + int m = Convert.ToInt32(60 * (value - d)); - var result = SerialPort.CommandChar($":Sts{dd:00}*{mm:00}#"); + var result = SerialPort.CommandChar($":Sts{d:00}*{m:00}#"); if (result != '1') throw new InvalidOperationException("Failed to set site latitude."); } @@ -214,7 +214,18 @@ namespace ASCOM.MeadeAutostar497.Controller } set { - throw new ASCOM.PropertyNotImplementedException("not done yet."); + if (value >= 360) + throw new ASCOM.InvalidValueException("Longitude cannot be greater than or equal to 360 degrees."); + + if (value < 0) + throw new ASCOM.InvalidValueException("Longitude cannot be lower than 0 degrees."); + + int d = Convert.ToInt32(Math.Floor(value)); + int m = Convert.ToInt32(60 * (value - d)); + + var result = SerialPort.CommandChar($":Sg{d:000}*{m:00}#"); + if (result != '1') + throw new InvalidOperationException("Failed to set site Longitude."); } }