BenMorris / NetArchTest

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

ResideInNamespaceMatching with nested+internal types #98

Closed kl1mm closed 2 years ago

kl1mm commented 2 years ago

Hi BenMorris, we ran into a problem when trying to validate our nested internal types, which we first filtered for ResideInNamespaceMatching.

After examining the NetArchTest code, we found the extension method TypeDefinitionExtensions.GetNamespace, which only checks IsNestedPrivate or IsNestedPublic. For testing, we changed our class from internal to private and it worked.

        public static string GetNamespace(this TypeDefinition typeDefinition)
        {
            if (typeDefinition.IsNestedPrivate || typeDefinition.IsNestedPublic)
            {
                return typeDefinition.DeclaringType.FullName;
            }

            return typeDefinition.Namespace;
        }
NeVeSpl commented 2 years ago

I do not know what the author had in mind by restricting it only to private and public nested types, especially that in another place it is less restrictive:

https://github.com/BenMorris/NetArchTest/blob/23fd270a24281727438deac84956bbba6873218a/src/NetArchTest.Rules/Extensions/TypeReferenceExtensions.cs#L23-L30

I guess it is my mistake, since I am the one who wrote both methods :D .