Add new namespace assertions to TypeSelectorAssertions
See original GitHub issueDescription
Currently, TypeSelectorAssertions only contains methods for asserting that types are (not) decorated with specific attributes, e.g., BeDecoratedWith<>
. However, TypeSelector contains more selectors that would also be useful as assertions.
One such group of selectors filter on the namespace of types and contain the methods ThatAreInNamespace
, ThatAreNotInNamespace
, ThatAreUnderNamespace
, and ThatAreNotUnderNamespace
. Corresponding assertions could be named BeInNamespace
, NotBeInNamespace
, BeUnderNamespace
, and NotBeUnderNamespace
.
Use case
A concrete use case I have that prompted this suggestion is that the JsonSubTypes library, which is used to deserialize JSON into one of a number of subclasses based on a discriminator value, only supports deserializing into subclasses in the same namespace as the base class. Therefore, I would like to be able to write a test that asserts that all subclasses of a specific class are in the same namespace as it, à la:
AllTypes.From(baseClassAssembly).ThatDeriveFrom<BaseClass>().Should().BeInNamespace(baseClassNamespace);
Draft implementation as custom extension
To support the use case above, I have implemented the BeInNamespace
assertion in our project as the following custom extension:
public static class TypeSelectorAssertionsExtensions
{
public static AndConstraint<TypeSelectorAssertions> BeInNamespace(this TypeSelectorAssertions typeSelectorAssertions, string @namespace, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(@namespace != null)
.FailWith("Types cannot have a null namespace")
.Then
.Given(() => typeSelectorAssertions.Subject)
.ForCondition(types => types.All(type => type.Namespace == @namespace))
.FailWith("Expected types to be in namespace {0}{reason}, but found {1}.",
_ => @namespace, types => types.Where(type => type.Namespace != @namespace));
return new AndConstraint<TypeSelectorAssertions>(typeSelectorAssertions);
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (16 by maintainers)
Top GitHub Comments
I’ve opened #1196 to track issue with
ThatAreUnderNamespace
matching partial namespaces.Thanks for fixing the partial and null namespace issues in #1197. (I never got around to creating the issue for it. Sorry.)
Now that it’s merged, I’ll get #1180 up to date and ready to merge.