[phobos] Silent failure of std.container unittests

Leandro Lucarella luca at llucax.com.ar
Fri Jul 16 22:20:34 PDT 2010



Michel Fortin, el 16 de julio a las 22:37 me escribiste:
> 
> Le 2010-07-16 à 21:03, Jonathan M Davis a écrit :
> 
> >> My thought is, that we might be running into issues because the idea of
> >> a unit test is being conflated with a function. What we really want is a
> >> collection of blocks of code that that are all run and if any of them
> >> trigger an assert, that block stops and the tests as a whole are
> >> considered to have failed.
> >> 
> >> As a point to start from, how about allow unittest as a statement type?
> >> 
> >> foreach(item; collection) unittest { asssert(foobar(item)); }
> > 
> > Regardless of whether you consider the assertion or the unittest block as the 
> > unit test (and I would certainly choose the latter), I don't see any point to 
> > this.
> 
> Walter had a point... what if you want to test multiple outputs of the same function on one go? Here's an example:
> 
> 	unittest {
> 		int[3] result = makeSomeTable(399, 383, 927);
> 		assert(result[0] == 281);
> 		assert(result[1] == 281);
> 		assert(result[2] == 281);
> 	}
> 
> Here, each assert is independent one from another and knowing that the first and the third fails but not the second might help diagnose the problem.
> 
> I think Jonathan's idea is quite on the spot. It'd allow you to write this:
> 
> 	unittest {
> 		int[3] result = makeSomeTable(399, 383, 927);
> 		unittest { assert(result[0] == 281); }
> 		unittest { assert(result[1] == 281); }
> 		unittest { assert(result[2] == 281); }
> 	}
> 
> and each "unittest assert" becomes an independent test that may fail but without interrupting the flow of the outside scope. The compiler could expand it like this:
> 
> 	unittest {
> 		int[3] result = makeSomeTable(399, 383, 927);
> 
> 		try
> 			assert(result[0] == 281);
> 		catch (AssertionError e)
> 			__unitTestLogFailure(e);
> 
> 		try
> 			assert(result[1] == 281);
> 		catch (AssertionError e)
> 			__unitTestLogFailure(e);
> 
> 		try
> 			assert(result[2] == 281);
> 		catch (AssertionError e)
> 			__unitTestLogFailure(e);
> 	}
> 
> And now you have a log entry for each failed assertion, because they're independent unit tests.
> 
> It'd also help reduce verbosity to not require the braces for one-statement unit tests.

Use a library function!

	unittest {
		int[3] result = makeSomeTable(399, 383, 927);
		check(result[0] == 281);
		check(result[1] == 281);
		check(result[2] == 281);
	}

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
<Damian_Des> Me anDa MaL eL CaPSLoCK



More information about the phobos mailing list