|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.ComponentModel.Design.Serialization; |
| 5 | + |
| 6 | +namespace System.Windows |
| 7 | +{ |
| 8 | + public class TextDecorationCollectionConverterTests |
| 9 | + { |
| 10 | + [Theory] |
| 11 | + [MemberData(nameof(ConvertFromString_TestData))] |
| 12 | + public void ConvertFromString_ReturnsExpected(TextDecorationCollection expected, string text) |
| 13 | + { |
| 14 | + TextDecorationCollection converted = TextDecorationCollectionConverter.ConvertFromString(text); |
| 15 | + |
| 16 | + // Check count |
| 17 | + Assert.Equal(expected.Count, converted.Count); |
| 18 | + |
| 19 | + // We require the order to be exact as well |
| 20 | + for (int i = 0; i < expected.Count; i++) |
| 21 | + { |
| 22 | + Assert.Equal(expected[i], converted[i]); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void ConvertFromString_NullValue_ReturnsNull() |
| 28 | + { |
| 29 | + // null is simply null (NOTE: This differs from instance method ConvertFrom, that will throw on null value) |
| 30 | + TextDecorationCollection? converted = TextDecorationCollectionConverter.ConvertFromString(null); |
| 31 | + |
| 32 | + Assert.Null(converted); |
| 33 | + } |
| 34 | + |
| 35 | + public static IEnumerable<object?[]> ConvertFromString_TestData |
| 36 | + { |
| 37 | + get |
| 38 | + { |
| 39 | + // "None" returns no items |
| 40 | + yield return new object[] { new TextDecorationCollection(), string.Empty }; |
| 41 | + yield return new object[] { new TextDecorationCollection(), " " }; |
| 42 | + yield return new object[] { new TextDecorationCollection(), "None" }; |
| 43 | + yield return new object[] { new TextDecorationCollection(), " None " }; |
| 44 | + |
| 45 | + // Order matters here |
| 46 | + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), "Strikethrough" }; |
| 47 | + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0]]), "Strikethrough " }; |
| 48 | + |
| 49 | + yield return new object[] { new TextDecorationCollection([TextDecorations.Underline[0], TextDecorations.Baseline[0]]), "Underline, Baseline" }; |
| 50 | + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], TextDecorations.Baseline[0]]), |
| 51 | + " Strikethrough ,Underline, Baseline " }; |
| 52 | + |
| 53 | + yield return new object[] { new TextDecorationCollection([TextDecorations.Strikethrough[0], TextDecorations.Underline[0], |
| 54 | + TextDecorations.Baseline[0], TextDecorations.OverLine[0]]), |
| 55 | + " Strikethrough ,Underline, Baseline , Overline " }; |
| 56 | + |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + [Theory] |
| 61 | + // Starts with a separator |
| 62 | + [InlineData(", Strikethrough ,Underline, Baseline ")] |
| 63 | + // Ends with a separator |
| 64 | + [InlineData(" Strikethrough ,Underline, Baseline, Overline, ")] |
| 65 | + // Duplicate item (must be unique) |
| 66 | + [InlineData(" Strikethrough , Strikethrough, ,Underline, Baseline ")] |
| 67 | + [InlineData(" Underline, Underline ")] |
| 68 | + // None must be specified alone |
| 69 | + [InlineData("None, Strikethrough ,Underline, Baseline ")] |
| 70 | + [InlineData("None, Strikethrough ,Underline, Baseline, Overline ")] |
| 71 | + // Invalid decoration at the end |
| 72 | + [InlineData(" Strikethrough ,Underline, Baseline, Overline, x ")] |
| 73 | + // Invalid decoration |
| 74 | + [InlineData(" Noneee ")] |
| 75 | + public void ConvertFromString_ThrowsArgumentException(string text) |
| 76 | + { |
| 77 | + Assert.Throws<ArgumentException>(() => TextDecorationCollectionConverter.ConvertFromString(text)); |
| 78 | + } |
| 79 | + |
| 80 | + [Theory] |
| 81 | + // Only valid type |
| 82 | + [InlineData(true, typeof(InstanceDescriptor))] |
| 83 | + // Invalid types |
| 84 | + [InlineData(false, typeof(TextDecorationCollection))] |
| 85 | + [InlineData(false, typeof(IEnumerable<TextDecoration>))] |
| 86 | + [InlineData(false, typeof(string))] |
| 87 | + public void CanConvertTo_ReturnsExpected(bool expected, Type destinationType) |
| 88 | + { |
| 89 | + TextDecorationCollectionConverter converter = new(); |
| 90 | + |
| 91 | + Assert.Equal(expected, converter.CanConvertTo(destinationType)); |
| 92 | + } |
| 93 | + |
| 94 | + [Theory] |
| 95 | + // Only valid type |
| 96 | + [InlineData(true, typeof(string))] |
| 97 | + // Invalid types |
| 98 | + [InlineData(false, typeof(TextDecorationCollection))] |
| 99 | + [InlineData(false, typeof(IEnumerable<TextDecoration>))] |
| 100 | + [InlineData(false, typeof(InstanceDescriptor))] |
| 101 | + public void CanConvertFrom_ReturnsExpected(bool expected, Type sourceType) |
| 102 | + { |
| 103 | + TextDecorationCollectionConverter converter = new(); |
| 104 | + |
| 105 | + Assert.Equal(expected, converter.CanConvertFrom(sourceType)); |
| 106 | + } |
| 107 | + |
| 108 | + [Theory] |
| 109 | + [MemberData(nameof(ConvertTo_ThrowsArgumentNullException_TestData))] |
| 110 | + public void ConvertTo_ThrowsArgumentNullException(object value, Type destinationType) |
| 111 | + { |
| 112 | + TextDecorationCollectionConverter converter = new(); |
| 113 | + |
| 114 | + Assert.Throws<ArgumentNullException>(() => converter.ConvertTo(null, null, value, destinationType)); |
| 115 | + } |
| 116 | + |
| 117 | + public static IEnumerable<object?[]> ConvertTo_ThrowsArgumentNullException_TestData |
| 118 | + { |
| 119 | + get |
| 120 | + { |
| 121 | + // null value and destinationType |
| 122 | + yield return new object?[] { null, null }; |
| 123 | + // supported value, null destinationType |
| 124 | + yield return new object?[] { new TextDecorationCollection(), null }; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void ConvertTo_ThrowsNotSupportedException() |
| 130 | + { |
| 131 | + TextDecorationCollectionConverter converter = new(); |
| 132 | + |
| 133 | + // supported value, bad destinationType |
| 134 | + Assert.Throws<NotSupportedException>(() => converter.ConvertTo(null, null, new TextDecorationCollection(), typeof(TextDecorationCollection))); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments