|
| 1 | +using System.Globalization; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using UnitsNet.Tests.Serialization; |
| 4 | +using UnitsNet.Units; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace UnitsNet.Serialization.JsonNet.Tests |
| 8 | +{ |
| 9 | + public class AbbreviatedUnitsConverterTests : JsonNetSerializationTestsBase |
| 10 | + { |
| 11 | + public AbbreviatedUnitsConverterTests() : base(new AbbreviatedUnitsConverter()) |
| 12 | + { |
| 13 | + } |
| 14 | + |
| 15 | + #region Serialization tests |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void DoubleQuantity_SerializedWithDoubleValueAndAbbreviatedUnit() |
| 19 | + { |
| 20 | + var quantity = new Mass(1.20, MassUnit.Milligram); |
| 21 | + var expectedJson = "{\"Value\":1.2,\"Unit\":\"mg\",\"Type\":\"Mass\"}"; |
| 22 | + |
| 23 | + var json = SerializeObject(quantity); |
| 24 | + |
| 25 | + Assert.Equal(expectedJson, json); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void DecimalQuantity_SerializedWithDecimalValueAndAbbreviatedUnit() |
| 30 | + { |
| 31 | + var quantity = new Information(1.20m, InformationUnit.Exabyte); |
| 32 | + var expectedJson = "{\"Value\":1.20,\"Unit\":\"EB\",\"Type\":\"Information\"}"; |
| 33 | + |
| 34 | + var json = SerializeObject(quantity); |
| 35 | + |
| 36 | + Assert.Equal(expectedJson, json); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void InterfaceObject_IncludesTypeInformation() |
| 41 | + { |
| 42 | + var testObject = new TestInterfaceObject { Quantity = new Information(1.20m, InformationUnit.Exabyte) }; |
| 43 | + var expectedJson = "{\"Quantity\":{\"Value\":1.20,\"Unit\":\"EB\",\"Type\":\"Information\"}}"; |
| 44 | + |
| 45 | + var json = SerializeObject(testObject); |
| 46 | + |
| 47 | + Assert.Equal(expectedJson, json); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void InterfaceObject_SerializesWithoutKnownTypeInformation() |
| 52 | + { |
| 53 | + var testObject = new TestInterfaceObject { Quantity = new Volume(1.2, VolumeUnit.Microliter) }; |
| 54 | + |
| 55 | + var expectedJson = "{\"Quantity\":{\"Value\":1.2,\"Unit\":\"µl\",\"Type\":\"Volume\"}}"; |
| 56 | + |
| 57 | + var json = SerializeObject(testObject); |
| 58 | + |
| 59 | + Assert.Equal(expectedJson, json); |
| 60 | + } |
| 61 | + |
| 62 | + #endregion |
| 63 | + |
| 64 | + #region Deserialization tests |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void DoubleIQuantity_DeserializedFromDoubleValueAndAbbreviatedUnit() |
| 68 | + { |
| 69 | + var json = "{\"Value\":1.2,\"Unit\":\"mg\",\"Type\":\"Mass\"}"; |
| 70 | + |
| 71 | + var quantity = DeserializeObject<IQuantity>(json); |
| 72 | + |
| 73 | + Assert.Equal(1.2, quantity.Value); |
| 74 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void DoubleQuantity_DeserializedFromDoubleValueAndAbbreviatedUnit() |
| 79 | + { |
| 80 | + var json = "{\"Value\":1.2,\"Unit\":\"mg\"}"; |
| 81 | + |
| 82 | + var quantity = DeserializeObject<Mass>(json); |
| 83 | + |
| 84 | + Assert.Equal(1.2, quantity.Value); |
| 85 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void DoubleIQuantity_DeserializedFromDoubleValueAndNonAmbiguousAbbreviatedUnit_WithoutQuantityType() |
| 90 | + { |
| 91 | + var json = "{\"Value\":1.2,\"Unit\":\"em\"}"; |
| 92 | + |
| 93 | + var quantity = DeserializeObject<Mass>(json); |
| 94 | + |
| 95 | + Assert.Equal(1.2, quantity.Value); |
| 96 | + Assert.Equal(MassUnit.EarthMass, quantity.Unit); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void ThrowsAmbiguousUnitParseException_WhenDeserializingAmbiguousAbbreviation_WithoutQuantityType() |
| 101 | + { |
| 102 | + var json = "{\"Value\":1.2,\"Unit\":\"mg\"}"; |
| 103 | + |
| 104 | + Assert.Throws<AmbiguousUnitParseException>(() => DeserializeObject<IQuantity>(json)); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void DoubleIQuantity_DeserializedFromDoubleValueAndAbbreviatedUnit_CaseInsensitive() |
| 109 | + { |
| 110 | + var json = "{\"value\":1.2,\"unit\":\"Mg\",\"type\":\"mass\"}"; |
| 111 | + |
| 112 | + var quantity = DeserializeObject<IQuantity>(json); |
| 113 | + |
| 114 | + Assert.Equal(1.2, quantity.Value); |
| 115 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void DoubleIQuantity_DeserializedFromDoubleValueAndAbbreviatedUnit_CaseSensitiveUnits() |
| 120 | + { |
| 121 | + var json = "{\"value\":1.2,\"unit\":\"Mbar\",\"type\":\"pressure\"}"; |
| 122 | + |
| 123 | + var megabar = DeserializeObject<IQuantity>(json); |
| 124 | + var millibar = DeserializeObject<IQuantity>(json.ToLower()); |
| 125 | + |
| 126 | + Assert.Equal(1.2, megabar.Value); |
| 127 | + Assert.Equal(1.2, millibar.Value); |
| 128 | + Assert.Equal(PressureUnit.Megabar, megabar.Unit); |
| 129 | + Assert.Equal(PressureUnit.Millibar, millibar.Unit); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public void UnitsNetExceptionThrown_WhenDeserializing_FromUnknownQuantityType() |
| 134 | + { |
| 135 | + var json = "{\"Value\":1.2,\"Unit\":\"mg\",\"Type\":\"invalid\"}"; |
| 136 | + |
| 137 | + Assert.Throws<UnitsNetException>(() => DeserializeObject<Mass>(json)); |
| 138 | + } |
| 139 | + |
| 140 | + [Fact] |
| 141 | + public void UnitsNotFoundExceptionThrown_WhenDeserializing_FromUnknownUnit() |
| 142 | + { |
| 143 | + var json = "{\"Value\":1.2,\"Unit\":\"invalid\",\"Type\":\"Mass\"}"; |
| 144 | + |
| 145 | + Assert.Throws<UnitNotFoundException>(() => DeserializeObject<Mass>(json)); |
| 146 | + } |
| 147 | + |
| 148 | + [Fact] |
| 149 | + public void DoubleIQuantity_DeserializedFromQuotedDoubleValueAndAbbreviatedUnit() |
| 150 | + { |
| 151 | + var json = "{\"Value\":\"1.2\",\"Unit\":\"mg\",\"Type\":\"Mass\"}"; |
| 152 | + |
| 153 | + var quantity = DeserializeObject<IQuantity>(json); |
| 154 | + |
| 155 | + Assert.Equal(1.2, quantity.Value); |
| 156 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 157 | + } |
| 158 | + |
| 159 | + [Fact] |
| 160 | + public void DoubleQuantity_DeserializedFromQuotedDoubleValueAndAbbreviatedUnit() |
| 161 | + { |
| 162 | + var json = "{\"Value\":\"1.2\",\"Unit\":\"mg\"}"; |
| 163 | + |
| 164 | + var quantity = DeserializeObject<Mass>(json); |
| 165 | + |
| 166 | + Assert.Equal(1.2, quantity.Value); |
| 167 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 168 | + } |
| 169 | + |
| 170 | + [Fact] |
| 171 | + public void DoubleZeroIQuantity_DeserializedFromAbbreviatedUnitAndNoValue() |
| 172 | + { |
| 173 | + var json = "{\"Unit\":\"mg\",\"Type\":\"Mass\"}"; |
| 174 | + |
| 175 | + var quantity = DeserializeObject<IQuantity>(json); |
| 176 | + |
| 177 | + Assert.Equal(0, quantity.Value); |
| 178 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 179 | + } |
| 180 | + |
| 181 | + [Fact] |
| 182 | + public void DoubleZeroQuantity_DeserializedFromAbbreviatedUnitAndNoValue() |
| 183 | + { |
| 184 | + var json = "{\"Unit\":\"mg\"}"; |
| 185 | + |
| 186 | + var quantity = DeserializeObject<Mass>(json); |
| 187 | + |
| 188 | + Assert.Equal(0, quantity.Value); |
| 189 | + Assert.Equal(MassUnit.Milligram, quantity.Unit); |
| 190 | + } |
| 191 | + |
| 192 | + [Fact] |
| 193 | + public void DoubleBaseUnitQuantity_DeserializedFromValueAndNoUnit() |
| 194 | + { |
| 195 | + var json = "{\"Value\":1.2,\"Type\":\"Mass\"}"; |
| 196 | + |
| 197 | + var quantity = DeserializeObject<IQuantity>(json); |
| 198 | + |
| 199 | + Assert.Equal(1.2, quantity.Value); |
| 200 | + Assert.Equal(Mass.BaseUnit, quantity.Unit); |
| 201 | + } |
| 202 | + |
| 203 | + [Fact] |
| 204 | + public void DoubleBaseUnitIQuantity_DeserializedFromValueAndNoUnit() |
| 205 | + { |
| 206 | + var json = "{\"Value\":1.2}"; |
| 207 | + |
| 208 | + var quantity = DeserializeObject<Mass>(json); |
| 209 | + |
| 210 | + Assert.Equal(1.2, quantity.Value); |
| 211 | + Assert.Equal(Mass.BaseUnit, quantity.Unit); |
| 212 | + } |
| 213 | + |
| 214 | + [Fact] |
| 215 | + public void DoubleZeroBaseIQuantity_DeserializedFromQuantityTypeOnly() |
| 216 | + { |
| 217 | + var json = "{\"Type\":\"Mass\"}"; |
| 218 | + |
| 219 | + var quantity = DeserializeObject<IQuantity>(json); |
| 220 | + |
| 221 | + Assert.Equal(0, quantity.Value); |
| 222 | + Assert.Equal(Mass.BaseUnit, quantity.Unit); |
| 223 | + } |
| 224 | + |
| 225 | + [Fact] |
| 226 | + public void DoubleZeroBaseQuantity_DeserializedFromEmptyInput() |
| 227 | + { |
| 228 | + var json = "{}"; |
| 229 | + |
| 230 | + var quantity = DeserializeObject<Mass>(json); |
| 231 | + |
| 232 | + Assert.Equal(0, quantity.Value); |
| 233 | + Assert.Equal(Mass.BaseUnit, quantity.Unit); |
| 234 | + } |
| 235 | + |
| 236 | + [Fact] |
| 237 | + public void DecimalIQuantity_DeserializedFromDecimalValueAndAbbreviatedUnit() |
| 238 | + { |
| 239 | + var json = "{\"Value\":1.200,\"Unit\":\"EB\",\"Type\":\"Information\"}"; |
| 240 | + |
| 241 | + var quantity = (Information) DeserializeObject<IQuantity>(json); |
| 242 | + |
| 243 | + Assert.Equal(1.200m, quantity.Value); |
| 244 | + Assert.Equal("1.200", quantity.Value.ToString(CultureInfo.InvariantCulture)); |
| 245 | + Assert.Equal(InformationUnit.Exabyte, quantity.Unit); |
| 246 | + } |
| 247 | + |
| 248 | + [Fact] |
| 249 | + public void DecimalQuantity_DeserializedFromDecimalValueAndAbbreviatedUnit() |
| 250 | + { |
| 251 | + var json = "{\"Value\":1.200,\"Unit\":\"EB\"}"; |
| 252 | + |
| 253 | + var quantity = DeserializeObject<Information>(json); |
| 254 | + |
| 255 | + Assert.Equal(1.200m, quantity.Value); |
| 256 | + Assert.Equal("1.200", quantity.Value.ToString(CultureInfo.InvariantCulture)); |
| 257 | + Assert.Equal(InformationUnit.Exabyte, quantity.Unit); |
| 258 | + } |
| 259 | + |
| 260 | + [Fact] |
| 261 | + public void DecimalIQuantity_DeserializedFromQuotedDecimalValueAndAbbreviatedUnit() |
| 262 | + { |
| 263 | + var json = "{\"Value\":\"1.200\",\"Unit\":\"EB\",\"Type\":\"Information\"}"; |
| 264 | + |
| 265 | + var quantity = (Information) DeserializeObject<IQuantity>(json); |
| 266 | + |
| 267 | + Assert.Equal(1.200m, quantity.Value); |
| 268 | + Assert.Equal("1.200", quantity.Value.ToString(CultureInfo.InvariantCulture)); |
| 269 | + Assert.Equal(InformationUnit.Exabyte, quantity.Unit); |
| 270 | + } |
| 271 | + |
| 272 | + [Fact] |
| 273 | + public void DecimalQuantity_DeserializedFromQuotedDecimalValueAndAbbreviatedUnit() |
| 274 | + { |
| 275 | + var json = "{\"Value\":\"1.200\",\"Unit\":\"EB\"}"; |
| 276 | + |
| 277 | + var quantity = DeserializeObject<Information>(json); |
| 278 | + |
| 279 | + Assert.Equal(1.200m, quantity.Value); |
| 280 | + Assert.Equal("1.200", quantity.Value.ToString(CultureInfo.InvariantCulture)); |
| 281 | + Assert.Equal(InformationUnit.Exabyte, quantity.Unit); |
| 282 | + } |
| 283 | + |
| 284 | + [Fact] |
| 285 | + public void DecimalZeroIQuantity_DeserializedFromAbbreviatedUnitAndNoValue() |
| 286 | + { |
| 287 | + var json = "{\"Unit\":\"EB\",\"Type\":\"Information\"}"; |
| 288 | + |
| 289 | + var quantity = (Information)DeserializeObject<IQuantity>(json); |
| 290 | + |
| 291 | + Assert.Equal(0, quantity.Value); |
| 292 | + Assert.Equal(InformationUnit.Exabyte, quantity.Unit); |
| 293 | + } |
| 294 | + |
| 295 | + [Fact] |
| 296 | + public void DecimalZeroQuantity_DeserializedFromAbbreviatedUnitAndNoValue() |
| 297 | + { |
| 298 | + var json = "{\"Unit\":\"EB\"}"; |
| 299 | + |
| 300 | + var quantity = DeserializeObject<Information>(json); |
| 301 | + |
| 302 | + Assert.Equal(0, quantity.Value); |
| 303 | + Assert.Equal(InformationUnit.Exabyte, quantity.Unit); |
| 304 | + } |
| 305 | + |
| 306 | + [Fact] |
| 307 | + public void DecimalBaseUnitIQuantity_DeserializedFromDecimalValueAndNoUnit() |
| 308 | + { |
| 309 | + var json = "{\"Value\":1.200,\"Type\":\"Information\"}"; |
| 310 | + |
| 311 | + var quantity = (Information)DeserializeObject<IQuantity>(json); |
| 312 | + |
| 313 | + Assert.Equal(1.200m, quantity.Value); |
| 314 | + Assert.Equal("1.200", quantity.Value.ToString(CultureInfo.InvariantCulture)); |
| 315 | + Assert.Equal(Information.BaseUnit, quantity.Unit); |
| 316 | + } |
| 317 | + |
| 318 | + [Fact] |
| 319 | + public void DecimalBaseUnitQuantity_DeserializedFromDecimalValueAndNoUnit() |
| 320 | + { |
| 321 | + var json = "{\"Value\":1.200}"; |
| 322 | + |
| 323 | + var quantity = DeserializeObject<Information>(json); |
| 324 | + |
| 325 | + Assert.Equal(1.200m, quantity.Value); |
| 326 | + Assert.Equal("1.200", quantity.Value.ToString(CultureInfo.InvariantCulture)); |
| 327 | + Assert.Equal(Information.BaseUnit, quantity.Unit); |
| 328 | + } |
| 329 | + |
| 330 | + [Fact] |
| 331 | + public void DecimalZeroBaseIQuantity_DeserializedFromQuantityTypeOnly() |
| 332 | + { |
| 333 | + var json = "{\"Type\":\"Information\"}"; |
| 334 | + |
| 335 | + var quantity = (Information)DeserializeObject<IQuantity>(json); |
| 336 | + |
| 337 | + Assert.Equal(0, quantity.Value); |
| 338 | + Assert.Equal(Information.BaseUnit, quantity.Unit); |
| 339 | + } |
| 340 | + |
| 341 | + [Fact] |
| 342 | + public void DecimalZeroBaseQuantity_DeserializedFromEmptyInput() |
| 343 | + { |
| 344 | + var json = "{}"; |
| 345 | + |
| 346 | + var quantity = DeserializeObject<Information>(json); |
| 347 | + |
| 348 | + Assert.Equal(0, quantity.Value); |
| 349 | + Assert.Equal(Information.BaseUnit, quantity.Unit); |
| 350 | + } |
| 351 | + |
| 352 | + #endregion |
| 353 | + |
| 354 | + #region Compatibility |
| 355 | + |
| 356 | + [JsonObject] |
| 357 | + class PlainOldDoubleQuantity |
| 358 | + { |
| 359 | + public double Value { get; set; } |
| 360 | + public string Unit { get; set; } |
| 361 | + } |
| 362 | + |
| 363 | + [JsonObject] |
| 364 | + class PlainOldDecimalQuantity |
| 365 | + { |
| 366 | + public decimal Value { get; set; } |
| 367 | + public string Unit { get; set; } |
| 368 | + } |
| 369 | + |
| 370 | + [Fact] |
| 371 | + public void LargeDecimalQuantity_DeserializedTo_PlainOldDecimalQuantity() |
| 372 | + { |
| 373 | + var quantity = new Information(2m * long.MaxValue, InformationUnit.Exabyte); |
| 374 | + |
| 375 | + var json = SerializeObject(quantity); |
| 376 | + var plainOldQuantity = JsonConvert.DeserializeObject<PlainOldDecimalQuantity>(json); |
| 377 | + |
| 378 | + Assert.Equal(2m * long.MaxValue, plainOldQuantity.Value); |
| 379 | + Assert.Equal("EB", plainOldQuantity.Unit); |
| 380 | + } |
| 381 | + |
| 382 | + [Fact] |
| 383 | + public void LargeDecimalQuantity_DeserializedTo_PlainOldDoubleQuantity() |
| 384 | + { |
| 385 | + var quantity = Information.FromExabytes(2m * long.MaxValue); |
| 386 | + |
| 387 | + var json = SerializeObject(quantity); |
| 388 | + var plainOldQuantity = JsonConvert.DeserializeObject<PlainOldDoubleQuantity>(json); |
| 389 | + |
| 390 | + Assert.Equal(2.0 * long.MaxValue, plainOldQuantity.Value); |
| 391 | + Assert.Equal("EB", plainOldQuantity.Unit); |
| 392 | + } |
| 393 | + |
| 394 | + #endregion |
| 395 | + |
| 396 | + } |
| 397 | +} |
0 commit comments