public void Convert() { // Arrange: create a world model. var world = new World(); var spitfire = new Fighter { Name = "Spitfire", PositionX = 100, PositionY = 200, Rotation = 45 }; var stuka = new Fighter { Name = "Stuka", PositionX = 300, PositionY = 400, Rotation = 90, Target = spitfire }; world.Fighters.Add(spitfire); world.Fighters.Add(stuka); // Act: convert to view model. var mainContext = new MainContext(null); var worldContext = new WorldContext(mainContext, world); // Assert: verify view model Assert.IsNotNull(worldContext.Fighters); Assert.AreEqual(2, worldContext.Fighters.Count); FighterContext spitfireContext = worldContext.Fighters.FirstOrDefault(f => f.Name == "Spitfire"); Assert.IsNotNull(spitfireContext); Assert.AreEqual("Spitfire", spitfireContext.Name); Assert.AreEqual(100.0, spitfireContext.Position.X); Assert.AreEqual(200.0, spitfireContext.Position.Y); Assert.AreEqual(45.0, spitfireContext.Rotation); Assert.AreEqual(null, spitfireContext.Target); FighterContext stukaContext = worldContext.Fighters.FirstOrDefault(f => f.Name == "Stuka"); Assert.IsNotNull(stukaContext); Assert.AreEqual("Stuka", stukaContext.Name); Assert.AreEqual(300.0, stukaContext.Position.X); Assert.AreEqual(400.0, stukaContext.Position.Y); Assert.AreEqual(90.0, stukaContext.Rotation); Assert.AreEqual(spitfireContext, stukaContext.Target); // Act: convert back to model. world = worldContext.CreateModel(); // Assert: correctness of created model. Assert.IsNotNull(world.Fighters); Assert.AreEqual(2, world.Fighters.Count); spitfire = world.Fighters.FirstOrDefault(f => f.Name == "Spitfire"); Assert.IsNotNull(spitfire); Assert.AreEqual("Spitfire", spitfire.Name); Assert.AreEqual(100, spitfire.PositionX); Assert.AreEqual(200, spitfire.PositionY); Assert.AreEqual(45, spitfire.Rotation); Assert.AreEqual(null, spitfire.Target); stuka = world.Fighters.FirstOrDefault(f => f.Name == "Stuka"); Assert.IsNotNull(stuka); Assert.AreEqual("Stuka", stuka.Name); Assert.AreEqual(300, stuka.PositionX); Assert.AreEqual(400, stuka.PositionY); Assert.AreEqual(90, stuka.Rotation); Assert.AreEqual(spitfire, stuka.Target); }
public void TestSerialization() { // Arrange: create a world var world = new World(); var spitfire = new Fighter {Name = "Spitfire", PositionX = 100, PositionY = 200, Rotation = 45}; var stuka = new Fighter{Name = "Stuka", PositionX = 300, PositionY = 400, Rotation = 90, Target = spitfire}; world.Fighters.Add(spitfire); world.Fighters.Add(stuka); // Act: Serialize to memory stream. // Tests should not change the environment, // so we prefer not to use the file system. var stream = new MemoryStream(); world.SaveTo(stream); Assert.IsTrue(stream.Length > 0 ); // Act: Deserialize and check result. stream.Position = 0; world = World.LoadFrom(stream); // Assert: correctness of deserialized world. Assert.IsNotNull(world.Fighters); Assert.AreEqual(2, world.Fighters.Count); spitfire = world.Fighters.FirstOrDefault(f => f.Name == "Spitfire"); Assert.IsNotNull(spitfire); Assert.AreEqual("Spitfire", spitfire.Name); Assert.AreEqual(100, spitfire.PositionX); Assert.AreEqual(200, spitfire.PositionY); Assert.AreEqual(45, spitfire.Rotation); Assert.AreEqual(null, spitfire.Target); stuka = world.Fighters.FirstOrDefault(f => f.Name == "Stuka"); Assert.IsNotNull(stuka); Assert.AreEqual("Stuka", stuka.Name); Assert.AreEqual(300, stuka.PositionX); Assert.AreEqual(400, stuka.PositionY); Assert.AreEqual(90, stuka.Rotation); Assert.AreEqual(spitfire, stuka.Target); // Dump result to debug window for demo purposes // WARNING: The StreamReader will close the stream when it is closed! // And the using statement will call Dispose on the StreamReader, // which will close the stream. // So we have to do this debug dump at the end of our test. stream.Position = 0; using (var reader = new StreamReader(stream)) { Debug.WriteLine(reader.ReadToEnd()); } }