|
| 1 | +using System; |
| 2 | +using FluentAssertions; |
| 3 | +using Xunit; |
| 4 | +using Polly.Caching.Serialization.System.Text.Json; |
| 5 | +using System.Text.Json; |
| 6 | + |
| 7 | +namespace Polly.Specs.Caching.Serialization.System.Text.Json.Specs |
| 8 | +{ |
| 9 | + public class JsonSerializerSpecs |
| 10 | + { |
| 11 | + readonly JsonSerializerOptions StandardSettings = new JsonSerializerOptions |
| 12 | + { |
| 13 | + WriteIndented = true |
| 14 | + }; |
| 15 | + |
| 16 | + #region Configuration |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public void Should_throw_when_JsonSerializerSettings_is_null() |
| 20 | + { |
| 21 | + Action configure = () => new JsonSerializer<object>(null); |
| 22 | + |
| 23 | + configure.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("jsonSerializerOptions"); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void Should_not_throw_when_configured_with_non_null_JsonSerializerSettings() |
| 28 | + { |
| 29 | + JsonSerializerOptions settings = new JsonSerializerOptions(); |
| 30 | + |
| 31 | + Action configure = () => new JsonSerializer<object>(settings); |
| 32 | + |
| 33 | + configure.ShouldNotThrow(); |
| 34 | + } |
| 35 | + |
| 36 | + #endregion |
| 37 | + |
| 38 | + #region Serialize |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void Should_serialize_using_configured_JsonSerializerSettings() |
| 42 | + { |
| 43 | + JsonSerializer<SampleClass> serializer = new JsonSerializer<SampleClass>(StandardSettings); |
| 44 | + |
| 45 | + SampleClass objectToSerialize = new SampleClass() |
| 46 | + { |
| 47 | + StringProperty = "<html></html>", |
| 48 | + IntProperty = 1 |
| 49 | + }; |
| 50 | + |
| 51 | + string serialized = serializer.Serialize(objectToSerialize); |
| 52 | + |
| 53 | + serialized.Should().Be("{\r\n \"StringProperty\": \"\\u003Chtml\\u003E\\u003C/html\\u003E\",\r\n \"IntProperty\": 1\r\n}"); |
| 54 | + } |
| 55 | + |
| 56 | + #endregion |
| 57 | + |
| 58 | + #region Deserialize |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void Should_deserialize_using_configured_JsonSerializerSettings() |
| 62 | + { |
| 63 | + JsonSerializer<SampleClass> serializer = new JsonSerializer<SampleClass>(StandardSettings); |
| 64 | + |
| 65 | + string serialized = "{\"StringProperty\":\"\\u003chtml\\u003e\\u003c/html\\u003e\",\"IntProperty\":1}"; |
| 66 | + |
| 67 | + SampleClass deserialized = serializer.Deserialize(serialized); |
| 68 | + |
| 69 | + deserialized.ShouldBeEquivalentTo(new |
| 70 | + { |
| 71 | + IntProperty = 1, |
| 72 | + StringProperty = "<html></html>" |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void Throws_on_deserialize_null() |
| 78 | + { |
| 79 | + JsonSerializer<object> serializer = new JsonSerializer<object>(StandardSettings); |
| 80 | + |
| 81 | + Action deserializeNull = () => serializer.Deserialize(null); |
| 82 | + |
| 83 | + deserializeNull.ShouldThrow<ArgumentNullException>(); |
| 84 | + } |
| 85 | + |
| 86 | + #endregion |
| 87 | + |
| 88 | + #region Round-trip |
| 89 | + |
| 90 | + [Theory] |
| 91 | + [MemberData(nameof(SampleClassData))] |
| 92 | + public void Should_roundtrip_all_variants_of_reference_type(SampleClass testValue) |
| 93 | + { |
| 94 | + JsonSerializer<SampleClass> serializer = new JsonSerializer<SampleClass>(StandardSettings); |
| 95 | + |
| 96 | + serializer.Deserialize(serializer.Serialize(testValue)).ShouldBeEquivalentTo(testValue); |
| 97 | + } |
| 98 | + |
| 99 | + public static TheoryData<SampleClass> SampleClassData => |
| 100 | + new TheoryData<SampleClass> |
| 101 | + { |
| 102 | + new SampleClass(), |
| 103 | + new SampleClass() |
| 104 | + { |
| 105 | + StringProperty = "<html></html>", |
| 106 | + IntProperty = 1 |
| 107 | + }, |
| 108 | + (SampleClass)null, |
| 109 | + default(SampleClass) |
| 110 | + }; |
| 111 | + |
| 112 | + public class SampleClass |
| 113 | + { |
| 114 | + public string StringProperty { get; set; } |
| 115 | + public int IntProperty { get; set; } |
| 116 | + } |
| 117 | + |
| 118 | + [Theory] |
| 119 | + [MemberData(nameof(SampleStringData))] |
| 120 | + public void Should_roundtrip_all_variants_of_string(String testValue) |
| 121 | + { |
| 122 | + JsonSerializer<String> serializer = new JsonSerializer<String>(StandardSettings); |
| 123 | + |
| 124 | + serializer.Deserialize(serializer.Serialize(testValue)).Should().Be(testValue); |
| 125 | + } |
| 126 | + |
| 127 | + public static TheoryData<String> SampleStringData => |
| 128 | + new TheoryData<String> |
| 129 | + { |
| 130 | + "some string", |
| 131 | + "", |
| 132 | + null, |
| 133 | + default(string), |
| 134 | + "null" |
| 135 | + }; |
| 136 | + |
| 137 | + [Theory] |
| 138 | + [MemberData(nameof(SampleNumericData))] |
| 139 | + public void Should_roundtrip_all_variants_of_numeric(int testValue) |
| 140 | + { |
| 141 | + JsonSerializer<int> serializer = new JsonSerializer<int>(StandardSettings); |
| 142 | + |
| 143 | + serializer.Deserialize(serializer.Serialize(testValue)).Should().Be(testValue); |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + public static TheoryData<int> SampleNumericData => |
| 148 | + new TheoryData<int> |
| 149 | + { |
| 150 | + -1, |
| 151 | + 0, |
| 152 | + 1, |
| 153 | + default(int) |
| 154 | + }; |
| 155 | + |
| 156 | + [Theory] |
| 157 | + [MemberData(nameof(SampleEnumData))] |
| 158 | + public void Should_roundtrip_all_variants_of_enum(SampleEnum testValue) |
| 159 | + { |
| 160 | + JsonSerializer<SampleEnum> serializer = new JsonSerializer<SampleEnum>(StandardSettings); |
| 161 | + |
| 162 | + serializer.Deserialize(serializer.Serialize(testValue)).Should().Be(testValue); |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + public static TheoryData<SampleEnum> SampleEnumData => |
| 167 | + new TheoryData<SampleEnum> |
| 168 | + { |
| 169 | + SampleEnum.FirstValue, |
| 170 | + SampleEnum.SecondValue, |
| 171 | + default(SampleEnum), |
| 172 | + }; |
| 173 | + |
| 174 | + public enum SampleEnum |
| 175 | + { |
| 176 | + FirstValue, |
| 177 | + SecondValue, |
| 178 | + } |
| 179 | + |
| 180 | + [Theory] |
| 181 | + [MemberData(nameof(SampleBoolData))] |
| 182 | + public void Should_roundtrip_all_variants_of_bool(bool testValue) |
| 183 | + { |
| 184 | + JsonSerializer<bool> serializer = new JsonSerializer<bool>(StandardSettings); |
| 185 | + |
| 186 | + serializer.Deserialize(serializer.Serialize(testValue)).Should().Be(testValue); |
| 187 | + } |
| 188 | + |
| 189 | + public static TheoryData<bool> SampleBoolData => |
| 190 | + new TheoryData<bool> |
| 191 | + { |
| 192 | + true, |
| 193 | + false, |
| 194 | + default(bool), |
| 195 | + }; |
| 196 | + |
| 197 | + [Theory] |
| 198 | + [MemberData(nameof(SampleNullableBoolData))] |
| 199 | + public void Should_roundtrip_all_variants_of_nullable_bool(bool? testValue) |
| 200 | + { |
| 201 | + JsonSerializer<bool?> serializer = new JsonSerializer<bool?>(StandardSettings); |
| 202 | + |
| 203 | + serializer.Deserialize(serializer.Serialize(testValue)).Should().Be(testValue); |
| 204 | + } |
| 205 | + |
| 206 | + public static TheoryData<bool?> SampleNullableBoolData => |
| 207 | + new TheoryData<bool?> |
| 208 | + { |
| 209 | + true, |
| 210 | + false, |
| 211 | + null, |
| 212 | + default(bool?), |
| 213 | + }; |
| 214 | + #endregion |
| 215 | + } |
| 216 | + |
| 217 | +} |
0 commit comments