How can one reliably run unittests
jfondren
julian.fondren at gmail.com
Fri Aug 27 12:08:53 UTC 2021
On Friday, 27 August 2021 at 11:33:14 UTC, Johan wrote:
> On Friday, 27 August 2021 at 11:15:44 UTC, jfondren wrote:
>> On Friday, 27 August 2021 at 10:33:02 UTC, Johan wrote:
>>>
>>> (by the way, I'm convinced `unittest` is a misfeature of the
>>> language. Besides the clear downside that is apparent from
>>> this thread,
>>
>> What clear downside is that? A minor nit about -main? That
>> there's a discussion happening at all?
>
> The downside that people expect it to work well.
Look at this:
```d
#! /usr/bin/env dub
/++ dub.sdl:
configuration "release" {
targetType "executable"
}
configuration "unittest" {
targetType "library"
dependency "silly" version="~>1.1.1"
}
+/
T factorial(T)(T number) {
if (number <= 1)
return 1;
else
return number * factorial!T(number - 1);
}
@("!5") unittest {
assert(factorial(5) == 120);
}
@("!0 and !1") unittest {
assert(factorial(0) == 1);
assert(factorial(1) == 1);
}
version (unittest) {
} else {
void main(string[] args) {
import std.conv : to;
import std.stdio : writeln;
writeln(args[1].to!int.factorial);
}
}
```
Use it like a script:
```
$ ./fact.d 5; ./fact.d 10
120
3628800
```
Run its tests (colors lost in translation):
```
$ dub -q test --single fact.d
✓ fact !5
✓ fact !0 and !1
Summary: 2 passed, 0 failed in 0 ms
```
That's not too bad, is it? It can definitely be improved, but
none of my Perl scripts have tests in them.
Nevermind the factorial, it's just something I picked out of the
pile.
More information about the Digitalmars-d
mailing list