From 8acf65b33ba6c8fe12498445dd96a304e18b0c7d Mon Sep 17 00:00:00 2001 From: linkrope Date: Wed, 31 Aug 2016 21:35:28 +0200 Subject: [PATCH] support for unit-threaded's fluent assertions --- README.md | 13 +++++++++++++ dub.json | 10 +++++++++- example.d | 2 +- fluent_assertions.d | 35 +++++++++++++++++++++++++++++++++++ src/dunit/framework.d | 20 +++++++++++++++++++- 5 files changed, 77 insertions(+), 3 deletions(-) create mode 100755 fluent_assertions.d diff --git a/README.md b/README.md index 344d675..31c6d75 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,19 @@ So, if you prefer TestNG's order of arguments, import `dunit.ng` or `dunit.ng.assertion` instead of the conventional `dunit` and `dunit.assertion`. +Fluent Assertions +----------------- + +The xUnit Testing Framework also supports the "fluent assertions" from +[unit-threaded](https://github.com/atilaneves/unit-threaded). + +For an example, have a look at [fluent-assertions](fluent_assertions.d). +Build and run the example using + + dub --config=fluent-assertions + +(When you get three failures, everything works fine.) + Related Projects ---------------- diff --git a/dub.json b/dub.json index 86304f0..de1e102 100644 --- a/dub.json +++ b/dub.json @@ -1,7 +1,7 @@ { "name": "d-unit", "description": "xUnit Testing Framework for D", - "copyright": "Copyright © 2013, Mario Kröplin", + "copyright": "Copyright © 2016, Mario Kröplin", "authors": ["Juan Manuel Cabo", "Mario Kröplin"], "license" : "BSL-1.0", "dependencies": {}, @@ -15,6 +15,14 @@ "name": "example", "targetType": "executable", "sourceFiles": ["example.d"] + }, + { + "name": "fluent-assertions", + "targetType": "executable", + "sourceFiles": ["fluent_assertions.d"], + "dependencies": { + "unit-threaded": ">=0.6.27" + } } ] } diff --git a/example.d b/example.d index 86dbbf9..3cdce66 100755 --- a/example.d +++ b/example.d @@ -1,7 +1,7 @@ #!/usr/bin/env dub /+ dub.sdl: name "example" -dependency "d-unit" version=">=0.7.2" +dependency "d-unit" version=">=0.8.0" +/ // Copyright Juan Manuel Cabo 2012. diff --git a/fluent_assertions.d b/fluent_assertions.d new file mode 100755 index 0000000..9674dea --- /dev/null +++ b/fluent_assertions.d @@ -0,0 +1,35 @@ +// Copyright Mario Kröplin 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +module fluent_assertion; + +import dunit; +import unit_threaded.should : shouldBeIn, shouldEqual, shouldNotEqual; + +class Test +{ + mixin UnitTest; + + @Test + public void shouldEqualFailure() + { + "bar".shouldEqual("baz"); + } + + @Test + public void shouldNotEqualFailure() + { + "foo".shouldNotEqual("foo"); + } + + @Test + public void shouldBeInFailure() + { + 42.shouldBeIn([0, 1, 2]); + } +} + +// either use the 'Main' mixin or call 'dunit_main(args)' +mixin Main; diff --git a/src/dunit/framework.d b/src/dunit/framework.d index a349b6c..f9719ba 100644 --- a/src/dunit/framework.d +++ b/src/dunit/framework.d @@ -234,7 +234,25 @@ body { try { - action(); + static if (__traits(compiles, { import unit_threaded.should : UnitTestException; })) + { + import unit_threaded.should : UnitTestException; + + try + { + action(); + } + catch (UnitTestException exception) + { + // convert exception to "fix" the message format + throw new AssertException('\n' ~ exception.msg, + exception.file, exception.line, exception); + } + } + else + { + action(); + } return true; } catch (AssertException exception)