public void ShouldDetectMultipleIndirectRuleBreaksWithMultipleViolationPaths()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("A").WithReferences("B", "C");
            context.HasProject("B").WithReferences("D");
            context.HasProject("C").WithReferences("D");
            context.HasProject("D");

            context.Add(RuleDemandingThat().Project("A").IndependentOfProject("D"));
            context.Add(RuleDemandingThat().Project("A").IndependentOfProject("B"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                ProjectIndependentOfMessage.ProjectIndependentOfProject("A", "D").Error()
                .ViolationPath("A", "B", "D")
                .ViolationPath("A", "C", "D"));
            context.ReportShouldContain(
                ProjectIndependentOfMessage.ProjectIndependentOfProject("A", "B").Error()
                .ViolationPath("A", "B"));
            context.ShouldIndicateFailure();
        }
Example #2
0
        public void ShouldReportFailureWhenPatternExclusionDoesNotRuleOutDependingThatMatchAlone()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("CompositionRoot");
            context.HasProject("CompositionRootSpecification").WithReferences("CompositionRoot");
            context.HasProject("CompositionRootTests").WithReferences("CompositionRoot");

            context.Add(RuleBuilder.RuleDemandingThat()
                        .Project("*")
                        .Except("*Tests*")
                        .IndependentOfProject("*CompositionRoot*"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                ProjectIndependentOfMessage.ProjectIndependentOfProject(
                    "* except *Tests*", "*CompositionRoot*").Error()
                .ViolationPath("CompositionRootSpecification", "CompositionRoot"));
            context.ReportShouldNotContainText("CompositionRootTests");
            context.ShouldIndicateFailure();
        }
        public void ShouldReportErrorWhenMethodsMatchingPatternAreNotDecoratedWithAttributes()
        {
            //GIVEN
            var context = new NScanDriver();

            const string className   = "MySpecification";
            const string methodName1 = "ShouldA";
            const string methodName2 = "ShouldB";

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(File("lol.cs").Containing(
                      Class(className).With(
                          Method(methodName1),
                          Method(methodName2))));

            context.Add(RuleDemandingThat().Project("*MyProject*").HasDecoratedMethods("*Specification", "Should*"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                HasMethodsNotDecoratedWithAttribute("*MyProject*", "*Specification", "Should*")
                .Error()
                .NonCompliantMethodFound(className, methodName1)
                .NonCompliantMethodFound(className, methodName2));
        }
Example #4
0
        public void ShouldReportErrorWhenMultipleProjectsHaveFilesInWrongNamespaces()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject1")
            .WithRootNamespace("MyProject1")
            .With(FileWithNamespace("lol1.cs", "WrongNamespace"))
            .With(FileWithNamespace("lol2.cs", "WrongNamespace"))
            .With(FileWithNamespace("lol3.cs", "MyProject1"));
            context.HasProject("MyProject2")
            .WithRootNamespace("MyProject2")
            .With(FileWithNamespace("lol1.cs", "WrongNamespace"))
            .With(FileWithNamespace("lol2.cs", "WrongNamespace"))
            .With(FileWithNamespace("lol3.cs", "MyProject2"));
            context.Add(RuleDemandingThat().Project("*MyProject*").HasCorrectNamespaces());

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                HasCorrectNamespacesMessage.HasCorrectNamespaces("*MyProject*").Error()
                .ExpectedNamespace("MyProject1", "MyProject1")
                .ButFoundIncorrectNamespaceFor("lol1.cs", "WrongNamespace")
                .ExpectedNamespace("MyProject1", "MyProject1")
                .ButFoundIncorrectNamespaceFor("lol2.cs", "WrongNamespace")
                .ExpectedNamespace("MyProject2", "MyProject2")
                .ButFoundIncorrectNamespaceFor("lol1.cs", "WrongNamespace")
                .ExpectedNamespace("MyProject2", "MyProject2")
                .ButFoundIncorrectNamespaceFor("lol2.cs", "WrongNamespace"));
            context.ReportShouldNotContainText("lol3");
        }
        public void ShouldReportSuccessWhenAFileHasUsingForItsOwnNamespace()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(FileWithNamespace("lol1.cs", "MyProject").Using("MyProject"));
            context.Add(RuleDemandingThat().Project("*MyProject*").HasNoCircularUsings());

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasNoCircularUsingsMessage.HasNoCircularUsings("*MyProject*").Ok());
        }
