public void ShouldThrowExceptionForMissingTypeDependency()
        {
            var dependant = new TestDependant1().RequiresAny(typeof(TestDependant2));

            var coll = new DependantCollection <TestDependant>(new[] { dependant });

            Assert.Throws <DependencyException>(() => Run(coll));
        }
        public void ShouldRemoveDependant()
        {
            DependantCollection <TestDependant> coll = new DependantCollection <TestDependant>();
            var dependant = new TestDependant1();

            coll.Add(dependant);
            coll.Remove(dependant);
            Assert.Empty(coll);
        }
        public void ShouldAddDependant()
        {
            // coll can be added and then should appear in the IEnumerable
            DependantCollection <TestDependant> coll = new DependantCollection <TestDependant>();
            var dependant = new TestDependant1();

            coll.Add(dependant);
            Assert.Contains(dependant, coll);
        }
 private void Run(DependantCollection <TestDependant> coll)
 {
     // obtains the dependants in dependency order from the collection
     // and then executes their callbacks in order
     foreach (var dep in coll.Ordered)
     {
         dep.Run();
     }
 }
        public void ShouldAddAllDependantsOnConstruction()
        {
            var expected = new TestDependant[]
            {
                new TestDependant1(), new TestDependant2()
            };
            DependantCollection <TestDependant> coll = new DependantCollection <TestDependant>(expected);

            Assert.Equal(expected, coll);
        }
        public void ShouldAddAllDependants()
        {
            DependantCollection <TestDependant> coll = new DependantCollection <TestDependant>();

            TestDependant[] expected = { new TestDependant1(), new TestDependant2() };

            coll.AddAll(expected);

            Assert.Equal(expected, coll);
        }
        public void ShouldThrowExceptionForTwoDependantsWithIdenticalTypeDependencies()
        {
            var dependant1 = new TestDependant1().RequiresAny(typeof(TestDependant));
            var dependant2 = new TestDependant2().RequiresAny(typeof(TestDependant));

            var coll = new DependantCollection <TestDependant>(new TestDependant[] { dependant1, dependant2 });

            var ioex = Assert.Throws <InvalidOperationException>(() => Run(coll));

            Assert.IsType <DependencyException>(ioex.InnerException);
        }
        public void ShouldReplaceDependant()
        {
            DependantCollection <TestDependant> coll = new DependantCollection <TestDependant>();
            var original    = new TestDependant1();
            var replacement = new TestDependant2();

            coll.Add(original);
            coll.Replace(original, replacement);

            Assert.Equal(new[] { replacement }, coll);
        }
        public void ShouldThrowExceptionForMissingTypeDependencyWhichMatchesSelf()
        {
            // similar to above, but here the dependency is declared as being on a type
            // which the dependant satisfies itself.  This should be ignored and an error
            // should still occur becausea dependency declared like this means 'there must be
            // *other* objects of the same type as me in here'.
            var dependant = new TestDependant1().RequiresAny(typeof(TestDependant));

            var coll = new DependantCollection <TestDependant>(new[] { dependant });

            Assert.Throws <DependencyException>(() => Run(coll));
        }
        public void ShouldOrderAllAddedDependants()
        {
            DependantCollection <TestDependant> coll = new DependantCollection <TestDependant>();
            int configured = 0;

            coll.Add(new TestDependant1(() => configured++));
            coll.Add(new TestDependant1(() => configured++));
            coll.Add(new TestDependant1(() => configured++));

            Run(coll);

            Assert.Equal(3, configured);
        }
        public void ShouldOrderDependantsInRegistrationOrder()
        {
            // coll must be applied in the order they're registered,
            // assuming no dependencies etc.
            DependantCollection <TestDependant> coll = new DependantCollection <TestDependant>();
            bool firstCalled  = true;
            bool secondCalled = true;

            coll.Add(new TestDependant1(() => firstCalled = true));
            coll.Add(new TestDependant1(() =>
            {
                Assert.True(firstCalled);
                secondCalled = true;
            }));

            Run(coll);
            Assert.True(secondCalled);
        }
        public void ShouldRemoveDependants()
        {
            DependantCollection <TestDependant> coll = new DependantCollection <TestDependant>();
            var toRemove = new TestDependant[]
            {
                new TestDependant1(),
                new TestDependant1(),
                new TestDependant1()
            };
            var remaining = new TestDependant[]
            {
                new TestDependant2()
            };

            coll.AddAll(toRemove);
            coll.AddAll(remaining);
            coll.RemoveAll(toRemove);

            Assert.Equal(remaining, coll);
        }
        private void RunTest(TestDependant[] coll,
                             Action <DependantCollection <TestDependant> > test = null,
                             Action reset       = null,
                             bool dontConfigure = false)
        {
            // runs the test for each unique permutation of the coll.
            // note that the test fails as soon as one order fails - and, because
            // I'm not proficient enouigh with Xunit yet - I'm just outputting the
            // indices of the items, in order, which led to the failure.
            // Remember - keep the number of input items to a reasonable amount; with a
            // suggested maximum of 9-10 as the number of permutations is (n!) where n is the
            // length of the coll array.
            int count = 0;

            foreach (var uniqueOrder in UniquePermutations(coll))
            {
                count++;
                var collection = new DependantCollection <TestDependant>(uniqueOrder);
                try
                {
                    if (!dontConfigure)
                    {
                        Run(collection);
                    }
                    test?.Invoke(collection);
                    reset?.Invoke();
                }
                catch (Exception)
                {
                    Output.WriteLine($"Ordering of failed items: { string.Join(", ", uniqueOrder.Select(i => Array.IndexOf(coll, i))) } ");
                    throw;
                }
            }

            Output.WriteLine($"Total number of permutations run: { count }");
        }
        public void ShouldBeEmptyOnConstruction()
        {
            DependantCollection <TestDependant> coll = new DependantCollection <TestDependant>();

            Assert.Empty(coll);
        }