|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +namespace Tests.Serialization; |
| 6 | + |
| 7 | +public class SourceSerializationForNumericPropertiesTests : SerializerTestBase |
| 8 | +{ |
| 9 | + [U] |
| 10 | + public void FloatValuesIncludeDecimal_AndAreNotRounded() |
| 11 | + { |
| 12 | + var numericTests = new NumericTests { Float = 1.0f }; |
| 13 | + |
| 14 | + var json = SourceSerializeAndGetJsonString(numericTests); |
| 15 | + |
| 16 | + json.Should().Be("{\"float\":1.0}"); |
| 17 | + |
| 18 | + var deserialized = SourceDeserializeJsonString<NumericTests>(json); |
| 19 | + |
| 20 | + deserialized.Float.Should().Be(1.0f); |
| 21 | + } |
| 22 | + |
| 23 | + [U] |
| 24 | + public void FloatMinValue_SerializesCorrectly() |
| 25 | + { |
| 26 | + var numericTests = new NumericTests { Float = float.MinValue }; |
| 27 | + |
| 28 | + var json = SourceSerializeAndGetJsonString(numericTests); |
| 29 | + |
| 30 | + json.Should().Be("{\"float\":-3.4028235E+38}"); |
| 31 | + |
| 32 | + var deserialized = SourceDeserializeJsonString<NumericTests>(json); |
| 33 | + |
| 34 | + deserialized.Float.Should().Be(float.MinValue); |
| 35 | + } |
| 36 | + |
| 37 | + [U] |
| 38 | + public void FloatMaxValue_SerializesCorrectly() |
| 39 | + { |
| 40 | + var numericTests = new NumericTests { Float = float.MaxValue }; |
| 41 | + |
| 42 | + var json = SourceSerializeAndGetJsonString(numericTests); |
| 43 | + |
| 44 | + json.Should().Be("{\"float\":3.4028235E+38}"); |
| 45 | + |
| 46 | + var deserialized = SourceDeserializeJsonString<NumericTests>(json); |
| 47 | + |
| 48 | + deserialized.Float.Should().Be(float.MaxValue); |
| 49 | + } |
| 50 | + |
| 51 | + [U] |
| 52 | + public void DoubleValuesIncludeFractionalPart_AndAreNotRounded() |
| 53 | + { |
| 54 | + var numericTests = new NumericTests { Double = 1.0 }; |
| 55 | + |
| 56 | + var json = SourceSerializeAndGetJsonString(numericTests); |
| 57 | + |
| 58 | + json.Should().Be("{\"double\":1.0}"); |
| 59 | + |
| 60 | + var deserialized = SourceDeserializeJsonString<NumericTests>(json); |
| 61 | + |
| 62 | + deserialized.Double.Should().Be(1.0); |
| 63 | + } |
| 64 | + |
| 65 | + [U] |
| 66 | + public void DoubleMinValue_SerializesCorrectly() |
| 67 | + { |
| 68 | + var numericTests = new NumericTests { Double = double.MinValue }; |
| 69 | + |
| 70 | + var json = SourceSerializeAndGetJsonString(numericTests); |
| 71 | + |
| 72 | + json.Should().Be("{\"double\":-1.7976931348623157E+308}"); |
| 73 | + |
| 74 | + var deserialized = SourceDeserializeJsonString<NumericTests>(json); |
| 75 | + |
| 76 | + deserialized.Double.Should().Be(double.MinValue); |
| 77 | + } |
| 78 | + |
| 79 | + [U] |
| 80 | + public void DoubleMaxValue_SerializesCorrectly() |
| 81 | + { |
| 82 | + var numericTests = new NumericTests { Double = double.MaxValue }; |
| 83 | + |
| 84 | + var json = SourceSerializeAndGetJsonString(numericTests); |
| 85 | + |
| 86 | + json.Should().Be("{\"double\":1.7976931348623157E+308}"); |
| 87 | + |
| 88 | + var deserialized = SourceDeserializeJsonString<NumericTests>(json); |
| 89 | + |
| 90 | + deserialized.Double.Should().Be(double.MaxValue); |
| 91 | + } |
| 92 | + |
| 93 | + [U] |
| 94 | + public void DoubleAsString_DeserializesCorrectly() |
| 95 | + { |
| 96 | + var json = "{\"double\":\"1.0\"}"; |
| 97 | + |
| 98 | + var deserialized = SourceDeserializeJsonString<NumericTests>(json); |
| 99 | + |
| 100 | + deserialized.Double.Should().Be(1.0); |
| 101 | + } |
| 102 | + |
| 103 | + [U] |
| 104 | + public void DecimalValuesIncludeDecimal_AndAreNotRounded() |
| 105 | + { |
| 106 | + var numericTests = new NumericTests { Decimal = 1.0m }; |
| 107 | + |
| 108 | + var json = SourceSerializeAndGetJsonString(numericTests); |
| 109 | + |
| 110 | + json.Should().Be("{\"decimal\":1.0}"); |
| 111 | + } |
| 112 | + |
| 113 | + private class NumericTests |
| 114 | + { |
| 115 | + public float? Float { get; set; } |
| 116 | + public double? Double { get; set; } |
| 117 | + public decimal? Decimal { get; set; } |
| 118 | + } |
| 119 | +} |
0 commit comments