コード例 #1
0
        public void DeserializeAsCubicMetresWithFullySerializedUnit_ShouldReturnValidResult()
        {
            // arrange
            string json           = @"{
  'cubicMetres': 123.456,
  'unit': {
    'name': 'cubic centimetre',
    'abbreviation': 'cm³',
    'valueInCubicMetres': '0.000001'
  }
}";
            var    converter      = new VolumeJsonConverter();
            var    expectedResult =
                new Volume(cubicMetres: (number)123.456m)
                .Convert(new VolumeUnit(
                             name: "cubic millimetre",
                             abbreviation: "cm³",
                             valueInCubicMetres: (number)0.000001));

            // act
            var result = JsonConvert.DeserializeObject <Volume>(json, converter);

            // assert
            result.Should().Be(expectedResult);
        }
コード例 #2
0
        public void Serialize_ShouldReturnValidJson(VolumeSerializeTestData testData)
        {
            // arrange
            var converter = new VolumeJsonConverter(testData.Format, testData.UnitFormat);

            // act
            string actualJson = JsonConvert.SerializeObject(testData.Quantity, converter);

            // assert
            actualJson.Should().MatchRegex(testData.ExpectedJsonPattern);
        }
コード例 #3
0
        public void DeserializeAsCubicMetres_ShouldReturnValidResult()
        {
            // arrange
            string json           = @"{
  'cubicMetres': 123.456
}";
            var    converter      = new VolumeJsonConverter();
            var    expectedResult =
                new Volume(cubicMetres: (number)123.456m);

            // act
            var result = JsonConvert.DeserializeObject <Volume>(json, converter);

            // assert
            result.Should().Be(expectedResult);
        }
コード例 #4
0
        public void DeserializeAsCubicMetresWithPredefinedUnit_ShouldReturnValidResult()
        {
            // arrange
            string json           = @"{
  'cubicMetres': 123.456,
  'unit': 'cm³'
}";
            var    converter      = new VolumeJsonConverter();
            var    expectedResult =
                new Volume(cubicMetres: (number)123.456m)
                .Convert(VolumeUnit.CubicMillimetre);

            // act
            var result = JsonConvert.DeserializeObject <Volume>(json, converter);

            // assert
            result.Should().Be(expectedResult);
        }
コード例 #5
0
        public void SerializeAndDeserializeWithoutCubicMetres_ShouldBeApproximatelyIdempotent()
        {
            // arrange
            var volume    = Fixture.Create <Volume>();
            var converter = new VolumeJsonConverter(VolumeJsonSerializationFormat.AsValueWithUnit);

            // act
            string serializedVolume1   = JsonConvert.SerializeObject(volume, converter);
            var    deserializedVolume1 = JsonConvert.DeserializeObject <Volume>(serializedVolume1, converter);
            string serializedVolume2   = JsonConvert.SerializeObject(deserializedVolume1, converter);
            var    deserializedVolume2 = JsonConvert.DeserializeObject <Volume>(serializedVolume2, converter);

            // assert
            deserializedVolume1.CubicMetres.Should().BeApproximately(volume.CubicMetres);
            deserializedVolume2.CubicMetres.Should().BeApproximately(volume.CubicMetres);

            deserializedVolume2.Should().Be(deserializedVolume1);
            serializedVolume2.Should().Be(serializedVolume1);
        }
コード例 #6
0
        public void SerializeAndDeserializeWithCubicMetres_ShouldBeIdempotent(VolumeJsonSerializationFormat format, LinearUnitJsonSerializationFormat unitFormat)
        {
            // arrange
            var volume    = Fixture.Create <Volume>();
            var converter = new VolumeJsonConverter(format, unitFormat);

            // act
            string serializedVolume1   = JsonConvert.SerializeObject(volume, converter);
            var    deserializedVolume1 = JsonConvert.DeserializeObject <Volume>(serializedVolume1, converter);
            string serializedVolume2   = JsonConvert.SerializeObject(deserializedVolume1, converter);
            var    deserializedVolume2 = JsonConvert.DeserializeObject <Volume>(serializedVolume2, converter);

            // assert
            deserializedVolume1.Should().Be(volume);
            deserializedVolume2.Should().Be(volume);

            deserializedVolume2.Should().Be(deserializedVolume1);
            serializedVolume2.Should().Be(serializedVolume1);
        }