Skip to content

Commit

Permalink
Merge pull request #66 from PHOENIXCONTACT/feature/enum-results-display
Browse files Browse the repository at this point in the history
Support the usage of Display on enum instruction results
  • Loading branch information
Toxantron authored Aug 22, 2023
2 parents 67ce83b + fe9072b commit dbdfa11
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ public class EnumInstructionAttribute : Attribute
/// <summary>
/// The title of the button generated for the enum value marked with this attribute.
/// </summary>
[Obsolete("Use Display Attribute instead")]
public string Title { get; }

/// <summary>
/// Create instruction attribute without title
/// </summary>
public EnumInstructionAttribute()
{

}

/// <summary>
/// Constructor with title
/// </summary>
/// <param name="title">The title</param>
[Obsolete("Use empty constructor and Display Attribute instead")]
public EnumInstructionAttribute(string title)
{
Title = title;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using Moryx.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -41,9 +42,11 @@ public EnumInstructionResult(Type resultEnum, Action<int, object> callback, para
foreach (var name in Enum.GetNames(resultEnum).Except(exceptions))
{
var member = resultEnum.GetMember(name)[0];
// Try to fetch display name or title from attribute
var displayName = member.GetDisplayName();
var attribute = (EnumInstructionAttribute)member.GetCustomAttributes(typeof(EnumInstructionAttribute), false).FirstOrDefault();

var text = attribute?.Title ?? name;
var text = displayName ?? attribute?.Title ?? name;
allValues[text] = (int)Enum.Parse(resultEnum, name);

if(attribute == null)
Expand Down
23 changes: 23 additions & 0 deletions src/Tests/Moryx.ControlSystem.Tests/EnumInstructionResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

namespace Moryx.ControlSystem.Tests
Expand Down Expand Up @@ -115,5 +116,27 @@ private class MyInput
{
public int Foo { get; set; }
}

private enum TestResults6
{
[EnumInstruction, Display(Name = "Value 1")]
Value1,
[EnumInstruction("Value 2")]
Value2,
[Display(Name = "Value 3")]
Value3
}

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

// Assert
Assert.AreEqual(2, instructionResult.Results.Count(), "There should be two results, because one does not have the EnumInstruction attribute");
Assert.AreEqual("Value 1", instructionResult.Results[0]);
Assert.AreEqual("Value 2", instructionResult.Results[1]);
}
}
}

0 comments on commit dbdfa11

Please sign in to comment.