Exemplo n.º 1
0
        public void Keys_GetEnumerator_Invalid(int count)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);

            NameObjectCollectionBase.KeysCollection keys = nameObjectCollection.Keys;
            IEnumerator enumerator = keys.GetEnumerator();

            // Has not started enumerating
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);

            // Has finished enumerating
            while (enumerator.MoveNext())
            {
                ;
            }
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);

            // Has reset enumerating
            enumerator.Reset();
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);

            // Modify collection
            enumerator.MoveNext();
            nameObjectCollection.Add("new-name", new Foo("new-value"));
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Reset());
            if (count > 0)
            {
                Assert.NotNull(enumerator.Current);
            }

            // Modified read only collection still throws
            nameObjectCollection.IsReadOnly = true;
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Reset());

            // Clear collection
            nameObjectCollection.IsReadOnly = false;
            enumerator = keys.GetEnumerator();
            enumerator.MoveNext();
            nameObjectCollection.Clear();
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Reset());
        }
Exemplo n.º 2
0
        public void Keys_GetEnumerator(int count)
        {
            MyNameObjectCollection nameObjectCollection = Helpers.CreateNameObjectCollection(count);

            NameObjectCollectionBase.KeysCollection keys = nameObjectCollection.Keys;
            Assert.NotSame(keys.GetEnumerator(), keys.GetEnumerator());

            IEnumerator enumerator = keys.GetEnumerator();

            for (int i = 0; i < 2; i++)
            {
                int counter = 0;
                while (enumerator.MoveNext())
                {
                    Assert.Equal(keys[counter], enumerator.Current);
                    counter++;
                }
                Assert.Equal(count, keys.Count);
                enumerator.Reset();
            }
        }