Пример #1
0
        public void Test()
        {
            var ids = new List <int> {
                1, 2, 3
            };
            var authors = new List <string> {
                "a", "b", "c"
            };
            var article = new Article
            {
                Ids     = ids,
                Authors = authors
            };

            var memento = Memento.Create()
                          .Register(article);

            article.Ids.Remove(2);
            article.Ids.Add(4);

            article.Authors.Remove("b");
            article.Authors.Add("d");

            memento.Rollback();

            CollectionAssert.AreEqual(new List <int> {
                1, 2, 3
            }, article.Ids);
            CollectionAssert.AreEqual(new List <string> {
                "a", "c", "d"
            }, article.Authors);
        }
Пример #2
0
        public void Test()
        {
            var article1 = new Article {
                Author = "DCastro"
            };
            var article2 = new Article {
                Author = "JBarbosa"
            };
            var articles = new HashSet <Article>
            {
                article1,
                article2
            };

            var memento = Memento.Create()
                          .RegisterCollection(articles);

            articles.Remove(article2);

            memento.Rollback();

            Assert.AreEqual(2, articles.Count);
            CollectionAssert.Contains(articles, article1);
            CollectionAssert.Contains(articles, article2);
        }
        public void Test()
        {
            var author = new Author {
                Name = "DCastro"
            };
            var article = new Article
            {
                Authors = new List <ICollection <Author> >
                {
                    new Collection <Author>
                    {
                        author
                    }
                }
            };

            var memento = Memento.Create()
                          .Register(article);

            article.Authors[0] = null;
            article.Authors    = null;
            author.Name        = "No one";

            memento.Rollback();

            Assert.NotNull(article.Authors);
            Assert.AreEqual(1, article.Authors.Count);
            Assert.NotNull(article.Authors[0]);
            Assert.AreEqual(1, article.Authors[0].Count);
            CollectionAssert.AreEqual("DCastro", article.Authors.First().First().Name);
        }
        public void TestFailSilently()
        {
            Article article = new Article
            {
                Pages = new BlockingCollection <int>(
                    new ConcurrentQueue <int>(
                        new[] { 1, 2, 3 })
                    )
            };

            //Act
            var memento = Memento.Create()
                          .Register(article);

            int ignore;

            article.Pages.TryTake(out ignore);
            article.Pages.TryTake(out ignore);
            article.Pages.CompleteAdding();

            memento.Rollback();

            //Assert
            CollectionAssert.AreEquivalent(new[] { 3 }, article.Pages);
        }
Пример #5
0
        public void WithMementoCollection()
        {
            var author1 = new Author {
                Name = "DCastro"
            };
            var author2 = new Author {
                Name = "JBarbosa"
            };
            var article = new Article
            {
                Authors = new Queue <Author>(new[]
                {
                    author1, author2
                })
            };

            var memento = Memento.Create()
                          .Register(article);

            article.Authors.Dequeue();
            author1.Name = "No one";
            author2.Name = "No one";

            memento.Rollback();

            CollectionAssert.AreEqual(new[] { author1, author2 }, article.Authors);
            Assert.AreEqual("DCastro", author1.Name);
            Assert.AreEqual("JBarbosa", author2.Name);
        }
Пример #6
0
        public IElement this[int index]
        {
            get
            {
                Memento memento = this.items[index];
                return(memento.IsReal ? memento.Element : memento.Create());
            }

            set
            {
                throw new NotSupportedException("Please use the overload that takes a template");
            }
        }
        public void TestRegisterCollection()
        {
            int[] numbers = { 1, 2, 3 };

            var memento = Memento.Create()
                          .RegisterCollection(numbers);

            numbers[1] = 9;

            memento.Rollback();

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, numbers);
        }
        public void Test()
        {
            Article.Count = 1;

            var memento = Memento.Create()
                          .RegisterProperty(() => Article.Count);

            Article.Count++;

            memento.Rollback();

            Assert.AreEqual(1, Article.Count);
        }
