Thread overview
[unittest] constness
Jan 17, 2015
Luc Bourhis
Jan 17, 2015
aldanor
Jan 17, 2015
ketmar
Jan 18, 2015
Jonathan M Davis
Jan 19, 2015
Luc Bourhis
Jan 20, 2015
Luc Bourhis
January 17, 2015
Testing constness implementation is easy:

const Foo a;
a.non_const_method(); // <<< compilation fails

but how would I catch that in a unittest?
January 17, 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()))
January 17, 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:
> 
> const Foo a;
> a.non_const_method(); // <<< compilation fails
> 
> 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. ;-)


January 18, 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

January 19, 2015
Thanks everybody for your help!
January 20, 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.