diff options
author | Hugh Bellamy <hughbellars@gmail.com> | 2017-07-13 01:52:07 +0300 |
---|---|---|
committer | Stephen Toub <stoub@microsoft.com> | 2017-07-13 01:52:07 +0300 |
commit | 1f0334d5066001231cb6d8955f848f33c1257c0b (patch) | |
tree | 6955c9bc6fcbdd9dcea78a5edb8c98003c19aad4 /src/System.ComponentModel.TypeConverter | |
parent | 035b1e367c18db600c9d7158102fd2ab4e7a7ed2 (diff) |
Add ComponentResourceManager tests (#22138)
* Add ComponentResourceManager tests
* Baseline netfx failures for #22145
Diffstat (limited to 'src/System.ComponentModel.TypeConverter')
5 files changed, 414 insertions, 5 deletions
diff --git a/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/ComponentResourceManager.cs b/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/ComponentResourceManager.cs index e7baf6f8e6..265da59ef2 100644 --- a/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/ComponentResourceManager.cs +++ b/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/ComponentResourceManager.cs @@ -132,12 +132,7 @@ namespace System.ComponentModel foreach (KeyValuePair<string, object> kvp in resources) { // See if this key matches our object. - // string key = kvp.Key; - if (key == null) - { - continue; - } if (IgnoreCase) { diff --git a/src/System.ComponentModel.TypeConverter/tests/ComponentResourceManagerTests.cs b/src/System.ComponentModel.TypeConverter/tests/ComponentResourceManagerTests.cs new file mode 100644 index 0000000000..d4534f2f99 --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/ComponentResourceManagerTests.cs @@ -0,0 +1,204 @@ +// 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 System.Globalization; +using System.Reflection; +using Xunit; + +namespace System.ComponentModel.Tests +{ + public class ComponentResourceManagerTests + { + [Fact] + public void Ctor_Default() + { + var resourceManager = new ComponentResourceManager(); + Assert.Null(resourceManager.BaseName); + Assert.False(resourceManager.IgnoreCase); + Assert.NotNull(resourceManager.ResourceSetType); + } + + [Theory] + [InlineData(typeof(int))] + public void Ctor_Type(Type type) + { + var resourceManager = new ComponentResourceManager(type); + Assert.Equal("Int32", resourceManager.BaseName); + Assert.False(resourceManager.IgnoreCase); + Assert.NotNull(resourceManager.ResourceSetType); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void ApplyResources_ValueExists_ReturnsExpected(bool ignoreCase) + { + var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) + { + IgnoreCase = ignoreCase + }; + + var value = new TestValue(); + resourceManager.ApplyResources(value, "Object"); + Assert.Equal("One", value.GetSetProperty); + Assert.Null(value.GetOnlyProperty); + Assert.Null(value.GetPrivateProperty()); + } + + private class TestValue + { + public string GetSetProperty { get; set; } + public string GetOnlyProperty { get; } + private string PrivateProperty { get; set; } + + public string GetPrivateProperty() => PrivateProperty; + } + + [Fact] + public void ApplyResources_AmibguousWithSameDeclaringType_ThrowsAmbiguousMatchException() + { + var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) + { + IgnoreCase = true + }; + + var value = new MulticasedClass(); + Assert.Throws<AmbiguousMatchException>(() => resourceManager.ApplyResources(value, "Object")); + } + + private class MulticasedClass + { + public string GetSetProperty { get; set; } + public string getsetproperty { get; set; } + } + + public static IEnumerable<object[]> AmbiguousWithDifferentDeclaringType_TestData() + { + yield return new object[] { new MulticaseSubClass() }; + yield return new object[] { new MulticaseSubSubClass() }; + } + + [Theory] + [MemberData(nameof(AmbiguousWithDifferentDeclaringType_TestData))] + public void ApplyResources_AmibguousWithDifferentDeclaringTypeInValueType_UsesMostDeclaredProperty<T>(T value) where T : MulticaseSubClass + { + var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) + { + IgnoreCase = true + }; + + resourceManager.ApplyResources(value, "Object"); + + Assert.Null(value.GetSetProperty); + Assert.Equal("One", value.getsetproperty); + } + + public class MulticaseBaseClass + { + public string GetSetProperty { get; set; } + } + + public class MulticaseSubClass : MulticaseBaseClass + { + public string getsetproperty { get; set; } + } + + public class MulticaseSubSubClass : MulticaseSubClass { } + + [Fact] + public void ApplyResources_IComponentWithNullSite_Success() + { + var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) + { + IgnoreCase = true + }; + + var value = new TestComponent(); + resourceManager.ApplyResources(value, "Object"); + Assert.Equal("One", value.GetSetProperty); + } + + [Fact] + public void ApplyResources_IComponentWithNonDesignModeSite_Success() + { + var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) + { + IgnoreCase = true + }; + + var value = new TestComponent { Site = new TestSite { DesignMode = false } }; + resourceManager.ApplyResources(value, "Object"); + Assert.Equal("One", value.GetSetProperty); + } + + [Fact] + [ActiveIssue(22145, TargetFrameworkMonikers.NetFramework)] + public void ApplyResources_IComponentWithDesignModeSite_Success() + { + var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) + { + IgnoreCase = true + }; + + var value = new TestComponent { Site = new TestSite { DesignMode = true } }; + resourceManager.ApplyResources(value, "Object"); + Assert.Equal("One", value.GetSetProperty); + } + + private class TestSite : ISite + { + public bool DesignMode { get; set; } + + public IComponent Component => throw new NotImplementedException(); + public IContainer Container => throw new NotImplementedException(); + public string Name { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public object GetService(Type serviceType) => null; + } + + private class TestComponent : IComponent + { + public ISite Site { get; set; } + +#pragma warning disable 0067 + public event EventHandler Disposed; +#pragma warning restore 0067 + + public void Dispose() { } + + public string GetSetProperty { get; set; } + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void ApplyResources_NoSuchValue_Nop(bool ignoreCase) + { + var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) + { + IgnoreCase = ignoreCase + }; + + resourceManager.ApplyResources("Value", "ObjectName"); + resourceManager.ApplyResources("Value", "ObjectName", CultureInfo.CurrentUICulture); + resourceManager.ApplyResources("Value", "ObjectName", CultureInfo.InvariantCulture); + } + + [Fact] + public void ApplyResources_NullValue_ThrowsArgumentNullException() + { + var resourceManager = new ComponentResourceManager(); + AssertExtensions.Throws<ArgumentNullException>("value", () => resourceManager.ApplyResources(null, "objectName")); + AssertExtensions.Throws<ArgumentNullException>("value", () => resourceManager.ApplyResources(null, "objectName", CultureInfo.CurrentCulture)); + } + + [Fact] + public void ApplyResources_NullObjectName_ThrowsArgumentNullException() + { + var resourceManager = new ComponentResourceManager(); + AssertExtensions.Throws<ArgumentNullException>("objectName", () => resourceManager.ApplyResources("value", null)); + AssertExtensions.Throws<ArgumentNullException>("objectName", () => resourceManager.ApplyResources("value", null, CultureInfo.CurrentCulture)); + } + } +} diff --git a/src/System.ComponentModel.TypeConverter/tests/Resources/TestResx.Designer.cs b/src/System.ComponentModel.TypeConverter/tests/Resources/TestResx.Designer.cs new file mode 100644 index 0000000000..b92d389fb5 --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/Resources/TestResx.Designer.cs @@ -0,0 +1,118 @@ +//------------------------------------------------------------------------------ +// <auto-generated> +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// </auto-generated> +//------------------------------------------------------------------------------ + +namespace Resources { + using System; + using System.Reflection; + + + /// <summary> + /// A strongly-typed resource class, for looking up localized strings, etc. + /// </summary> + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class TestResx { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal TestResx() { + } + + /// <summary> + /// Returns the cached ResourceManager instance used by this class. + /// </summary> + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Resources.TestResx", typeof(TestResx).GetTypeInfo().Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// <summary> + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// </summary> + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// <summary> + /// Looks up a localized string similar to No Name. + /// </summary> + internal static string _ { + get { + return ResourceManager.GetString("", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Value-One. + /// </summary> + internal static string Object { + get { + return ResourceManager.GetString("Object", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Third. + /// </summary> + internal static string Object_GetOnlyProperty { + get { + return ResourceManager.GetString("Object.GetOnlyProperty", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to One. + /// </summary> + internal static string Object_GetSetProperty { + get { + return ResourceManager.GetString("Object.GetSetProperty", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Second. + /// </summary> + internal static string Object_NoSuchProperty { + get { + return ResourceManager.GetString("Object.NoSuchProperty", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Third. + /// </summary> + internal static string Object_PrivateProperty { + get { + return ResourceManager.GetString("Object.PrivateProperty", resourceCulture); + } + } + } +} diff --git a/src/System.ComponentModel.TypeConverter/tests/Resources/TestResx.resx b/src/System.ComponentModel.TypeConverter/tests/Resources/TestResx.resx new file mode 100644 index 0000000000..cc748dee7e --- /dev/null +++ b/src/System.ComponentModel.TypeConverter/tests/Resources/TestResx.resx @@ -0,0 +1,79 @@ +<?xml version="1.0" encoding="utf-8"?> +<root> + <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> + <xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> + <xsd:element name="root" msdata:IsDataSet="true"> + <xsd:complexType> + <xsd:choice maxOccurs="unbounded"> + <xsd:element name="metadata"> + <xsd:complexType> + <xsd:sequence> + <xsd:element name="value" type="xsd:string" minOccurs="0" /> + </xsd:sequence> + <xsd:attribute name="name" use="required" type="xsd:string" /> + <xsd:attribute name="type" type="xsd:string" /> + <xsd:attribute name="mimetype" type="xsd:string" /> + <xsd:attribute ref="xml:space" /> + </xsd:complexType> + </xsd:element> + <xsd:element name="assembly"> + <xsd:complexType> + <xsd:attribute name="alias" type="xsd:string" /> + <xsd:attribute name="name" type="xsd:string" /> + </xsd:complexType> + </xsd:element> + <xsd:element name="data"> + <xsd:complexType> + <xsd:sequence> + <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> + <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> + </xsd:sequence> + <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> + <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> + <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> + <xsd:attribute ref="xml:space" /> + </xsd:complexType> + </xsd:element> + <xsd:element name="resheader"> + <xsd:complexType> + <xsd:sequence> + <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> + </xsd:sequence> + <xsd:attribute name="name" type="xsd:string" use="required" /> + </xsd:complexType> + </xsd:element> + </xsd:choice> + </xsd:complexType> + </xsd:element> + </xsd:schema> + <resheader name="resmimetype"> + <value>text/microsoft-resx</value> + </resheader> + <resheader name="version"> + <value>2.0</value> + </resheader> + <resheader name="reader"> + <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <resheader name="writer"> + <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> + </resheader> + <data name="" xml:space="preserve"> + <value>No Name</value> + </data> + <data name="Object" xml:space="preserve"> + <value>Value-One</value> + </data> + <data name="Object.GetSetProperty" xml:space="preserve"> + <value>One</value> + </data> + <data name="Object.NoSuchProperty" xml:space="preserve"> + <value>Second</value> + </data> + <data name="Object.GetOnlyProperty" xml:space="preserve"> + <value>Third</value> + </data> + <data name="Object.PrivateProperty" xml:space="preserve"> + <value>Third</value> + </data> +</root>
\ No newline at end of file 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 23c5f5cdcd..34890a6caf 100644 --- a/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj +++ b/src/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj @@ -33,6 +33,7 @@ <Compile Include="ByteConvertersTests.cs" /> <Compile Include="CharConverterTests.cs" /> <Compile Include="CollectionConverterTests.cs" /> + <Compile Include="ComponentResourceManagerTests.cs" /> <Compile Include="ConverterTestBase.cs" /> <Compile Include="CustomTypeDescriptorTests.cs" /> <Compile Include="DateTimeConverterTests.cs" /> @@ -58,6 +59,11 @@ <Compile Include="PropertyDescriptorCollectionTests.cs" /> <Compile Include="PropertyDescriptorTests.cs" /> <Compile Include="ProvidePropertyAttributeTests.cs" /> + <Compile Include="Resources\TestResx.Designer.cs"> + <AutoGen>True</AutoGen> + <DesignTime>True</DesignTime> + <DependentUpon>TestResx.resx</DependentUpon> + </Compile> <Compile Include="SampleClasses.cs" /> <Compile Include="SByteConverterTests.cs" /> <Compile Include="SingleConverterTests.cs" /> @@ -89,5 +95,12 @@ <Name>RemoteExecutorConsoleApp</Name> </ProjectReference> </ItemGroup> + <ItemGroup> + <EmbeddedResource Include="Resources\TestResx.resx"> + <Generator>ResXFileCodeGenerator</Generator> + <LastGenOutput>TestResx.Designer.cs</LastGenOutput> + </EmbeddedResource> + </ItemGroup> + <ItemGroup /> <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> </Project>
\ No newline at end of file |