Skip to content

Commit

Permalink
added tests for solution
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed Oct 13, 2019
1 parent a4b9c95 commit 49e143d
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 1 deletion.
46 changes: 46 additions & 0 deletions TwinCAT.JsonExtension.Tests/DebugSymbol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using TwinCAT.Ads;
using TwinCAT.TypeSystem;

namespace TwinCAT.JsonExtension.Tests
{
internal class DebugSymbol : ITcAdsSymbol, ITcAdsSymbol5
{
public long IndexGroup { get; set; }
public long IndexOffset { get; set; }
public int Size { get; set; }
public AdsDatatypeId Datatype { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Comment { get; set; }
public bool IsPersistent { get; set;}
public bool IsBitType { get; set;}
public bool IsReference { get; set;}
public bool IsPointer { get; set;}
public bool IsTypeGuid { get; set;}
public bool IsReadOnly { get; set;}
public bool IsTcComInterfacePointer { get; set;}
public int ContextMask { get; set;}
public bool IsArray { get; set;}
public int ArrayDimensions { get; set;}
public AdsDatatypeArrayInfo[] ArrayInfos { get; set;}
public ReadOnlyTypeAttributeCollection Attributes { get; set;}
public bool IsEnum { get; set;}
public bool IsStruct { get; set;}
public bool HasRpcMethods { get; set;}
public ReadOnlyRpcMethodCollection RpcMethods { get; set;}
public DataTypeCategory Category { get; set;}
public int BitSize { get; set;}
public int ByteSize { get; set;}
public bool IsRecursive(IEnumerable<ITcAdsSymbol5> parents)
{
throw new System.NotImplementedException();
}

public ITcAdsDataType DataType { get; set;}
public AdsDatatypeId DataTypeId { get; set;}
public string TypeName { get; set;}
public bool IsStatic { get; set;}
}
}
55 changes: 55 additions & 0 deletions TwinCAT.JsonExtension.Tests/DebugType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Linq;
using TwinCAT.Ads;
using TwinCAT.Ads.Internal;
using TwinCAT.TypeSystem;

namespace TwinCAT.JsonExtension.Tests
{
internal class DebugType : ITcAdsDataType
{
public int Size { get; set;}
public bool IsBitType { get; set;}
public int BitSize { get; set;}
public int ByteSize { get; set;}
public bool IsByteAligned { get; set;}
public int Id { get; set;}
public DataTypeCategory Category { get; set;}
public string Name { get; set;}
public string Namespace { get; set;}
public string FullName { get; set;}
public bool IsPrimitive { get; set;}
public bool IsContainer { get; set;}
public bool IsPointer { get; set;}
public bool IsReference { get; set;}
public ReadOnlyTypeAttributeCollection Attributes { get; set;}
public string Comment { get; set;}
public IDataType ResolveType(DataTypeResolveStrategy type)
{
throw new NotImplementedException();
}

public AdsDatatypeId DataTypeId { get; set;}
public bool HasArrayInfo { get; set;}
public ReadOnlyDimensionCollection Dimensions { get; set;}
public bool HasRpcMethods { get; set;}
public ReadOnlyRpcMethodCollection RpcMethods { get; set;}
public ITcAdsDataType BaseType { get; set;}
public string BaseTypeName { get; set;}
public bool HasEnumInfo { get; set;}
public ReadOnlyEnumValueCollection EnumInfos { get; set;}
public ReadOnlyEnumValueCollection EnumValues { get; set;}
public ReadOnlySubItemCollection SubItems { get; set;}
public bool HasSubItemInfo { get; set;}
public bool IsEnum { get; set;}
public bool IsArray { get; set;}
public bool IsStruct { get; set;}
public bool IsSubItem { get; set;}
public bool IsAlias { get; set;}
public bool IsString { get; set;}
public Type ManagedType { get; set;}
public bool IsOversamplingArray { get; set;}
public AdsDataTypeFlags Flags { get; set;}
public bool IsJaggedArray { get; set;}
}
}
98 changes: 98 additions & 0 deletions TwinCAT.JsonExtension.Tests/TestReadWriteOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Shouldly;
using TwinCAT.Ads;

namespace TwinCAT.JsonExtension.Tests
{
public class TestReadWriteOperations
{
[SetUp]
public void Setup()
{
}

[Test]
public async Task ReadSymbolWithDifferentType()
{
var clientMock = new Mock<IAdsSymbolicAccess>();

var value = 10;
clientMock.Setup(client => client.ReadSymbol(It.IsAny<ITcAdsSymbol>()))
.Returns(value);
var symbol = new DebugSymbol();
clientMock.Setup(client => client.ReadSymbolInfo(It.IsAny<string>()))
.Returns(symbol);

var variableName = "test.object";
var readVariable = await clientMock.Object.ReadAsync<string>(variableName);

clientMock.Verify(client => client.ReadSymbolInfo(variableName), Times.Once);
clientMock.Verify(client => client.ReadSymbol(symbol), Times.Once);

readVariable.ShouldBe(value.ToString());
}

[Test]
public async Task ReadSymbol()
{
var clientMock = new Mock<IAdsSymbolicAccess>();

int value = 10;
clientMock.Setup(client => client.ReadSymbol(It.IsAny<ITcAdsSymbol>()))
.Returns(value);
var symbol = new DebugSymbol();
clientMock.Setup(client => client.ReadSymbolInfo(It.IsAny<string>()))
.Returns(symbol);

var variableName = "test.object";
var readVariable = await clientMock.Object.ReadAsync<int>(variableName);

clientMock.Verify(client => client.ReadSymbolInfo(variableName), Times.Once);
clientMock.Verify(client => client.ReadSymbol(symbol), Times.Once);

readVariable.ShouldBe(value);
}

[Test]
public async Task WriteSymbol()
{
var clientMock = new Mock<IAdsSymbolicAccess>();

var symbol = new DebugSymbol();
var targetType = typeof(int);
symbol.DataType = new DebugType(){ManagedType = targetType};

clientMock.Setup(client => client.ReadSymbolInfo(It.IsAny<string>()))
.Returns(symbol);
var variableName = "test.object";
int value = 10;
await clientMock.Object.WriteAsync(variableName, value);

clientMock.Verify(client => client.ReadSymbolInfo(variableName), Times.Once);
clientMock.Verify(client => client.WriteSymbol(symbol, value), Times.Once);
}

[Test]
public async Task WriteSymbolWithDifferentType()
{
var clientMock = new Mock<IAdsSymbolicAccess>();

var symbol = new DebugSymbol();
var targetType = typeof(string);
symbol.DataType = new DebugType() { ManagedType = targetType };

clientMock.Setup(client => client.ReadSymbolInfo(It.IsAny<string>()))
.Returns(symbol);
var variableName = "test.object";
int value = 10;
await clientMock.Object.WriteAsync(variableName, value);

clientMock.Verify(client => client.ReadSymbolInfo(variableName), Times.Once);
clientMock.Verify(client => client.WriteSymbol(symbol, value.ToString()), Times.Once);
}
}
}
22 changes: 22 additions & 0 deletions TwinCAT.JsonExtension.Tests/TwinCAT.JsonExtension.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net45</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Beckhoff.TwinCAT.Ads" Version="4.3.7" />
<PackageReference Include="Moq" Version="4.13.0" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TwinCAT.JsonExtension\TwinCAT.JsonExtension.csproj" />
</ItemGroup>

</Project>
8 changes: 7 additions & 1 deletion TwinCAT.JsonExtension.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.352
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwinCAT.JsonExtension", "TwinCAT.JsonExtension\TwinCAT.JsonExtension.csproj", "{781232EA-EB55-49AE-BFA2-1926F590873D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TwinCAT.JsonExtension", "TwinCAT.JsonExtension\TwinCAT.JsonExtension.csproj", "{781232EA-EB55-49AE-BFA2-1926F590873D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwinCAT.JsonExtension.Tests", "TwinCAT.JsonExtension.Tests\TwinCAT.JsonExtension.Tests.csproj", "{20ADDB82-8C7B-446C-B177-A05999290DF7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{781232EA-EB55-49AE-BFA2-1926F590873D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{781232EA-EB55-49AE-BFA2-1926F590873D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{781232EA-EB55-49AE-BFA2-1926F590873D}.Release|Any CPU.Build.0 = Release|Any CPU
{20ADDB82-8C7B-446C-B177-A05999290DF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20ADDB82-8C7B-446C-B177-A05999290DF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20ADDB82-8C7B-446C-B177-A05999290DF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20ADDB82-8C7B-446C-B177-A05999290DF7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 49e143d

Please sign in to comment.