Example #6
0
        public void ShouldReportErrorForProjectThatDoesNotHaveSpecifiedFramework()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject").WithTargetFramework(TargetFramework.Default);

            context.Add(RuleDemandingThat().Project("*MyProject*").HasProperty("TargetFramework", "netstandard2.1"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasProperty("*MyProject*", "TargetFramework", "netstandard2.1").Error()
                                        .ProjectHasAnotherPropertyValue("MyProject", "TargetFramework", TargetFramework.Default));
        }
Example #7
0
        public void ShouldReportErrorForProjectThatDoesNotHaveSpecifiedAttribute()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject");

            context.Add(RuleDemandingThat().Project("*MyProject*").HasProperty("Trolololo", "lol2"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasProperty("*MyProject*", "Trolololo", "lol2").Error()
                                        .ProjectDoesNotHavePropertySetExplicitly("MyProject", "Trolololo"));
        }
Example #8
0
        public void ShouldNotReportErrorWhenMultipleFilesOfSingleProjectAreInCorrectNamespace()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(FileWithNamespace("lol1.cs", "MyProject"))
            .With(FileWithNamespace("lol2.cs", "MyProject"));
            context.Add(RuleDemandingThat().Project("*MyProject*").HasCorrectNamespaces());

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasCorrectNamespacesMessage.HasCorrectNamespaces("*MyProject*").Ok());
        }
Example #9
0
        public void ShouldNotReportErrorWhenThereAreNoFilesInMatchedProjects()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject1")
            .WithRootNamespace("MyProject2");
            context.HasProject("MyProject2")
            .WithRootNamespace("MyProject2");
            context.Add(RuleDemandingThat().Project("*MyProject*").HasCorrectNamespaces());

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasCorrectNamespacesMessage.HasCorrectNamespaces($"*MyProject*").Ok());
        }
Example #10
0
        public void ShouldReportSuccessWhenThereAreNoCircularDependenciesBetweenNamespaces()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(FileWithNamespace("Port.cs", "MyProject.Ports"))
            .With(FileWithNamespace("Adapter.cs", "MyProject.Adapters"));
            context.Add(RuleDemandingThat().Project("*MyProject*").HasNoUsings(@from: "MyProject.Ports*", to: "MyProject.Adapters*"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasNoUsings("*MyProject*", "MyProject.Ports*", "MyProject.Adapters*").Ok());
        }
        public void ShouldReportSuccessWhenNoFilesInProject()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject");

            context.Add(RuleDemandingThat().Project("*MyProject*").HasDecoratedMethods("*Specification", "Should*"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                HasMethodsNotDecoratedWithAttribute("*MyProject*", "*Specification", "Should*").Ok());
        }
            public void ShouldReportErrorForMultipleProjectsThatDoNotHaveSpecifiedFramework()
            {
                //GIVEN
                var context = new NScanDriver();

                context.HasProject("MyProject1").WithTargetFramework(TargetFramework.Default);
                context.HasProject("MyProject2").WithTargetFramework(TargetFramework.Default);

                context.Add(RuleDemandingThat().Project("*MyProject*").HasTargetFramework("netstandard2.1"));

                //WHEN
                context.PerformAnalysis();

                //THEN
                context.ReportShouldContain(HasFramework("*MyProject*", "netstandard2.1").Error()
                                            .ProjectHasAnotherTargetFramework("MyProject1", TargetFramework.Default)
                                            .ProjectHasAnotherTargetFramework("MyProject2", TargetFramework.Default));
            }
        public void ShouldReportErrorWhenACycleIsDiscovered()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(FileWithNamespace("lol1.cs", "MyProject").Using("MyProject.Util"))
            .With(FileWithNamespace("lol2.cs", "MyProject.Util").Using("MyProject"));
            context.Add(RuleDemandingThat().Project("*MyProject*").HasNoCircularUsings());

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasNoCircularUsingsMessage.HasNoCircularUsings("*MyProject*").Error()
                                        .CycleFound("MyProject", "MyProject", "MyProject.Util", "MyProject"));
        }
        public void ShouldReportAllSatisfiedRules()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("A");
            context.HasProject("B");

            context.Add(RuleDemandingThat().Project("A")
                        .IndependentOfProject("B"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                ProjectIndependentOfMessage.ProjectIndependentOfProject("A", "B").Ok());
            context.ShouldIndicateSuccess();
        }
