Probably a trivial question regarding version identifier and unittest

Adam D. Ruppe destructionator at gmail.com
Tue May 12 02:17:36 UTC 2020


On Tuesday, 12 May 2020 at 01:54:49 UTC, WhatMeWorry wrote:
> version(demos) unittest
> {
>     import arsd.terminal;
>
>     void main()
>
> Shouldn't the version identifier demos and the unittest option 
> activate the test block and therefore defines main() which then 
> give the "Start Address"?

The unittest {} block is actually a special syntax for a 
function. So the main function in here is a *nested* function 
inside the unittest function and thus doesn't count as a starting 
point. It is like if you defined

void MyTest() {
    void main() {}
}

The main there is a nested local helper function and thus 
wouldn't count as a start point either.

> Also, what is the isolated main; command right after the main 
> function?

That's just calling the defined child function so it actually 
gets executed. (I didn't put the parens there, but you could: 
`main();`, to make it clear it is just calling the function.

You can compile and run the tests btw with the command line:

dmd terminal.d -unittest -version=demos -main

the -main option at the end tells the compiler to add an empty 
main() function at top-level for you to satisfy the linker.


The reason I wrote it all this way is for the documentation. Look 
here for an example of how it looks when it is rendered:

http://dpldocs.info/experimental-docs/arsd.terminal.html#color

The documentation version there is a complete function my readers 
can copy/paste in their own files to use as a starting point. 
(the "// exclude from docs" comment is actually recognized by my 
doc generator to skip that line, see: 
http://dpldocs.info/experimental-docs/adrdox.syntax.html#documented-unittests which lets me tweak the user-visible output while still keeping it machine-checked internally too.)

Now, why is it in a unittest block? Because then the samples are 
easier for me to compile and run as a batch to help me make sure 
they still work. (This isn't exactly perfect - since they are 
still defined in the module, they can see private members and 
imports, so it is still possible it will work for me but not for 
my readers. But it makes it less likely to it that problem.)

Lastly, why is it in a demos block? That's just because normal 
unit tests are not supposed to be interactive and these are 
(interactive things make better doc samples). So the version 
block makes sure they are not run accidentally like by a user 
doing `dub test` at an outer layer. You have to opt into running 
these blocks by adding the version flag to the build too.

You can really see this in simpledisplay.d's docs that have full 
sample games you can copy/paste and tweak!

http://dpldocs.info/experimental-docs/arsd.simpledisplay.html#pong
source:
http://dpldocs.info/experimental-docs/source/arsd.simpledisplay.d.html#L492

except yikes I forgot to put the version thing there! so someone 
dub testing simpledisplay will have to play through a round of 
pong and minesweeper to pass the tests :P lol


More information about the Digitalmars-d-learn mailing list