digitalmars.D - [unittest] constness
- Luc Bourhis (4/4) Jan 16 2015 Testing constness implementation is easy:
- aldanor (3/7) Jan 16 2015 Something like this?
- ketmar via Digitalmars-d (10/16) Jan 16 2015 i don't think that you can do it with one command, but at least you can
- Jonathan M Davis via Digitalmars-d (18/22) Jan 17 2015 std.datetime has tests like this for that:
- Luc Bourhis (1/1) Jan 19 2015 Thanks everybody for your help!
- Luc Bourhis (8/9) Jan 20 2015 Just one point I forgot to mention: the compiler chokes on
Testing constness implementation is easy: const Foo a; a.non_const_method(); // <<< compilation fails but how would I catch that in a unittest?
Jan 16 2015
On Saturday, 17 January 2015 at 00:38:09 UTC, Luc Bourhis wrote:Testing constness implementation is easy: const Foo a; a.non_const_method(); // <<< compilation fails but how would I catch that in a unittest?Something like this? static assert(!__traits(compiles, a.non_const_method()))
Jan 16 2015
On Sat, 17 Jan 2015 00:38:08 +0000 Luc Bourhis via Digitalmars-d <digitalmars-d puremagic.com> wrote:Testing constness implementation is easy: =20 const Foo a; a.non_const_method(); // <<< compilation fails =20 but how would I catch that in a unittest?i don't think that you can do it with one command, but at least you can use `__trait(compiles, ...)` to check if it compiles. and if it's not, you can either assume that the test is passed, or investigate the thing further, examining the type of `a` (using `isMutable!`, for example), or creating `Unqual!(typeof(a)) aa` and try "compiles" with it. it can be done with cool and hard to decipher template to ease your life. just don't forget to document what that template intended to so, so you will not force to reverse-engineer it later. ;-)
Jan 16 2015
On Saturday, January 17, 2015 00:38:08 Luc Bourhis via Digitalmars-d wrote:Testing constness implementation is easy: const Foo a; a.non_const_method(); // <<< compilation fails but how would I catch that in a unittest?std.datetime has tests like this for that: const cdate = Date(1999, 7, 6); immutable idate = Date(1999, 7, 6); static assert(!__traits(compiles, cdate.year = 1999)); static assert(!__traits(compiles, idate.year = 1999)); And you can make them one-liners by doing something like static assert(!__traits(compiles, {const date = Date(1999, 7, 6); date.year = 1999; })); though I think that it's probably better to not make them one-liners so that the part that you want to test is isolated and doesn't test other stuff - e.g. if the constructor call suddenly stopped compiling in that example for some reason, the test would still pass, but it wouldn't be because of what you were trying to test for. Other tests would probably catch it in the case of the constructor, but still, minimizing what ends up in assertion reduces the risk of accidentally have the test pass for the wrong reasons - especially when the assertion is for something _not_ compiling. - Jonathan M Davis
Jan 17 2015
On Monday, 19 January 2015 at 16:12:59 UTC, Luc Bourhis wrote:Thanks everybody for your help!Just one point I forgot to mention: the compiler chokes on static assert(!__traits(compiles, xc[0] = 1.0)); with: found '=' when expecting ')' following template argument list But static assert(!__traits(compiles, (xc[0] = 1.0))); works.
Jan 20 2015