示例#1
0
        public void CreatesEmptyStateMemento()
        {
            // Arrange
            var shapeBag = new ShapeBag();

            // Act
            var result = shapeBag.CreateMemento();

            // Assert
            CollectionAssert.IsEmpty(result.State);
        }
示例#2
0
        public void CreatesMementoWithOneItem()
        {
            // Arrange
            var shapeBag = new ShapeBag();

            shapeBag.AddShape("triangle", new Triangle());

            // Act
            var memento = shapeBag.CreateMemento();

            // Assert
            Assert.AreEqual(1, memento.State.Count);
            Assert.AreEqual("triangle", memento.State.First().Key);
            Assert.IsInstanceOf <Triangle>(memento.State.First().Value);
        }
示例#3
0
        public void RestoresEmptyStateAfterAddingTwoItems()
        {
            // Arrange
            var shapeBag = new ShapeBag();
            var memento  = shapeBag.CreateMemento();

            shapeBag.AddShape("triangle", new Triangle());
            shapeBag.AddShape("rectangle", new Rectangle());

            // Act
            shapeBag.RestoreMemento(memento);
            var result = shapeBag.GetShapes();

            // Assert
            CollectionAssert.IsEmpty(result);
        }
示例#4
0
        internal static void Main(string[] args)
        {
            var bag = new ShapeBag();

            while (true)
            {
                Console.WriteLine("Press 1 to save rectangle, 2 for triangle, 3 to save state, 4 to recover state, 5 to show bag, 0 to exit");
                var input = Convert.ToInt32(Console.ReadLine());
                if (input == 0)
                {
                    break;
                }

                switch (input)
                {
                case 1:
                    bag.AddShape($"rectangle{Guid.NewGuid()}", new Rectangle());
                    Console.WriteLine("Saved rectangle");
                    break;

                case 2:
                    bag.AddShape($"triangle{Guid.NewGuid()}", new Triangle());
                    Console.WriteLine("Saved triangle");
                    break;

                case 3:
                    _memento = bag.CreateMemento();
                    Console.WriteLine("Created checkpoint");
                    break;

                case 4:
                    bag.RestoreMemento(_memento);
                    Console.WriteLine("Restored checkpoint");
                    break;

                case 5:
                    foreach (var item in bag.GetShapes())
                    {
                        Console.WriteLine($"{item.Key} is {item.Value.GetType().Name}");
                    }
                    break;
                }

                Console.ReadLine();
                Console.Clear();
            }
        }
示例#5
0
        public void RestoresStateWithOneItemStateAfterOneExtraItem()
        {
            // Arrange
            var shapeBag = new ShapeBag();

            shapeBag.AddShape("triangle", new Triangle());
            var memento = shapeBag.CreateMemento();

            shapeBag.AddShape("rectangle", new Rectangle());

            // Act
            shapeBag.RestoreMemento(memento);
            var result = shapeBag.GetShapes();

            // Assert
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("triangle", result.First().Key);
            Assert.IsInstanceOf <Triangle>(result.First().Value);
        }