Пример #9
0
        public void Test()
        {
            Article article = new Article {
                Title = "Draft"
            };

            var memento = Memento.Create()
                          .Register(article);

            article.Title = "Something else";
            memento.Rollback();

            Assert.AreEqual("Draft", article.Title);
        }
Пример #10
0
        public void Test()
        {
            Article.Count = new Count {
                Views = 1
            };

            var memento = Memento.Create()
                          .RegisterProperty(() => Article.Count.Views);

            Article.Count.Views++;

            memento.Rollback();

            Assert.AreEqual(1, Article.Count.Views);
        }
Пример #11
0
        public void Test()
        {
            var article = new Article("Draft", 1, "DCastro");

            var memento = Memento.Create()
                          .Register(article);

            article.ChangeState("Something else", 2, "No one");

            memento.Rollback();

            Assert.AreEqual("Draft", article.GetTitle());
            Assert.AreEqual(1, article.GetId());
            Assert.AreEqual("DCastro", article.GetAuthor());
        }
Пример #12
0
        public void Test()
        {
            var article = new Article();

            Article.Count = 1;

            var memento = Memento.Create()
                          .Register(article);

            Article.Count++;

            memento.Rollback();

            Assert.AreEqual(1, Article.Count);
        }
Пример #13
0
        public void Test()
        {
            Article article = new Article
            {
                Pages = new Queue <int>(
                    new[] { 1, 2, 3 })
            };

            //Act
            var memento = Memento.Create();

            var ex = Assert.Throws <CollectionException>(() => memento.Register(article));

            StringAssert.Contains("ICollectionAdapter", ex.Message);
        }
Пример #14
0
        public void TestDoesntRestoreRegularProperty()
        {
            var article = new Article {
                Id = 1
            };

            var memento = Memento.Create()
                          .Register(article);

            article.Id = 2;

            memento.Rollback();

            Assert.AreEqual(2, article.Id);
        }
Пример #15
0
        public void Test()
        {
            var article = new Article
            {
                Pages = new SetMock <int> {
                    1, 2, 3
                }
            };

            var memento = Memento.Create()
                          .Register(article);

            memento.Rollback();

            Assert.True(((SetMock <int>)article.Pages).UnionWithCalled);
        }
Пример #16
0
        public void Test()
        {
            var article = new Article
            {
                Ids = new ExplicitCollection(new[] { 1, 2, 3 })
            };

            var memento = Memento.Create()
                          .Register(article);

            (article.Ids as ICollection <int>).Remove(2);

            memento.Rollback();

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, article.Ids);
        }
Пример #17
0
        public void Test()
        {
            IArticle <string> article = new Article {
                Title = "Draft", Id = 1
            };

            var memento = Memento.Create()
                          .Register(article);

            (article as Article).Title = "Something else";
            (article as Article).Id    = 2;

            memento.Rollback();

            Assert.AreEqual("Draft", article.Title);
            Assert.AreEqual(1, (article as Article).Id);
        }
Пример #18
0
        public void Test()
        {
            var author = new Author {
                Name = "DCastro"
            };
            var article = new Article
            {
                Author = author
            };

            var memento = Memento.Create()
                          .Register(article);

            author.Name = "No one";

            memento.Rollback();

            Assert.AreEqual("No one", article.Author.Name);
        }
        public void Test()
        {
            Article article = new Article
            {
                Pages = new[] { 1, 2, 3 }
            };

            //Act
            var memento = Memento.Create()
                          .Register(article);

            article.Pages[1] = 9;
            article.Pages[2] = 8;

            memento.Rollback();

            //Assert
            CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, article.Pages);
        }
Пример #20
0
        public void TestTypeAsConversion()
        {
            var article = new Article
            {
                Author = new Person
                {
                    Name = "DCastro"
                }
            };

            var memento = Memento.Create()
                          .RegisterProperty(article, a => (a.Author as Person).Name);

            ((Person)article.Author).Name = "No one";

            memento.Rollback();

            Assert.AreEqual("DCastro", ((Person)article.Author).Name);
        }
