fluent-asserts 0.14.0-alpha.6

Fluent assertions done right


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Build Status Line Coverage DUB Version DUB Installs Percentage of issues still open Average time to resolve an issue

Writing unit tests is easy with Dlang. The unittest block allows you to start writing tests and to be productive with no special setup.

Unfortunately the assert expression does not help you to write expressive asserts, and in case of a failure it's hard to find why an assert failed. The fluent-asserts library allows you to more naturally specify the expected outcome of a TDD or BDD-style test.

To begin

  1. Add the DUB dependency: https://code.dlang.org/packages/fluent-asserts
  2. Import it:

    in dub.json:

        ...
        "configurations": [
            ...
            {
                "name": "unittest",
                "dependencies": {
                    "fluent-asserts": "~>0.13.0",
                    ...
                }
            },
            ...
        ]
        ...
    

    in your source files:

    version(unittest) import fluent.asserts;
    
  3. Use it:
    unittest {
        true.should.equal(false).because("this is a failing assert");
    }

    unittest {
        Assert.equal(true, false, "this is a failing assert");
    }
  1. Run the tests:
➜  dub test --compiler=ldc2

asciicast

API Docs

The library provides the expect, should templates and the Assert struct.

Expect

expect is the main assert function exposed by this library. It takes a parameter which is the value that is tested. You can use any assert operation provided by the base library or any other operations that was registered by a third party library.

Expect expect(T)(lazy T testedValue, ...);
Expect expect(void delegate() callable, ...);

...

expect(testedValue).to.equal(42);

In addition, the library provides the not and because modifiers that allow to improve your asserts.

not negates the assert condition:

expect(testedValue).to.not.equal(42);

because allows you to add a custom message:

    expect(true).to.equal(false).because("of test reasons");
    /// will output this message: Because of test reasons, true should equal `false`.

Should

should is designed to be used in combination with Uniform Function Call Syntax (UFCS), and is an alias for expect.

    auto should(T)(lazy T testData, ...);

So the following statements are equivalent

testedValue.should.equal(42);
expect(testedValue).to.equal(42);

In addition, you can use not and because modifiers with should.

not negates the assert condition:

    testedValue.should.not.equal(42);
    true.should.equal(false).because("of test reasons");

Assert

Assert is a wrapper for the expect function, that allows you to use the asserts with a different syntax.

For example, the following lines are equivalent:

    expect(testedValue).to.equal(42);
    Assert.equal(testedValue, 42);

All the asserts that are available using the expect syntax are available with Assert. If you want to negate the check, just add not before the assert name:

    Assert.notEqual(testedValue, 42);

You can use fluent asserts with:

Do you already have a lot of tests?

If you want to get the failure location for failing tests written using the Dlang's assert you can use the fluent assert handler which will add extra information to the default assert message.

    shared static this() {
        import fluent.asserts;
        setupFluentHandler;
    }

License

MIT. See LICENSE for details.

Authors:
  • Szabo Bogdan
Dependencies:
ddmp, unit-threaded, libdparse
Versions:
1.0.0 2022-Aug-24
1.0.0-beta.2 2022-Aug-24
1.0.0-beta.1 2022-Aug-12
0.14.0-alpha.13 2022-May-02
0.14.0-alpha.11 2021-Dec-16
Show all 63 versions
Download Stats:
  • 2 downloads today

  • 48 downloads this week

  • 182 downloads this month

  • 48672 downloads total

Score:
2.1
Short URL:
fluent-asserts.dub.pm