Skip to content

Commit

Permalink
added notification tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed Jun 30, 2020
1 parent 780abe1 commit 576393c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
1 change: 0 additions & 1 deletion TFU002/TFU002.Logic/GatewayNotificationsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Serilog;
using Sharp7.Rx.Enums;
using Sharp7.Rx.Interfaces;
using TFU002.Interfaces.Extensions;
using TwinCAT.Ads;
using TwinCAT.Ads.Reactive;
using TwinCAT.TypeSystem;
Expand Down
88 changes: 88 additions & 0 deletions TFU002/TFU002.Tests/GatewayNotificationExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Net.Sockets;
using System.Reactive.Linq;
using Moq;
using Sharp7.Rx.Enums;
using Sharp7.Rx.Interfaces;
using TFU002.Logic;
using Xunit;

namespace TFU002.Tests
{
public class GatewayNotificationExtensionsTests
{
public static Mock<IPlc> CreatePlcMock<T>()
{
var mock = new Mock<IPlc>();
mock.Setup(plc => plc.CreateNotification<T>(It.IsAny<string>(), It.IsAny<TransmissionMode>(), It.IsAny<TimeSpan>()))
.Returns(Observable.Never<T>());
return mock;
}

[Theory]
[ClassData(typeof(TestTypeData))]
public void CreateTypedPlcNotification(Type type)
{
var plcMock = (Mock<IPlc>)typeof(GatewayNotificationExtensionsTests).GetMethod("CreatePlcMock")?.MakeGenericMethod(type).Invoke(null, null);

plcMock.Object
.GetTypedS7Notification(type, "address", null, null, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100));


if (type == typeof(byte[]))
{
plcMock.Verify(plc => plc.CreateNotification<byte[]>("address", TransmissionMode.Cyclic, TimeSpan.FromMilliseconds(100)));
return;
}

var typecode = Type.GetTypeCode(type);
switch (typecode)
{
case TypeCode.Boolean:
plcMock.Verify(plc => plc.CreateNotification<bool>("address", TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100)));
break;
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Char:
plcMock.Verify(plc => plc.CreateNotification<byte>("address", TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100)));
break;
case TypeCode.Int16:
case TypeCode.UInt16:
plcMock.Verify(plc => plc.CreateNotification<short>("address", TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100)));
break;
case TypeCode.Int32:
case TypeCode.UInt32:
plcMock.Verify(plc => plc.CreateNotification<int>("address", TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100)));
break;
case TypeCode.Int64:
case TypeCode.UInt64:
plcMock.Verify(plc => plc.CreateNotification<long>("address", TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100)));
break;
case TypeCode.Single:
plcMock.Verify(plc => plc.CreateNotification<float>("address", TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100)));
break;
default:
throw new ArgumentException($"Unsupported Type {type.Name}");
}
}

}

public class TestTypeData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[] { typeof(byte[]) };
yield return new object[] { typeof(byte) };
yield return new object[] { typeof(bool) };
yield return new object[] { typeof(int) };
yield return new object[] { typeof(short) };
yield return new object[] { typeof(float) };
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
5 changes: 5 additions & 0 deletions TFU002/TFU002.Tests/TFU002.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Moq" Version="4.14.4" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TFU002.Logic\TFU002.Logic.csproj" />
</ItemGroup>

</Project>

0 comments on commit 576393c

Please sign in to comment.