Branch: Tag:

2018-04-03

2018-04-03 13:11:50 by Karl Gustav Sterneberg <kg@roxen.com>

Added assert functions to testsuite.h and pike_test_common.pike

They work like the old test functions with one execption, they do not
swallow exceptions.

124:    }   }    -  - 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;
135:    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 );
144:    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;
158:    // test_text_args in the test.    report_1st( test_text, test_text_args, check_return );    +  if( err && throw_error ) { +  report_test_failure( err, test_text, test_text_args, st ); +  throw( err ); +  }    if( check_return )    check_return( result, err, test_text, test_text_args,st );    else if( err )
169:         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." );    } -  +  } + }    -  + 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 )   {
212:   }      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 ); -  +  if( throw_error ) { +  throw( err ); +  } +  }    else -  if( !res ) +  if( !res ) {    report_test_failure( sprintf ("expected non-zero, got %O", res),    cb, args, st); -  +  if( throw_error ) { +  error( sprintf ("expected non-zero, got %O", 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",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 )   {
261:   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", 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", res); +  report_test_failure( message, cb, args, st ); +  if( throw_error ) { +  error( message ); +  } +  } else {    report_test_ok( err, cb, args, st ); -  +  }    };   }   
297:    ({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 );