From 0c2680b836dfb7ec96fb50f5e77c03175013a605 Mon Sep 17 00:00:00 2001 From: Spencer Farley <2847259+farlee2121@users.noreply.github.com> Date: Mon, 4 Mar 2024 11:58:05 -0600 Subject: [PATCH 1/2] Clarify the exclusivity of cases in winforms type mapping --- .../WinformsTypeMappers.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/System.Resources.NetStandard/WinformsTypeMappers.cs b/System.Resources.NetStandard/WinformsTypeMappers.cs index 073a7b8..b30380c 100644 --- a/System.Resources.NetStandard/WinformsTypeMappers.cs +++ b/System.Resources.NetStandard/WinformsTypeMappers.cs @@ -8,19 +8,14 @@ public static Func InterceptWinformsTypes(Func typeNa { if (type.AssemblyQualifiedName == typeof(ResXFileRef).AssemblyQualifiedName) { - return NetStandard.ResXConstants.ResxFileRefTypeInfo; - } - - if (type.AssemblyQualifiedName == typeof(ResXNullRef).AssemblyQualifiedName) + return ResXConstants.ResxFileRefTypeInfo; + } + else if (type.AssemblyQualifiedName == typeof(ResXNullRef).AssemblyQualifiedName) { - return NetStandard.ResXConstants.ResxNullRefTypeInfo; + return ResXConstants.ResxNullRefTypeInfo; } - - - if (typeNameConverter != null) - return typeNameConverter(type); - - return null; + else if (typeNameConverter != null) return typeNameConverter(type); + else return null; }; } From a947f4f6b6abf75af8782ff806f65c3dd632f99d Mon Sep 17 00:00:00 2001 From: Spencer Farley <2847259+farlee2121@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:15:48 -0600 Subject: [PATCH 2/2] Test vs editor compatibility for ResXNullRef node writes Also fixed a misunderstanding in the round trip test --- Tests/Example.Designer.cs | 46 ++++++++++++-------- Tests/Example.resx | 4 +- Tests/ResxDataNodeTests.cs | 29 ++++++++++++- Tests/ResxWithNullRef.resx | 65 +++++++++++++++++++++++++++++ Tests/System.Resources.Tests.csproj | 4 ++ 5 files changed, 126 insertions(+), 22 deletions(-) create mode 100644 Tests/ResxWithNullRef.resx diff --git a/Tests/Example.Designer.cs b/Tests/Example.Designer.cs index f40662e..3f12d7b 100644 --- a/Tests/Example.Designer.cs +++ b/Tests/Example.Designer.cs @@ -1,6 +1,7 @@ //------------------------------------------------------------------------------ // // 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. @@ -80,9 +81,9 @@ internal static System.Drawing.Bitmap ErrorControl { } /// - /// Looks up a localized string similar to <?xml version="1.0" encoding="utf-8" ?> - ///<Root> - /// <Element>Text</Element> + /// Looks up a localized string similar to <?xml version="1.0" encoding="utf-8" ?> + ///<Root> + /// <Element>Text</Element> ///</Root>. /// internal static string FileRef { @@ -92,17 +93,17 @@ internal static string FileRef { } /// - /// Looks up a localized string similar to <?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= [rest of string was truncated]";. + /// Looks up a localized string similar to <?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="va [rest of string was truncated]";. /// internal static string ResxWithFileRef { get { @@ -111,12 +112,21 @@ internal static string ResxWithFileRef { } /// - /// Looks up a localized resource of type System.Object. + /// Looks up a localized string similar to <?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="va [rest of string was truncated]";. /// - internal static object ResxWithNullRef { + internal static string ResxWithNullRef { get { - object obj = ResourceManager.GetObject("ResxWithNullRef", resourceCulture); - return ((object)(obj)); + return ResourceManager.GetString("ResxWithNullRef", resourceCulture); } } diff --git a/Tests/Example.resx b/Tests/Example.resx index 4f01339..0d40358 100644 --- a/Tests/Example.resx +++ b/Tests/Example.resx @@ -133,7 +133,7 @@ TestResources\Files\text.ansi.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 - - + + ResxWithNullRef.resx;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 \ No newline at end of file diff --git a/Tests/ResxDataNodeTests.cs b/Tests/ResxDataNodeTests.cs index abc602d..634239c 100644 --- a/Tests/ResxDataNodeTests.cs +++ b/Tests/ResxDataNodeTests.cs @@ -102,7 +102,7 @@ private List ReaderToNodes(ResXResourceReader reader) } [Fact] - public void ResxDataNode_ResXFileRefsWrittenBackWithSameAssemblyInfo() + public void ResxDataNode_ResXFileRefs_WrittenBackWithSameAssemblyInfo() { // This test ensures compatibility with tooling like Visual Studio's visual ResX editor @@ -190,7 +190,7 @@ public void ResxDataNode_CreateForResXNullRef() [Fact] public void ResxDataNode_ResXNullRef_RoundTrip() { - var referencedFileContent = Example.ResxWithNullRef; + object referencedFileContent = null; var nodeInfo = new DataNodeInfo { Name = "ResxWithNullRef", @@ -217,5 +217,30 @@ public void ResxDataNode_ResXNullRef_RoundTrip() Assert.Equal(referencedFileContent, dictionary.GetValueOrDefault(nodeInfo.Name)); } } + + [Fact] + public void ResxDataNode_ResXNullRefs_WrittenBackWithSameAssemblyInfo() + { + // This test ensures compatibility with tooling like Visual Studio's visual ResX editor + + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + string originalResx = Example.ResxWithNullRef; + StringBuilder writerOutput = new StringBuilder(); + + using (var reader = new ResXResourceReader(new StringReader(originalResx))) + { + reader.UseResXDataNodes = true; + var dataNodes = ReaderToNodes(reader); + + using (ResXResourceWriter writer = new ResXResourceWriter(new StringWriter(writerOutput))) + { + dataNodes.ForEach(writer.AddResource); + writer.Generate(); + } + } + + Assert.Equal(originalResx, writerOutput.ToString()); + } } } diff --git a/Tests/ResxWithNullRef.resx b/Tests/ResxWithNullRef.resx new file mode 100644 index 0000000..749ff9b --- /dev/null +++ b/Tests/ResxWithNullRef.resx @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + + + \ No newline at end of file diff --git a/Tests/System.Resources.Tests.csproj b/Tests/System.Resources.Tests.csproj index 09d3baf..ae2ef37 100644 --- a/Tests/System.Resources.Tests.csproj +++ b/Tests/System.Resources.Tests.csproj @@ -26,9 +26,13 @@ + + + Designer +