/// <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
            }
        }
        private void RunDependencyTest(Type input, bool expectToFind = true)
        {
            // Arrange
            var dependencies = new List <string> {
                typeof(ExampleDependency).FullName
            };
            var search  = new DependencySearch(target => dependencies.Any(target.StartsWith));
            var subject = Types
                          .InAssembly(Assembly.GetAssembly(input))
                          .That(HaveName(input.Name))
                          .GetTypeDefinitions().ToArray();

            // Act
            var result = search.FindTypesWithDependenciesMatch(subject).GetResults().ToArray();

            // Assert
            if (expectToFind)
            {
                Assert.Single(result);                                          // Only one dependency found
                Assert.Equal(result.First().FullName, result.First().FullName); // The correct dependency found
            }
            else
            {
                Assert.Empty(result); // No dependencies found
            }
        }
Example #3
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());
        }
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
        public void FindTypesWithAllDependencies_Found(string[] dependecies, string comment)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
        {
            // Arrange
            var search   = new DependencySearch();
            var typeList = Types
                           .InAssembly(Assembly.GetAssembly(typeof(HasDependency)))
                           .That()
                           .ResideInNamespace(typeof(HasDependency).Namespace)
                           .GetTypeDefinitions();

            // Act
            var result = search.FindTypesThatHaveDependencyOnAll(typeList, dependecies);

            // Assert
            Assert.Single(result);                                                   // One type found
            Assert.Equal(typeof(HasDependencies).FullName, result.First().FullName); // Correct type returned
        }
        public void FindTypesThatOnlyOnlyHaveDependenciesOnAll_Found()
        {
            // Arrange
            var search   = new DependencySearch();
            var typeList = Types
                           .InAssembly(Assembly.GetAssembly(typeof(Class_A)))
                           .That()
                           .ResideInNamespace(typeof(Class_A).Namespace)
                           .And()
                           .HaveNameStartingWith("Class")
                           .GetTypeDefinitions();

            // Act
            var result = search.FindTypesThatOnlyOnlyHaveDependenciesOnAll(typeList, new string[] { typeof(Dependency_1).FullName, typeof(Dependency_2).FullName, "System" });

            // Assert
            Assert.Equal(1, result.Count);
            Assert.Equal(typeof(Class_G).FullName, result[0].FullName);
        }
        public void FindTypesWithAllDependencies_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.FindTypesWithAllDependencies(typeList, new List <string> {
                typeof(ExampleDependency).FullName, typeof(AnotherExampleDependency).FullName
            });

            // Assert
            Assert.Single(result);                                                   // One type found
            Assert.Equal(typeof(HasDependencies).FullName, result.First().FullName); // Correct type returned
        }
        public void FindTypesWithAllDependencies_PatternMatchedNamespaces_NotReturned()
        {
            // In this example, searching for a dependency on "NamespaceMatch" should not return classes in "NamespaceMatchToo"

            // Arrange
            var search   = new DependencySearch();
            var typeList = Types
                           .InAssembly(Assembly.GetAssembly(typeof(HasDependency)))
                           .That()
                           .HaveName("NamespaceMatchingExample")
                           .GetTypeDefinitions();

            // Act
            var result = search.FindTypesWithAllDependencies(typeList, new List <string> {
                typeof(PatternMatch).Namespace
            });

            // Assert: Before PR#36 this would have returned classes in NamespaceMatchToo in the results
            Assert.Empty(result); // No results returned
        }
        public void FindTypesThatHaveDependencyOnAny_Found()
        {
            // Arrange
            var search   = new DependencySearch();
            var typeList = Types
                           .InAssembly(Assembly.GetAssembly(typeof(Class_A)))
                           .That()
                           .ResideInNamespace(typeof(Class_A).Namespace)
                           .And()
                           .HaveNameStartingWith("Class")
                           .GetTypeDefinitions();

            // Act
            var result = search.FindTypesThatHaveDependencyOnAny(typeList, new string[] { typeof(Dependency_1).FullName, typeof(Dependency_2).FullName });

            // Assert
            Assert.Equal(6, result.Count);
            Assert.Equal(typeof(Class_C).FullName, result[0].FullName);
            Assert.Equal(typeof(Class_D).FullName, result[1].FullName);
            Assert.Equal(typeof(Class_E).FullName, result[2].FullName);
            Assert.Equal(typeof(Class_F).FullName, result[3].FullName);
            Assert.Equal(typeof(Class_G).FullName, result[4].FullName);
            Assert.Equal(typeof(Class_H).FullName, result[5].FullName);
        }
        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
            var result = search.FindTypesWithDependencies(subject, new List <string> {
                typeof(ExampleDependency).FullName
            });

            // Assert
            if (expectToFind)
            {
                Assert.Single(result);                                          // Only one dependency found
                Assert.Equal(result.First().FullName, result.First().FullName); // The correct dependency found
            }
            else
            {
                Assert.Equal(0, result.Count); // No dependencies found
            }
        }