From: Buyaa Date: Fri, 8 Jan 2021 17:40:24 +0000 (-0800) Subject: Fix inconistent minus sign with different culture (#46588) X-Git-Tag: submit/tizen/20210909.063632~3872 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2a39be086914da90d2f32d2e4db8b1f720096cc8;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Fix inconistent minus sign with different culture (#46588) * Fix inconistent minus sign with different culture --- diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs index fbc3509..56774c9 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XContainer.cs @@ -807,47 +807,24 @@ namespace System.Xml.Linq internal static string GetStringValue(object value) { - string? s = value as string; - if (s != null) - { - return s; - } - else if (value is double) - { - s = XmlConvert.ToString((double)value); - } - else if (value is float) - { - s = XmlConvert.ToString((float)value); - } - else if (value is decimal) - { - s = XmlConvert.ToString((decimal)value); - } - else if (value is bool) - { - s = XmlConvert.ToString((bool)value); - } - else if (value is DateTime) - { - s = XmlConvert.ToString((DateTime) value, XmlDateTimeSerializationMode.RoundtripKind); - } - else if (value is DateTimeOffset) - { - s = XmlConvert.ToString((DateTimeOffset)value); - } - else if (value is TimeSpan) - { - s = XmlConvert.ToString((TimeSpan)value); - } - else if (value is XObject) - { - throw new ArgumentException(SR.Argument_XObjectValue); - } - else - { - s = value.ToString(); - } + string? s = value switch + { + string stringValue => stringValue, + int intValue => XmlConvert.ToString(intValue), + double doubleValue => XmlConvert.ToString(doubleValue), + long longValue => XmlConvert.ToString(longValue), + float floatValue => XmlConvert.ToString(floatValue), + decimal decimalValue => XmlConvert.ToString(decimalValue), + short shortValue => XmlConvert.ToString(shortValue), + sbyte sbyteValue => XmlConvert.ToString(sbyteValue), + bool boolValue => XmlConvert.ToString(boolValue), + DateTime dtValue => XmlConvert.ToString(dtValue, XmlDateTimeSerializationMode.RoundtripKind), + DateTimeOffset dtoValue => XmlConvert.ToString(dtoValue), + TimeSpan tsValue => XmlConvert.ToString(tsValue), + XObject => throw new ArgumentException(SR.Argument_XObjectValue), + _ => value.ToString() + }; + if (s == null) throw new ArgumentException(SR.Argument_ConvertToString); return s; } diff --git a/src/libraries/System.Private.Xml.Linq/tests/misc/XAttribute.cs b/src/libraries/System.Private.Xml.Linq/tests/misc/XAttribute.cs index 4f6b05f..1da4154 100644 --- a/src/libraries/System.Private.Xml.Linq/tests/misc/XAttribute.cs +++ b/src/libraries/System.Private.Xml.Linq/tests/misc/XAttribute.cs @@ -1,10 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; using System.Collections.Generic; -using System.Linq; -using Microsoft.Test.ModuleCore; +using System.Globalization; +using System.Tests; using Xunit; namespace System.Xml.Linq.Tests @@ -17,5 +16,72 @@ namespace System.Xml.Linq.Tests // Ensure we are compatible with the .NET Framework Assert.Equal("CreatedTime=\"2018-01-01T12:13:14Z\"", new XAttribute("CreatedTime", new DateTime(2018, 1, 1, 12, 13, 14, DateTimeKind.Utc)).ToString()); } + + public static IEnumerable NumericValuesWithMinusSign() + { + yield return new object[] { -123 }; + yield return new object[] { -123f }; + yield return new object[] { -123L }; + yield return new object[] { (short)-123 }; + yield return new object[] { -12.3 }; + yield return new object[] { -12.3m }; + yield return new object[] { (sbyte)-123 }; + } + + [Theory] + [MemberData(nameof(NumericValuesWithMinusSign))] + public void MinusSignWithDifferentTypeSwedishCulture(object value) + { + CultureInfo newCulture = null; + try + { + newCulture = new CultureInfo("sv-SE"); + } + catch (CultureNotFoundException) { /* Do nothing */ } + + using (new ThreadCultureChange(newCulture)) + { + Assert.Equal('-', (new XAttribute("a", value)).Value[0]); + } + } + + [Theory] + [MemberData(nameof(NumericValuesWithMinusSign))] + public void MinusSignWithDifferentTypeNoCulture(object value) + { + Assert.Equal('-', (new XAttribute("a", value)).Value[0]); + } + + public static IEnumerable NonNumericValues() + { + yield return new object[] { true, "true" }; + yield return new object[] { new DateTimeOffset(2018, 1, 1, 12, 13, 14, TimeSpan.Zero), "2018-01-01T12:13:14Z" }; + yield return new object[] { new TimeSpan(12, 13, 14), "PT12H13M14S" }; + yield return new object[] { "-123\n", "-123\n" }; + } + + [Theory] + [MemberData(nameof(NonNumericValues))] + public void NonNumericTypeSwedishCulture(object value, string expected) + { + CultureInfo newCulture = null; + try + { + newCulture = new CultureInfo("sv-SE"); + } + catch (CultureNotFoundException) { /* Do nothing */ } + + using (new ThreadCultureChange(newCulture)) + { + Assert.Equal(expected, (new XAttribute("a", value)).Value); + } + } + + [Theory] + [MemberData(nameof(NonNumericValues))] + public void NonNumericTypesNoCulture(object value, string expected) + { + Assert.Equal(expected, (new XAttribute("a", value)).Value); + } } }