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()); } }
public WorldContext(MainContext parent, World worldModel) { Main = parent; // Create fighter contexts from fighter models. // Use an anonymous class to associate // the original model with the new context. var newFighterPairs = from model in worldModel.Fighters select new { model, context = new FighterContext(this) { Name = model.Name, Position = new Point(model.PositionX, model.PositionY), Rotation = model.Rotation } }; // For fast lookup, convert the list of model <-> context pairs to a dictionary. Dictionary<Fighter, FighterContext> newFighterMap = newFighterPairs.ToDictionary(pair => pair.model, pair => pair.context); // Set the Target properties (these are possibly cyclically connected) foreach (var pair in newFighterMap) { Fighter targetFighter = pair.Key.Target; if (targetFighter != null) { FighterContext fighterContext = pair.Value; fighterContext.Target = newFighterMap[targetFighter]; } } Fighters = new UndoableCollection<FighterContext>(this, newFighterMap.Values); }
public World CreateModel() { // Create fighter models from fighter contexts. // Use an anonymous class to associate // the original context with the new model. var newFighterPairs = from context in Fighters select new { context, model = new Fighter { Name = context.Name, PositionX = context.Position.X, PositionY = context.Position.Y, Rotation = context.Rotation } }; // For fast lookup, convert the list of context <-> model pairs to a dictionary. Dictionary<FighterContext, Fighter> newFighterMap = newFighterPairs.ToDictionary(pair => pair.context, pair => pair.model); // Set the Target properties (these are possibly cyclically connected) foreach (var pair in newFighterMap) { FighterContext targetFighter = pair.Key.Target; if (targetFighter != null) { Fighter fighterModel = pair.Value; fighterModel.Target = newFighterMap[targetFighter]; } } var world = new World(); world.Fighters.AddRange(newFighterMap.Values); return world; }