CTFE unit tests

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jul 15 15:36:33 PDT 2013


On Mon, Jul 15, 2013 at 02:20:06PM -0700, Timothee Cour wrote:
> On Mon, Jul 15, 2013 at 2:07 PM, Dicebot <public at dicebot.lv> wrote:
> 
> > On Monday, 15 July 2013 at 20:50:59 UTC, Timothee Cour wrote:
> >
> >> These named unittests would ideally be referable to inside DDOC (as
> >> opposed
> >> to current horrible practice of duplicating examples inside documentation,
> >> which are never in sync)
> >>
> >
> > http://dlang.org/changelog.**html#documentedunittest<http://dlang.org/changelog.html#documentedunittest>
> >
> 
> Thanks, forgot it appeared in 2.063. Now the feature just needs to be used
> ! (so many out of date examples embedded in documentation at the moment).

One problem with these ddoc-unittest, though, is that you still have
some issues when writing ddocs for templated structs/classes. For
example:

	struct MyStruct(T) {
		/// Docs for MyMethod
		void MyMethod(...) {
			...
		}

		/// ddoc-unittest
		unittest {
			MyStruct!int exampleStruct;
			...
		}
	}

The problem is that your unittest will be instantiated for every type T
that you instantiate MyStruct with, even though the resulting code is
identical (it's generally a good idea to explicitly write, e.g.,
MyStruct!int in ddoc unittests, since you want users to see a concrete
example, not something involving T implicitly or explicitly). This
wastes memory when compiling, and increases program startup time by
running redundant tests.

One workaround is to use static if to restrict the instantiation of the
unittest:

	struct MyStruct(T) {
		/// Docs for MyMethod
		void MyMethod(...) {
			...
		}

		static if (is(T == int)) {
			/// ddoc-unittest
			unittest {
				MyStruct!int exampleStruct;
				...
			}
		}
	}

	// Force instantiation of MyStruct!int so that unittest will
	// actually get instantiated once!
	//
	// Problem: if you forget to do this, your unittest may never
	// actually be compiled, so it doesn't actually run.
	version(unittest)
		MyStruct!int _dummy;

This isn't ideal, since there's possibility of bugs if you forget that
last part to instantiate MyStruct!int: your unittest will never actually
run. Plus, it's a hassle to keep typing out these static ifs.


T

-- 
In order to understand recursion you must first understand recursion.


More information about the Digitalmars-d mailing list