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.
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: 
8de5b49cdf 2010-11-09        kinaba: /// Unittest helper that asserts an expression must throw something
8de5b49cdf 2010-11-09        kinaba: 
7465fcdd7f 2010-11-09        kinaba: void assert_throw(ExceptionType=Throwable,
7465fcdd7f 2010-11-09        kinaba: 	T, string fn=__FILE__, size_t ln=__LINE__)(lazy T t, string msg="")
8de5b49cdf 2010-11-09        kinaba: {
7465fcdd7f 2010-11-09        kinaba: 	static if( is(ExceptionType == Throwable) )
7465fcdd7f 2010-11-09        kinaba: 		try
7465fcdd7f 2010-11-09        kinaba: 			{ t(); }
7465fcdd7f 2010-11-09        kinaba: 		catch(ExceptionType)
7465fcdd7f 2010-11-09        kinaba: 			{ return; }
7465fcdd7f 2010-11-09        kinaba: 	else
7465fcdd7f 2010-11-09        kinaba: 		try
7465fcdd7f 2010-11-09        kinaba: 			{ t(); }
7465fcdd7f 2010-11-09        kinaba: 		catch(ExceptionType)
7465fcdd7f 2010-11-09        kinaba: 			{ return; }
7465fcdd7f 2010-11-09        kinaba: 		catch(Throwable e)
7465fcdd7f 2010-11-09        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)
2459e9a821 2010-11-09        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)
8de5b49cdf 2010-11-09        kinaba: 			{ onAssertErrorMsg(fn, ln, msg.length ? msg : "exception ["~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: }
8de5b49cdf 2010-11-09        kinaba: 
8de5b49cdf 2010-11-09        kinaba: /* [Todo] is there any way to clearnly implement "assert_compiles" and "assert_not_compile"? */