How can one reliably run unittests

jfondren julian.fondren at gmail.com
Sun Sep 5 16:08:50 UTC 2021


On Sunday, 5 September 2021 at 15:52:51 UTC, jfondren wrote:
> On Sunday, 5 September 2021 at 15:04:58 UTC, H. S. Teoh wrote:
>>
>> I really don't understand the logic of this: surely the 
>> compiler ought to be able to know which module main() is 
>> declared in, so why does it need the user to select one 
>> specific module to be run?
>
> ```
> $ grep -H . mod?.d
> mod1.d:void main(string[] args) {
> mod1.d:    import std.stdio : writeln;
> mod1.d:    writeln(args[1 .. $]);
> mod1.d:}
> $ dmd -run mod1.d mod2.d mod3.d
> ["mod2.d", "mod3.d"]
> ```
>
> mod1.d is a program that prints its args that I'd like to run 
> with some arguments. mod2.d and mod3.d are not modules that 
> even exist; I just want them printed out like that. 
> Distinguishing between dmd and program arguments isn't a matter 
> of compiler smarts.

More:

```
$ grep -H . mod?.d
mod1.d:void main(string[] args) {
mod1.d:    import std.stdio : writeln;
mod1.d:    writeln(args[1 .. $]);
mod1.d:}
mod2.d:shared static this() {
mod2.d:    import std.stdio : writeln;
mod2.d:    writeln("module init: mod2.d");
mod2.d:}
mod3.d:shared static this() {
mod3.d:    import std.stdio : writeln;
mod3.d:    writeln("module init: mod3.d");
mod3.d:}
$ dmd -run mod1.d mod2.d mod3.d
["mod2.d", "mod3.d"]
$ dmd mod2.d mod3.d -run mod1.d mod2.d mod3.d
module init: mod2.d
module init: mod3.d
["mod2.d", "mod3.d"]
$ dmd mod2.d mod1.d -run mod3.d mod2.d mod1.d
module init: mod2.d
module init: mod3.d
["mod2.d", "mod1.d"]
$ echo 'import mod2, mod3;' >> mod1.d
$ dmd -i -run mod1.d
module init: mod2.d
module init: mod3.d
[]
```

In particular:

```
$ dmd mod2.d mod3.d -run mod1.d mod2.d mod3.d
$ dmd mod2.d mod1.d -run mod3.d mod2.d mod1.d
```

dmd links all the objects together to make an executable and runs 
it, and the behavior's the same with main()-having mod1.d and 
main()-lacking mod3.d as the argument after -run.


More information about the Digitalmars-d mailing list