Skip to content

Commit

Permalink
Merge pull request #28 from slawor/fix/EnumInstructionResult
Browse files Browse the repository at this point in the history
Added unittests and fixed EnumInstructionResult
  • Loading branch information
Toxantron authored Dec 10, 2022
2 parents c11363d + deb06aa commit 5849b4f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Moryx.ControlSystem/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Moryx.ControlSystem.Tests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public EnumInstructionResult(Type resultEnum, Action<int> callback, params strin
{
_callback = callback;

var allHidden = true;
var allValues = new Dictionary<string, int>();

foreach (var name in Enum.GetNames(resultEnum).Except(exceptions))
{
var member = resultEnum.GetMember(name)[0];
Expand All @@ -38,12 +38,19 @@ public EnumInstructionResult(Type resultEnum, Action<int> callback, params strin
var text = attribute?.Title ?? name;
allValues[text] = (int)Enum.Parse(resultEnum, name);

if (attribute != null && !attribute.Hide)
if(attribute == null)
{
allHidden = false;
}
else if(!attribute.Hide)
{
allHidden = false;
_valueMap[text] = allValues[text];
}
}

// If we found no entries, the display attribute was not used and we take all entries
if (_valueMap.Count == 0)
if (_valueMap.Count == 0 && !allHidden)
_valueMap = allValues;
}

Expand Down
99 changes: 99 additions & 0 deletions src/Tests/Moryx.ControlSystem.Tests/EnumInstructionResultTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using Moryx.ControlSystem.VisualInstructions;
using NUnit.Framework;
using System.Linq;

namespace Moryx.ControlSystem.Tests
{
[TestFixture]
public class EnumInstructionResultTests
{
private enum TestResults1
{
Value1,
Value2
}

[Test]
public void GetAllValuesIfNoResultIsDecorated()
{
// Act
var instructionResult = new EnumInstructionResult(typeof(TestResults1), result => { });

// Assert
Assert.AreEqual(2, instructionResult.Results.Count(), "There should be 2 results because all of the results are not decorated");
}

private enum TestResults2
{
[EnumInstruction("Value1")]
Value1,
[EnumInstruction("Value2")]
Value2
}

[Test]
public void GetAllValuesIfAllResultsAreDecorated()
{
// Act
var instructionResult = new EnumInstructionResult(typeof(TestResults2), result => { });

// Assert
Assert.AreEqual(2, instructionResult.Results.Count(), "There should be 2 results because all of the results are decorated");
}

private enum TestResults3
{
[EnumInstruction("Value1")]
Value1,
Value2
}

[Test]
public void GetValuesWithAnAttribute()
{
// Act
var instructionResult = new EnumInstructionResult(typeof(TestResults3), result => { });

// Assert
Assert.AreEqual(1, instructionResult.Results.Count(), "There should be 1 result because only one value is decorated");
}

private enum TestResults4
{
[EnumInstruction("Value1", Hide = true)]
Value1,
Value2
}

[Test]
public void GetAllValuesIfThereAreHiddenAndNotDecoratedResults()
{
// Act
var instructionResult = new EnumInstructionResult(typeof(TestResults4), result => { });

// Assert
Assert.AreEqual(2, instructionResult.Results.Count(), "There should be no results because there are hidden and not decorated results");
}

private enum TestResults5
{
[EnumInstruction("Value1", Hide = true)]
Value1,
[EnumInstruction("Value2", Hide = true)]
Value2
}

[Test]
public void GetNoValuesIfAllResultsAreHidden()
{
// Act
var instructionResult = new EnumInstructionResult(typeof(TestResults5), result => { });

// Assert
Assert.AreEqual(0, instructionResult.Results.Count(), "There should be no results because all of them are hidden");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Moq" />

<PackageReference Include="Moryx.AbstractionLayer.TestTools" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Moryx.ControlSystem.Tests
{

[TestFixture]
public class ProductionSessionTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

0 comments on commit 5849b4f

Please sign in to comment.