File Annotation
Not logged in
8de5b49cdf 2010-11-09        kinaba: /**
8de5b49cdf 2010-11-09        kinaba:  * Authors: k.inaba
8de5b49cdf 2010-11-09        kinaba:  * License: NYSL 0.9982 http://www.kmonos.net/nysl/
8de5b49cdf 2010-11-09        kinaba:  *
8de5b49cdf 2010-11-09        kinaba:  * Unittest helpers.
6f0ec5b7c9 2010-11-11        kinaba:  * TODO: use stderr instead of stdout. the problem is that std.cstream is xxxx....
6f0ec5b7c9 2010-11-11        kinaba:  * TODO: is there any way to clearnly implement "assert_compiles" and "assert_not_compile"?
8de5b49cdf 2010-11-09        kinaba:  */
8de5b49cdf 2010-11-09        kinaba: module tricks.test;
8de5b49cdf 2010-11-09        kinaba: import std.conv : to;
8de5b49cdf 2010-11-09        kinaba: import core.exception;
8de5b49cdf 2010-11-09        kinaba: 
6f0ec5b7c9 2010-11-11        kinaba: version(unittest)
6f0ec5b7c9 2010-11-11        kinaba: {
6f0ec5b7c9 2010-11-11        kinaba: 	import std.stdio;
6f0ec5b7c9 2010-11-11        kinaba: 	import core.runtime;
6f0ec5b7c9 2010-11-11        kinaba: 
6f0ec5b7c9 2010-11-11        kinaba: 	// Install custom test runner
6f0ec5b7c9 2010-11-11        kinaba: 	static this()
6f0ec5b7c9 2010-11-11        kinaba: 	{
6f0ec5b7c9 2010-11-11        kinaba: 		Runtime.moduleUnitTester = function()
6f0ec5b7c9 2010-11-11        kinaba: 		{
6f0ec5b7c9 2010-11-11        kinaba: 			Throwable ee = null;
6f0ec5b7c9 2010-11-11        kinaba: 			size_t failed=0;
6f0ec5b7c9 2010-11-11        kinaba: 			foreach(m; ModuleInfo) if(m) if(auto fp=m.unitTest)
6f0ec5b7c9 2010-11-11        kinaba: 			{
6f0ec5b7c9 2010-11-11        kinaba: 				writeln("[TEST] ", m.name);
6f0ec5b7c9 2010-11-11        kinaba: 				try {
6f0ec5b7c9 2010-11-11        kinaba: 					fp();
6f0ec5b7c9 2010-11-11        kinaba: 				} catch( Throwable e ) {
6f0ec5b7c9 2010-11-11        kinaba: 					if(ee is null)
6f0ec5b7c9 2010-11-11        kinaba: 						ee = e;
6f0ec5b7c9 2010-11-11        kinaba: 					failed++;
6f0ec5b7c9 2010-11-11        kinaba: 					writeln("  !! ",e.file,"(",e.line,"): ",e.msg);
6f0ec5b7c9 2010-11-11        kinaba: 				}
6f0ec5b7c9 2010-11-11        kinaba: 			}
6f0ec5b7c9 2010-11-11        kinaba: 			if(ee !is null)
6f0ec5b7c9 2010-11-11        kinaba: 			{
6f0ec5b7c9 2010-11-11        kinaba: 				writeln("[TEST] ",failed," modules failed. The first error was:");
6f0ec5b7c9 2010-11-11        kinaba: 				writeln(ee);
6f0ec5b7c9 2010-11-11        kinaba: 				write("[TEST] press enter to exit.");
6f0ec5b7c9 2010-11-11        kinaba: 				readln();
6f0ec5b7c9 2010-11-11        kinaba: 				return false;
6f0ec5b7c9 2010-11-11        kinaba: 			}
6f0ec5b7c9 2010-11-11        kinaba: 			return true;
6f0ec5b7c9 2010-11-11        kinaba: 		};
6f0ec5b7c9 2010-11-11        kinaba: 	}
6f0ec5b7c9 2010-11-11        kinaba: }
6f0ec5b7c9 2010-11-11        kinaba: 
8de5b49cdf 2010-11-09        kinaba: /// Unittest helper that asserts an expression must throw something
8de5b49cdf 2010-11-09        kinaba: 
6f0ec5b7c9 2010-11-11        kinaba: void assert_throw(ExcT=Throwable, T, string fn=__FILE__, size_t ln=__LINE__)(lazy T t, string msg="")
8de5b49cdf 2010-11-09        kinaba: {
6f0ec5b7c9 2010-11-11        kinaba: 	static if( is(ExcT == Throwable) )
7465fcdd7f 2010-11-09        kinaba: 		try
7465fcdd7f 2010-11-09        kinaba: 			{ t(); }
6f0ec5b7c9 2010-11-11        kinaba: 		catch(ExcT)
7465fcdd7f 2010-11-09        kinaba: 			{ return; }
7465fcdd7f 2010-11-09        kinaba: 	else
7465fcdd7f 2010-11-09        kinaba: 		try
7465fcdd7f 2010-11-09        kinaba: 			{ t(); }
6f0ec5b7c9 2010-11-11        kinaba: 		catch(ExcT)
7465fcdd7f 2010-11-09        kinaba: 			{ return; }
7465fcdd7f 2010-11-09        kinaba: 		catch(Throwable e)
6f0ec5b7c9 2010-11-11        kinaba: 			{ onAssertErrorMsg(fn, ln, msg.length ? msg : "bad exception \n  >> "~e.toString()); }
2459e9a821 2010-11-09        kinaba: 	onAssertErrorMsg(fn, ln, msg.length ? msg : "not thrown");
8de5b49cdf 2010-11-09        kinaba: }
8de5b49cdf 2010-11-09        kinaba: 
8de5b49cdf 2010-11-09        kinaba: /// Unittest helper that asserts an expression must not throw anything
8de5b49cdf 2010-11-09        kinaba: 
38fcc662be 2010-11-10        kinaba: T assert_nothrow(T, string fn=__FILE__, size_t ln=__LINE__)(lazy T t, string msg="")
8de5b49cdf 2010-11-09        kinaba: {
8de5b49cdf 2010-11-09        kinaba: 	try
8de5b49cdf 2010-11-09        kinaba: 		{ return t(); }
8de5b49cdf 2010-11-09        kinaba: 	catch(Throwable e)
6f0ec5b7c9 2010-11-11        kinaba: 		{ onAssertErrorMsg(fn, ln, msg.length ? msg : "bad exception \n  >> "~e.toString()); }
8de5b49cdf 2010-11-09        kinaba: 	assert(false);
8de5b49cdf 2010-11-09        kinaba: }
8de5b49cdf 2010-11-09        kinaba: 
8de5b49cdf 2010-11-09        kinaba: unittest
8de5b49cdf 2010-11-09        kinaba: {
8de5b49cdf 2010-11-09        kinaba: 	auto error = {throw new Error("hello");};
8de5b49cdf 2010-11-09        kinaba: 	auto nothing = (){};
8de5b49cdf 2010-11-09        kinaba: 	auto assertError = {assert(0);};
8de5b49cdf 2010-11-09        kinaba: 
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow          ( assert_nothrow(nothing()) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_nothrow(error()) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_nothrow(assertError()) );
8de5b49cdf 2010-11-09        kinaba: 
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow          ( assert_throw!Error(error()) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_throw!Error(nothing()) );
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow          ( assert_throw!Error(assertError()) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_throw!AssertError(error()) );
8de5b49cdf 2010-11-09        kinaba: }
8de5b49cdf 2010-11-09        kinaba: 
8de5b49cdf 2010-11-09        kinaba: template assertOp(string op)
8de5b49cdf 2010-11-09        kinaba: {
d78d700f7a 2010-11-09        kinaba: 	void assertOp(A, B, string fn=__FILE__, size_t ln=__LINE__)(A a, B b, string msg="")
8de5b49cdf 2010-11-09        kinaba: 	{
8de5b49cdf 2010-11-09        kinaba: 		try
8de5b49cdf 2010-11-09        kinaba: 			{ if( mixin("a"~op~"b") ) return; }
8de5b49cdf 2010-11-09        kinaba: 		catch(Throwable e)
6f0ec5b7c9 2010-11-11        kinaba: 			{ onAssertErrorMsg(fn, ln, msg.length ? msg : "bad exception \n  >> "~e.toString()); }
0f02103885 2010-11-09        kinaba: 		onAssertErrorMsg(fn, ln, msg.length ? msg : to!string(a)~" !"~op~" "~to!string(b));
8de5b49cdf 2010-11-09        kinaba: 	}
8de5b49cdf 2010-11-09        kinaba: }
8de5b49cdf 2010-11-09        kinaba: 
38fcc662be 2010-11-10        kinaba: alias assertOp!(`==`) assert_eq; /// asserts two operands are ==
38fcc662be 2010-11-10        kinaba: alias assertOp!(`!=`) assert_ne; /// asserts two operands are !=
38fcc662be 2010-11-10        kinaba: alias assertOp!(`<`)  assert_lt; /// asserts two operands are <
38fcc662be 2010-11-10        kinaba: alias assertOp!(`<=`) assert_le; /// asserts two operands are <=
38fcc662be 2010-11-10        kinaba: alias assertOp!(`>`)  assert_gt; /// asserts two operands are >
38fcc662be 2010-11-10        kinaba: alias assertOp!(`>=`) assert_ge; /// asserts two operands are >=
8de5b49cdf 2010-11-09        kinaba: 
8de5b49cdf 2010-11-09        kinaba: unittest
8de5b49cdf 2010-11-09        kinaba: {
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow( assert_eq(1, 1) );
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow( assert_ne(1, 0) );
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow( assert_lt(0, 1) );
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow( assert_le(0, 1) );
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow( assert_le(0, 0) );
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow( assert_gt(1, 0) );
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow( assert_ge(1, 0) );
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow( assert_ge(0, 0) );
8de5b49cdf 2010-11-09        kinaba: 
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_eq(1, 0) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_ne(1, 1) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_lt(1, 1) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_lt(1, 0) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_le(1, 0) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_gt(0, 0) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_gt(0, 1) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_ge(0, 1) );
8de5b49cdf 2010-11-09        kinaba: 
8de5b49cdf 2010-11-09        kinaba: 	class Temp { bool opEquals(int x){return x/x==x;} }
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_eq(new Temp, 0) );
8de5b49cdf 2010-11-09        kinaba: 	assert_nothrow          ( assert_eq(new Temp, 1) );
8de5b49cdf 2010-11-09        kinaba: 	assert_throw!AssertError( assert_eq(new Temp, 2) );
8de5b49cdf 2010-11-09        kinaba: }