fluent-asserts 0.10.1
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:
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
- Add the DUB dependency: https://code.dlang.org/packages/fluent-asserts
- Import it:
in
dub.json
:... "configurations": [ ... { "name": "unittest", "dependencies": { "fluent-asserts": "~>0.9.0", ... } }, ... ] ...
in your source files:
version(unittest) import fluent.asserts;
- Use it:
unittest {
true.should.equal(false).because("this is a failing assert");
}
unittest {
Assert.equal(true, false, "this is a failing assert");
}
- Run the tests:
➜ dub test --compiler=ldc2
API Docs
The library provides the should
template and the Assert
struct.
Should
should
can be used in combination with Uniform Function Call Syntax (UFCS)
auto should(T)(lazy const T testData);
So the following statements are equivalent
testedValue.should.equal(42);
should(testedValue).equal(42);
In addition, the library provides the not
and because
modifiers that allow to improve your asserts.
not
negates the assert condition:
testedValue.should.not.equal(42);
because
allows you to add a custom message:
true.should.equal(false).because("of test reasons");
///will output this message: Because of test reasons, true should equal `false`.
Assert
Assert
is a wrapper for the should struct that allows you to use the asserts with a different syntax.
For example, the following lines are equivalent:
testedValue.should.equal(42);
Assert.equal(testedValue, 42);
All the asserts that are available using the should
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;
}
Too much verbosity in the assert messages?
If you want to get less informations when an assert fails, you can use several versions:
DisableDiffResult
- will not show a diff for the string equal assertDisableSourceResult
- will not show the source code where the test failedDisableMessageResult
- will not show the defaul message for the assert
In dub.json:
"versions": [
"DisableDiffResult",
"DisableSourceResult",
"DisableMessageResult"
],
License
MIT. See LICENSE for details.
- Registered by Szabo Bogdan
- 0.10.1 released 6 years ago
- gedaiu/fluent-asserts
- fluentasserts.szabobogdan.com/
- MIT
- Copyright © 2018, Szabo Bogdan
- Authors:
- Sub packages:
- fluent-asserts:core
- Dependencies:
- ddmp, vibe-d:data, unit-threaded, vibe-d:http, libdparse
- Versions:
-
1.0.1 2024-Jun-19 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 - Download Stats:
-
-
8 downloads today
-
60 downloads this week
-
221 downloads this month
-
51050 downloads total
-
- Score:
- 2.9
- Short URL:
- fluent-asserts.dub.pm