|
1 | 1 | using System;
|
2 | 2 | using Xunit;
|
| 3 | +using Xunit.Abstractions; |
3 | 4 | using Xunit.Sdk;
|
| 5 | +using SerializationHelper = Xunit.Sdk.SerializationHelper; |
| 6 | +using TestMethodDisplay = Xunit.Sdk.TestMethodDisplay; |
| 7 | + |
| 8 | +#if NET6_0 |
| 9 | +using System.Numerics; |
| 10 | +#endif |
4 | 11 |
|
5 | 12 | public class SerializationHelperTests
|
6 | 13 | {
|
@@ -49,16 +56,93 @@ public static void CannotRoundTripTypesFromTheGAC()
|
49 | 56 | #endif
|
50 | 57 | }
|
51 | 58 |
|
52 |
| - public class IsSerializable |
| 59 | + public class Serialization |
53 | 60 | {
|
54 |
| - [Fact] |
55 |
| - public void TypeSerialization() |
| 61 | + public static TheoryData<object, string> ValidValueData = new TheoryData<object, string> |
56 | 62 | {
|
57 |
| - Assert.True(SerializationHelper.IsSerializable(typeof(string))); // Can serialization types from mscorlib |
58 |
| - Assert.True(SerializationHelper.IsSerializable(typeof(SerializationHelper))); // Can serialize types from local libraries |
59 |
| -#if NETFRAMEWORK |
60 |
| - Assert.False(SerializationHelper.IsSerializable(typeof(Uri))); // Can't serialize types from the GAC |
| 63 | + { 'a', "System.Char:97" }, |
| 64 | + { "a", "System.String:YQ==" }, |
| 65 | + { (byte)42, "System.Byte:42" }, |
| 66 | + { (sbyte)-42, "System.SByte:-42" }, |
| 67 | + { (ushort)42, "System.UInt16:42" }, |
| 68 | + { (short)-42, "System.Int16:-42" }, |
| 69 | + { 42U, "System.UInt32:42" }, |
| 70 | + { -42, "System.Int32:-42" }, |
| 71 | + { 42UL, "System.UInt64:42" }, |
| 72 | + { -42L, "System.Int64:-42" }, |
| 73 | + { 42.2112F, "System.Single:RWxlbWVudFR5cGU6U3lzdGVtLlN0cmluZzpVM2x6ZEdWdExrSjVkR1U9ClJhbms6U3lzdGVtLkludDMyOjEKVG90YWxMZW5ndGg6U3lzdGVtLkludDMyOjQKTGVuZ3RoMDpTeXN0ZW0uSW50MzI6NApMb3dlckJvdW5kMDpTeXN0ZW0uSW50MzI6MApJdGVtMDpTeXN0ZW0uQnl0ZTo2OQpJdGVtMTpTeXN0ZW0uQnl0ZToyMTYKSXRlbTI6U3lzdGVtLkJ5dGU6NDAKSXRlbTM6U3lzdGVtLkJ5dGU6NjY=" }, |
| 74 | + { 42.2112D, "System.Double:RWxlbWVudFR5cGU6U3lzdGVtLlN0cmluZzpVM2x6ZEdWdExrSjVkR1U9ClJhbms6U3lzdGVtLkludDMyOjEKVG90YWxMZW5ndGg6U3lzdGVtLkludDMyOjgKTGVuZ3RoMDpTeXN0ZW0uSW50MzI6OApMb3dlckJvdW5kMDpTeXN0ZW0uSW50MzI6MApJdGVtMDpTeXN0ZW0uQnl0ZTozNwpJdGVtMTpTeXN0ZW0uQnl0ZToxMTcKSXRlbTI6U3lzdGVtLkJ5dGU6MgpJdGVtMzpTeXN0ZW0uQnl0ZToxNTQKSXRlbTQ6U3lzdGVtLkJ5dGU6OApJdGVtNTpTeXN0ZW0uQnl0ZToyNwpJdGVtNjpTeXN0ZW0uQnl0ZTo2OQpJdGVtNzpTeXN0ZW0uQnl0ZTo2NA==" }, |
| 75 | + { 42.2112M, "System.Decimal:42.2112" }, |
| 76 | + { true, "System.Boolean:True" }, |
| 77 | + { new DateTime(2112L), "System.DateTime:0001-01-01T00:00:00.0002112" }, |
| 78 | + { new DateTimeOffset(2112L, TimeSpan.Zero), "System.DateTimeOffset:0001-01-01T00:00:00.0002112+00:00" }, |
| 79 | + { new TimeSpan(3, 4, 5), "System.TimeSpan:03:04:05" }, |
| 80 | +#if NET6_0 |
| 81 | + { new DateOnly(2023, 8, 9), "System.DateOnly:738740" }, |
| 82 | + { new TimeOnly(21, 12, 42, 567), "System.TimeOnly:763625670000" }, |
| 83 | + { new BigInteger(42), "System.Numerics.BigInteger, System.Runtime.Numerics:42" }, |
61 | 84 | #endif
|
| 85 | + { typeof(string), "System.Type:System.String" }, |
| 86 | + { TestMethodDisplay.Method, "Xunit.Sdk.TestMethodDisplay, xunit.core:Method" }, |
| 87 | + { new[] { 1, 2, 3 }, "System.Int32[]:RWxlbWVudFR5cGU6U3lzdGVtLlN0cmluZzpVM2x6ZEdWdExrbHVkRE15ClJhbms6U3lzdGVtLkludDMyOjEKVG90YWxMZW5ndGg6U3lzdGVtLkludDMyOjMKTGVuZ3RoMDpTeXN0ZW0uSW50MzI6MwpMb3dlckJvdW5kMDpTeXN0ZW0uSW50MzI6MApJdGVtMDpTeXN0ZW0uSW50MzI6MQpJdGVtMTpTeXN0ZW0uSW50MzI6MgpJdGVtMjpTeXN0ZW0uSW50MzI6Mw==" }, |
| 88 | + { 42, "System.Int32:42" }, |
| 89 | + { new MySerializable(42), "SerializationHelperTests+Serialization+MySerializable, test.xunit.execution:dmFsdWU6U3lzdGVtLkludDMyOjQy" }, |
| 90 | + }; |
| 91 | + |
| 92 | + [Theory] |
| 93 | + [MemberData(nameof(ValidValueData))] |
| 94 | + public void ValidValues<T>(T value, string serializedValue) |
| 95 | + { |
| 96 | + // Serialization |
| 97 | + var serialized = SerializationHelper.Serialize(value); |
| 98 | + |
| 99 | + Assert.Equal(serializedValue, serialized); |
| 100 | + |
| 101 | + // Deserialization |
| 102 | + var deserialized = SerializationHelper.Deserialize<T>(serializedValue); |
| 103 | + |
| 104 | + Assert.Equal(value, deserialized); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void InvalidValue() |
| 109 | + { |
| 110 | + var obj = new object(); |
| 111 | + |
| 112 | + Assert.False(SerializationHelper.IsSerializable(obj)); |
| 113 | + var ex = Record.Exception(() => SerializationHelper.Serialize(obj)); |
| 114 | + var argEx = Assert.IsType<ArgumentException>(ex); |
| 115 | + Assert.Equal("value", argEx.ParamName); |
| 116 | + Assert.StartsWith("We don't know how to serialize type System.Object", argEx.Message); |
| 117 | + } |
| 118 | + |
| 119 | + class MySerializable : IXunitSerializable, IEquatable<MySerializable> |
| 120 | + { |
| 121 | + int value; |
| 122 | + |
| 123 | + [Obsolete("For deserialization purposes only")] |
| 124 | + public MySerializable() |
| 125 | + { } |
| 126 | + |
| 127 | + public MySerializable(int value) |
| 128 | + { |
| 129 | + this.value = value; |
| 130 | + } |
| 131 | + |
| 132 | + public void Deserialize(IXunitSerializationInfo info) |
| 133 | + { |
| 134 | + value = info.GetValue<int>(nameof(value)); |
| 135 | + } |
| 136 | + |
| 137 | + public bool Equals(MySerializable other) |
| 138 | + { |
| 139 | + return other != null && value == other.value; |
| 140 | + } |
| 141 | + |
| 142 | + public void Serialize(IXunitSerializationInfo info) |
| 143 | + { |
| 144 | + info.AddValue(nameof(value), value); |
| 145 | + } |
62 | 146 | }
|
63 | 147 | }
|
64 | 148 | }
|
0 commit comments