tbf/TBFTests/Rig/RegisterReaders/iPerlASICReader/communication/C4/IperlHatProtocol/IperlHatFrameBuilderTests.cs

179 lines
6.5 KiB
C#

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TBF.Rig.RegisterReaders.iPerlASICReader.communication.C4.diagnosticLed;
using TBF.Rig.RegisterReaders.iPerlASICReader.communication.C4.hexLogger;
using TBF.Rig.RegisterReaders.iPerlASICReader.communication.C4.IperlHatProtocol;
using TBF.Rig.RegisterReaders.iPerlASICReader.communication.C4.protocolCommons;
namespace TBFTests.Rig.RegisterReaders.iPerlASICReader.communication.C4.IperlHatProtocol
{
[TestClass]
public class IperlHatFrameBuilderTests
{
[TestMethod]
public void Encode_ViewFactoryId_Command()
{
byte[] frame = new IperlHatFrameBuilder()
.RequestResponse(true)
.AddCommand(ProtocolCommand.ViewFactoryId)
.BuildBytes();
byte[] expected =
{
Constants.Start, // START
Constants.Write, // DIRECTION
0x05, // LEN
(byte)ProtocolCommand.ViewFactoryId, // COMMAND
Constants.End // END
};
CollectionAssert.AreEqual(expected, frame);
string log = IperlHatLogger.DescribeTx(frame);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(frame));
}
[TestMethod]
public void Encode_VERS_Command()
{
byte[] frame = new IperlHatFrameBuilder()
.RequestResponse(true)
.AddCommand(ProtocolCommand.Question)
.AddPayload(Constants.Version)
.BuildBytes();
byte[] expected = HexFormatter.HexStringToByteArray("53 57 3F 76 65 72 73 0D");
CollectionAssert.AreEqual(expected, frame);
string log = IperlHatLogger.DescribeTx(frame);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(frame));
}
[TestMethod]
public void Encode_VERS2_Command()
{
byte[] frame = new IperlHatFrameBuilder()
.RequestResponse(true)
.SetVersionCommand()
.BuildBytes();
byte[] expected = HexFormatter.HexStringToByteArray("53 57 3F 76 65 72 73 0D");
CollectionAssert.AreEqual(expected, frame);
string log = IperlHatLogger.DescribeTx(frame);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(frame));
}
[TestMethod]
public void Encode_ViewProgrammableId_Command()
{
byte[] frame = new IperlHatFrameBuilder()
.RequestResponse(true)
.AddCommand(ProtocolCommand.ViewProgrammableId)
.BuildBytes();
byte[] expected = HexFormatter.HexStringToByteArray("53 57 05 03 0D");
CollectionAssert.AreEqual(expected, frame);
string log = IperlHatLogger.DescribeTx(frame);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(frame));
}
[TestMethod]
public void Encode_ViewState_Command()
{
byte[] frame = new IperlHatFrameBuilder()
.RequestResponse(true)
.AddCommand(ProtocolCommand.ViewState)
.BuildBytes();
byte[] expected = HexFormatter.HexStringToByteArray("53 57 05 19 0D");
CollectionAssert.AreEqual(expected, frame);
string log = IperlHatLogger.DescribeTx(frame);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(frame));
}
[TestMethod]
public void Encode_SetState_Idle_Command()
{
byte[] frame = new IperlHatFrameBuilder()
.RequestResponse(true)
.AddCommand(ProtocolCommand.SetState)
.AddSubCommand(ProtocolStatuses.Idle) // Idle
.BuildBytes();
byte[] expected = HexFormatter.HexStringToByteArray("53 57 06 1A 01 0D");
CollectionAssert.AreEqual(expected, frame);
string log = IperlHatLogger.DescribeTx(frame);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(frame));
}
[TestMethod]
public void Encode_SetState_Active_Command()
{
byte[] frame = new IperlHatFrameBuilder()
.RequestResponse(true)
.AddCommand(ProtocolCommand.SetState)
.AddSubCommand(ProtocolStatuses.Active) // Active
.BuildBytes();
byte[] expected = HexFormatter.HexStringToByteArray("53 57 06 1A 02 0D");
CollectionAssert.AreEqual(expected, frame);
string log = IperlHatLogger.DescribeTx(frame);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(frame));
}
[TestMethod]
public void Encode_SetDiagnosticLEDState_Status4_Command()
{
// ----------- Arrange -----------
byte[] request = new IperlHatFrameBuilder()
.RequestResponse(true)
.AddDeviceCommand(ProtocolDeviceSubCommand.SetDiagnosticLEDState)
.AddPayload(DiagnosticLedState.State4)
.BuildBytes();
byte[] expected = HexFormatter.HexStringToByteArray("53 57 07 FD 60 04 0D");
CollectionAssert.AreEqual(expected, request);
string log = IperlHatLogger.DescribeTx(request);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(request));
}
[TestMethod]
public void AddDiagnosticLedState()
{
// ----------- Arrange -----------
byte[] request = new IperlHatFrameBuilder()
.AddDiagnosticLedState(DiagnosticLedState.State4)
.BuildBytes();
byte[] expected = HexFormatter.HexStringToByteArray("53 57 07 FD 60 04 0D");
CollectionAssert.AreEqual(expected, request);
string log = IperlHatLogger.DescribeTx(request);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(request));
}
}
}