Example #1
0
        private static void RunVerification <T>(
            DescribedSerializationBase describedSerialization,
            RoundtripSerializationVerification <T> verificationCallback,
            AppDomainScenarios appDomainScenario,
            T deserializedObject)
        {
            new { verificationCallback }.AsArg().Must().NotBeNull();

            try
            {
                verificationCallback(describedSerialization, deserializedObject);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(Invariant($"Failed to roundtrip specified object to/from {describedSerialization.SerializerRepresentation.SerializationKind} {describedSerialization.GetSerializationFormat()} using {describedSerialization.SerializerRepresentation.SerializationConfigType.ResolveFromLoadedTypes().ToStringReadable()} with the App Domain Scenario '{appDomainScenario}'.  Serialized payload is: {describedSerialization.GetSerializedPayloadAsEncodedString()}.  Deserialized object is: {deserializedObject}."), ex);
            }
        }
 /// <summary>
 /// Test roundtrip serialization to/from JSON, asserting that the expected/provided value is equal to the deserialized value using
 /// the specified callback, with the serialization configuration type specified.
 /// </summary>
 /// <typeparam name="T">The type being tested.</typeparam>
 /// <param name="expected">The value to serialize, which should be equal to the resulting deserialized object.</param>
 /// <param name="verificationCallback">Callback to verify that the expected/provided value is equal to the deserialized value.</param>
 /// <param name="jsonSerializationConfigurationType">Optional type of the serialization configuration to use for JSON testing.  DEFAULT is null; <see cref="NullJsonSerializationConfiguration"/> will be used.</param>
 /// <param name="formats">The serialization formats to test.</param>
 /// <param name="appDomainScenarios">Optional value that specifies various scenarios of serializing and de-serializing in the current App Domain or a new App Domain.  DEFAULT is test the roundtrip in a new App Domain and also to serialize in a new App Domain and de-serialize in a new, but different App Domain.</param>
 public static void RoundtripSerializeViaJsonWithCallbackVerification <T>(
     this T expected,
     RoundtripSerializationVerification <T> verificationCallback,
     Type jsonSerializationConfigurationType           = null,
     IReadOnlyCollection <SerializationFormat> formats = null,
     AppDomainScenarios appDomainScenarios             = DefaultAppDomainScenarios)
 {
     expected.RoundtripSerializeWithCallbackVerification(
         verificationCallback,
         null,
         jsonSerializationConfigurationType,
         null,
         false,
         true,
         false,
         formats,
         appDomainScenarios);
 }
