diff options
author | Hugh Bellamy <hughbellars@gmail.com> | 2017-07-11 06:29:22 +0300 |
---|---|---|
committer | Stephen Toub <stoub@microsoft.com> | 2017-07-11 06:29:22 +0300 |
commit | 9430efeb8ee5b722bde59b45de2e5f4551764ad1 (patch) | |
tree | b7eb3ae4f277b9662ef3668ac67dcb1431c01ebc /src/System.ComponentModel.TypeConverter | |
parent | 4ab41d47ad08e6e70a97f811c02afb6f93d5cd80 (diff) |
Add AmbientValueAttribute tests and fix a debug assertion (#22027)
* Add AmbientValueAttribute tests and fix a debug assertion
* Address PR feedback
Diffstat (limited to 'src/System.ComponentModel.TypeConverter')
3 files changed, 172 insertions, 56 deletions
diff --git a/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/AmbientValueAttribute.cs b/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/AmbientValueAttribute.cs index 4b26b4074b..f1be518264 100644 --- a/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/AmbientValueAttribute.cs +++ b/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/AmbientValueAttribute.cs @@ -2,33 +2,26 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; -using System.ComponentModel; -using System.Diagnostics; -using System.Globalization; -using System.Runtime.InteropServices; -using System.Security.Permissions; +using System.Diagnostics.CodeAnalysis; namespace System.ComponentModel { /// <summary> - /// <para>Specifies the ambient value for a property. The ambient value is the value you - /// can set into a property to make it inherit its ambient.</para> + /// Specifies the ambient value for a property. The ambient value is the value you + /// can set into a property to make it inherit its ambient. /// </summary> - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")] + [SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")] [AttributeUsage(AttributeTargets.All)] public sealed class AmbientValueAttribute : Attribute { /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class, converting the - /// specified value to the - /// specified type, and using the U.S. English culture as the - /// translation - /// context.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class, converting the + /// specified value to the specified type, and using the U.S. English culture as the + /// translation context. /// </summary> public AmbientValueAttribute(Type type, string value) { - // The try/catch here is because attributes should never throw exceptions. We would fail to + // The try/catch here is because attributes should never throw exceptions. We would fail to // load an otherwise normal class. try { @@ -36,42 +29,45 @@ namespace System.ComponentModel } catch { - Debug.Fail($"Ambient value attribute of type {type.FullName} threw converting from the string '{value}'."); } } /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a Unicode - /// character.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a Unicode + /// character. /// </summary> public AmbientValueAttribute(char value) { Value = value; } + /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using an 8-bit unsigned - /// integer.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using an 8-bit unsigned + /// integer. /// </summary> public AmbientValueAttribute(byte value) { Value = value; } + /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a 16-bit signed - /// integer.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a 16-bit signed + /// integer. /// </summary> public AmbientValueAttribute(short value) { Value = value; } + /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a 32-bit signed - /// integer.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a 32-bit signed + /// integer. /// </summary> public AmbientValueAttribute(int value) { Value = value; } + /// <summary> /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a 64-bit signed /// integer.</para> @@ -80,34 +76,36 @@ namespace System.ComponentModel { Value = value; } + /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a - /// single-precision floating point - /// number.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a + /// single-precision floating point number. /// </summary> public AmbientValueAttribute(float value) { Value = value; } + /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a - /// double-precision floating point - /// number.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a + /// double-precision floating point number. /// </summary> public AmbientValueAttribute(double value) { Value = value; } + /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a <see cref='System.Boolean'/> - /// value.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a <see cref='System.Boolean'/> + /// value. /// </summary> public AmbientValueAttribute(bool value) { Value = value; } + /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a <see cref='System.String'/>.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a <see cref='System.String'/>. /// </summary> public AmbientValueAttribute(string value) { @@ -115,8 +113,8 @@ namespace System.ComponentModel } /// <summary> - /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> - /// class.</para> + /// Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> + /// class. /// </summary> public AmbientValueAttribute(object value) { @@ -124,11 +122,7 @@ namespace System.ComponentModel } /// <summary> - /// <para> - /// Gets the ambient value of the property this - /// attribute is - /// bound to. - /// </para> + /// Gets the ambient value of the property this attribute is bound to. /// </summary> public object Value { get; } @@ -139,26 +133,14 @@ namespace System.ComponentModel return true; } - AmbientValueAttribute other = obj as AmbientValueAttribute; - - if (other != null) + if (obj is AmbientValueAttribute other) { - if (Value != null) - { - return Value.Equals(other.Value); - } - else - { - return (other.Value == null); - } + return Value != null ? Value.Equals(other.Value) : other.Value == null; } + return false; } - public override int GetHashCode() - { - return base.GetHashCode(); - } + public override int GetHashCode() => base.GetHashCode(); } } - diff --git a/src/System.ComponentModel.TypeConverter/tests/AmbientValueAttributeTests.cs b/src/System.ComponentModel.TypeConverter/tests/AmbientValueAttributeTests.cs new file mode 100644 index 0000000000..166670a484 --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/AmbientValueAttributeTests.cs @@ -0,0 +1,133 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Xunit; + +namespace System.ComponentModel.Tests +{ + public class AmbientValueAttributeTests + { + [Theory] + [InlineData(null, null, null)] + [InlineData(typeof(int*), "1", null)] + [InlineData(typeof(string), "1", "1")] + [InlineData(typeof(int), "1", 1)] + public void Ctor_Type_Value(Type type, string value, object expectedValue) + { + var attribute = new AmbientValueAttribute(type, value); + Assert.Equal(expectedValue, attribute.Value); + } + + [Fact] + public void Ctor_Char() + { + char value = 'a'; + var args = new AmbientValueAttribute(value); + Assert.Equal(value, args.Value); + } + + [Fact] + public void Ctor_Byte() + { + byte value = 123; + var args = new AmbientValueAttribute(value); + Assert.Equal(value, args.Value); + } + + [Fact] + public void Ctor_Short() + { + short value = 123; + var args = new AmbientValueAttribute(value); + Assert.Equal(value, args.Value); + } + + [Fact] + public void Ctor_Int() + { + int value = 123; + var args = new AmbientValueAttribute(value); + Assert.Equal(value, args.Value); + } + + [Fact] + public void Ctor_Long() + { + long value = 123; + var args = new AmbientValueAttribute(value); + Assert.Equal(value, args.Value); + } + + [Fact] + public void Ctor_Float() + { + float value = 123; + var args = new AmbientValueAttribute(value); + Assert.Equal(value, args.Value); + } + + [Fact] + public void Ctor_Double() + { + double value = 123; + var args = new AmbientValueAttribute(value); + Assert.Equal(value, args.Value); + } + + [Fact] + public void Ctor_Bool() + { + bool value = true; + var args = new AmbientValueAttribute(value); + Assert.Equal(value, args.Value); + } + + [Theory] + [InlineData(null)] + [InlineData("Value")] + public void Ctor_String(string value) + { + var args = new AmbientValueAttribute(value); + Assert.Same(value, args.Value); + } + + [Theory] + [InlineData(null)] + [InlineData("Value")] + public void Ctor_Object(object value) + { + var args = new AmbientValueAttribute(value); + Assert.Same(value, args.Value); + } + + public static IEnumerable<object[]> Equals_TestData() + { + var attribute = new AmbientValueAttribute(true); + yield return new object[] { attribute, attribute, true }; + yield return new object[] { new AmbientValueAttribute(true), new AmbientValueAttribute(true), true }; + yield return new object[] { new AmbientValueAttribute(true), new AmbientValueAttribute(false), false }; + yield return new object[] { new AmbientValueAttribute(true), new AmbientValueAttribute(null), false }; + yield return new object[] { new AmbientValueAttribute(null), new AmbientValueAttribute(false), false }; + yield return new object[] { new AmbientValueAttribute(null), new AmbientValueAttribute(null), true }; + + yield return new object[] { new AmbientValueAttribute(true), new object(), false }; + yield return new object[] { new AmbientValueAttribute(true), null, false }; + } + + [Theory] + [MemberData(nameof(Equals_TestData))] + public void Equals_Object_ReturnsExpected(AmbientValueAttribute attribute, object other, bool expected) + { + Assert.Equal(expected, attribute.Equals(other)); + } + + [Fact] + public void GetHashCode_Invoke_ReturnsConsistentValue() + { + var attribute = new AmbientValueAttribute(null); + Assert.Equal(attribute.GetHashCode(), attribute.GetHashCode()); + } + } +} diff --git a/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj b/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj index 7327afe6ec..23c5f5cdcd 100644 --- a/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj +++ b/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj @@ -25,6 +25,7 @@ <Compile Include="$(CommonTestPath)\System\PlatformDetection.cs"> <Link>Common\tests\System\PlatformDetection.cs</Link> </Compile> + <Compile Include="AmbientValueAttributeTests.cs" /> <Compile Include="ArrayConverterTests.cs" /> <Compile Include="AttributeCollectionTests.cs" /> <Compile Include="AttributeProviderAttributeTests.cs" /> |