August 25, 2021
On Wednesday, 25 August 2021 at 12:44:54 UTC, Steven Schveighoffer wrote:
> [snip]

Would it be possible to use an external unit testing framework to get all the tests when compiling with -betterC automatically without manually listing all the modules?

August 25, 2021

On 8/25/21 10:44 AM, jmh530 wrote:

>

On Wednesday, 25 August 2021 at 12:44:54 UTC, Steven Schveighoffer wrote:

>

[snip]

Would it be possible to use an external unit testing framework to get all the tests when compiling with -betterC automatically without manually listing all the modules?

betterC does not create ModuleInfo. So I don't think so.

Potentially you could register tests via pragma(crt_constructor) calls. It would be messy.

Nothing would be as nice as the way the compiler currently sets up the moduleinfo.

-Steve

August 25, 2021

On Wednesday, 25 August 2021 at 14:50:04 UTC, Steven Schveighoffer wrote:

>

On 8/25/21 10:44 AM, jmh530 wrote:

>

On Wednesday, 25 August 2021 at 12:44:54 UTC, Steven Schveighoffer wrote:

>

[snip]

Would it be possible to use an external unit testing framework to get all the tests when compiling with -betterC automatically without manually listing all the modules?

betterC does not create ModuleInfo. So I don't think so.

Potentially you could register tests via pragma(crt_constructor) calls. It would be messy.

Nothing would be as nice as the way the compiler currently sets up the moduleinfo.

-Steve

Thanks.

August 25, 2021

On Wednesday, 25 August 2021 at 11:46:18 UTC, jfondren wrote:

>

[...]
alias tests = AliasSeq!(__traits(getUnitTests, [...]

drop that useless AliasSeq please ;)

August 25, 2021
On 8/24/2021 5:21 AM, deadalnix wrote:
> Adding a main for libraries is required, or it won't link, but adding one to executable is going to cause a link error.

This is indeed a real problem. It was solved with the -main switch to dmd, which will just stick one in for you. I use it all the time for unit testing modules.
August 25, 2021
On 8/24/2021 5:39 AM, Atila Neves wrote:
> The way I'd like it to work personally would be to CTFE unit tests in a module and have them run whenever I save the file. No linker, no main function, just that.

Interestingly, a lot of the tests for ImportC are in the form of CTFE and _Static_assert(). It works so well, I plan on using that method wherever I can.
August 25, 2021
On Wednesday, 25 August 2021 at 20:49:32 UTC, Walter Bright wrote:
> On 8/24/2021 5:21 AM, deadalnix wrote:
>> Adding a main for libraries is required, or it won't link, but adding one to executable is going to cause a link error.
>
> This is indeed a real problem. It was solved with the -main switch to dmd, which will just stick one in for you. I use it all the time for unit testing modules.

The problem with -main is that it's too blunt an instrument: it will add a main function whether the code you're compiling already has one or not. That means you have to keep track of which modules have main functions and which ones don't, so that you (or more realistically, your build system) can add -main only when it's necessary.

If we had something like -main=ifMissing that added an empty main function only if the modules being compiled didn't already have one, then it would be possible to unit-test any D module with a command line like

    dmd -i -unittest -main=ifMissing -run mymodule.d

Being able to use the same command for everything makes automation much easier--you can write it in a makefile rule, put it in a script, etc.
August 25, 2021
On Wednesday, 25 August 2021 at 21:05:42 UTC, Paul Backus wrote:
> If we had something like -main=ifMissing that added an empty main function only if the modules being compiled didn't already have one, then it would be possible to unit-test any D module with a command line like
>
>     dmd -i -unittest -main=ifMissing -run mymodule.d
>
> Being able to use the same command for everything makes automation much easier--you can write it in a makefile rule, put it in a script, etc.

Although at this point, the -main=ifMissing functionality should just be part of -unittest
August 25, 2021
On Wednesday, 25 August 2021 at 21:17:54 UTC, jfondren wrote:
> On Wednesday, 25 August 2021 at 21:05:42 UTC, Paul Backus wrote:
>> If we had something like -main=ifMissing that added an empty main function only if the modules being compiled didn't already have one, then it would be possible to unit-test any D module with a command line like
>>
>>     dmd -i -unittest -main=ifMissing -run mymodule.d
>>
>> Being able to use the same command for everything makes automation much easier--you can write it in a makefile rule, put it in a script, etc.
>
> Although at this point, the -main=ifMissing functionality should just be part of -unittest

Or rather, -unittest -run ? Since you could dmd -c -unittest a bunch of modules and then link them together.
August 25, 2021
On Wednesday, 25 August 2021 at 21:19:36 UTC, jfondren wrote:
> On Wednesday, 25 August 2021 at 21:17:54 UTC, jfondren wrote:
>> On Wednesday, 25 August 2021 at 21:05:42 UTC, Paul Backus wrote:
>>> If we had something like -main=ifMissing that added an empty main function only if the modules being compiled didn't already have one, then it would be possible to unit-test any D module with a command line like
>>>
>>>     dmd -i -unittest -main=ifMissing -run mymodule.d
>>>
>>> Being able to use the same command for everything makes automation much easier--you can write it in a makefile rule, put it in a script, etc.
>>
>> Although at this point, the -main=ifMissing functionality should just be part of -unittest
>
> Or rather, -unittest -run ? Since you could dmd -c -unittest a bunch of modules and then link them together.

Even if we have a rule like "-unittest -run implies -main=ifMissing", I think there should still be a separate flag for it--for documentation if nothing else.