Example #15
0
        public void ShouldReportSuccessWhenAllProjectsHaveSpecifiedFramework()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithTargetFramework(TargetFramework.Default);

            context.Add(
                RuleDemandingThat().Project("*MyProject*")
                .HasProperty("TargetFramework", TargetFramework.Default));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                HasProperty("*MyProject*", "TargetFramework", TargetFramework.Default).Ok());
        }
        public void ShouldReportSuccessWhenNoProjectHasSpecifiedPackageReference()
        {
            //GIVEN
            var projectName = Any.String();
            var packageName = Any.String();
            var context     = new NScanDriver();

            context.HasProject(projectName);

            context.Add(RuleDemandingThat().Project(projectName).IndependentOfPackage(packageName));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                ProjectIndependentOfMessage.ProjectIndependentOfPackage(projectName, packageName).Ok());
            context.ShouldIndicateSuccess();
        }
        public void ShouldDetectRuleBreakWithGlobExpression()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("Posts.Domain").WithReferences("Posts.Ports");
            context.HasProject("Posts.Ports");

            context.Add(RuleDemandingThat().Project("*.Domain").IndependentOfProject("*.Ports"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                ProjectIndependentOfMessage.ProjectIndependentOfProject("*.Domain", "*.Ports").Error()
                .ViolationPath("Posts.Domain", "Posts.Ports"));
            context.ShouldIndicateFailure();
        }
Example #18
0
        public void ShouldReportErrorWhenNoCsProjectMatchesThePattern()
        {
            //bug implement this behavior!
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(FileWithNamespaces("lol.cs", "MyProject", "MyProject2"));
            context.Add(RuleDemandingThat().Project("*Trolololo*").HasCorrectNamespaces());

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasCorrectNamespacesMessage
                                        .HasCorrectNamespaces("*Trolololo*").Error()
                                        .NoProjectFoundMatching("*Trolololo*"));
        }
Example #19
0
        public void ShouldReportErrorWhenAFileHasMoreThanOneNamespace()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(FileWithNamespaces("lol.cs", "MyProject", "MyProject2"));
            context.Add(RuleDemandingThat().Project("*MyProject*").HasCorrectNamespaces());

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasCorrectNamespacesMessage
                                        .HasCorrectNamespaces("*MyProject*").Error()
                                        .ExpectedNamespace("MyProject", "MyProject")
                                        .ButHasMultipleNamespaces("lol.cs", "MyProject", "MyProject2"));
        }
