cdece02001-01-29Per Hedbor int current_test, tests_failed;
2556b92001-01-31Per Hedbor int verbose;
fba4b12001-02-01Per Hedbor void create( int vb ) { verbose = vb; }
2556b92001-01-31Per Hedbor 
cdece02001-01-29Per Hedbor  string describe_arglist( array args ) { array res = ({}); foreach( args, mixed arg ) if( mappingp(arg) || arrayp(arg) ) res+=({sprintf("%t<%d>",arg,sizeof(arg))});
6a6a8c2001-01-30Per Hedbor  else if( objectp( arg ) ) if( arg->is_module ) res += ({ sprintf("%s",arg->my_configuration()->otomod[arg])}); else if( arg->is_configuration )
30e3142001-01-31Per Hedbor  res += ({ sprintf("%s", arg->name ) });
6a6a8c2001-01-30Per Hedbor  else res += ({ sprintf("%O", arg ) });
cdece02001-01-29Per Hedbor  else res+=({sprintf("%O",arg)});
49374b2001-02-05Per Hedbor  return replace(res * ", ","%","%%");
cdece02001-01-29Per Hedbor }
18a68b2009-09-17Martin 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 )
cdece02001-01-29Per Hedbor {
2556b92001-01-31Per Hedbor  if( !verbose ) return;
30e3142001-01-31Per 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 = '~';
18a68b2009-09-17Martin Stjernholm  if (arrayp (cb)) // Got line number info. 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)));
cdece02001-01-29Per Hedbor }
2556b92001-01-31Per Hedbor string indent( int l, string what ) { array q = what/"\n"; // if( q[-1] == "" ) q = q[..sizeof(q)-2]; string i = (" "*l+"| ");
18a68b2009-09-17Martin Stjernholm  return i+q*("\n"+i);
2556b92001-01-31Per Hedbor }
cdece02001-01-29Per Hedbor string do_describe_error( mixed err ) {
18a68b2009-09-17Martin Stjernholm  if (!stringp (err)) err = describe_backtrace (err); if (has_suffix (err, "\n")) err = err[..<1]; return indent(2, err) + "\n";
cdece02001-01-29Per Hedbor }
18a68b2009-09-17Martin Stjernholm void report_test_failure( mixed err, function|string|array cb, array args, int st )
cdece02001-01-29Per Hedbor {
2556b92001-01-31Per Hedbor  if( verbose ) report_debug(" FAILED\n");
18a68b2009-09-17Martin Stjernholm  else { if (arrayp (cb)) { // Got line number info. 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")); }
2556b92001-01-31Per Hedbor  if( err ) report_debug( do_describe_error( err ) );
18a68b2009-09-17Martin Stjernholm  report_debug ("\n");
cdece02001-01-29Per Hedbor  tests_failed++; }
18a68b2009-09-17Martin Stjernholm void report_test_ok( mixed err, function|string|array cb, array args, int st )
cdece02001-01-29Per Hedbor {
2556b92001-01-31Per Hedbor  if( verbose ) { int tt = (gethrtime()-st); if( tt > 200000 ) report_debug(" %4dms\n", tt/1000); else report_debug( " PASS\n" ); // if( err ) report_error( do_describe_error( err ) ); }
cdece02001-01-29Per Hedbor }
18a68b2009-09-17Martin Stjernholm mixed test_generic( function check_return, function|array cb, mixed ... args )
cdece02001-01-29Per Hedbor { current_test++; mixed result;
30e3142001-01-31Per Hedbor  report_1st( cb, args, check_return );
cdece02001-01-29Per Hedbor  int st = gethrtime();
18a68b2009-09-17Martin Stjernholm  function test_fn = arrayp (cb) ? cb[2] : cb;
cdece02001-01-29Per Hedbor  mixed err = catch {
18a68b2009-09-17Martin Stjernholm  result = test_fn( @args );
cdece02001-01-29Per 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; }
18a68b2009-09-17Martin 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; }
cdece02001-01-29Per Hedbor 
18a68b2009-09-17Martin Stjernholm void check_error( mixed res, mixed err, function|string|array cb, array args, int st )
cdece02001-01-29Per Hedbor { if( err ) report_test_ok( err, cb, args, st ); else report_test_failure( "Expected error", cb, args, st ); }
18a68b2009-09-17Martin Stjernholm void check_is_module( mixed res, mixed err, function|string|array cb, array args, int st )
cdece02001-01-29Per 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 ); }
18a68b2009-09-17Martin Stjernholm void check_is_configuration( mixed res, mixed err, function|string|array cb, array args, int st)
cdece02001-01-29Per 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 ); }
18a68b2009-09-17Martin Stjernholm void check_true( mixed res, mixed err, function|string|array cb, array args, int st )
cdece02001-01-29Per Hedbor { if( err ) report_test_failure( err, cb, args, st ); else if( !res )
18a68b2009-09-17Martin Stjernholm  report_test_failure( sprintf ("expected non-zero, got %O", res), cb, args, st);
cdece02001-01-29Per Hedbor  else report_test_ok( err, cb, args, st ); }
6a6a8c2001-01-30Per Hedbor 
18a68b2009-09-17Martin Stjernholm void check_false( mixed res, mixed err, function|string|array cb, array args, int st )
30e3142001-01-31Per 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 ); }
6a6a8c2001-01-30Per Hedbor  function check_is( mixed m ) { return
18a68b2009-09-17Martin Stjernholm  lambda( mixed res, mixed err, function|string|array cb, array args, int st )
6a6a8c2001-01-30Per 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 ); }; }
30e3142001-01-31Per Hedbor mixed pass( mixed arg ) { return arg; }
6a6a8c2001-01-30Per Hedbor function check_equal( mixed m ) { return
18a68b2009-09-17Martin Stjernholm  lambda( mixed res, mixed err, function|string|array cb, array args, int st )
6a6a8c2001-01-30Per 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
18a68b2009-09-17Martin Stjernholm  lambda( mixed res, mixed err, function|string|array cb, array args, int st )
6a6a8c2001-01-30Per Hedbor  { if( err ) report_test_failure( err, cb, args, st ); else if( equal( res, m ))
18a68b2009-09-17Martin Stjernholm  report_test_failure(sprintf("Got %O, expected different value", res),
6a6a8c2001-01-30Per Hedbor  cb,args,st); else report_test_ok( err, cb, args, st ); }; }
2556b92001-01-31Per Hedbor 
18a68b2009-09-17Martin 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 )
2556b92001-01-31Per Hedbor { return test_generic( 0, f, @args ); }
18a68b2009-09-17Martin Stjernholm mixed test_true( function|array f, mixed ... args )
2556b92001-01-31Per Hedbor { return test_generic( check_true, f, @args ); }
18a68b2009-09-17Martin Stjernholm mixed test_false( function|array f, mixed ... args )
2556b92001-01-31Per Hedbor { return test_generic( check_false, f, @args ); }
18a68b2009-09-17Martin Stjernholm mixed test_error( function|array f, mixed ... args )
2556b92001-01-31Per Hedbor { return test_generic( check_error, f, @args ); }
18a68b2009-09-17Martin Stjernholm mixed test_equal( mixed what, function|array f, mixed ... args )
2556b92001-01-31Per Hedbor { return test_generic( check_equal( what ), f, @args ); }
18a68b2009-09-17Martin Stjernholm mixed test_not_equal( mixed what, function|array f, mixed ... args )
2556b92001-01-31Per Hedbor { return test_generic( check_not_equal( what ), f, @args ); }
7cad992001-02-01Per Hedbor  void run_tests( Configuration c ); void low_run_tests( Configuration c, function go_on ) {
49374b2001-02-05Per Hedbor  mixed err = catch { run_tests( c ); };
b183312009-09-14Martin Stjernholm  if( err ) {
49374b2001-02-05Per Hedbor  write( describe_backtrace( err ) );
b183312009-09-14Martin Stjernholm  go_on (++current_test, ++tests_failed); } else go_on (++current_test, tests_failed);
7cad992001-02-01Per Hedbor }