Skip to content

Commit

Permalink
Merge branch 'tkouba-develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-hassan committed Jul 10, 2019
2 parents e9340b0 + c8ed98b commit 9f647b6
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 12 deletions.
33 changes: 25 additions & 8 deletions src/CommandLine/BaseAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public abstract class BaseAttribute : Attribute
private int min;
private int max;
private object @default;
private string helpText;
private Infrastructure.LocalizableAttributeProperty helpText;
private string metaValue;
private Type resourceType;

/// <summary>
/// Initializes a new instance of the <see cref="CommandLine.BaseAttribute"/> class.
Expand All @@ -22,8 +23,9 @@ protected internal BaseAttribute()
{
min = -1;
max = -1;
helpText = string.Empty;
helpText = new Infrastructure.LocalizableAttributeProperty(nameof(HelpText));
metaValue = string.Empty;
resourceType = null;
}

/// <summary>
Expand Down Expand Up @@ -90,11 +92,8 @@ public object Default
/// </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>
Expand All @@ -105,7 +104,12 @@ public string MetaValue
get { return metaValue; }
set
{
metaValue = value ?? throw new ArgumentNullException("value");
if (value == null)
{
throw new ArgumentNullException("value");
}

metaValue = value;
}
}

Expand All @@ -117,5 +121,18 @@ public bool Hidden
get;
set;
}

/// <summary>
/// Gets or sets the <see cref="System.Type"/> that contains the resources for <see cref="HelpText"/>.
/// </summary>
public Type ResourceType
{
get { return resourceType; }
set
{
resourceType =
helpText.ResourceType = value;
}
}
}
}
58 changes: 58 additions & 0 deletions src/CommandLine/Infrastructure/LocalizableAttributeProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CommandLine.Infrastructure
{
internal class LocalizableAttributeProperty
{
private string _propertyName;
private string _value;
private Type _type;
private PropertyInfo _localizationPropertyInfo;

public LocalizableAttributeProperty(string propertyName)
{
_propertyName = propertyName;
}

public string Value
{
get { return GetLocalizedValue(); }
set
{
_localizationPropertyInfo = null;
_value = value;
}
}

public Type ResourceType
{
set
{
_localizationPropertyInfo = null;
_type = value;
}
}

private string GetLocalizedValue()
{
if (String.IsNullOrEmpty(_value) || _type == null)
return _value;
if (_localizationPropertyInfo == null)
{
// Static class IsAbstract
if (!_type.IsVisible)
throw new ArgumentException("Invalid resource type", _propertyName);
PropertyInfo propertyInfo = _type.GetProperty(_value, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static);
if (propertyInfo == null || !propertyInfo.CanRead || propertyInfo.PropertyType != typeof(string))
throw new ArgumentException("Invalid resource property name", _propertyName);
_localizationPropertyInfo = propertyInfo;
}
return (string)_localizationPropertyInfo.GetValue(null, null);
}
}

}
31 changes: 31 additions & 0 deletions tests/CommandLine.Tests/Fakes/ResourceFakes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommandLine.Tests.Fakes
{
public static class StaticResource
{
public static string HelpText { get { return "Localized HelpText"; } }
}

public class NonStaticResource
{
public static string HelpText { get { return "Localized HelpText"; } }
public static string WriteOnlyText { set { value?.ToString(); } }
private static string PrivateHelpText { get { return "Localized HelpText"; } }
}

public class NonStaticResource_WithNonStaticProperty
{
public string HelpText { get { return "Localized HelpText"; } }
}

internal class InternalResource
{
public static string HelpText { get { return "Localized HelpText"; } }
}

}
36 changes: 32 additions & 4 deletions tests/CommandLine.Tests/Unit/BaseAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace CommandLine.Tests.Unit
Expand All @@ -20,12 +16,44 @@ public static void Default(object defaultValue)
Assert.Equal(defaultValue, baseAttribute.Default);
}

[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 HelpText(string helpText, Type resourceType, string expected)
{
TestBaseAttribute baseAttribute = new TestBaseAttribute();
baseAttribute.HelpText = helpText;
baseAttribute.ResourceType = resourceType;

Assert.Equal(expected, baseAttribute.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)
{
TestBaseAttribute baseAttribute = new TestBaseAttribute();
baseAttribute.HelpText = helpText;
baseAttribute.ResourceType = resourceType;

// Verify exception
Assert.Throws<ArgumentException>(() => baseAttribute.HelpText.ToString());
}


private class TestBaseAttribute : BaseAttribute
{
public TestBaseAttribute()
{
// Do nothing
}
}

}
}

0 comments on commit 9f647b6

Please sign in to comment.