Example #20
0
        public void ShouldReportFailureWhenThereAreForbiddenUsingDependenciesBetweenNamespaces()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(FileWithNamespace("Port.cs", "MyProject.Ports").Using("MyProject.Adapters"))
            .With(FileWithNamespace("Adapter.cs", "MyProject.Adapters"));
            context.Add(RuleDemandingThat().Project("*MyProject*")
                        .HasNoUsings(@from: "MyProject.Ports*", to: "MyProject.Adapters*"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasNoUsings("*MyProject*", "MyProject.Ports*", "MyProject.Adapters*").Error()
                                        .UsingsPathFound("MyProject", "MyProject.Ports", "MyProject.Adapters"));
        }
        public void ShouldReportFailureWhenProjectsHasSpecifiedPackageReferenceDirectly()
        {
            //GIVEN
            var projectName = Any.String();
            var packageName = Any.String();
            var context     = new NScanDriver();

            context.HasProject(projectName).WithPackages(packageName);

            context.Add(RuleDemandingThat().Project(projectName).IndependentOfPackage(packageName));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                ProjectIndependentOfMessage.ProjectIndependentOfPackage(projectName, packageName).Error()
                .ViolationPath(projectName));
            context.ShouldIndicateFailure();
        }
Example #22
0
        ShouldReportErrorWhenMultipleNestedFilesOfSingleProjectAreInWrongNamespaceEvenThoughSomeAreInTheRightOne()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(FileWithNamespace($"Domain{Path.DirectorySeparatorChar}lol4.cs", "MyProject.Domain"))
            .With(FileWithNamespace($"Domain{Path.DirectorySeparatorChar}lol5.cs", "MyProject"));
            context.Add(RuleDemandingThat().Project("*MyProject*").HasCorrectNamespaces());

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(HasCorrectNamespacesMessage
                                        .HasCorrectNamespaces("*MyProject*").Error()
                                        .ExpectedNamespace("MyProject", "MyProject")
                                        .ButFoundIncorrectNamespaceFor($"Domain{Path.DirectorySeparatorChar}lol5.cs", "MyProject"));
            context.ReportShouldNotContainText("lol4");
        }
Example #23
0
        public void ShouldReportIndirectRuleBreak()
        {
            //GIVEN
            var projectName  = Any.String("project1");
            var projectName2 = Any.String("project2");
            var assemblyName = Any.String("assembly");
            var context      = new NScanDriver();

            context.HasProject(projectName).WithReferences(projectName2);
            context.HasProject(projectName2).WithAssemblyReferences(assemblyName);

            context.Add(RuleDemandingThat().Project(projectName).IndependentOfAssembly(assemblyName));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                ProjectIndependentOfAssembly(projectName, assemblyName).Error()
                .ViolationPath(projectName, projectName2));
            context.ShouldIndicateFailure();
        }
Example #24
0
        public void ShouldReportSuccessWhenPatternExclusionRulesOutDependingThatWouldOtherwiseMatch()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("CompositionRoot");
            context.HasProject("CompositionRootSpecification").WithReferences("CompositionRoot");

            context.Add(RuleBuilder.RuleDemandingThat()
                        .Project("*")
                        .Except("*Specification*")
                        .IndependentOfProject("*CompositionRoot*"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                ProjectIndependentOfMessage.ProjectIndependentOfProject(
                    "* except *Specification*", "*CompositionRoot*"
                    ).Ok());
            context.ShouldIndicateSuccess();
        }
        public void ShouldReportSuccessWhenEveryMethodMatchingPatternHasAttributes()
        {
            //GIVEN
            var context = new NScanDriver();

            context.HasProject("MyProject")
            .WithRootNamespace("MyProject")
            .With(File("lol.cs").Containing(
                      Class("MySpecification").With(
                          Method("ShouldA")
                          .DecoratedWithAttribute(Any.String()),
                          Method("ShouldB")
                          .DecoratedWithAttribute(Any.String())
                          )));

            context.Add(RuleDemandingThat().Project("*MyProject*").HasDecoratedMethods("*Specification", "Should*"));

            //WHEN
            context.PerformAnalysis();

            //THEN
            context.ReportShouldContain(
                HasMethodsNotDecoratedWithAttribute("*MyProject*", "*Specification", "Should*").Ok());
        }