www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Just-run-the-unittests

reply "Sergei Nosov" <sergei.nosov gmail.com> writes:
Hi!

Suppose I have a small .d script that has a main. Is there any 
simple way to just run the unit tests without running main at all?

I thought -main switch was intended for this, but apparently it 
works only if there's no main defined at all, otherwise, it 
issues a double main definition error.

I could place main into a separate module but its really awkward 
to create 2 files for a small script.
Mar 16 2014
parent reply "safety0ff" <safety0ff.dev gmail.com> writes:
On Sunday, 16 March 2014 at 07:59:33 UTC, Sergei Nosov wrote:
 Hi!

 Suppose I have a small .d script that has a main. Is there any 
 simple way to just run the unit tests without running main at 
 all?
Here's the first thing that came to mind: If you never want to both unit tests and regular main: ---- code begins ---- import std.stdio; version(unittest) void main(){} else void main() { writeln("Hello world!"); } unittest { writeln("Hello unit testing world!"); } ---- code ends ---- If you sometimes want to have your normal main with unit testing you can replace "version(unittest)" with "version(nopmain)" or some other custom version identifier and compile with -version=nopmain when you want the dummy main.
Mar 16 2014
parent reply "Sergei Nosov" <sergei.nosov gmail.com> writes:
On Sunday, 16 March 2014 at 08:22:04 UTC, safety0ff wrote:
 On Sunday, 16 March 2014 at 07:59:33 UTC, Sergei Nosov wrote:
 Hi!

 Suppose I have a small .d script that has a main. Is there any 
 simple way to just run the unit tests without running main at 
 all?
Here's the first thing that came to mind: If you never want to both unit tests and regular main: ---- code begins ---- import std.stdio; version(unittest) void main(){} else void main() { writeln("Hello world!"); } unittest { writeln("Hello unit testing world!"); } ---- code ends ---- If you sometimes want to have your normal main with unit testing you can replace "version(unittest)" with "version(nopmain)" or some other custom version identifier and compile with -version=nopmain when you want the dummy main.
Thx! That's better, but I think -main switch could be made to work like 'add or replace main by stub' instead of just 'add'. I don't think it'll hurt anybody, what do you think?
Mar 16 2014
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/16/14, Sergei Nosov <sergei.nosov gmail.com> wrote:
 Thx! That's better, but I think -main switch could be made to
 work like 'add or replace main by stub' instead of just 'add'. I
 don't think it'll hurt anybody, what do you think?
It can't work, because main could be stored in a pre-built object or static library that's passed to DMD.
Mar 16 2014
parent reply "Rikki Cattermole" <alphaglosined gmail.com> writes:
On Sunday, 16 March 2014 at 10:15:00 UTC, Andrej Mitrovic wrote:
 It can't work, because main could be stored in a pre-built 
 object or
 static library that's passed to DMD.
Hmm I really should consider making a DIP for a deferred CTFE block. Could really come in handy for a situation like this. In the format of if -main specified use e.g. -version=D_Main to remove the call to _Dmain symbol. Removes the whole linker issue altogether.
Mar 16 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Sunday, 16 March 2014 at 12:57:04 UTC, Rikki Cattermole wrote:
 On Sunday, 16 March 2014 at 10:15:00 UTC, Andrej Mitrovic wrote:
 It can't work, because main could be stored in a pre-built 
 object or
 static library that's passed to DMD.
Hmm I really should consider making a DIP for a deferred CTFE block. Could really come in handy for a situation like this. In the format of if -main specified use e.g. -version=D_Main to remove the call to _Dmain symbol. Removes the whole linker issue altogether.
Linker will still complain about duplicate symbols. This issue can't be solved within existing C-compatible object file toolchain.
Mar 16 2014
parent "Rikki Cattermole" <alphaglosined gmail.com> writes:
On Sunday, 16 March 2014 at 20:22:15 UTC, Dicebot wrote:
 On Sunday, 16 March 2014 at 12:57:04 UTC, Rikki Cattermole 
 wrote:
 On Sunday, 16 March 2014 at 10:15:00 UTC, Andrej Mitrovic 
 wrote:
 It can't work, because main could be stored in a pre-built 
 object or
 static library that's passed to DMD.
Hmm I really should consider making a DIP for a deferred CTFE block. Could really come in handy for a situation like this. In the format of if -main specified use e.g. -version=D_Main to remove the call to _Dmain symbol. Removes the whole linker issue altogether.
Linker will still complain about duplicate symbols. This issue can't be solved within existing C-compatible object file toolchain.
I was inferring that no stub _Dmain would be added. Since it would no longer be called.
Mar 16 2014