Пример #21
0
        public void Test()
        {
            Author author = new Author {
                Name = "DCastro"
            };
            Article article = new Article {
                Author = author
            };

            var memento = Memento.Create()
                          .RegisterProperty(article, a => a.Author.Name);

            article.Author = new Author {
                Name = "No one"
            };

            memento.Rollback();

            Assert.AreEqual("DCastro", article.Author.Name);
        }
Пример #22
0
        public void Test()
        {
            var article = new Article
            {
                Ids = null
            };

            var memento = Memento.Create()
                          .Register(article);

            article.Ids = new List <int> {
                1
            };

            memento.Rollback();

            CollectionAssert.AreEqual(new List <int> {
                1
            }, article.Ids);
        }
        public void Test()
        {
            Article article = new Article
            {
                Pages = new Queue <int>(
                    new[] { 1, 2, 3 })
            };

            //Act
            var memento = Memento.Create()
                          .Register(article);

            article.Pages.Dequeue();
            article.Pages.Dequeue();

            memento.Rollback();

            //Assert
            CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, article.Pages);
        }
Пример #24
0
        public void TestWithMementoCollectionAttribute()
        {
            var author = new Author {
                Name = "DCastro"
            };
            var article = new Article2
            {
                Authors = new List <Author> {
                    author
                }
            };

            var memento = Memento.Create()
                          .Register(article);

            author.Name = "No one";

            memento.Rollback();

            Assert.AreEqual("No one", article.Authors.First().Name);
        }
        public void Test()
        {
            ConcreteArticle concreteArticle = new ConcreteArticle {
                Title = "Draft"
            };
            AbstractArticle abstractArticle = new ConcreteArticle {
                Title = "Draft"
            };

            var memento = Memento.Create()
                          .RegisterProperty(concreteArticle, a => a.Title)
                          .RegisterProperty(abstractArticle, a => a.Title);

            concreteArticle.Title = "Something else";
            abstractArticle.Title = "Something else";

            memento.Rollback();

            Assert.AreEqual("Draft", concreteArticle.Title);
            Assert.AreEqual("Draft", abstractArticle.Title);
        }
Пример #26
0
        public void WithMementoProperty()
        {
            var mainAuthor = new Author {
                Name = "DCastro"
            };

            var article = new Article
            {
                MainAuthor = mainAuthor
            };

            var memento = Memento.Create()
                          .Register(article);

            mainAuthor.Name    = "No one";
            article.MainAuthor = new Author();

            memento.Rollback();

            Assert.AreSame(mainAuthor, article.MainAuthor);
            Assert.AreEqual("DCastro", mainAuthor.Name);
        }
Пример #27
0
        public void Test()
        {
            Article article = new Article
            {
                Pages = new ConcurrentStack <int>(
                    new[] { 1, 2, 3 })
            };

            //Act
            var memento = Memento.Create()
                          .Register(article);

            int ignore;

            article.Pages.TryPop(out ignore);
            article.Pages.TryPop(out ignore);

            memento.Rollback();

            //Assert
            CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, article.Pages);
        }
        public void Test()
        {
            var author = new Author {
                Name = "DCastro"
            };
            var article = new Article
            {
                Authors = new SimpleCollection <Author> {
                    author
                }
            };

            var memento = Memento.Create()
                          .Register(article);


            author.Name = "No one";

            memento.Rollback();

            CollectionAssert.AreEqual("DCastro", article.Authors.First().Name);
        }
Пример #29
0
        public void Test()
        {
            var article1 = new Article {
                Author = "DCastro"
            };
            var article2 = new Article {
                Author = "JBarbosa"
            };
            var articles = new Queue <Article>(new[] { article1, article2 });

            var memento = Memento.Create()
                          .RegisterCollection(new QueueAdapter <Article>
            {
                Collection = articles
            });

            articles.Dequeue();

            memento.Rollback();

            Assert.AreEqual(2, articles.Count);
            CollectionAssert.Contains(articles, article1);
            CollectionAssert.Contains(articles, article2);
        }