public void ApplyTo_NonMatchingSerializable_False()
        {
            var serializable = new Serializable { D = 3m, S = "s" };
            var subject = new SerializationConstraint<Serializable>(new JsonRoundtripSerializer<Serializable>(),
                Is.Not.SameAs(serializable)
                    .And.Property("S").EqualTo("sS")
                    .And.Property("D").EqualTo(3m));

            Assert.That(matches(subject,serializable), Is.False);
        }
        public void WriteMessageTo_NonMatchingSerializable_ActualContainsOffendingValueAndActualObject()
        {
            var serializable = new Serializable { D = 3m, S = "s" };
            var subject = new SerializationConstraint<Serializable>(new JsonRoundtripSerializer<Serializable>(),
                Is.Not.SameAs(serializable)
                    .And.Property("S").EqualTo("sS")
                    .And.Property("D").EqualTo(3m));

            Assert.That(getMessage(subject, serializable), Does.Contain(TextMessageWriter.Pfx_Actual + "\"s\"").And
                .Contains(" -> <" + typeof(Serializable).FullName + ">"));
        }
        public void CanBeNewedUp()
        {
            var serializable = new Serializable { D = 3m, S = "s" };

            var serializer = Substitute.For<IRoundtripSerializer<Serializable>>();
            serializer.Deserialize().Returns(serializable);

            Assert.That(serializable,
                new SerializationConstraint<Serializable>(serializer,
                    Has.Property("S").EqualTo("s")
                    .And.Property("D").EqualTo(3m)));
        }
        public void CanBeCreatedWithExtension()
        {
            var serializable = new Serializable { D = 3m, S = "s" };

            var serializer = Substitute.For<IRoundtripSerializer<Serializable>>();
            serializer.Deserialize().Returns(serializable);

            Assert.That(serializable,
                Must.Be.Serializable(serializer,
                    Has.Property("S").EqualTo("s")
                    .And.Property("D").EqualTo(3m)));
        }
        public void CanBeNewedUp()
        {
            string serializationRepresentation = "representation";
            var deserialized = new Serializable { D = 3m, S = "s" };

            var deserializer = Substitute.For<IDeserializer>();
            deserializer.Deserialize<Serializable>(serializationRepresentation).Returns(deserialized);

            Assert.That(serializationRepresentation,
                new DeserializationConstraint<Serializable>(deserializer,
                    Has.Property("S").EqualTo("s")
                        .And.Property("D").EqualTo(3m)));
        }
        public void CanBeCreatedWithExtension()
        {
            string serializationRepresentation = "representation";
            var deserialized = new Serializable { D = 3m, S = "s" };

            var deserializer = Substitute.For<IDeserializer>();
            deserializer.Deserialize<Serializable>(serializationRepresentation).Returns(deserialized);

            Assert.That(serializationRepresentation,
                Must.Be.Deserializable<Serializable>(deserializer,
                    Has.Property("S").EqualTo("s")
                        .And.Property("D").EqualTo(3m)));
        }
        public void ApplyTo_AppliesConstraintToDeserialized()
        {
            string serializationRepresentation = "representation";
            var deserialized = new Serializable { D = 3m, S = "s" };

            var deserializer = Substitute.For<IDeserializer>();
            var constraint = Substitute.For<Constraint>();
            deserializer.Deserialize<Serializable>(serializationRepresentation).Returns(deserialized);

            var subject = new DeserializationConstraint<Serializable>(deserializer, constraint);
            subject.ApplyTo(serializationRepresentation);

            constraint.Received().ApplyTo(deserialized);
        }
        public void ApplyTo_AppliesConstraintToDeserialized()
        {
            var serializable = new Serializable { D = 3m, S = "s" };
            var deserialized = new Serializable();

            var serializer = Substitute.For<IRoundtripSerializer<Serializable>>();
            var constraint = Substitute.For<Constraint>();
            serializer.Deserialize().Returns(deserialized);

            var subject = new SerializationConstraint<Serializable>(serializer, constraint);
            subject.ApplyTo(serializable);

            constraint.Received().ApplyTo(deserialized);
        }
        public void ApplyTo_AsksDeserializerToDeserializeWhatWasSerialized()
        {
            var serializable = new Serializable { D = 3m, S = "s" };

            var serializer = Substitute.For<IRoundtripSerializer<Serializable>>();

            var subject = new SerializationConstraint<Serializable>(serializer, Is.Null);
            subject.ApplyTo(serializable);

            ICall[] receivedCalls = serializer.ReceivedCalls().ToArray();

            // first call: .Serialize(serializable)
            Assert.That(receivedCalls[0].GetMethodInfo().Name, Is.EqualTo("Serialize"));
            Assert.That(receivedCalls[0].GetArguments()[0], Is.SameAs(serializable));

            // second call: Deserialize()
            Assert.That(receivedCalls[1].GetMethodInfo().Name, Is.EqualTo("Deserialize"));

            // third call: Dispose()
            Assert.That(receivedCalls[2].GetMethodInfo().Name, Is.EqualTo("Dispose"));
        }