/// <summary>
        /// Run a generic test against a target type to ensure that a single dependency is picked up by the search.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="expectToFind"></param>
        private void RunDependencyTest(Type input, bool expectToFind = true)
        {
            // Arrange
            var search  = new DependencySearch();
            var subject = Types
                          .InAssembly(Assembly.GetAssembly(input))
                          .That().HaveName(input.Name).GetTypeDefinitions();

            // Act
            // Search against the type name and its namespace - this demonstrates that namespace based searches also work
            var resultClass = search.FindTypesWithAnyDependencies(subject, new List <string> {
                typeof(ExampleDependency).FullName
            });
            var resultNamespace = search.FindTypesWithAnyDependencies(subject, new List <string> {
                typeof(ExampleDependency).Namespace
            });

            // Assert
            if (expectToFind)
            {
                Assert.Single(resultClass);                                                   // Only one dependency found
                Assert.Equal(resultClass.First().FullName, resultClass.First().FullName);     // The correct dependency found
                Assert.Single(resultNamespace);                                               // Only one dependency found
                Assert.Equal(resultNamespace.First().FullName, resultClass.First().FullName); // The correct dependency found
            }
            else
            {
                Assert.Equal(0, resultClass.Count);     // No dependencies found
                Assert.Equal(0, resultNamespace.Count); // No dependencies found
            }
        }
Example #2
0
        private void FindTypesWithAnyDependencies(IEnumerable <TypeDefinition> inputTypes, IEnumerable <string> dependenciesToSearch)
        {
            var search = new DependencySearch();

            // Act
            var result = search.FindTypesWithAnyDependencies(inputTypes, dependenciesToSearch);

            Assert.Equal(inputTypes.Count(), result.Count());
        }
        public void FindTypesWithAnyDependencies_PublicProperty_Found()
        {
            // Arrange
            var search   = new DependencySearch();
            var typeList = Types
                           .InAssembly(Assembly.GetAssembly(typeof(HasDependency)))
                           .That()
                           .ResideInNamespace(typeof(HasDependency).Namespace)
                           .GetTypeDefinitions();

            // Act
            var result = search.FindTypesWithAnyDependencies(typeList, new List <string> {
                typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName
            });

            // Assert
            Assert.Equal(3, result.Count);                                           // Three types found
            Assert.Equal(typeof(HasDependencies).FullName, result.First().FullName); // Correct types returned...
            Assert.Equal(typeof(HasAnotherDependency).FullName, result.Skip(1).First().FullName);
            Assert.Equal(typeof(HasDependency).FullName, result.Last().FullName);
        }