Example #3
0
        /// <summary>
        /// Test roundtrip serialization, asserting that the expected/provided value is equal to the deserialized value using
        /// the specified callback, with the serialization configuration type(s) specified.
        /// </summary>
        /// <typeparam name="T">The type being tested.</typeparam>
        /// <param name="expected">The value to serialize, which should be equal to the resulting deserialized object.</param>
        /// <param name="verificationCallback">Callback to verify that the expected/provided value is equal to the deserialized value.</param>
        /// <param name="bsonSerializationConfigurationType">Optional type of the serialization configuration to use for BSON testing.  DEFAULT is null; <see cref="NullBsonSerializationConfiguration"/> will be used.</param>
        /// <param name="jsonSerializationConfigurationType">Optional type of the serialization configuration to use for JSON testing.  DEFAULT is null; <see cref="NullJsonSerializationConfiguration"/> will be used.</param>
        /// <param name="propertyBagSerializationConfigurationType">Optional type of the serialization configuration to use for Property Bag testing.  DEFAULT is null; <see cref="NullPropertyBagSerializationConfiguration"/> will be used.</param>
        /// <param name="testBson">Optional value indicating whether to test serialization to/from BSON.  DEFAULT is true.</param>
        /// <param name="testJson">Optional value indicating whether to test serialization to/from JSON.  DEFAULT is true.</param>
        /// <param name="testPropertyBag">Optional value indicating whether to test serialization to/from a Property Bag.  DEFAULT is false.</param>
        /// <param name="formats">The serialization formats to test.</param>
        /// <param name="appDomainScenarios">Optional value that specifies various scenarios of serializing and de-serializing in the current App Domain or a new App Domain.  DEFAULT is test the roundtrip in a new App Domain and also to serialize in a new App Domain and de-serialize in a new, but different App Domain.</param>
        public static void RoundtripSerializeWithCallbackVerification <T>(
            this T expected,
            RoundtripSerializationVerification <T> verificationCallback,
            Type bsonSerializationConfigurationType        = null,
            Type jsonSerializationConfigurationType        = null,
            Type propertyBagSerializationConfigurationType = null,
            bool testBson        = true,
            bool testJson        = true,
            bool testPropertyBag = false,
            IReadOnlyCollection <SerializationFormat> formats = null,
            AppDomainScenarios appDomainScenarios             = DefaultAppDomainScenarios)
        {
            new { appDomainScenarios }.AsArg().Must().NotBeEqualTo(AppDomainScenarios.None);

            formats = formats ?? new[] { SerializationFormat.String, SerializationFormat.Binary };

            formats.AsArg().Must().NotBeNullNorEmptyEnumerable();

            var serializerRepresentations = new List <SerializerRepresentation>();

            if (testBson)
            {
                var serializerDescription = new SerializerRepresentation(SerializationKind.Bson, bsonSerializationConfigurationType?.ToRepresentation());

                serializerRepresentations.Add(serializerDescription);
            }

            if (testJson)
            {
                var serializerRepresentation = new SerializerRepresentation(SerializationKind.Json, jsonSerializationConfigurationType?.ToRepresentation());

                serializerRepresentations.Add(serializerRepresentation);
            }

            if (testPropertyBag)
            {
                var serializerDescription = new SerializerRepresentation(SerializationKind.PropertyBag, propertyBagSerializationConfigurationType?.ToRepresentation());

                serializerRepresentations.Add(serializerDescription);
            }

            if (!serializerRepresentations.Any())
            {
                throw new InvalidOperationException("No serializers are being tested.");
            }

            Func <SerializerRepresentation, SerializationFormat, object, DescribedSerialization> serializeFunc = Serialize;

            Func <DescribedSerialization, T> deserializeFunc = Deserialize <T>;

            Func <SerializerRepresentation, SerializationFormat, T, Tuple <string, T> > serializeAndDeserializeFunc = SerializeAndDeserialize <T>;

            foreach (var serializerRepresentation in serializerRepresentations)
            {
                foreach (var format in formats)
                {
                    if (appDomainScenarios.HasFlag(AppDomainScenarios.RoundtripInCurrentAppDomain))
                    {
                        var serializeAndDeserializeResult = serializeAndDeserializeFunc(serializerRepresentation, format, expected);

                        RunVerification(serializerRepresentation, format, verificationCallback, AppDomainScenarios.RoundtripInCurrentAppDomain, serializeAndDeserializeResult.Item1, serializeAndDeserializeResult.Item2);
                    }

                    if (appDomainScenarios.HasFlag(AppDomainScenarios.RoundtripInNewAppDomain))
                    {
                        var serializeAndDeserializeResult = serializeAndDeserializeFunc.ExecuteInNewAppDomain(serializerRepresentation, format, expected);

                        RunVerification(serializerRepresentation, format, verificationCallback, AppDomainScenarios.RoundtripInNewAppDomain, serializeAndDeserializeResult.Item1, serializeAndDeserializeResult.Item2);
                    }

                    if (appDomainScenarios.HasFlag(AppDomainScenarios.SerializeInCurrentAppDomainAndDeserializeInNewAppDomain))
                    {
                        var describedSerialization = serializeFunc(serializerRepresentation, format, expected);

                        var deserializedObject = deserializeFunc.ExecuteInNewAppDomain(describedSerialization);

                        RunVerification(serializerRepresentation, format, verificationCallback, AppDomainScenarios.SerializeInCurrentAppDomainAndDeserializeInNewAppDomain, describedSerialization.SerializedPayload, deserializedObject);
                    }

                    if (appDomainScenarios.HasFlag(AppDomainScenarios.SerializeInNewAppDomainAndDeserializeInNewAppDomain))
                    {
                        var describedSerialization = serializeFunc.ExecuteInNewAppDomain(serializerRepresentation, format, expected);

                        var deserializedObject = deserializeFunc.ExecuteInNewAppDomain(describedSerialization);

                        RunVerification(serializerRepresentation, format, verificationCallback, AppDomainScenarios.SerializeInNewAppDomainAndDeserializeInNewAppDomain, describedSerialization.SerializedPayload, deserializedObject);
                    }
                }
            }
        }