public void IndexOfTests()
        {
            var testContext = _propertyTestData.GetContext();
            var x           = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                          testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var a = testContext.GetChildElement("a");
            var b = testContext.GetChildElement("b");
            var c = testContext.GetChildElement("c");

            Assert.True(x.IndexOf(a) < 0);
            Assert.True(x.IndexOf(b) < 0);
            Assert.True(x.IndexOf(c) < 0);

            x.AddCopy(a);
            x.AddCopy(b);

            Assert.Equal(0, x.IndexOf(a));
            Assert.Equal(1, x.IndexOf(b));
            Assert.True(x.IndexOf(c) < 0);

            x.AddCopy(c);

            Assert.Equal(0, x.IndexOf(a));
            Assert.Equal(1, x.IndexOf(b));
            Assert.Equal(2, x.IndexOf(c));

            Assert.True(x.IndexOf(null) < 0);

            x.Dispose();
            Assert.Throws <ObjectDisposedException>(() => x.IndexOf(a));
        }
        public void IndexerTests()
        {
            var testContext = _propertyTestData.GetContext();
            var x           = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                          testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var a = testContext.GetChildElement("a");
            var b = testContext.GetChildElement("b");
            var c = testContext.GetChildElement("c");

            x.AddCopy(a);
            x.AddCopy(b);
            x.AddCopy(c);

            Assert.Equal(a, x[0], ConfigurationObjectComparer.Instance);
            Assert.Equal(b, x[1], ConfigurationObjectComparer.Instance);
            Assert.Equal(c, x[2], ConfigurationObjectComparer.Instance);

            var change = (NotifyCollectionChangedAction)(-1);

            x.CollectionChanged += (sender, args) => { change = args.Action; };

            x[0] = c;

            Assert.Equal(NotifyCollectionChangedAction.Replace, change);

            x[2] = a;

            Assert.Equal(c, x[0], ConfigurationObjectComparer.Instance);
            Assert.Equal(b, x[1], ConfigurationObjectComparer.Instance);
            Assert.Equal(a, x[2], ConfigurationObjectComparer.Instance);

            x.Dispose();
            Assert.Throws <ObjectDisposedException>(() => x[0]);
        }
        public void CopyToTests()
        {
            var testContext = _propertyTestData.GetContext();
            var x           = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                          testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var a = testContext.GetChildElement("a");
            var b = testContext.GetChildElement("b");
            var c = testContext.GetChildElement("c");

            x.AddCopy(a);
            x.AddCopy(b);
            x.AddCopy(c);

            var array = new IChildElement[3];

            x.CopyTo(array, 0);

            Assert.Equal(a, array[0], ConfigurationObjectComparer.Instance);
            Assert.Equal(b, array[1], ConfigurationObjectComparer.Instance);
            Assert.Equal(c, array[2], ConfigurationObjectComparer.Instance);

            Assert.Throws <ArgumentOutOfRangeException>(() => x.CopyTo(array, 1));

            Assert.Throws <ArgumentOutOfRangeException>(() => x.CopyTo(array, -1));

            array = new IChildElement[2];

            Assert.Throws <ArgumentOutOfRangeException>(() => x.CopyTo(array, 0));

            x.Dispose();
            Assert.Throws <ObjectDisposedException>(() => x.CopyTo(array, 0));
        }
        public void InsertTests_WithCollection()
        {
            var testContext = _propertyTestData.GetContext();
            var x           = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                          testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var a = testContext.GetChildElement("a");
            var b = testContext.GetChildElement("b");
            var c = testContext.GetChildElement("c");
            var d = testContext.GetChildElement("d");
            var e = testContext.GetChildElement("e");

            x.AddCopy(a);
            x.AddCopy(c);

            var changes = new List <NotifyCollectionChangedAction>();

            x.CollectionChanged += (sender, args) => { changes.Add(args.Action); };

            x.InsertCopy(1, b);

            Assert.Equal(2, changes.Count);
            Assert.Equal(NotifyCollectionChangedAction.Add, changes[0]);
            Assert.Equal(NotifyCollectionChangedAction.Move, changes[1]);

            Assert.Equal(3, x.Count);

            Assert.Equal(a, x[0], ConfigurationObjectComparer.Instance);
            Assert.Equal(b, x[1], ConfigurationObjectComparer.Instance);
            Assert.Equal(c, x[2], ConfigurationObjectComparer.Instance);

            x.InsertCopy(x.Count, d);

            Assert.Equal(4, x.Count);
            Assert.Equal(a, x[0], ConfigurationObjectComparer.Instance);
            Assert.Equal(b, x[1], ConfigurationObjectComparer.Instance);
            Assert.Equal(c, x[2], ConfigurationObjectComparer.Instance);
            Assert.Equal(d, x[3], ConfigurationObjectComparer.Instance);

            x.InsertCopy(0, e);

            Assert.Equal(5, x.Count);
            Assert.Equal(e, x[0], ConfigurationObjectComparer.Instance);
            Assert.Equal(a, x[1], ConfigurationObjectComparer.Instance);
            Assert.Equal(b, x[2], ConfigurationObjectComparer.Instance);
            Assert.Equal(c, x[3], ConfigurationObjectComparer.Instance);
            Assert.Equal(d, x[4], ConfigurationObjectComparer.Instance);

            Assert.Throws <ArgumentOutOfRangeException>(() => { x.InsertCopy(-1, a); });
            Assert.Throws <ArgumentOutOfRangeException>(() => { x.InsertCopy(x.Count + 1, a); });

            Assert.Throws <TypeMismatchException>(() => { x.Insert(1, a); });

            x.InsertCopy(0, null);
            Assert.Null(x[0]);

            x.Dispose();
            Assert.Throws <ObjectDisposedException>(() => x.InsertCopy(1, b));
        }
