cdece0 | 2001-01-29 | Per Hedbor | | int current_test, tests_failed;
|
2556b9 | 2001-01-31 | Per Hedbor | | int verbose;
|
fba4b1 | 2001-02-01 | Per Hedbor | | void create( int vb ) { verbose = vb; }
|
2556b9 | 2001-01-31 | Per Hedbor | |
|
cdece0 | 2001-01-29 | Per Hedbor | |
string describe_arglist( array args )
{
array res = ({});
foreach( args, mixed arg )
if( mappingp(arg) || arrayp(arg) )
res+=({sprintf("%t<%d>",arg,sizeof(arg))});
|
6a6a8c | 2001-01-30 | Per Hedbor | | else if( objectp( arg ) )
if( arg->is_module )
res += ({ sprintf("%s",arg->my_configuration()->otomod[arg])});
else if( arg->is_configuration )
|
30e314 | 2001-01-31 | Per Hedbor | | res += ({ sprintf("%s", arg->name ) });
|
6a6a8c | 2001-01-30 | Per Hedbor | | else
res += ({ sprintf("%O", arg ) });
|
cdece0 | 2001-01-29 | Per Hedbor | | else
res+=({sprintf("%O",arg)});
|
49374b | 2001-02-05 | Per Hedbor | | return replace(res * ", ","%","%%");
|
cdece0 | 2001-01-29 | Per Hedbor | | }
|
18a68b | 2009-09-17 | Martin Stjernholm | | string describe_test (function|string cb, array args)
{
if (functionp (cb))
return sprintf ("%O(%s)", cb, describe_arglist (args));
if (sizeof (args))
catch {return sprintf (cb, @args);};
return cb;
}
string pad_to_column (string str, int col, string cont_prefix)
{
array(string) split = str / "\n";
if (sizeof (split) > 1 && split[-1] == "") split = split[..<1];
if (sizeof (split[-1]) > col) split += ({""});
split[-1] += " " * (col - sizeof (split[-1]));
return split * ("\n" + cont_prefix);
}
void report_1st(function|string|array cb, array args, function check )
|
cdece0 | 2001-01-29 | Per Hedbor | | {
|
2556b9 | 2001-01-31 | Per Hedbor | | if( !verbose )
return;
|
30e314 | 2001-01-31 | Per Hedbor | | int checkid = ' ';
if( check == check_error )
checkid = '#';
else if( check == check_false )
checkid = '!';
else if( check != check_is_configuration &&
check == check_is_module )
checkid = '~';
|
18a68b | 2009-09-17 | Martin Stjernholm | |
if (arrayp (cb))
report_error ("%3d %s:%d:\n"
" %c %s ",
current_test, cb[0], cb[1], checkid,
pad_to_column (describe_test (cb[2], args),
66, sprintf (" %c ", checkid)));
else
report_error("%3d%c %s ", current_test, checkid,
pad_to_column (describe_test (cb, args),
66, sprintf (" %c ", checkid)));
|
cdece0 | 2001-01-29 | Per Hedbor | | }
|
2556b9 | 2001-01-31 | Per Hedbor | | string indent( int l, string what )
{
array q = what/"\n";
string i = (" "*l+"| ");
|
18a68b | 2009-09-17 | Martin Stjernholm | | return i+q*("\n"+i);
|
2556b9 | 2001-01-31 | Per Hedbor | | }
|
cdece0 | 2001-01-29 | Per Hedbor | | string do_describe_error( mixed err )
{
|
18a68b | 2009-09-17 | Martin Stjernholm | | if (!stringp (err)) err = describe_backtrace (err);
if (has_suffix (err, "\n")) err = err[..<1];
return indent(2, err) + "\n";
|
cdece0 | 2001-01-29 | Per Hedbor | | }
|
18a68b | 2009-09-17 | Martin Stjernholm | | void report_test_failure( mixed err,
function|string|array cb, array args, int st )
|
cdece0 | 2001-01-29 | Per Hedbor | | {
|
2556b9 | 2001-01-31 | Per Hedbor | | if( verbose )
report_debug(" FAILED\n");
|
18a68b | 2009-09-17 | Martin Stjernholm | | else {
if (arrayp (cb)) {
report_debug (indent (2, sprintf ("%s:%d: FAILED",
cb[0], cb[1])) + "\n" +
do_describe_error(describe_test (cb[2], args)));
}
else
report_debug(do_describe_error(describe_test (cb, args) + " FAILED"));
}
|
2556b9 | 2001-01-31 | Per Hedbor | | if( err )
report_debug( do_describe_error( err ) );
|
18a68b | 2009-09-17 | Martin Stjernholm | | report_debug ("\n");
|
cdece0 | 2001-01-29 | Per Hedbor | | tests_failed++;
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | void report_test_ok( mixed err, function|string|array cb, array args, int st )
|
cdece0 | 2001-01-29 | Per Hedbor | | {
|
2556b9 | 2001-01-31 | Per Hedbor | | if( verbose )
{
int tt = (gethrtime()-st);
if( tt > 200000 )
report_debug(" %4dms\n", tt/1000);
else
report_debug( " PASS\n" );
}
|
cdece0 | 2001-01-29 | Per Hedbor | | }
|
18a68b | 2009-09-17 | Martin Stjernholm | | mixed test_generic( function check_return, function|array cb, mixed ... args )
|
cdece0 | 2001-01-29 | Per Hedbor | | {
current_test++;
mixed result;
|
30e314 | 2001-01-31 | Per Hedbor | | report_1st( cb, args, check_return );
|
cdece0 | 2001-01-29 | Per Hedbor | | int st = gethrtime();
|
18a68b | 2009-09-17 | Martin Stjernholm | | function test_fn = arrayp (cb) ? cb[2] : cb;
|
cdece0 | 2001-01-29 | Per Hedbor | | mixed err = catch {
|
18a68b | 2009-09-17 | Martin Stjernholm | | result = test_fn( @args );
|
cdece0 | 2001-01-29 | Per Hedbor | | };
if( check_return )
check_return( result, err, cb, args,st );
else if( err )
report_test_failure( err, cb, args,st );
else
report_test_ok( err, cb, args,st );
return result;
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | mixed test_really_generic( function check_return, function(void:mixed) test_fn,
string|array test_text, array test_text_args )
{
current_test++;
mixed result;
report_1st( test_text, test_text_args, check_return );
int st = gethrtime();
mixed err = catch {
result = test_fn();
};
if( check_return )
check_return( result, err, test_text, test_text_args,st );
else if( err )
report_test_failure( err, test_text, test_text_args,st );
else
report_test_ok( err, test_text, test_text_args,st );
return result;
}
|
cdece0 | 2001-01-29 | Per Hedbor | |
|
18a68b | 2009-09-17 | Martin Stjernholm | | void check_error( mixed res, mixed err,
function|string|array cb, array args, int st )
|
cdece0 | 2001-01-29 | Per Hedbor | | {
if( err )
report_test_ok( err, cb, args, st );
else
report_test_failure( "Expected error", cb, args, st );
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | void check_is_module( mixed res, mixed err,
function|string|array cb, array args, int st )
|
cdece0 | 2001-01-29 | Per Hedbor | | {
if( err )
report_test_failure( err, cb, args, st );
else
if( !objectp(res) || !res->is_module || !res->my_configuration() )
report_test_failure( sprintf("Got %O, expected module", res),cb,args,
st);
else
report_test_ok( err, cb, args, st );
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | void check_is_configuration( mixed res, mixed err,
function|string|array cb, array args, int st)
|
cdece0 | 2001-01-29 | Per Hedbor | | {
if( err )
report_test_failure( err, cb, args, st );
else
if( !objectp(res) || !res->is_configuration )
report_test_failure( sprintf("Got %O, expected configuration", res),cb,args, st);
else
report_test_ok( err, cb, args, st );
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | void check_true( mixed res, mixed err,
function|string|array cb, array args, int st )
|
cdece0 | 2001-01-29 | Per Hedbor | | {
if( err )
report_test_failure( err, cb, args, st );
else
if( !res )
|
18a68b | 2009-09-17 | Martin Stjernholm | | report_test_failure( sprintf ("expected non-zero, got %O", res),
cb, args, st);
|
cdece0 | 2001-01-29 | Per Hedbor | | else
report_test_ok( err, cb, args, st );
}
|
6a6a8c | 2001-01-30 | Per Hedbor | |
|
18a68b | 2009-09-17 | Martin Stjernholm | | void check_false( mixed res, mixed err,
function|string|array cb, array args, int st )
|
30e314 | 2001-01-31 | Per Hedbor | | {
if( err )
report_test_failure( err, cb, args, st );
else
if( res )
report_test_failure( sprintf("expected zero, got %O",res), cb, args, st);
else
report_test_ok( err, cb, args, st );
}
|
6a6a8c | 2001-01-30 | Per Hedbor | |
function check_is( mixed m )
{
return
|
18a68b | 2009-09-17 | Martin Stjernholm | | lambda( mixed res, mixed err, function|string|array cb, array args, int st )
|
6a6a8c | 2001-01-30 | Per Hedbor | | {
if( err )
report_test_failure( err, cb, args, st );
else
if( res != m )
report_test_failure(sprintf("Got %O, expected %O", res,m),
cb,args,st);
else
report_test_ok( err, cb, args, st );
};
}
|
30e314 | 2001-01-31 | Per Hedbor | | mixed pass( mixed arg )
{
return arg;
}
|
6a6a8c | 2001-01-30 | Per Hedbor | | function check_equal( mixed m )
{
return
|
18a68b | 2009-09-17 | Martin Stjernholm | | lambda( mixed res, mixed err, function|string|array cb, array args, int st )
|
6a6a8c | 2001-01-30 | Per Hedbor | | {
if( err )
report_test_failure( err, cb, args, st );
else
if( !equal( res, m ))
report_test_failure(sprintf("Got %O, expected %O", res,m),
cb,args,st);
else
report_test_ok( err, cb, args, st );
};
}
function check_not_equal( mixed m )
{
return
|
18a68b | 2009-09-17 | Martin Stjernholm | | lambda( mixed res, mixed err, function|string|array cb, array args, int st )
|
6a6a8c | 2001-01-30 | Per Hedbor | | {
if( err )
report_test_failure( err, cb, args, st );
else
if( equal( res, m ))
|
18a68b | 2009-09-17 | Martin Stjernholm | | report_test_failure(sprintf("Got %O, expected different value", res),
|
6a6a8c | 2001-01-30 | Per Hedbor | | cb,args,st);
else
report_test_ok( err, cb, args, st );
};
}
|
2556b9 | 2001-01-31 | Per Hedbor | |
|
18a68b | 2009-09-17 | Martin Stjernholm | | mixed cpp_test_true (string file, int line, function(void:mixed) test_fn,
string test_text, array test_text_args)
{
return test_really_generic (check_true, test_fn,
({file, line, test_text}), test_text_args);
}
mixed test( function|array f, mixed ... args )
|
2556b9 | 2001-01-31 | Per Hedbor | | {
return test_generic( 0, f, @args );
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | mixed test_true( function|array f, mixed ... args )
|
2556b9 | 2001-01-31 | Per Hedbor | | {
return test_generic( check_true, f, @args );
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | mixed test_false( function|array f, mixed ... args )
|
2556b9 | 2001-01-31 | Per Hedbor | | {
return test_generic( check_false, f, @args );
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | mixed test_error( function|array f, mixed ... args )
|
2556b9 | 2001-01-31 | Per Hedbor | | {
return test_generic( check_error, f, @args );
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | mixed test_equal( mixed what, function|array f, mixed ... args )
|
2556b9 | 2001-01-31 | Per Hedbor | | {
return test_generic( check_equal( what ), f, @args );
}
|
18a68b | 2009-09-17 | Martin Stjernholm | | mixed test_not_equal( mixed what, function|array f, mixed ... args )
|
2556b9 | 2001-01-31 | Per Hedbor | | {
return test_generic( check_not_equal( what ), f, @args );
}
|
7cad99 | 2001-02-01 | Per Hedbor | |
void run_tests( Configuration c );
void low_run_tests( Configuration c,
function go_on )
{
|
49374b | 2001-02-05 | Per Hedbor | | mixed err = catch {
run_tests( c );
};
|
b18331 | 2009-09-14 | Martin Stjernholm | | if( err ) {
|
49374b | 2001-02-05 | Per Hedbor | | write( describe_backtrace( err ) );
|
b18331 | 2009-09-14 | Martin Stjernholm | | go_on (++current_test, ++tests_failed);
}
else
go_on (++current_test, tests_failed);
|
7cad99 | 2001-02-01 | Per Hedbor | | }
|