Exemple #1
0
        public void NonSeekableStreamWithOffsetAttributeShouldThrowInvalidOperationException()
        {
            var stream     = new NonSeekableStream();
            var serializer = new BinarySerialization.BinarySerializer();

            serializer.Serialize(stream, Cerealize());
        }
Exemple #2
0
        public void NonSeekableStreamDeserializationTest()
        {
            var stream     = new NonSeekableStream();
            var serializer = new BinarySerialization.BinarySerializer();

            serializer.Serialize(stream, new Iron());
        }
Exemple #3
0
        public void Roundtrip()
        {
            Cereal cereal = Cerealize();


            using (var stream = new MemoryStream())
            {
                _serializer.Serialize(stream, cereal);
                stream.Position = 0;

                Assert.AreEqual(BinarySerialization.Endianness.Big, _serializer.Endianness);

                //File.WriteAllBytes("c:\\temp\\out.bin", stream.ToArray());


                var cereal2 = _serializer.Deserialize <Cereal>(stream);

                Assert.AreEqual("Cheeri", cereal2.Name);
                Assert.AreEqual(cereal.Manufacturer, cereal2.Manufacturer);
                Assert.AreEqual(cereal.NutritionalInformation.Fat, cereal2.NutritionalInformation.Fat);
                Assert.AreEqual(cereal.NutritionalInformation.Calories, cereal2.NutritionalInformation.Calories);
                Assert.AreEqual(cereal.NutritionalInformation.VitaminA, cereal2.NutritionalInformation.VitaminA);
                Assert.AreEqual(cereal.NutritionalInformation.VitaminB, cereal2.NutritionalInformation.VitaminB);
                Assert.IsTrue(cereal.NutritionalInformation.OtherNestedStuff.SequenceEqual(
                                  cereal2.NutritionalInformation.OtherNestedStuff));
                Assert.IsTrue(cereal.NutritionalInformation.OtherNestedStuff2.SequenceEqual(
                                  cereal2.NutritionalInformation.OtherNestedStuff2));

                Assert.IsTrue(cereal.NutritionalInformation.Toys.SequenceEqual(cereal2.NutritionalInformation.Toys));

                Assert.IsInstanceOfType(cereal.NutritionalInformation.Ingredients.MainIngredient, typeof(Iron));

                Assert.AreEqual(cereal2.DoubleField, cereal2.DoubleField);
                Assert.IsTrue(cereal2.OtherStuff.Contains("app"));
                Assert.IsTrue(cereal2.OtherStuff.Contains("pea"));
                Assert.IsTrue(cereal2.OtherStuff.Contains("ban"));
                Assert.AreEqual(3, cereal2.OtherStuff.Count);
                Assert.AreEqual(cereal2.OtherStuff.Count, cereal2.OtherStuffCount);
                Assert.AreEqual(CerealShape.Circular, cereal2.Shape);
                Assert.AreEqual(CerealShape.Square, cereal2.DefinitelyNotTheShape);
                Assert.IsNull(cereal2.DontSerializeMe);
                Assert.AreEqual(cereal.SerializeMe, cereal2.SerializeMe);
                Assert.AreEqual(3, cereal2.ArrayOfInts.Length);
                Assert.AreEqual(1, cereal2.ArrayOfInts[0]);
                Assert.AreEqual(2, cereal2.ArrayOfInts[1]);
                Assert.AreEqual(3, cereal2.ArrayOfInts[2]);
                Assert.AreEqual(cereal.NutritionalInformation.WeirdOutlierLengthedField.Length / 2.0, cereal2.Outlier);

                Assert.IsTrue(cereal.ExplicitlyTerminatedList.SequenceEqual(cereal2.ExplicitlyTerminatedList));
                Assert.IsTrue(cereal.ImplicitlyTerminatedList.SequenceEqual(cereal2.ImplicitlyTerminatedList));

                //string weirdOutlierLengthedField = cereal.NutritionalInformation.WeirdOutlierLengthedField;
                //Assert.AreEqual(weirdOutlierLengthedField.Substring(0, (int)cereal.Outlier*2),
                //                cereal2.NutritionalInformation.WeirdOutlierLengthedField);

                var reader = new StreamReader(cereal2.Disclaimer);
                Assert.AreEqual(Disclaimer, reader.ReadToEnd());
            }
        }
Exemple #4
0
        public void NullGraphSerializationShouldSerializeNothing()
        {
            var serializer = new BinarySerialization.BinarySerializer();
            var stream     = new MemoryStream();

            serializer.Serialize(stream, null);
            Assert.AreEqual(0, stream.Length);
        }
        public void UnknownTypeSerializationTest()
        {
            var unknownTypeClass = new UnknownTypeClass {Field = "hello"};

            var serializer = new BinarySerialization.BinarySerializer();

            var stream = new MemoryStream();
            serializer.Serialize(stream, unknownTypeClass);
        }
        public void SubtypesOnUnknownTypeFieldShouldThrowBindingException()
        {
            var unknownTypeClass = new InvalidUnknownTypeClass { Field = "hello" };

            var serializer = new BinarySerialization.BinarySerializer();

            var stream = new MemoryStream();
            serializer.Serialize(stream, unknownTypeClass);
        }
Exemple #7
0
        public void SubtypesOnUnknownTypeFieldShouldThrowBindingException()
        {
            var unknownTypeClass = new InvalidUnknownTypeClass {
                Field = "hello"
            };

            var serializer = new BinarySerialization.BinarySerializer();

            var stream = new MemoryStream();

            serializer.Serialize(stream, unknownTypeClass);
        }
Exemple #8
0
        public void UnknownTypeSerializationTest()
        {
            var unknownTypeClass = new UnknownTypeClass {
                Field = "hello"
            };

            var serializer = new BinarySerialization.BinarySerializer();

            var stream = new MemoryStream();

            serializer.Serialize(stream, unknownTypeClass);
        }
        public void BindingAcrossUnknownBoundaryTest()
        {
            var childClass = new BindingAcrossUnknownBoundaryChildClass {Subfield = "hello"};
            var unknownTypeClass = new BindingAcrossUnknownBoundaryClass
            {
                Field = childClass
            };

            var serializer = new BinarySerialization.BinarySerializer();

            var stream = new MemoryStream();
            serializer.Serialize(stream, unknownTypeClass);

            var data = stream.ToArray();

            Assert.AreEqual((byte)childClass.Subfield.Length, data[0]);
        }
Exemple #10
0
        public void BindingAcrossUnknownBoundaryTest()
        {
            var childClass = new BindingAcrossUnknownBoundaryChildClass {
                Subfield = "hello"
            };
            var unknownTypeClass = new BindingAcrossUnknownBoundaryClass
            {
                Field = childClass
            };

            var serializer = new BinarySerialization.BinarySerializer();

            var stream = new MemoryStream();

            serializer.Serialize(stream, unknownTypeClass);

            var data = stream.ToArray();

            Assert.AreEqual((byte)childClass.Subfield.Length, data[0]);
        }
Exemple #11
0
        public void NullStreamSerializationShouldThrowNullArgumentException()
        {
            var serializer = new BinarySerialization.BinarySerializer();

            serializer.Serialize(null, new object());
        }