Thread overview
newbie baffled by unittest "need 'this'" error msg
Sep 01, 2004
Lynn A
Re: newbie baffled by unittest
Sep 01, 2004
Nick
Sep 02, 2004
Lynn Allan
Sep 02, 2004
J C Calvarese
September 01, 2004
<alert content="newbie">

I'm either unclear about using -unittest (likely), or the example in the
documentation is not only incomplete, but flawed (i.e. won't compile).

from: http://www.digitalmars.com/d/class.html#unittest

"For example, given a class Sum that is used to add two values: "

#class Sum
#{
#  int add(int x, int y) { return x + y; }
#
#  unittest
#  {
#    assert(add(3,4) == 7);
#    assert(add(-2,0) == -2);
#  }
#}

With the above code, "dmd Sum.d" compiles fine, but "dmd -unittest Sum.d"
results in error message:
need 'this' to access member add

Should the code snippet actually be something like the following:
#  unittest
#  {
#    writefln("Reached Sum.unittest");
#    Sum myUnitTester = new Sum;
#    assert(myUnitTester.add(3,4) == 7);
#    assert(myUnitTester.add(-2,0) == -2);
#  }
#}

With the above change, the following test.d that imports Sum will compile and work:

#import std.stdio;
#import Sum;
#
#void main (char[][] args)
#{
#  writefln("Reached Test.main");
#
#  Sum mySummer = new Sum;
#  int myResult = mySummer.add(1, 2);
#  writefln("myResult: ", myResult);
#}

Am I confused and leaving something out such that the original code snippet will compile and work?

Also, the documentation would benefit from specifically noting how to enable
unittest code. After some head-scratching, I looked in the archives and the
output from "dmd" with no commandline argurments. Currently the documentation
reads:
"A compiler or linker switch will remove the test code from the final build."

Perhaps this would be more helpful to newbies like myself if it read:
"The -unittest dmd switch will include the test code in the build. The -release
switch will leave out the unittest code from the final build."

From The WB circa 2002-April:
http://www.digitalmars.com/d/archives/4607.html
"Unit testing [along with DBC] has in my mind risen to become a main feature of
D."

Lynn A.

</alert>


September 01, 2004
In article <ch4mk4$2a68$1@digitaldaemon.com>, Lynn A says...
>
>[...]
>Should the code snippet actually be something like the following:
>#  unittest
>#  {
>#    writefln("Reached Sum.unittest");
>#    Sum myUnitTester = new Sum;
>#    assert(myUnitTester.add(3,4) == 7);
>#    assert(myUnitTester.add(-2,0) == -2);
>#  }
>#}
>
>[...]
>
>Am I confused and leaving something out such that the original code snippet will compile and work?

No, I think you are entirely correct. The documentation example will only work if add() is made static.

>Perhaps this would be more helpful to newbies like myself if it read:
>"The -unittest dmd switch will include the test code in the build. The -release
>switch will leave out the unittest code from the final build."

I agree. But you should only mention the -unittest flag in this context. The -release option doesn't effect unittest code at all.

Nick


September 02, 2004
> No, I think you are entirely correct. The documentation example will only
> work
> if add() is made static.

Per your confirmation, I have submitted the documentation adjustment via
wiki4d to the author/maintainer of the "Classes#unittest" documentation (The
WB or jcc7?)
http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Class


September 02, 2004
Lynn Allan wrote:
>>No, I think you are entirely correct. The documentation example will only work
>>if add() is made static.
> 
> 
> Per your confirmation, I have submitted the documentation adjustment via wiki4d to the author/maintainer of the "Classes#unittest" documentation (The WB or jcc7?)
> http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Class

Walter is the maintainer of the official D specification at digitalmars.com.

Anyone can contribute to the Wiki4D pages. (I just happen to be the most active Wiki contributor at the moment.)

-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/