Skip to content

Commit

Permalink
Merge pull request #473 from moh-hassan/localize-verb
Browse files Browse the repository at this point in the history
Localize VerbAttribute
  • Loading branch information
moh-hassan authored Jul 16, 2019
2 parents 9f647b6 + 09c8f70 commit 86d7582
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/CommandLine/VerbAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace CommandLine
/// Models a verb command specification.
/// </summary>
[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;

/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.VerbAttribute"/> class.
Expand All @@ -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;
}

/// <summary>
Expand All @@ -48,11 +51,16 @@ public bool Hidden
/// </summary>
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");
}
/// <summary>
/// Gets or sets the <see cref="System.Type"/> that contains the resources for <see cref="HelpText"/>.
/// </summary>
public Type ResourceType
{
get => resourceType;
set => resourceType =helpText.ResourceType = value;
}
}
}
}
50 changes: 50 additions & 0 deletions tests/CommandLine.Tests/Unit/VerbAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentException>(() => verbAttribute.HelpText);
}

private class TestVerbAttribute : VerbAttribute
{
public TestVerbAttribute() : base("verb")
{
// Do nothing
}
}
}
}

0 comments on commit 86d7582

Please sign in to comment.