|
| 1 | +namespace ValveKeyValue.Test |
| 2 | +{ |
| 3 | + class NullablePropertyDeserializationTestCase |
| 4 | + { |
| 5 | + #region Deserialization - present values |
| 6 | + |
| 7 | + [Test] |
| 8 | + public void NullableStringPropertiesAreDeserialized() |
| 9 | + { |
| 10 | + using var stream = TestDataHelper.OpenResource("Text.nullable_types.vdf"); |
| 11 | + var obj = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize<NullableObject>(stream); |
| 12 | + |
| 13 | + Assert.That(obj.Name, Is.EqualTo("TestName")); |
| 14 | + Assert.That(obj.Description, Is.EqualTo("TestDescription")); |
| 15 | + } |
| 16 | + |
| 17 | + [Test] |
| 18 | + public void NullableIntPropertyIsDeserializedWhenPresent() |
| 19 | + { |
| 20 | + using var stream = TestDataHelper.OpenResource("Text.nullable_types.vdf"); |
| 21 | + var obj = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize<NullableObject>(stream); |
| 22 | + |
| 23 | + Assert.That(obj.Age, Is.EqualTo(42)); |
| 24 | + } |
| 25 | + |
| 26 | + [Test] |
| 27 | + public void NullableListPropertyIsDeserializedWhenPresent() |
| 28 | + { |
| 29 | + using var stream = TestDataHelper.OpenResource("Text.nullable_types.vdf"); |
| 30 | + var obj = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize<NullableObject>(stream); |
| 31 | + |
| 32 | + Assert.That(obj.Numbers, Is.Not.Null); |
| 33 | + Assert.That(obj.Numbers, Has.Count.EqualTo(3)); |
| 34 | + Assert.That(obj.Numbers![0], Is.EqualTo(1)); |
| 35 | + Assert.That(obj.Numbers[1], Is.EqualTo(2)); |
| 36 | + Assert.That(obj.Numbers[2], Is.EqualTo(3)); |
| 37 | + } |
| 38 | + |
| 39 | + #endregion |
| 40 | + |
| 41 | + #region Deserialization - missing values remain null |
| 42 | + |
| 43 | + [Test] |
| 44 | + public void NullableStringPropertyRemainsNullWhenMissing() |
| 45 | + { |
| 46 | + using var stream = TestDataHelper.OpenResource("Text.nullable_types.vdf"); |
| 47 | + var obj = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize<NullableObject>(stream); |
| 48 | + |
| 49 | + Assert.That(obj.MissingProperty, Is.Null); |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void NullableIntPropertyRemainsNullWhenMissing() |
| 54 | + { |
| 55 | + using var stream = TestDataHelper.OpenResource("Text.nullable_types.vdf"); |
| 56 | + var obj = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize<NullableObject>(stream); |
| 57 | + |
| 58 | + Assert.That(obj.MissingInt, Is.Null); |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public void NullableListPropertyRemainsNullWhenMissing() |
| 63 | + { |
| 64 | + using var stream = TestDataHelper.OpenResource("Text.nullable_types.vdf"); |
| 65 | + var obj = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize<NullableObject>(stream); |
| 66 | + |
| 67 | + Assert.That(obj.MissingList, Is.Null); |
| 68 | + } |
| 69 | + |
| 70 | + #endregion |
| 71 | + |
| 72 | + #region Deserialization - required properties missing from data |
| 73 | + |
| 74 | + [Test] |
| 75 | + public void RequiredStringPropertyIsDefaultWhenMissingFromData() |
| 76 | + { |
| 77 | + // VDF only has Name/Description/Age/Numbers, but RequiredObject expects RequiredName |
| 78 | + var vdf = "\"object\"\n{\n\t\"Name\"\t\"hello\"\n}"; |
| 79 | + var obj = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize<RequiredObject>(vdf); |
| 80 | + |
| 81 | + // required is bypassed by GetUninitializedObject - property stays at default |
| 82 | + Assert.That(obj.RequiredName, Is.Null); |
| 83 | + Assert.That(obj.Name, Is.EqualTo("hello")); |
| 84 | + } |
| 85 | + |
| 86 | + #endregion |
| 87 | + |
| 88 | + #region Serialization - nullable properties |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void SerializesNullableStringProperty() |
| 92 | + { |
| 93 | + var obj = new NullableObject |
| 94 | + { |
| 95 | + Name = "TestName", |
| 96 | + Description = null, |
| 97 | + }; |
| 98 | + |
| 99 | + string text; |
| 100 | + using (var ms = new MemoryStream()) |
| 101 | + { |
| 102 | + KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Serialize(ms, obj, "object"); |
| 103 | + ms.Seek(0, SeekOrigin.Begin); |
| 104 | + using var reader = new StreamReader(ms); |
| 105 | + text = reader.ReadToEnd(); |
| 106 | + } |
| 107 | + |
| 108 | + // Present nullable string should be serialized |
| 109 | + Assert.That(text, Does.Contain("\"Name\"")); |
| 110 | + Assert.That(text, Does.Contain("\"TestName\"")); |
| 111 | + |
| 112 | + // Null string should be omitted |
| 113 | + Assert.That(text, Does.Not.Contain("\"Description\"")); |
| 114 | + } |
| 115 | + |
| 116 | + [Test] |
| 117 | + public void SerializesNullableIntProperty() |
| 118 | + { |
| 119 | + var obj = new NullableObject |
| 120 | + { |
| 121 | + Age = 42, |
| 122 | + MissingInt = null, |
| 123 | + }; |
| 124 | + |
| 125 | + string text; |
| 126 | + using (var ms = new MemoryStream()) |
| 127 | + { |
| 128 | + KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Serialize(ms, obj, "object"); |
| 129 | + ms.Seek(0, SeekOrigin.Begin); |
| 130 | + using var reader = new StreamReader(ms); |
| 131 | + text = reader.ReadToEnd(); |
| 132 | + } |
| 133 | + |
| 134 | + // Present nullable int should be serialized |
| 135 | + Assert.That(text, Does.Contain("\"Age\"")); |
| 136 | + Assert.That(text, Does.Contain("\"42\"")); |
| 137 | + |
| 138 | + // Null int? should be omitted |
| 139 | + Assert.That(text, Does.Not.Contain("\"MissingInt\"")); |
| 140 | + } |
| 141 | + |
| 142 | + [Test] |
| 143 | + public void SerializesNullableListProperty() |
| 144 | + { |
| 145 | + var obj = new NullableObject |
| 146 | + { |
| 147 | + Numbers = [10, 20], |
| 148 | + MissingList = null, |
| 149 | + }; |
| 150 | + |
| 151 | + string text; |
| 152 | + using (var ms = new MemoryStream()) |
| 153 | + { |
| 154 | + KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Serialize(ms, obj, "object"); |
| 155 | + ms.Seek(0, SeekOrigin.Begin); |
| 156 | + using var reader = new StreamReader(ms); |
| 157 | + text = reader.ReadToEnd(); |
| 158 | + } |
| 159 | + |
| 160 | + Assert.That(text, Does.Contain("\"Numbers\"")); |
| 161 | + Assert.That(text, Does.Contain("\"10\"")); |
| 162 | + Assert.That(text, Does.Contain("\"20\"")); |
| 163 | + Assert.That(text, Does.Not.Contain("\"MissingList\"")); |
| 164 | + } |
| 165 | + |
| 166 | + #endregion |
| 167 | + |
| 168 | + #region Round-trip |
| 169 | + |
| 170 | + [Test] |
| 171 | + public void NullablePropertiesRoundTrip() |
| 172 | + { |
| 173 | + var original = new NullableObject |
| 174 | + { |
| 175 | + Name = "RoundTrip", |
| 176 | + Age = 99, |
| 177 | + Numbers = [1, 2, 3], |
| 178 | + }; |
| 179 | + |
| 180 | + var serializer = KVSerializer.Create(KVSerializationFormat.KeyValues1Text); |
| 181 | + using var ms = new MemoryStream(); |
| 182 | + serializer.Serialize(ms, original, "object"); |
| 183 | + ms.Seek(0, SeekOrigin.Begin); |
| 184 | + var deserialized = serializer.Deserialize<NullableObject>(ms); |
| 185 | + |
| 186 | + Assert.That(deserialized.Name, Is.EqualTo("RoundTrip")); |
| 187 | + Assert.That(deserialized.Age, Is.EqualTo(99)); |
| 188 | + Assert.That(deserialized.Numbers, Has.Count.EqualTo(3)); |
| 189 | + Assert.That(deserialized.Description, Is.Null); |
| 190 | + Assert.That(deserialized.MissingInt, Is.Null); |
| 191 | + Assert.That(deserialized.MissingList, Is.Null); |
| 192 | + } |
| 193 | + |
| 194 | + #endregion |
| 195 | + |
| 196 | + #region Test classes |
| 197 | + |
| 198 | + class NullableObject |
| 199 | + { |
| 200 | + public string? Name { get; set; } |
| 201 | + public string? Description { get; set; } |
| 202 | + public int? Age { get; set; } |
| 203 | + public List<int>? Numbers { get; set; } |
| 204 | + public string? MissingProperty { get; set; } |
| 205 | + public int? MissingInt { get; set; } |
| 206 | + public List<string>? MissingList { get; set; } |
| 207 | + } |
| 208 | + |
| 209 | + class RequiredObject |
| 210 | + { |
| 211 | + public required string RequiredName { get; set; } |
| 212 | + public string? Name { get; set; } |
| 213 | + } |
| 214 | + |
| 215 | + #endregion |
| 216 | + } |
| 217 | +} |
0 commit comments