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 | | }
|
30e314 | 2001-01-31 | Per Hedbor | | void report_1st(function 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 = '~';
|
752014 | 2001-01-31 | Per Hedbor | | report_error("%3d %c%-66s ", current_test,
checkid,sprintf("%O("+describe_arglist( args )+")",cb)[..65]
|
30e314 | 2001-01-31 | Per Hedbor | | );
|
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+"| ");
return i+q*("\n"+i)+"\n";
}
|
cdece0 | 2001-01-29 | Per Hedbor | | string do_describe_error( mixed err )
{
if( stringp( err ) )
|
2556b9 | 2001-01-31 | Per Hedbor | | return indent(2,err + (strlen(err)?(err[-1] == '\n' ? "": "\n" ):""));
|
cdece0 | 2001-01-29 | Per Hedbor | | err = (array)err;
err[1] = err[1][sizeof(err[1])-3..];
|
2556b9 | 2001-01-31 | Per Hedbor | | return indent(2, describe_backtrace( err ) );
|
cdece0 | 2001-01-29 | Per Hedbor | | }
void report_test_failure( mixed err, function cb, array args, int st )
{
|
2556b9 | 2001-01-31 | Per Hedbor | | if( verbose )
report_debug(" FAILED\n");
report_debug(do_describe_error(sprintf( "%O( %s ) FAILED\n",
cb, describe_arglist( args ))));
if( err )
report_debug( do_describe_error( err ) );
|
cdece0 | 2001-01-29 | Per Hedbor | | tests_failed++;
}
void report_test_ok( mixed err, function cb, array args, int st )
{
|
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 | | }
|
2556b9 | 2001-01-31 | Per Hedbor | | mixed test_generic( function check_return, function 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();
mixed err = catch {
result = cb( @args );
};
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;
}
void check_error( mixed res, mixed err, function cb, array args,int st )
{
if( err )
report_test_ok( err, cb, args, st );
else
report_test_failure( "Expected error", cb, args, st );
}
void check_is_module( mixed res, mixed err, function cb, array args, int st )
{
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 );
}
void check_is_configuration( mixed res, mixed err, function cb, array args,
int st)
{
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 );
}
|
30e314 | 2001-01-31 | Per Hedbor | | void check_true( mixed res, mixed err, function cb, array args, int st )
|
cdece0 | 2001-01-29 | Per Hedbor | | {
if( err )
report_test_failure( err, cb, args, st );
else
if( !res )
|
2556b9 | 2001-01-31 | Per Hedbor | | report_test_failure( "expected non-zero, got 0", cb, args, st);
|
cdece0 | 2001-01-29 | Per Hedbor | | else
report_test_ok( err, cb, args, st );
}
|
6a6a8c | 2001-01-30 | Per Hedbor | |
|
30e314 | 2001-01-31 | Per Hedbor | | void check_false( mixed res, mixed err, function cb, array args, int st )
{
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
lambda( mixed res, mixed err, function cb, array args, int st )
{
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
lambda( mixed res, mixed err, function cb, array args, int st )
{
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
lambda( mixed res, mixed err, function cb, array args, int st )
{
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 );
};
}
|
2556b9 | 2001-01-31 | Per Hedbor | |
mixed test( function f, mixed ... args )
{
return test_generic( 0, f, @args );
}
mixed test_true( function f, mixed ... args )
{
return test_generic( check_true, f, @args );
}
mixed test_false( function f, mixed ... args )
{
return test_generic( check_false, f, @args );
}
mixed test_error( function f, mixed ... args )
{
return test_generic( check_error, f, @args );
}
mixed test_equal( mixed what, function f, mixed ... args )
{
return test_generic( check_equal( what ), f, @args );
}
mixed test_not_equal( mixed what, function f, mixed ... args )
{
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 );
};
if( err )
write( describe_backtrace( err ) );
|
7cad99 | 2001-02-01 | Per Hedbor | | go_on( current_test, tests_failed );
}
|