Example #5
0
        public void TestCompareComparableTypes()
        {
            var testContext = _propertyTestData.GetContext();

            var x = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var customObjectA = testContext.GetChildElement("a");
            var configObjectA = x.AddCopy(customObjectA);

            var otherCustomObject = new ChildElementMock();

            otherCustomObject.Name  = customObjectA.Name;
            otherCustomObject.Value = customObjectA.Value;

            Assert.NotEqual(customObjectA, configObjectA);
            Assert.Equal(customObjectA, configObjectA, ConfigurationObjectComparer.Instance);

            Assert.Equal(configObjectA, configObjectA, ConfigurationObjectComparer.Instance);
            Assert.Equal(customObjectA, customObjectA, ConfigurationObjectComparer.Instance);

            Assert.Equal(otherCustomObject, configObjectA, ConfigurationObjectComparer.Instance);
            Assert.Equal(otherCustomObject, customObjectA, ConfigurationObjectComparer.Instance);

            Assert.NotEqual(customObjectA, null, ConfigurationObjectComparer.Instance);
            Assert.NotEqual(customObjectA, null, ConfigurationObjectComparer.Instance);
            Assert.NotEqual(otherCustomObject, null, ConfigurationObjectComparer.Instance);
            Assert.NotEqual(configObjectA, null, ConfigurationObjectComparer.Instance);

            Assert.NotEqual(null, customObjectA, ConfigurationObjectComparer.Instance);
            Assert.NotEqual(null, customObjectA, ConfigurationObjectComparer.Instance);
            Assert.NotEqual(null, otherCustomObject, ConfigurationObjectComparer.Instance);
            Assert.NotEqual(null, configObjectA, ConfigurationObjectComparer.Instance);
        }
        public void ContainsTests()
        {
            var testContext = _propertyTestData.GetContext();
            var x           = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                          testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var a = testContext.GetChildElement("a");
            var b = testContext.GetChildElement("b");
            var c = testContext.GetChildElement("c");

            x.AddCopy(a);
            x.AddCopy(b);

            Assert.Contains(a, x, ConfigurationObjectComparer.Instance);
            Assert.Contains(b, x, ConfigurationObjectComparer.Instance);
            Assert.DoesNotContain(c, x, ConfigurationObjectComparer.Instance);
            Assert.DoesNotContain(null, x, ConfigurationObjectComparer.Instance);

            x.Dispose();
            Assert.Throws <ObjectDisposedException>(() => x.Contains(a));
        }
        public void PropertyChangedTests()
        {
            var testContext = _propertyTestData.GetContext();

            var x = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var a = testContext.GetChildElement("a");
            var b = testContext.GetChildElement("b");

            var callbackCount = 0;

            x.PropertyChanged += (source, args) => { ++callbackCount; };

            x.AddCopy(a);

            Assert.Equal(1, callbackCount);

            x.AddCopy(b);

            Assert.Equal(2, callbackCount);
        }
