예제 #1
0
        public void DefaultSerializerFactory_CreateForType_ReturnsCorrectSerializer(Type serializeable, Type serializerType)
        {
            var factory    = SerializerFactory.CreateDefault();
            var serializer = factory.Create(serializeable);

            Assert.IsInstanceOf(serializerType, serializer);
        }
예제 #2
0
        private TContract CreateProxyContract(Transporter light)
        {
            var memebers   = ProxyContractFactory.ParseContractInterface(typeof(TContract));
            var dispatcher = _contractBuilder.ReceiveDispatcherFactory();

            var outputMessages = memebers.GetMethods().Select(m => new MessageTypeInfo
            {
                ReturnType    = m.Value.ReturnType,
                ArgumentTypes = m.Value.GetParameters().Select(p => p.ParameterType).ToArray(),
                MessageId     = (short)m.Key
            });

            var inputMessages = memebers.GetProperties().Select(m => new MessageTypeInfo
            {
                ArgumentTypes = ReflectionHelper.GetDelegateInfoOrNull(m.Value.PropertyType).ParameterTypes,
                ReturnType    = ReflectionHelper.GetDelegateInfoOrNull(m.Value.PropertyType).ReturnType,
                MessageId     = (short)m.Key
            });

            var messenger = new Messenger(
                light,
                SerializerFactory.CreateDefault(_contractBuilder.UserSerializationRules.ToArray()),
                DeserializerFactory.CreateDefault(_contractBuilder.UserDeserializationRules.ToArray()),
                outputMessages: outputMessages.ToArray(),
                inputMessages: inputMessages.ToArray()
                );

            var interlocutor = new Interlocutor(messenger, dispatcher, _contractBuilder.MaxAnswerTimeoutDelay);

            var contract = ProxyContractFactory.CreateProxyContract <TContract>(interlocutor);

            return(contract);
        }
        public void SerializationThrowsRule_throws()
        {
            //Just check the Moq generator behaves as expected
            var rule       = IntegrationTestsHelper.GetThrowsSerializationRuleFor <string>();
            var serializer = rule.GetSerializer(typeof(string), SerializerFactory.CreateDefault());

            Assert.Throws <Exception>(() => serializer.Serialize(new object(), new MemoryStream()));
        }
예제 #4
0
        private static object SerializeAndDeserializeBack(object origin)
        {
            var serializer   = SerializerFactory.CreateDefault().Create(origin.GetType());
            var deserializer = DeserializerFactory.CreateDefault().Create(origin.GetType());
            var stream       = new MemoryStream();

            serializer.Serialize(origin, stream);
            stream.Position = 0;
            var deserialized = deserializer.Deserialize(stream, (int)stream.Length);

            return(deserialized);
        }
예제 #5
0
        private static T SerializeAndDeserializeBack <T>(T origin)
        {
            var type         = typeof(T);
            var serializer   = SerializerFactory.CreateDefault().Create(type);
            var deserializer = DeserializerFactory.CreateDefault().Create(type);
            var stream       = new MemoryStream();

            serializer.Serialize(origin, stream);
            stream.Position = 0;
            var deserialized = (T)deserializer.Deserialize(stream, (int)stream.Length);

            return(deserialized);
        }
예제 #6
0
        public void ComplexTypesSequence_SerializeAndDeserializeBack_OriginAndDeserializedAreEqual()
        {
            var origin = new object[]
            {
                "fortyTwo",
                "",
                new int[] { 1, 2, 3, 4 },
                new User
                {
                    Age      = 18,
                    IsFemale = true,
                    Name     = "Kate"
                },
                new MyStruct()
                {
                    MyBool = true,
                    MyDate = DateTime.Now,
                    MyInt  = 42,
                    MyLong = 42,
                }
            };

            var sequenceTypes = origin.Select(o => o.GetType()).ToArray();

            var serializer   = SerializerFactory.CreateDefault().Create(sequenceTypes);
            var deserializer = DeserializerFactory.CreateDefault().Create(sequenceTypes);
            var stream       = new MemoryStream();

            serializer.Serialize(origin, stream);
            stream.Position = 0;
            var deserialized = (object[])deserializer.Deserialize(stream, (int)stream.Length);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(origin[0], deserialized[0]);
                Assert.AreEqual(origin[1], deserialized[1]);
                CollectionAssert.AreEqual((IEnumerable)origin[2], (IEnumerable)deserialized[2]);
                Assert.IsTrue(((User)origin[3]).IsSameTo((User)deserialized[3]));
                Assert.IsTrue(((MyStruct)origin[4]).IsSameTo((MyStruct)deserialized[4]));
                Assert.AreEqual(origin.Length, deserialized.Length);
            });
        }
예제 #7
0
        public void PrimitiveTypesSequence_SerializeAndDeserializeBack_OriginAndDeserializedAreEqual()
        {
            var origin = new object[]
            {
                "fortyTwo",
                42,
                42d,
                MyEnumDefType.Two,
                MyEnumULongType.Max
            };
            var sequenceTypes = origin.Select(o => o.GetType()).ToArray();

            var serializer   = SerializerFactory.CreateDefault().Create(sequenceTypes);
            var deserializer = DeserializerFactory.CreateDefault().Create(sequenceTypes);
            var stream       = new MemoryStream();

            serializer.Serialize(origin, stream);
            stream.Position = 0;
            var deserialized = (object[])deserializer.Deserialize(stream, (int)stream.Length);

            CollectionAssert.AreEqual(origin, deserialized);
        }