From 09c8f70d03a3c6ee8ded2476b955d3956fd782c4 Mon Sep 17 00:00:00 2001 From: "Moh.Hassan" Date: Thu, 11 Jul 2019 21:14:50 +0200 Subject: [PATCH] Localize VerbAttribute --- src/CommandLine/VerbAttribute.cs | 26 ++++++---- .../Unit/VerbAttributeTests.cs | 50 +++++++++++++++++++ 2 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 tests/CommandLine.Tests/Unit/VerbAttributeTests.cs diff --git a/src/CommandLine/VerbAttribute.cs b/src/CommandLine/VerbAttribute.cs index 0078a7a8..5515bd20 100644 --- a/src/CommandLine/VerbAttribute.cs +++ b/src/CommandLine/VerbAttribute.cs @@ -8,10 +8,12 @@ namespace CommandLine /// Models a verb command specification. /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)] - public sealed class VerbAttribute : Attribute + //public sealed class VerbAttribute : Attribute + public class VerbAttribute : Attribute { private readonly string name; - private string helpText; + private Infrastructure.LocalizableAttributeProperty helpText; + private Type resourceType; /// /// Initializes a new instance of the class. @@ -23,7 +25,8 @@ public VerbAttribute(string name) if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("name"); this.name = name; - this.helpText = string.Empty; + helpText = new Infrastructure.LocalizableAttributeProperty(nameof(HelpText)); + resourceType = null; } /// @@ -48,11 +51,16 @@ public bool Hidden /// public string HelpText { - get { return helpText; } - set - { - helpText = value ?? throw new ArgumentNullException("value"); - } + get => helpText.Value??string.Empty; + set => helpText.Value = value ?? throw new ArgumentNullException("value"); + } + /// + /// Gets or sets the that contains the resources for . + /// + public Type ResourceType + { + get => resourceType; + set => resourceType =helpText.ResourceType = value; } } -} \ No newline at end of file +} diff --git a/tests/CommandLine.Tests/Unit/VerbAttributeTests.cs b/tests/CommandLine.Tests/Unit/VerbAttributeTests.cs new file mode 100644 index 00000000..4dc1dd30 --- /dev/null +++ b/tests/CommandLine.Tests/Unit/VerbAttributeTests.cs @@ -0,0 +1,50 @@ +using System; +using Xunit; + +namespace CommandLine.Tests +{ + //Test localization of VerbAttribute + public class VerbAttributeTests + { + [Theory] + [InlineData("", null, "")] + [InlineData("", typeof(Fakes.StaticResource), "")] + [InlineData("Help text", null, "Help text")] + [InlineData("HelpText", typeof(Fakes.StaticResource), "Localized HelpText")] + [InlineData("HelpText", typeof(Fakes.NonStaticResource), "Localized HelpText")] + public static void VerbHelpText(string helpText, Type resourceType, string expected) + { + TestVerbAttribute verbAttribute = new TestVerbAttribute + { + HelpText = helpText, + ResourceType = resourceType + }; + + Assert.Equal(expected, verbAttribute.HelpText); + } + [Theory] + [InlineData("HelpText", typeof(Fakes.NonStaticResource_WithNonStaticProperty))] + [InlineData("WriteOnlyText", typeof(Fakes.NonStaticResource))] + [InlineData("PrivateOnlyText", typeof(Fakes.NonStaticResource))] + [InlineData("HelpText", typeof(Fakes.InternalResource))] + public void ThrowsHelpText(string helpText, Type resourceType) + { + TestVerbAttribute verbAttribute = new TestVerbAttribute + { + HelpText = helpText, + ResourceType = resourceType + }; + + // Verify exception + Assert.Throws(() => verbAttribute.HelpText); + } + + private class TestVerbAttribute : VerbAttribute + { + public TestVerbAttribute() : base("verb") + { + // Do nothing + } + } + } +}