Example #8
0
        public void TestCopyValue_Collection()
        {
            var root = _propertyTestData.GetContext().Configuration.ConfigurationRoot;

            var propertyInfo = typeof(IRootElement).GetProperty(nameof(IRootElement.ChildCollection), BindingFlags.Instance | BindingFlags.Public);
            var propertyDef  = new PropertyDef(propertyInfo, new ConfigurationObjectSettings());

            var collection = new ConfigurationCollection <IChildElement>(null, propertyDef, root, new ConfigurationObjectSettings());

            collection.AddCopy(new ChildElementMock()
            {
                Name  = "NAME-1",
                Value = 1
            });
            collection.AddCopy(new ChildElementMock()
            {
                Name  = "NAME-2",
                Value = 2
            });
            collection.AddCopy(new ChildElementMock()
            {
                Name  = "NAME-3",
                Value = 3
            });

            var copy = propertyDef.CopyValue(propertyDef.Implementation, collection, null, root);

            Assert.NotNull(copy);
            Assert.Equal(collection.Count, copy.Count);

            for (var n = 0; n < collection.Count; ++n)
            {
                Assert.NotNull(copy[n]);
                Assert.Equal(collection[n].Name, copy[n].Name);
                Assert.Equal(collection[n].Value, copy[n].Value);
            }
        }
        public void RemoveTests()
        {
            var testContext = _propertyTestData.GetContext();
            var x           = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                          testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var a = testContext.GetChildElement("a");
            var b = testContext.GetChildElement("b");
            var c = testContext.GetChildElement("c");
            var d = testContext.GetChildElement("d");

            x.AddCopy(a);
            x.AddCopy(b);
            x.AddCopy(c);

            var change = (NotifyCollectionChangedAction)(-1);

            x.CollectionChanged += (sender, args) => { change = args.Action; };

            Assert.True(x.Remove(b));
            Assert.Equal(NotifyCollectionChangedAction.Remove, change);

            change = (NotifyCollectionChangedAction)(-1);
            Assert.False(x.Remove(b));
            Assert.Equal((NotifyCollectionChangedAction)(-1), change);

            Assert.Equal(2, x.Count);
            Assert.Equal(a, x[0], ConfigurationObjectComparer.Instance);
            Assert.Equal(c, x[1], ConfigurationObjectComparer.Instance);

            Assert.True(x.Remove(0));

            Assert.Equal(1, x.Count);
            Assert.Equal(c, x[0], ConfigurationObjectComparer.Instance);

            x.RemoveAt(0);

            Assert.Equal(0, x.Count);

            x.AddCopy(a);
            x.AddCopy(b);
            x.AddCopy(c);

            x.RemoveAt(2);

            Assert.Equal(2, x.Count);
            Assert.Equal(a, x[0], ConfigurationObjectComparer.Instance);
            Assert.Equal(b, x[1], ConfigurationObjectComparer.Instance);

            Assert.False(x.Remove(d));
            Assert.False(x.Remove(2));

            x.Dispose();
            Assert.Throws <ObjectDisposedException>(() => x.RemoveAt(0));
            Assert.Throws <ObjectDisposedException>(() => x.Remove(a));
        }
        public void IsDirtyTests()
        {
            var testContext = _propertyTestData.GetContext();
            var a           = testContext.GetChildElement("a");
            var b           = testContext.GetChildElement("b");
            var c           = testContext.GetChildElement("c");
            var e           = testContext.GetChildElement("e");

            var x = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings(), a, b, c);

            Assert.False(x.IsDirty);

            x.AddCopy(e);

            Assert.True(x.IsDirty);
        }
Example #11
0
        public void TestCompareMismatchedTypes()
        {
            var testContext = _propertyTestData.GetContext();

            var x = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var customObjectA = testContext.GetChildElement("a");
            var configObjectA = x.AddCopy(customObjectA);

            var otherCustomObject = new ChildElementMock();

            otherCustomObject.Name  = customObjectA.Name;
            otherCustomObject.Value = customObjectA.Value;

            var otherCustomObjectMultiInterface = new ChildElementMockMultiInterface();

            otherCustomObjectMultiInterface.Name  = customObjectA.Name;
            otherCustomObjectMultiInterface.Value = customObjectA.Value;

            Assert.NotEqual(customObjectA, otherCustomObjectMultiInterface);
            Assert.NotEqual((IChildElement)otherCustomObject, otherCustomObjectMultiInterface);
            Assert.NotEqual(configObjectA, otherCustomObjectMultiInterface);
        }
        public void EnumeratorTests()
        {
            var testContext = _propertyTestData.GetContext();
            var x           = new ConfigurationCollection <IChildElement>(null, testContext.ChildConfigurationCollectionPropertyDef,
                                                                          testContext.Configuration.ConfigurationRoot, new ConfigurationObjectSettings());

            var a = testContext.GetChildElement("a");
            var b = testContext.GetChildElement("b");
            var c = testContext.GetChildElement("c");

            foreach (var item in x)
            {
                Assert.True(false);
            }

            x.AddCopy(a);
            x.AddCopy(b);
            x.AddCopy(c);

            var n = 0;

            foreach (var item in (IEnumerable <IChildElement>)x)
            {
                switch (n++)
                {
                case 0:
                    Assert.Equal(a, item, ConfigurationObjectComparer.Instance);
                    break;

                case 1:
                    Assert.Equal(b, item, ConfigurationObjectComparer.Instance);
                    break;

                case 2:
                    Assert.Equal(c, item, ConfigurationObjectComparer.Instance);
                    break;

                default:
                    Assert.True(false);
                    break;
                }
            }

            n = 0;

            foreach (IChildElement item in (IEnumerable)x)
            {
                switch (n++)
                {
                case 0:
                    Assert.Equal(a, item, ConfigurationObjectComparer.Instance);
                    break;

                case 1:
                    Assert.Equal(b, item, ConfigurationObjectComparer.Instance);
                    break;

                case 2:
                    Assert.Equal(c, item, ConfigurationObjectComparer.Instance);
                    break;

                default:
                    Assert.True(false);
                    break;
                }
            }

            x.Dispose();
            Assert.Throws <ObjectDisposedException>(() =>
            {
                foreach (var y in x)
                {
                }

                ;
            });
        }