From fe9072b027a6f4cabd2cfd442b6cd95feeb41bf7 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Tue, 22 Aug 2023 16:07:59 +0200 Subject: [PATCH] Support the usage of Display on enum instruction results --- .../EnumInstructionAttribute.cs | 11 ++++++++- .../EnumInstructionResult.cs | 5 +++- .../EnumInstructionResultTests.cs | 23 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Moryx.ControlSystem/VisualInstructions/EnumInstructionAttribute.cs b/src/Moryx.ControlSystem/VisualInstructions/EnumInstructionAttribute.cs index 34e5bf2..2e572c1 100644 --- a/src/Moryx.ControlSystem/VisualInstructions/EnumInstructionAttribute.cs +++ b/src/Moryx.ControlSystem/VisualInstructions/EnumInstructionAttribute.cs @@ -19,12 +19,21 @@ public class EnumInstructionAttribute : Attribute /// /// The title of the button generated for the enum value marked with this attribute. /// + [Obsolete("Use Display Attribute instead")] public string Title { get; } + /// + /// Create instruction attribute without title + /// + public EnumInstructionAttribute() + { + + } + /// /// Constructor with title /// - /// The title + [Obsolete("Use empty constructor and Display Attribute instead")] public EnumInstructionAttribute(string title) { Title = title; diff --git a/src/Moryx.ControlSystem/VisualInstructions/EnumInstructionResult.cs b/src/Moryx.ControlSystem/VisualInstructions/EnumInstructionResult.cs index cfe3350..cdc03bf 100644 --- a/src/Moryx.ControlSystem/VisualInstructions/EnumInstructionResult.cs +++ b/src/Moryx.ControlSystem/VisualInstructions/EnumInstructionResult.cs @@ -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; @@ -41,9 +42,11 @@ public EnumInstructionResult(Type resultEnum, Action 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) diff --git a/src/Tests/Moryx.ControlSystem.Tests/EnumInstructionResultTests.cs b/src/Tests/Moryx.ControlSystem.Tests/EnumInstructionResultTests.cs index cefc967..6475f5b 100644 --- a/src/Tests/Moryx.ControlSystem.Tests/EnumInstructionResultTests.cs +++ b/src/Tests/Moryx.ControlSystem.Tests/EnumInstructionResultTests.cs @@ -3,6 +3,7 @@ using Moryx.ControlSystem.VisualInstructions; using NUnit.Framework; +using System.ComponentModel.DataAnnotations; using System.Linq; namespace Moryx.ControlSystem.Tests @@ -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]); + } } }