示例#1
0
        public void ClearRemoveAllItems()
        {
            var collection = new SimpleEventCollection {
                "A", "B", "C"
            };

            collection.Clear();

            Assert.AreEqual(0, collection.Count);
            Assert.IsFalse(collection.Contains("A"));
            Assert.IsFalse(collection.Contains("B"));
            Assert.IsFalse(collection.Contains("C"));
            Assert.AreEqual(string.Empty, string.Join(string.Empty, collection));
        }
示例#2
0
        public void RemoveRemovesItem()
        {
            var collection = new SimpleEventCollection {
                "A", "B", "C"
            };

            Assert.IsTrue(collection.Remove("B"));
            Assert.IsFalse(collection.Remove("D"));
            Assert.AreEqual(2, collection.Count);
            Assert.IsTrue(collection.Contains("A"));
            Assert.IsFalse(collection.Contains("B"));
            Assert.IsTrue(collection.Contains("C"));
            Assert.AreEqual("AC", string.Join(string.Empty, collection));
        }
示例#3
0
        public void CopyToCopiesItems()
        {
            // arrange
            var collection = new SimpleEventCollection {
                "A", "B", "C"
            };
            var array = new string[3];

            // act & assert
            Assert.IsFalse(collection.SequenceEqual(array));

            collection.CopyTo(array, 0);
            Assert.IsTrue(collection.SequenceEqual(array));
        }
示例#4
0
        public void AddAddsItem()
        {
            var collection = new SimpleEventCollection();

            collection.Add("A");
            collection.Add("B");
            collection.Add("C");

            Assert.AreEqual(3, collection.Count);
            Assert.IsTrue(collection.Contains("A"));
            Assert.IsTrue(collection.Contains("B"));
            Assert.IsTrue(collection.Contains("C"));
            Assert.AreEqual("ABC", string.Join(string.Empty, collection));
        }
示例#5
0
        public void SuccessfulClearWorks()
        {
            var collection = new SimpleEventCollection {
                "A", "B", "C"
            };
            string result = string.Empty;

            collection.ItemRemoving += (sender, args) => { };
            collection.ItemRemoved  += (sender, args) => { result += args.Item; };

            collection.Clear();

            Assert.AreEqual(0, collection.Count);
            Assert.AreEqual("ABC", result);
        }
示例#6
0
        public void ForbiddenClearFails()
        {
            var collection = new SimpleEventCollection {
                "A", "B", "C"
            };
            string result = string.Empty;

            collection.ItemRemoving += (sender, args) => { args.IsCanceled = args.Item == "B"; };
            collection.ItemRemoved  += (sender, args) => { result += args.Item; };

            var error = Assert.Throws <ItemsCantBeRemovedException <string> >(() => collection.Clear());

            Assert.AreEqual(3, collection.Count);
            Assert.IsNullOrEmpty(result);
            Assert.AreEqual("B", error.Items.Single());
        }
示例#7
0
        public void AddWorksOnlyIfAllowed()
        {
            var    collection = new SimpleEventCollection();
            string result     = string.Empty;

            collection.ItemAdding += (sender, args) => { args.IsCanceled = args.Item == "B"; };
            collection.ItemAdded  += (sender, args) => { result += args.Item; };

            collection.Add("A");
            collection.Add("B");
            collection.Add("C");

            Assert.AreEqual(2, collection.Count);
            Assert.IsTrue(collection.Contains("A"));
            Assert.IsFalse(collection.Contains("B"));
            Assert.IsTrue(collection.Contains("C"));
            Assert.AreEqual("AC", result);
        }
示例#8
0
        public void RemoveWorksOnlyIfAllowed()
        {
            var collection = new SimpleEventCollection {
                "A", "B", "C"
            };
            string result = string.Empty;

            collection.ItemRemoving += (sender, args) => { args.IsCanceled = args.Item == "B"; };
            collection.ItemRemoved  += (sender, args) => { result += args.Item; };

            Assert.IsTrue(collection.Remove("A"));
            Assert.IsFalse(collection.Remove("B"));
            Assert.IsTrue(collection.Remove("C"));
            Assert.AreEqual(1, collection.Count);
            Assert.IsFalse(collection.Contains("A"));
            Assert.IsTrue(collection.Contains("B"));
            Assert.IsFalse(collection.Contains("C"));
            Assert.AreEqual("AC", result);
        }