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

Static classes are verified as abstract classes. is this correct? #258

Open
jnormen opened this issue Apr 22, 2024 · 3 comments
Open

Static classes are verified as abstract classes. is this correct? #258

jnormen opened this issue Apr 22, 2024 · 3 comments

Comments

@jnormen
Copy link

jnormen commented Apr 22, 2024

I have a test where I check if I have abstract classes if so then I want it to check if the suffix is Base.
But when I run the tests it complain on my Extensions classes and my Logger (source generated once.)

Non of them are marked as abstract. What do I miss? a bug? intented?

 [Fact]
    public void Abstract_Classes_Should_Have_Base_Suffix()
    {
        Classes()
            .That()
            .AreAbstract()
            .Should().HaveNameEndingWith("Base")
            .Because("It's a design rule to name the abstract classes with the suffix 'Base'.")
            .Check(Architecture);
    }

complain:

...
Infrastructure.Loggers.ServiceLogger does have name ServiceLogger
Lindex.Core.WebApi.EndpointsExtension does have name EndpointsExtension

example of an logger extension:

  public static partial class ServiceLogger
    {
        [LoggerMessage(
        Level = LogLevel.Information,
        EventName = "ServiceStarting",
        Message = "Start service '{ServiceName}'")]
        public static partial void ServiceStarting(this ILogger logger, string serviceName);

in this case it's partial.

   public static class EndpointsExtension
    {
        public static IEndpointRouteBuilder UseAllEndpoints(this IEndpointRouteBuilder builder)
        {
@jnormen
Copy link
Author

jnormen commented Apr 23, 2024

Think I know why. seams IL treat static and abstract as same :(

@mak638
Copy link
Collaborator

mak638 commented May 25, 2024

Hi, unfortunately this is correct - static classes are automatically treated as abstract. But they are treated as sealed as well! Therefore if you want to check only abstract classes that are non-static you could try the following workaround:

[Fact]
public void Abstract_Classes_Should_Have_Base_Suffix()
{
    Classes()
        .That()
        .AreAbstract()
        .And()
        .AreNotSealed()
        .Should().HaveNameEndingWith("Base")
        .Because("It's a design rule to name the abstract classes with the suffix 'Base'.")
        .Check(Architecture);
}

Please let me know if this workaround works for you. If so maybe we can further improve our Class class :)

@jnormen
Copy link
Author

jnormen commented May 26, 2024

Cool that seams to work :)
Did not know about the sealed thing. Maybe I shall check the IL more often :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants