Code Monkey home page Code Monkey logo

fluent.idioms's Introduction

Fluent.Idioms

A small set of extensions to make fixtures and assertions more fluent! Wow!

Motivation

This project was motivated by this article here. In the article, the author demonstrates a way to verify the use of guarded parameters in constructors and methods by asserting that invalid values throw various types of exceptions. A brief overview of my evolution through what would be my initial code and then a refactoring inspired by this article is demonstrated here:

// the Billboard class constructor
public Billboard(string message)
{
    if (message is null)
    {
        throw new ArgumentNullException(nameof(message));
    }

    this.Message = message;
}

// example test
[Fact]
public void ConstructorGuardsAgainstNullParameters()
{
    Action nullMessageAction = () => new Billboard(null);

    // shoutout to FluentAssertions for Should() :)
    nullMessageAction.Should().Throw<ArgumentNullException>();
}

Without completely covering the article, this looks fairly simple right? Well let's take a look at how this test grows as we add a class with multiple constructor parameters. We'll add a Painter who will paint the Billboard.

// the painter class constructor
public Painter(string firstName, string lastName, string socialSecurityNumber)
{
    if (firstName is null)
    {
        throw new ArgumentNullException(nameof(firstName));
    }

    if (lastName is null)
    {
        throw new ArgumentNullException(nameof(lastName));
    }

    if (socialSecurityNumber is null)
    {
    throw new ArgumentNullException(nameof(socialSecurityNumber));
    }

    FirstName = firstName;
    LastName = lastName;
    SocialSecurityNumber = socialSecurityNumber;
}

// another example test
[Theory]
[InlineData("Foo", "Bar", "123-45-6789")]
public void ConstructorGuardsAgainstNullParameters(string firstName, string lastName, string socialSecurityNumber)
{
    Action nullFirstNameAction = () => new Painter(null, lastName, socialSecurityNumber);

    Action nullLastNameAction = () => new Painter(firstName, null, socialSecurityNumber);

    Action nullSocialSecurityNumberAction = () => new Painter(firstName, lastName, null);

    nullFirstNameAction.Should().Throw<ArgumentNullException>();
    nullLastNameAction.Should().Throw<ArgumentNullException>();
    nullSocialSecurityNumberAction.Should().Throw<ArgumentNullException>();
}

See how the above tests grow in complexity with more parameters, the article presents a novel method for solving this issue. Demonstrated for reference in the two tests below.

[Fact]
public void BillboardConstructorGuardsAgainstNullParameters()
{
    var fixture = new Fixture();
    var assertion = new GuardClauseAssertion(fixture);
    assertion.Verify(typeof(Billboard).GetConstructors());
}

[Fact]
public void PainterConstructorGuardsAgainstNullParameters()
{
    var fixture = new Fixture();
    var assertion = new GuardClauseAssertion(fixture);
    assertion.Verify(typeof(Painter).GetConstructors());
}

While this is a significant improvement over the previous iterations of the tests, I wanted a way to make these tests flow more fluently. This code exercise ultimately lead to a few extension methods demonstrated below.

[Fact]
public void BillboardConstructorGuardsAgainstNullParameters() =>
    typeof(Painter)
        .Constructors()
        .ShouldGuardAgainstNullParameters();

[Fact]
public void PainterConstructorGuardsAgainstNullParameters() =>
    typeof(Painter)
        .Constructors()
        .ShouldGuardAgainstNullParameters();

So essentially the inner workings of this extension is to wrap a customized fixture in an extension method and using the GuardClauseAssertion Idiom in a more fluent expression format.

fluent.idioms's People

Contributors

kylemcmaster avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.