BenMorris / NetArchTest

A fluent API for .Net that can enforce architectural rules in unit tests.
MIT License
1.36k stars 81 forks source link

Inconsistent policy behaviour #124

Closed Parus1 closed 1 year ago

Parus1 commented 1 year ago

Hi, the below example seems to produce correct results, i.e. fails on the InterfaceImplementationB class:

public interface IInterfaceA { }
public interface IInterfaceB { }

internal class InterfaceImplementationA : IInterfaceA { }
public class InterfaceImplementationB : IInterfaceB { }

  [Fact]
  public void InterfaceImplementationsShouldBeInternal()
  {
      var policy = Policy.Define("Interface Implementations", "Interface implementations should be internal")
                         .For(NetArchTest.Rules.Types.FromPath(AppDomain.CurrentDomain.BaseDirectory))
                         .Add(types =>
                             types.That()
                                  .ImplementInterface(typeof(IInterfaceB))
                                  .Should()
                                  .NotBePublic())
                         .Add(types =>
                             types.That()
                                  .ImplementInterface(typeof(IInterfaceA))
                                  .Should()
                                  .NotBePublic());

      var results = policy.Evaluate();
      Assert.False(results.HasViolations);
  }

But, when I change the order of policy rules to the below, the test starts passing:

    [Fact]
    public void InterfaceImplementationsShouldBeInternal()
    {
        var policy = Policy.Define("Interface Implementations", "Interface implementations should be internal")
                           .For(NetArchTest.Rules.Types.FromPath(AppDomain.CurrentDomain.BaseDirectory))
                           .Add(types =>
                               types.That()
                                    .ImplementInterface(typeof(IInterfaceA))
                                    .Should()
                                    .NotBePublic())
                           .Add(types =>
                               types.That()
                                    .ImplementInterface(typeof(IInterfaceB))
                                    .Should()
                                    .NotBePublic());

        var results = policy.Evaluate();
        Assert.False(results.HasViolations);
    }

Am I missing something in the implementation of policies, or is there a potential issue here?