Thread overview
Re: unittests running before static ctors??
Jan 26, 2012
Jonathan M Davis
Jan 26, 2012
H. S. Teoh
Jan 26, 2012
Jonathan M Davis
January 26, 2012
On Thursday, January 26, 2012 10:08:24 H. S. Teoh wrote:
> I just noticed that unittests are running before static class ctors. Is this a bug??
> 
> I suppose the intention is for unittests to make sure the class implementation is OK before using it, but this makes it impossible to use unittests to make sure that static ctors are setting up the initial states correctly.

I would definitely think that that's a bug. If you're seeing that happen, please report it.

- Jonathan M Davis
January 26, 2012
On Thu, Jan 26, 2012 at 01:10:28PM -0500, Jonathan M Davis wrote:
> On Thursday, January 26, 2012 10:08:24 H. S. Teoh wrote:
> > I just noticed that unittests are running before static class ctors. Is this a bug??
[...]
> I would definitely think that that's a bug. If you're seeing that happen, please report it.
[...]

Hmm. It appears that I'm misunderstanding D syntax. What does a "static {...}" block in a class mean? I had this code:

	class A {
		static {
			this() { ... }
			...
		}
		unittest { ... }
	}

It appears that this() is being interpreted as a non-static ctor, which
is why it is never run before the unittest (it's never run at all). When
I change it to:

	class A {
		static this() { ... }
		static { ... }
		unittest { ... }
	}

Then it works as expected. So I guess I don't quite understand what a static {...} block means.


T

-- 
People tell me that I'm skeptical, but I don't believe it.
January 26, 2012
On Thursday, January 26, 2012 10:20:28 H. S. Teoh wrote:
> On Thu, Jan 26, 2012 at 01:10:28PM -0500, Jonathan M Davis wrote:
> > On Thursday, January 26, 2012 10:08:24 H. S. Teoh wrote:
> > > I just noticed that unittests are running before static class ctors.
> > > Is
> > > this a bug??
> 
> [...]
> 
> > I would definitely think that that's a bug. If you're seeing that
> > happen,
> > please report it.
> 
> [...]
> 
> Hmm. It appears that I'm misunderstanding D syntax. What does a "static {...}" block in a class mean? I had this code:
> 
> class A {
> static {
> this() { ... }
> ...
> }
> unittest { ... }
> }
> 
> It appears that this() is being interpreted as a non-static ctor, which
> is why it is never run before the unittest (it's never run at all). When
> I change it to:
> 
> class A {
> static this() { ... }
> static { ... }
> unittest { ... }
> }
> 
> Then it works as expected. So I guess I don't quite understand what a static {...} block means.

static {} should make all of the declarations within it static. If you did public {} it would make all of the declarations in it public, or if you did pure {} it would make all of the declarations in it pure. It looks like you ran into this bug:

http://d.puremagic.com/issues/show_bug.cgi?id=5868

- Jonathan M Davis