Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-87] Adds Convention.NamespaceMustStartWith & Convention.Namespace.MustEndWith #93

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void NameMustStartWithConventionSpecification_Success()
.Should()
.BeTrue();
}

[Test]
public void NameMustStartWithConventionSpecification_FailsIfNameDoesNotStartWithSuppliedPrefix()
{
Expand All @@ -320,7 +320,7 @@ public void NameMustStartWithConventionSpecification_FailsIfNameDoesNotStartWith
result.IsSatisfied.Should().BeFalse();
result.Failures.Should().HaveCount(1);
}

private class ClassSuffix
{
}
Expand All @@ -334,7 +334,7 @@ public void NameMustEndWithConventionSpecification_Success()
.Should()
.BeTrue();
}

[Test]
public void NameMustEndWithConventionSpecification_FailsIfNameDoesNotEndWithSuppliedPrefix()
{
Expand Down Expand Up @@ -387,6 +387,46 @@ public void MustLiveInNamespaceConventionSpecification_FailsIfTypeDoesNotLiveInT
result.Failures.Should().HaveCount(1);
}

[Test]
public void NamespaceMustStartWithConventionSpecification_Success()
{
typeof(NamespaceMember)
.MustConformTo(Convention.NamespaceMustStartWith("Conventional.Tests.Conven"))
.IsSatisfied
.Should()
.BeTrue();
}

[Test]
public void NamespaceMustStartWithConventionSpecification_FailsIfTypeDoesNotLiveInANamespaceStartingWithPrefix()
{
var result = typeof (NamespaceMember)
.MustConformTo(Convention.NamespaceMustStartWith("Conventional.Potato"));

result.IsSatisfied.Should().BeFalse();
result.Failures.Should().HaveCount(1);
}

[Test]
public void NamespaceMustEndWithConventionSpecification_Success()
{
typeof(NamespaceMember)
.MustConformTo(Convention.NamespaceMustEndWith(".Conventional.Conventions"))
.IsSatisfied
.Should()
.BeTrue();
}

[Test]
public void NamespaceMustEndWithConventionSpecification_FailsIfTypeDoesNotLiveInANamespaceEndingWithSuffix()
{
var result = typeof (NamespaceMember)
.MustConformTo(Convention.NamespaceMustEndWith(".Conventional.Potato"));

result.IsSatisfied.Should().BeFalse();
result.Failures.Should().HaveCount(1);
}

private class HasADefaultConstructor
{
}
Expand Down Expand Up @@ -477,7 +517,7 @@ public HasIllegalDependencies(Dependency dependency)
{
}
}

[Test]
public void MustNotTakeADependencyOnConventionSpecification_FailsIfTheIdentifiedConstructorParameterExists()
{
Expand Down Expand Up @@ -506,7 +546,7 @@ public void MustHaveAppropriateConstructorsConventionSpecification_Success()
.MustConformTo(Convention.MustHaveAppropriateConstructors)
.IsSatisfied
.Should()
.BeTrue();
.BeTrue();
}

private class DoesNotHaveAppropriateConstructors
Expand Down Expand Up @@ -549,7 +589,7 @@ public void RequiresACorrespondingImplementationOfConventionSpecification_Succes
.MustConformTo(Convention.RequiresACorrespondingImplementationOf(typeof(SomeGeneric<,>), new [] { typeof(SomeGenericImplementation) }))
.IsSatisfied
.Should()
.BeTrue();
.BeTrue();
}

private interface ISomeGeneric<T1, T2>
Expand Down Expand Up @@ -694,7 +734,7 @@ public void EnumerablePropertiesMustBeEagerLoadedConventionSpecification_Ignores

private class HasLazilyLoadedEnumerables
{
public IEnumerable<string> Names { get; set; }
public IEnumerable<string> Names { get; set; }
}

[Test]
Expand Down Expand Up @@ -767,8 +807,8 @@ public void CollectionPropertiesMustNotHaveSetters_FailsWhenAMutableCollectionPr

result.IsSatisfied.Should().BeFalse();
result.Failures.Should().HaveCount(1);
}
}

private class HasImmutableProperties
{
public HasImmutableProperties(string[] names, int age)
Expand Down
12 changes: 11 additions & 1 deletion src/Core/Conventional/Convention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static NameMustStartWithConventionSpecification NameMustStartWith(string
{
return new NameMustStartWithConventionSpecification(prefix);
}

public static NameMustEndWithConventionSpecification NameMustEndWith(string suffix)
{
return new NameMustEndWithConventionSpecification(suffix);
Expand All @@ -41,6 +41,16 @@ public static MustLiveInNamespaceConventionSpecification MustLiveInNamespace(str
return new MustLiveInNamespaceConventionSpecification(nameSpace);
}

public static NamespaceMustStartWithConventionSpecification NamespaceMustStartWith(string prefix)
{
return new NamespaceMustStartWithConventionSpecification(prefix);
}

public static NamespaceMustEndWithConventionSpecification NamespaceMustEndWith(string suffix)
{
return new NamespaceMustEndWithConventionSpecification(suffix);
}

public static MustHaveADefaultConstructorConventionSpecification MustHaveADefaultConstructor => new MustHaveADefaultConstructorConventionSpecification();

public static MustHaveANonPublicDefaultConstructorConventionSpecification MustHaveANonPublicDefaultConstructor => new MustHaveANonPublicDefaultConstructorConventionSpecification();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace Conventional.Conventions
{
public class NamespaceMustEndWithConventionSpecification : ConventionSpecification
{
private readonly string _namespaceSuffix;

public NamespaceMustEndWithConventionSpecification(string suffix) => _namespaceSuffix = suffix;

protected override string FailureMessage => "Must live in namespace ending with {0} but actually lives in namespace {1}";

public override ConventionResult IsSatisfiedBy(Type type)
{
if (type.Namespace != null && type.Namespace.EndsWith(_namespaceSuffix))
{
return ConventionResult.Satisfied(type.FullName);
}

return ConventionResult.NotSatisfied(type.FullName, FailureMessage.FormatWith(_namespaceSuffix, type.Namespace));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace Conventional.Conventions
{
public class NamespaceMustStartWithConventionSpecification : ConventionSpecification
{
private readonly string _namespacePrefix;

public NamespaceMustStartWithConventionSpecification(string prefix) => _namespacePrefix = prefix;

protected override string FailureMessage => "Must live in namespace beginning with {0} but actually lives in namespace {1}";

public override ConventionResult IsSatisfiedBy(Type type)
{
if (type.Namespace != null && type.Namespace.StartsWith(_namespacePrefix))
{
return ConventionResult.Satisfied(type.FullName);
}

return ConventionResult.NotSatisfied(type.FullName, FailureMessage.FormatWith(_namespacePrefix, type.Namespace));
}
}
}