public void CanDeserialize()
        {
            PinballMachine pbm = new PinballMachine();
            PinballMachine pbm2;
            string fileName = Path.GetTempFileName();

            pbm.Save(fileName);
            pbm2 = PinballMachine.FromFile(fileName);

            Assert.IsNotNull(pbm2);
            Assert.AreEqual(pbm.Elements.Count(), pbm2.Elements.Count());

            // Cleanup
            File.Delete(fileName);
        }
        public void CanSerialize()
        {
            // Arrange
            PinballMachine pbm = new PinballMachine();
            string fileName = Path.GetTempFileName();
            FileInfo info;

            // Act
            pbm.Save(fileName);
            info = new FileInfo(fileName);

            // Assert
            Assert.IsTrue( info.Length > 0 );

            // Cleanup
            File.Delete(fileName);
        }
        public void CanSerializeWithUserObject()
        {
            PinballMachine pbm = new PinballMachine();
            PinballMachine pbm2;
            Flipper flipper = new LeftFlipper() { X = 5, Y = 5 };
            Stream stream = new MemoryStream();

            pbm.Add(flipper);
            pbm.Save(stream);
            stream.Position = 0;
            pbm2 = PinballMachine.FromStream(stream);

            Assert.AreEqual(1, pbm2.DynamicElements.Count);
            Assert.AreEqual(flipper.X, pbm2.DynamicElements.Last().X, 0.1);
            Assert.AreEqual(flipper.Y, pbm2.DynamicElements.Last().Y, 0.1);
        }
        public void CanSerializeTwiceIntoSameFile()
        {
            // Arrange
            var mShorter = new PinballMachine();
            var mLonger = new PinballMachine();
            string file = Path.GetTempFileName();
            mLonger.Add(new Bumper());

            // Act
            mLonger.Save(file);
            mShorter.Save(file);

            var newMachine = PinballMachine.FromFile(file);
            Assert.IsTrue(newMachine.IsValid());
        }
        public void HasBidirectionalConsistencyAfterDeserialization()
        {
            PinballMachine pbm = new PinballMachine();
            PinballMachine pbm2;
            Stream stream = new MemoryStream();

            pbm.Save(stream);
            stream.Seek(0, SeekOrigin.Begin);
            pbm2 = PinballMachine.FromStream(stream);

            Assert.AreSame(pbm2, pbm2.DynamicElements.Owner);
        }