D allows programmers to define their tests alongside the actual code in a unittest scope[1]. When compiled with a special flag, the compiler will build a binary containing the tests instead of the actual application. This is a strightforward and easy way to run tests and works well with Mesons test() command. Since using just one flag name to enable unittest mode would be too boring, compiler developers invented multiple ones. Adding this helper method makes it easy for people writing Meson build descriptions for D projects to enable unittestmode. [1]: https://dlang.org/spec/unittest.htmlpull/685/head
parent
3eb90414f6
commit
57c54a678c
5 changed files with 56 additions and 0 deletions
@ -0,0 +1,34 @@ |
||||
|
||||
import std.stdio; |
||||
|
||||
uint getFour () |
||||
{ |
||||
auto getTwo () |
||||
{ |
||||
return 1 + 1; |
||||
} |
||||
|
||||
return getTwo () + getTwo (); |
||||
} |
||||
|
||||
void main () |
||||
{ |
||||
import core.stdc.stdlib : exit; |
||||
|
||||
writeln ("Four: ", getFour ()); |
||||
exit (4); |
||||
} |
||||
|
||||
unittest |
||||
{ |
||||
writeln ("TEST"); |
||||
import core.stdc.stdlib : exit; |
||||
|
||||
assert (getFour () > 2); |
||||
assert (getFour () == 4); |
||||
|
||||
// we explicitly terminate here to give the unittest program a different exit
|
||||
// code than the main application has.
|
||||
// (this prevents the regular main() from being executed)
|
||||
exit (0); |
||||
} |
@ -0,0 +1 @@ |
||||
usr/bin/dapp?exe |
@ -0,0 +1,8 @@ |
||||
project('D Unittests', 'd') |
||||
|
||||
e = executable('dapp', 'app.d', install : true) |
||||
test('dapp_run', e, should_fail: true) |
||||
|
||||
e_test = executable('dapp_test', 'app.d', |
||||
d_args: meson.get_compiler('d').get_unittest_flag()) |
||||
test('dapp_test', e_test) |
Loading…
Reference in new issue