From 671d3ebfb86c2a976a828a7552775142ffc3c87a Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 4 Oct 2024 15:35:44 +0200 Subject: [PATCH 1/5] Add test coverage for public surface of TextDecorationCollectionConverter --- ...TextDecorationCollectionConverter.Tests.cs | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs new file mode 100644 index 00000000000..522b8266b40 --- /dev/null +++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs @@ -0,0 +1,137 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel.Design.Serialization; + +namespace System.Windows +{ + public class TextDecorationCollectionConverterTests + { + [Theory] + [MemberData(nameof(ConvertFromString_TestData))] + public void ConvertFromString_ReturnsExpected(TextDecorationCollection expected, string text) + { + TextDecorationCollection converted = TextDecorationCollectionConverter.ConvertFromString(text); + + // Check count + Assert.Equal(expected.Count, converted.Count); + + // We require the order to be exact as well + for (int i = 0; i < expected.Count; i++) + { + Assert.Equal(expected[i], converted[i]); + } + } + + [Fact] + public void ConvertFromString_NullValue_ReturnsNull() + { + // null is simply null (NOTE: This differs from instance method ConvertFrom, that will throw on null value) + TextDecorationCollection? converted = TextDecorationCollectionConverter.ConvertFromString(null); + + Assert.Null(converted); + } + + public static IEnumerable ConvertFromString_TestData + { + get + { + // "None" returns no items + yield return new object[] { new TextDecorationCollection(), string.Empty }; + yield return new object[] { new TextDecorationCollection(), " " }; + yield return new object[] { new TextDecorationCollection(), "None" }; + yield return new object[] { new TextDecorationCollection(), " None " }; + + // Order matters here + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), "Strikethrough" }; + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), "Strikethrough " }; + + yield return new object[] { new TextDecorationCollection([TextDecorations.Underline[0], TextDecorations.Baseline[0]]), "Underline, Baseline" }; + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], TextDecorations.Baseline[0]]), + " Strikethrough ,Underline, Baseline " }; + + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], + TextDecorations.Baseline[0], TextDecorations.OverLine[0]]), + " Strikethrough ,Underline, Baseline , Overline " }; + + } + } + + [Theory] + // Starts with a separator + [InlineData(", Strikethrough ,Underline, Baseline ")] + // Ends with a separator + [InlineData(" Strikethrough ,Underline, Baseline, Overline, ")] + // Duplicate item (must be unique) + [InlineData(" Strikethrough , Strikethrough, ,Underline, Baseline ")] + [InlineData(" Underline, Underline ")] + // None must be specified alone + [InlineData("None, Strikethrough ,Underline, Baseline ")] + [InlineData("None, Strikethrough ,Underline, Baseline, Overline ")] + // Invalid decoration at the end + [InlineData(" Strikethrough ,Underline, Baseline, Overline, x ")] + // Invalid decoration + [InlineData(" Noneee ")] + public void ConvertFromString_ThrowsArgumentException(string text) + { + Assert.Throws(() => TextDecorationCollectionConverter.ConvertFromString(text)); + } + + [Theory] + // Only valid type + [InlineData(true, typeof(InstanceDescriptor))] + // Invalid types + [InlineData(false, typeof(TextDecorationCollection))] + [InlineData(false, typeof(IEnumerable))] + [InlineData(false, typeof(string))] + public void CanConvertTo_ReturnsExpected(bool expected, Type destinationType) + { + TextDecorationCollectionConverter converter = new(); + + Assert.Equal(expected, converter.CanConvertTo(destinationType)); + } + + [Theory] + // Only valid type + [InlineData(true, typeof(string))] + // Invalid types + [InlineData(false, typeof(TextDecorationCollection))] + [InlineData(false, typeof(IEnumerable))] + [InlineData(false, typeof(InstanceDescriptor))] + public void CanConvertFrom_ReturnsExpected(bool expected, Type sourceType) + { + TextDecorationCollectionConverter converter = new(); + + Assert.Equal(expected, converter.CanConvertFrom(sourceType)); + } + + [Theory] + [MemberData(nameof(ConvertTo_ThrowsArgumentNullException_TestData))] + public void ConvertTo_ThrowsArgumentNullException(object value, Type destinationType) + { + TextDecorationCollectionConverter converter = new(); + + Assert.Throws(() => converter.ConvertTo(null, null, value, destinationType)); + } + + public static IEnumerable ConvertTo_ThrowsArgumentNullException_TestData + { + get + { + // null value and destinationType + yield return new object?[] { null, null }; + // supported value, null destinationType + yield return new object?[] { new TextDecorationCollection(), null }; + } + } + + [Fact] + public void ConvertTo_ThrowsNotSupportedException() + { + TextDecorationCollectionConverter converter = new(); + + // supported value, bad destinationType + Assert.Throws(() => converter.ConvertTo(null, null, new TextDecorationCollection(), typeof(TextDecorationCollection))); + } + } +} From 8a4f071c341d519cfb11c373c1a2f53667a9b1b2 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Wed, 22 Jan 2025 11:57:21 +0100 Subject: [PATCH 2/5] Convert to file-scoped namespace --- ...TextDecorationCollectionConverter.Tests.cs | 215 +++++++++--------- 1 file changed, 107 insertions(+), 108 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs index 522b8266b40..e511ed029da 100644 --- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs +++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs @@ -3,135 +3,134 @@ using System.ComponentModel.Design.Serialization; -namespace System.Windows +namespace System.Windows; + +public class TextDecorationCollectionConverterTests { - public class TextDecorationCollectionConverterTests + [Theory] + [MemberData(nameof(ConvertFromString_TestData))] + public void ConvertFromString_ReturnsExpected(TextDecorationCollection expected, string text) { - [Theory] - [MemberData(nameof(ConvertFromString_TestData))] - public void ConvertFromString_ReturnsExpected(TextDecorationCollection expected, string text) - { - TextDecorationCollection converted = TextDecorationCollectionConverter.ConvertFromString(text); + TextDecorationCollection converted = TextDecorationCollectionConverter.ConvertFromString(text); - // Check count - Assert.Equal(expected.Count, converted.Count); + // Check count + Assert.Equal(expected.Count, converted.Count); - // We require the order to be exact as well - for (int i = 0; i < expected.Count; i++) - { - Assert.Equal(expected[i], converted[i]); - } + // We require the order to be exact as well + for (int i = 0; i < expected.Count; i++) + { + Assert.Equal(expected[i], converted[i]); } + } - [Fact] - public void ConvertFromString_NullValue_ReturnsNull() - { - // null is simply null (NOTE: This differs from instance method ConvertFrom, that will throw on null value) - TextDecorationCollection? converted = TextDecorationCollectionConverter.ConvertFromString(null); + [Fact] + public void ConvertFromString_NullValue_ReturnsNull() + { + // null is simply null (NOTE: This differs from instance method ConvertFrom, that will throw on null value) + TextDecorationCollection? converted = TextDecorationCollectionConverter.ConvertFromString(null); - Assert.Null(converted); - } + Assert.Null(converted); + } - public static IEnumerable ConvertFromString_TestData + public static IEnumerable ConvertFromString_TestData + { + get { - get - { - // "None" returns no items - yield return new object[] { new TextDecorationCollection(), string.Empty }; - yield return new object[] { new TextDecorationCollection(), " " }; - yield return new object[] { new TextDecorationCollection(), "None" }; - yield return new object[] { new TextDecorationCollection(), " None " }; - - // Order matters here - yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), "Strikethrough" }; - yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), "Strikethrough " }; - - yield return new object[] { new TextDecorationCollection([TextDecorations.Underline[0], TextDecorations.Baseline[0]]), "Underline, Baseline" }; - yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], TextDecorations.Baseline[0]]), - " Strikethrough ,Underline, Baseline " }; - - yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], - TextDecorations.Baseline[0], TextDecorations.OverLine[0]]), - " Strikethrough ,Underline, Baseline , Overline " }; - - } - } + // "None" returns no items + yield return new object[] { new TextDecorationCollection(), string.Empty }; + yield return new object[] { new TextDecorationCollection(), " " }; + yield return new object[] { new TextDecorationCollection(), "None" }; + yield return new object[] { new TextDecorationCollection(), " None " }; - [Theory] - // Starts with a separator - [InlineData(", Strikethrough ,Underline, Baseline ")] - // Ends with a separator - [InlineData(" Strikethrough ,Underline, Baseline, Overline, ")] - // Duplicate item (must be unique) - [InlineData(" Strikethrough , Strikethrough, ,Underline, Baseline ")] - [InlineData(" Underline, Underline ")] - // None must be specified alone - [InlineData("None, Strikethrough ,Underline, Baseline ")] - [InlineData("None, Strikethrough ,Underline, Baseline, Overline ")] - // Invalid decoration at the end - [InlineData(" Strikethrough ,Underline, Baseline, Overline, x ")] - // Invalid decoration - [InlineData(" Noneee ")] - public void ConvertFromString_ThrowsArgumentException(string text) - { - Assert.Throws(() => TextDecorationCollectionConverter.ConvertFromString(text)); - } + // Order matters here + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), "Strikethrough" }; + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), "Strikethrough " }; - [Theory] - // Only valid type - [InlineData(true, typeof(InstanceDescriptor))] - // Invalid types - [InlineData(false, typeof(TextDecorationCollection))] - [InlineData(false, typeof(IEnumerable))] - [InlineData(false, typeof(string))] - public void CanConvertTo_ReturnsExpected(bool expected, Type destinationType) - { - TextDecorationCollectionConverter converter = new(); + yield return new object[] { new TextDecorationCollection([TextDecorations.Underline[0], TextDecorations.Baseline[0]]), "Underline, Baseline" }; + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], TextDecorations.Baseline[0]]), + " Strikethrough ,Underline, Baseline " }; + + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], + TextDecorations.Baseline[0], TextDecorations.OverLine[0]]), + " Strikethrough ,Underline, Baseline , Overline " }; - Assert.Equal(expected, converter.CanConvertTo(destinationType)); } + } - [Theory] - // Only valid type - [InlineData(true, typeof(string))] - // Invalid types - [InlineData(false, typeof(TextDecorationCollection))] - [InlineData(false, typeof(IEnumerable))] - [InlineData(false, typeof(InstanceDescriptor))] - public void CanConvertFrom_ReturnsExpected(bool expected, Type sourceType) - { - TextDecorationCollectionConverter converter = new(); + [Theory] + // Starts with a separator + [InlineData(", Strikethrough ,Underline, Baseline ")] + // Ends with a separator + [InlineData(" Strikethrough ,Underline, Baseline, Overline, ")] + // Duplicate item (must be unique) + [InlineData(" Strikethrough , Strikethrough, ,Underline, Baseline ")] + [InlineData(" Underline, Underline ")] + // None must be specified alone + [InlineData("None, Strikethrough ,Underline, Baseline ")] + [InlineData("None, Strikethrough ,Underline, Baseline, Overline ")] + // Invalid decoration at the end + [InlineData(" Strikethrough ,Underline, Baseline, Overline, x ")] + // Invalid decoration + [InlineData(" Noneee ")] + public void ConvertFromString_ThrowsArgumentException(string text) + { + Assert.Throws(() => TextDecorationCollectionConverter.ConvertFromString(text)); + } - Assert.Equal(expected, converter.CanConvertFrom(sourceType)); - } + [Theory] + // Only valid type + [InlineData(true, typeof(InstanceDescriptor))] + // Invalid types + [InlineData(false, typeof(TextDecorationCollection))] + [InlineData(false, typeof(IEnumerable))] + [InlineData(false, typeof(string))] + public void CanConvertTo_ReturnsExpected(bool expected, Type destinationType) + { + TextDecorationCollectionConverter converter = new(); - [Theory] - [MemberData(nameof(ConvertTo_ThrowsArgumentNullException_TestData))] - public void ConvertTo_ThrowsArgumentNullException(object value, Type destinationType) - { - TextDecorationCollectionConverter converter = new(); + Assert.Equal(expected, converter.CanConvertTo(destinationType)); + } - Assert.Throws(() => converter.ConvertTo(null, null, value, destinationType)); - } + [Theory] + // Only valid type + [InlineData(true, typeof(string))] + // Invalid types + [InlineData(false, typeof(TextDecorationCollection))] + [InlineData(false, typeof(IEnumerable))] + [InlineData(false, typeof(InstanceDescriptor))] + public void CanConvertFrom_ReturnsExpected(bool expected, Type sourceType) + { + TextDecorationCollectionConverter converter = new(); + + Assert.Equal(expected, converter.CanConvertFrom(sourceType)); + } - public static IEnumerable ConvertTo_ThrowsArgumentNullException_TestData + [Theory] + [MemberData(nameof(ConvertTo_ThrowsArgumentNullException_TestData))] + public void ConvertTo_ThrowsArgumentNullException(object value, Type destinationType) + { + TextDecorationCollectionConverter converter = new(); + + Assert.Throws(() => converter.ConvertTo(null, null, value, destinationType)); + } + + public static IEnumerable ConvertTo_ThrowsArgumentNullException_TestData + { + get { - get - { - // null value and destinationType - yield return new object?[] { null, null }; - // supported value, null destinationType - yield return new object?[] { new TextDecorationCollection(), null }; - } + // null value and destinationType + yield return new object?[] { null, null }; + // supported value, null destinationType + yield return new object?[] { new TextDecorationCollection(), null }; } + } - [Fact] - public void ConvertTo_ThrowsNotSupportedException() - { - TextDecorationCollectionConverter converter = new(); + [Fact] + public void ConvertTo_ThrowsNotSupportedException() + { + TextDecorationCollectionConverter converter = new(); - // supported value, bad destinationType - Assert.Throws(() => converter.ConvertTo(null, null, new TextDecorationCollection(), typeof(TextDecorationCollection))); - } + // supported value, bad destinationType + Assert.Throws(() => converter.ConvertTo(null, null, new TextDecorationCollection(), typeof(TextDecorationCollection))); } } From d81434a5372b2f75bee82dd8b65b7c46ed98180c Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Wed, 22 Jan 2025 12:05:50 +0100 Subject: [PATCH 3/5] Move stuff around in union with other tests --- ...TextDecorationCollectionConverter.Tests.cs | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs index e511ed029da..acf439e3fdf 100644 --- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs +++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs @@ -8,7 +8,35 @@ namespace System.Windows; public class TextDecorationCollectionConverterTests { [Theory] - [MemberData(nameof(ConvertFromString_TestData))] + // Only valid type + [InlineData(true, typeof(string))] + // Invalid types + [InlineData(false, typeof(TextDecorationCollection))] + [InlineData(false, typeof(IEnumerable))] + [InlineData(false, typeof(InstanceDescriptor))] + public void CanConvertFrom_ReturnsExpected(bool expected, Type sourceType) + { + TextDecorationCollectionConverter converter = new(); + + Assert.Equal(expected, converter.CanConvertFrom(sourceType)); + } + + [Theory] + // Only valid type + [InlineData(true, typeof(InstanceDescriptor))] + // Invalid types + [InlineData(false, typeof(TextDecorationCollection))] + [InlineData(false, typeof(IEnumerable))] + [InlineData(false, typeof(string))] + public void CanConvertTo_ReturnsExpected(bool expected, Type destinationType) + { + TextDecorationCollectionConverter converter = new(); + + Assert.Equal(expected, converter.CanConvertTo(destinationType)); + } + + [Theory] + [MemberData(nameof(ConvertFromString_ReturnsExpected_Data))] public void ConvertFromString_ReturnsExpected(TextDecorationCollection expected, string text) { TextDecorationCollection converted = TextDecorationCollectionConverter.ConvertFromString(text); @@ -23,16 +51,7 @@ public void ConvertFromString_ReturnsExpected(TextDecorationCollection expected, } } - [Fact] - public void ConvertFromString_NullValue_ReturnsNull() - { - // null is simply null (NOTE: This differs from instance method ConvertFrom, that will throw on null value) - TextDecorationCollection? converted = TextDecorationCollectionConverter.ConvertFromString(null); - - Assert.Null(converted); - } - - public static IEnumerable ConvertFromString_TestData + public static IEnumerable ConvertFromString_ReturnsExpected_Data { get { @@ -57,6 +76,15 @@ public static IEnumerable ConvertFromString_TestData } } + [Fact] + public void ConvertFromString_NullValue_ReturnsNull() + { + // null is simply null (NOTE: This differs from instance method ConvertFrom, that will throw on null value) + TextDecorationCollection? converted = TextDecorationCollectionConverter.ConvertFromString(null); + + Assert.Null(converted); + } + [Theory] // Starts with a separator [InlineData(", Strikethrough ,Underline, Baseline ")] @@ -77,34 +105,6 @@ public void ConvertFromString_ThrowsArgumentException(string text) Assert.Throws(() => TextDecorationCollectionConverter.ConvertFromString(text)); } - [Theory] - // Only valid type - [InlineData(true, typeof(InstanceDescriptor))] - // Invalid types - [InlineData(false, typeof(TextDecorationCollection))] - [InlineData(false, typeof(IEnumerable))] - [InlineData(false, typeof(string))] - public void CanConvertTo_ReturnsExpected(bool expected, Type destinationType) - { - TextDecorationCollectionConverter converter = new(); - - Assert.Equal(expected, converter.CanConvertTo(destinationType)); - } - - [Theory] - // Only valid type - [InlineData(true, typeof(string))] - // Invalid types - [InlineData(false, typeof(TextDecorationCollection))] - [InlineData(false, typeof(IEnumerable))] - [InlineData(false, typeof(InstanceDescriptor))] - public void CanConvertFrom_ReturnsExpected(bool expected, Type sourceType) - { - TextDecorationCollectionConverter converter = new(); - - Assert.Equal(expected, converter.CanConvertFrom(sourceType)); - } - [Theory] [MemberData(nameof(ConvertTo_ThrowsArgumentNullException_TestData))] public void ConvertTo_ThrowsArgumentNullException(object value, Type destinationType) From c11c5ce8ffc3cc516182af19ec5b6f20a8291d6a Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Wed, 22 Jan 2025 12:38:25 +0100 Subject: [PATCH 4/5] Add multitude of other tests --- ...TextDecorationCollectionConverter.Tests.cs | 124 +++++++++++++++++- 1 file changed, 122 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs index acf439e3fdf..850a17de03c 100644 --- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs +++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.ComponentModel.Design.Serialization; +using System.Globalization; namespace System.Windows; @@ -35,6 +36,84 @@ public void CanConvertTo_ReturnsExpected(bool expected, Type destinationType) Assert.Equal(expected, converter.CanConvertTo(destinationType)); } + [Theory] + [MemberData(nameof(ConvertFrom_ReturnsExpected_Data))] + public void ConvertFrom_ReturnsExpected(TextDecorationCollection expected, CultureInfo cultureInfo, string text) + { + TextDecorationCollectionConverter converter = new(); + + TextDecorationCollection? converted = (TextDecorationCollection?)converter.ConvertFrom(null, cultureInfo, text); + + // Check count + Assert.NotNull(converted); + Assert.Equal(expected.Count, converted.Count); + + // We require the order to be exact as well + for (int i = 0; i < expected.Count; i++) + { + Assert.Equal(expected[i], converted[i]); + } + } + + public static IEnumerable ConvertFrom_ReturnsExpected_Data + { + get + { + // "None" returns no items + yield return new object[] { new TextDecorationCollection(), CultureInfo.InvariantCulture, string.Empty }; + yield return new object[] { new TextDecorationCollection(), CultureInfo.InvariantCulture, " " }; + yield return new object[] { new TextDecorationCollection(), CultureInfo.InvariantCulture, "None" }; + yield return new object[] { new TextDecorationCollection(), CultureInfo.InvariantCulture, " None " }; + + yield return new object[] { new TextDecorationCollection(), new CultureInfo("ru-RU"), string.Empty }; + yield return new object[] { new TextDecorationCollection(), new CultureInfo("no-NO"), " " }; + yield return new object[] { new TextDecorationCollection(), new CultureInfo("no-NO"), "None" }; + yield return new object[] { new TextDecorationCollection(), new CultureInfo("ru-RU"), " None " }; + + // Order matters here + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), new CultureInfo("no-NO"), "Strikethrough" }; + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), new CultureInfo("ru-RU"), "Strikethrough " }; + + yield return new object[] { new TextDecorationCollection([TextDecorations.Underline[0], TextDecorations.Baseline[0]]), + new CultureInfo("no-NO"), + "Underline, Baseline" }; + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], TextDecorations.Baseline[0]]), + new CultureInfo("ru-RU"), + " Strikethrough ,Underline, Baseline " }; + + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], + TextDecorations.Baseline[0], TextDecorations.OverLine[0]]), + new CultureInfo("fr-FR"), " Strikethrough ,Underline, Baseline , Overline " }; + + } + } + + [Theory] + // Starts with a separator + [InlineData(", Strikethrough ,Underline, Baseline ")] + // Ends with a separator + [InlineData(" Strikethrough ,Underline, Baseline, Overline, ")] + // Duplicate item (must be unique) + [InlineData(" Strikethrough , Strikethrough, ,Underline, Baseline ")] + [InlineData(" Underline, Underline ")] + // None must be specified alone + [InlineData("None, Strikethrough ,Underline, Baseline ")] + [InlineData("None, Strikethrough ,Underline, Baseline, Overline ")] + // Invalid decoration at the end + [InlineData(" Strikethrough ,Underline, Baseline, Overline, x ")] + // Invalid decoration + [InlineData(" Noneee ")] + // Invalid data type + [InlineData(double.PositiveInfinity)] + [InlineData(1554554)] + [InlineData(125.4d)] + public void ConvertFrom_ThrowsArgumentException(object? source) + { + TextDecorationCollectionConverter converter = new(); + + Assert.Throws(() => converter.ConvertFrom(null, null, source)); + } + [Theory] [MemberData(nameof(ConvertFromString_ReturnsExpected_Data))] public void ConvertFromString_ReturnsExpected(TextDecorationCollection expected, string text) @@ -106,7 +185,48 @@ public void ConvertFromString_ThrowsArgumentException(string text) } [Theory] - [MemberData(nameof(ConvertTo_ThrowsArgumentNullException_TestData))] + [MemberData(nameof(ConvertTo_ReturnsExpected_Data))] + public void ConvertTo_ReturnsExpected(TextDecorationCollection expected, object? value, Type destinationType) + { + TextDecorationCollectionConverter converter = new(); + + InstanceDescriptor? result = (InstanceDescriptor?)converter.ConvertTo(null, null, value, destinationType); + + Assert.NotNull(result); + + // Create instance using the InstanceDescriptor + TextDecorationCollection? actual = (TextDecorationCollection?)result.Invoke(); + + // Check instance + Assert.NotNull(actual); + Assert.Equal(expected.Count, actual.Count); + + // We require the order to be exact as well + for (int i = 0; i < expected.Count; i++) + { + Assert.Equal(expected[i], actual[i]); + } + } + + public static IEnumerable ConvertTo_ReturnsExpected_Data + { + get + { + // Single decoration + yield return new object[] { new TextDecorationCollection(new TextDecoration[1] { TextDecorations.Underline[0] }), + new TextDecorationCollection(new TextDecoration[1] { TextDecorations.Underline[0] }), + + typeof(InstanceDescriptor) }; + + // Multiple decorations + yield return new object[] { new TextDecorationCollection(new TextDecoration[3] { TextDecorations.Underline[0], TextDecorations.OverLine[0], TextDecorations.Baseline[0] }), + new TextDecorationCollection(new TextDecoration[3] { TextDecorations.Underline[0], TextDecorations.OverLine[0], TextDecorations.Baseline[0] }), + typeof(InstanceDescriptor) }; + } + } + + [Theory] + [MemberData(nameof(ConvertTo_ThrowsArgumentNullException_Data))] public void ConvertTo_ThrowsArgumentNullException(object value, Type destinationType) { TextDecorationCollectionConverter converter = new(); @@ -114,7 +234,7 @@ public void ConvertTo_ThrowsArgumentNullException(object value, Type destination Assert.Throws(() => converter.ConvertTo(null, null, value, destinationType)); } - public static IEnumerable ConvertTo_ThrowsArgumentNullException_TestData + public static IEnumerable ConvertTo_ThrowsArgumentNullException_Data { get { From e9f67a4ade4f1cd3443ae7d67fd7b648e73b200f Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Wed, 22 Jan 2025 12:48:00 +0100 Subject: [PATCH 5/5] Add different source types based on IEnumerable --- .../TextDecorationCollectionConverter.Tests.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs index 850a17de03c..177eae7298b 100644 --- a/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs +++ b/src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/TextDecorationCollectionConverter.Tests.cs @@ -215,13 +215,22 @@ public static IEnumerable ConvertTo_ReturnsExpected_Data // Single decoration yield return new object[] { new TextDecorationCollection(new TextDecoration[1] { TextDecorations.Underline[0] }), new TextDecorationCollection(new TextDecoration[1] { TextDecorations.Underline[0] }), - typeof(InstanceDescriptor) }; // Multiple decorations yield return new object[] { new TextDecorationCollection(new TextDecoration[3] { TextDecorations.Underline[0], TextDecorations.OverLine[0], TextDecorations.Baseline[0] }), new TextDecorationCollection(new TextDecoration[3] { TextDecorations.Underline[0], TextDecorations.OverLine[0], TextDecorations.Baseline[0] }), typeof(InstanceDescriptor) }; + + // Source value just needs to be IEnumerable + + // T[] + yield return new object[] { new TextDecorationCollection(new TextDecoration[3] { TextDecorations.Underline[0], TextDecorations.OverLine[0], TextDecorations.Baseline[0] }), + new TextDecoration[3] { TextDecorations.Underline[0], TextDecorations.OverLine[0], TextDecorations.Baseline[0] }, typeof(InstanceDescriptor) }; + + // List + yield return new object[] { new TextDecorationCollection(new TextDecoration[3] { TextDecorations.Underline[0], TextDecorations.OverLine[0], TextDecorations.Baseline[0] }), + new List { TextDecorations.Underline[0], TextDecorations.OverLine[0], TextDecorations.Baseline[0] }, typeof(InstanceDescriptor) }; } }