Roxen.git / server / etc / test / tests / pike_test_common.pike

version» Context lines:

Roxen.git/server/etc/test/tests/pike_test_common.pike:117:    {    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 ) );    }   }    -  - mixed test_generic( function check_return, function|array cb, mixed ... args ) + private mixed test_generic_low( function check_return, +  function|array cb, +  bool throw_error, +  mixed ... args )   {    current_test++;    mixed result;    report_1st( cb, args, check_return );    int st = gethrtime();    function test_fn = arrayp (cb) ? cb[2] : cb;    mixed err = catch {    result = test_fn( @args );    }; -  if( check_return ) +  if( err && throw_error ) { +  report_test_failure( err, cb, args, st ); +  throw( err ); +  } +  // Not all check-functions support the extra throw_error arg. +  if ( check_return && throw_error ) { +  check_return( result, err, cb, args,st, throw_error); +  } +  else 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;   }    -  + mixed test_generic( function check_return, function|array cb, mixed ... args ) + { +  return test_generic_low( check_return, cb, false, @args ); + } +  + mixed assert_generic( function check_return, function|array cb, mixed ... args ) + { +  return test_generic_low( check_return, cb, true, @args ); + } +    mixed test_really_generic( function check_return, function(void:mixed) test_fn, -  string|array test_text, array test_text_args ) +  string|array test_text, array test_text_args, +  bool|void throw_error )   {    current_test++;    mixed result;    int st = gethrtime();    mixed err = catch {    result = test_fn();    };       // Write out the test after running it, since the macros change    // test_text_args in the test.    report_1st( test_text, test_text_args, check_return );    -  if( check_return ) +  if( err && throw_error ) { +  report_test_failure( err, test_text, test_text_args, st ); +  throw( err ); +  } +  if( check_return && throw_error ) +  check_return( result, err, test_text, test_text_args, st, throw_error ); +  else 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;   }         void check_error( mixed res, mixed err, -  function|string|array cb, array args, int st ) +  function|string|array cb, array args, int st, +  bool|void throw_error )   { -  if( err ) +  if( err ) {    report_test_ok( err, cb, args, st ); -  else +  } +  else {    report_test_failure( "Expected error", cb, args, st ); -  +  if( throw_error ) { +  error( "Expected error but none was thrown.\n" );    } -  +  } + }    -  + private void assert_throws_error( mixed res, mixed err, +  function|string|array cb, array args, int st ) + { +  check_error( res, err, cb, args, st, true ); + } +    void check_is_module( mixed res, mixed err,    function|string|array 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
Roxen.git/server/etc/test/tests/pike_test_common.pike:196:    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 );   }      void silent_check_true( mixed res, mixed err, -  function|string|array cb, array args, int st ) +  function|string|array cb, array args, int st, +  bool|void throw_error )   { -  if (err || !res) +  if( err || !res ) {    report_test_failure( err, cb, args, st ); -  else +  if( throw_error ) { +  if( err ) { +  throw( err ); +  } else { +  error( "Condition failed.\n" ); +  } +  } +  } else {    report_test_ok( 0, cb, args, st );    } -  + }      void check_true( mixed res, mixed err, -  function|string|array cb, array args, int st ) +  function|string|array cb, array args, int st, +  bool|void throw_error )   { -  if( err ) +  if( err ) {    report_test_failure( err, cb, args, st ); -  else -  if( !res ) +  if( throw_error ) { +  throw( err ); +  } +  } +  else if( !res ) {    report_test_failure( sprintf ("expected non-zero, got %O", res),    cb, args, st); -  else +  if( throw_error ) { +  error( sprintf ("expected non-zero, got %O\n", res) ); +  } +  } else {    report_test_ok( err, cb, args, st );    } -  + }    - void check_false( mixed res, mixed err, + private void assert_result_true( mixed res, mixed err,    function|string|array cb, array args, int st )   { -  if( err ) +  check_true(res, err, cb, args, st, true); + } +  +  + void check_false( mixed res, mixed err, +  function|string|array cb, array args, int st, +  bool|void throw_error ) +  + { +  if( err ) {    report_test_failure( err, cb, args, st ); -  +  if( throw_error ) { +  throw( err ); +  } +  }    else -  if( res ) +  if( res ) {    report_test_failure( sprintf("expected zero, got %O",res), cb, args, st); -  +  if( throw_error ) { +  error( sprintf("expected zero, got %O\n", res) ); +  } +  }    else    report_test_ok( err, cb, args, st );   }    -  + private void assert_result_false( mixed res, mixed err, +  function|string|array cb, array args, int st ) + { +  check_false( res, err, cb, args, st, true ); + }      function check_is( mixed m )   {    return    lambda( mixed res, mixed err, function|string|array cb, array args, int st )    {    if( err )    report_test_failure( err, cb, args, st );    else    if( res != m )
Roxen.git/server/etc/test/tests/pike_test_common.pike:254:   }      mixed pass( mixed arg )   {    return arg;   }      function check_equal( mixed m )   {    return -  lambda( mixed res, mixed err, function|string|array cb, array args, int st ) +  lambda( mixed res, mixed err, function|string|array cb, array args, int st, +  bool|void throw_error )    { -  if( err ) +  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 +  if( throw_error ) { +  throw( err ); +  } +  } else if( !equal( res, m )) { +  string message = sprintf("Got %O, expected %O.\n", res,m); +  report_test_failure( message, cb, args, st ); +  if( throw_error ) { +  error( message ); +  } +  } else {    report_test_ok( err, cb, args, st ); -  +  }    };   }      function check_not_equal( mixed m )   {    return -  lambda( mixed res, mixed err, function|string|array cb, array args, int st ) +  lambda( mixed res, mixed err, function|string|array cb, array args, int st, +  bool|void throw_error )    { -  if( err ) +  if( err ) {    report_test_failure( err, cb, args, st ); -  else -  if( equal( res, m )) -  report_test_failure(sprintf("Got %O, expected different value", res), -  cb,args,st); -  else +  if( throw_error ) { +  throw( err ); +  } +  } else if( equal( res, m )) { +  string message = sprintf("Got %O, expected different value.\n", res); +  report_test_failure( message, cb, args, st ); +  if( throw_error ) { +  error( message ); +  } +  } else {    report_test_ok( err, cb, args, st ); -  +  }    };   }      mixed cpp_test_true (string file, int line, function(void:mixed) test_fn,    string test_text, array test_text_args)   {    return test_really_generic (silent_check_true, test_fn,    ({file, line, test_text}), test_text_args);   }    -  + mixed cpp_assert_true (string file, int line, function(void:mixed) test_fn, +  string test_text, array test_text_args) + { +  return test_really_generic (silent_check_true, test_fn, +  ({file, line, test_text}), test_text_args, true); + } +    mixed test( function|array f, mixed ... args )   {    return test_generic( 0, f, @args );   }    -  + mixed assert( function|array f, mixed ... args ) + { +  return assert_generic( 0, f, @args ); + } +    mixed test_true( function|array f, mixed ... args )   {    return test_generic( check_true, f, @args );   }    -  + mixed assert_true( function|array f, mixed ... args ) + { +  return assert_generic( assert_result_true, f, @args ); + } +    mixed test_false( function|array f, mixed ... args )   {    return test_generic( check_false, f, @args );   }    -  + mixed assert_false( function|array f, mixed ... args ) + { +  return assert_generic( assert_result_false, f, @args ); + } +    mixed test_error( function|array f, mixed ... args )   {    return test_generic( check_error, f, @args );   }    -  + mixed assert_error( function|array f, mixed ... args ) + { +  return assert_generic( assert_throws_error, f, @args ); + } +    mixed test_equal( mixed what, function|array f, mixed ... args )   {    return test_generic( check_equal( what ), f, @args );   }    -  + mixed assert_equal( mixed what, function|array f, mixed ... args ) + { +  return assert_generic( check_equal( what ), f, @args ); + } +    mixed test_not_equal( mixed what, function|array f, mixed ... args )   {    return test_generic( check_not_equal( what ), f, @args );   }    -  + mixed assert_not_equal( mixed what, function|array f, mixed ... args ) + { +  return test_generic( check_not_equal( what ), f, @args ); + }      void run_tests( Configuration c );      void low_run_tests( Configuration c,    function go_on )   {    mixed err = catch {    run_tests( c );    };    if( err ) {    write( "################ " + describe_backtrace( err ) );    go_on (++current_test, ++tests_failed);    }    else    go_on (++current_test, tests_failed);   }