Added unit tests for reading and writing the utcDate.

Fixed a couple of defects in the code that was setting the utcDate.
This commit is contained in:
2019-05-01 13:20:03 +01:00
parent ef982a3aba
commit eb3c7710d7
3 changed files with 119 additions and 13 deletions
@@ -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
{
@@ -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<ASCOM.InvalidOperationException>( () => {_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<ASCOM.InvalidOperationException>(() => { _telescopeController.utcDate = testDateTime; });
Assert.That(exception.Message, Is.EqualTo("Failed to set local date"));
}
}
}
@@ -869,6 +869,7 @@ namespace ASCOM.MeadeAutostar497
{
get
{
//todo implementing this, it exists.
bool tracking = true;
tl.LogMessage("Tracking", "Get - " + tracking.ToString());
return tracking;
@@ -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.");
}
}