[Dune] Testing for exceptions

Carsten Gräser graeser at mi.fu-berlin.de
Thu Apr 6 10:34:07 CEST 2017


Hi Lasse,
I'd propose to explicitily check if the function throws
and then pass the result to the test suite. This could be
done by a lambda, but having a helper function is maybe
more intuitive.

(Disclaimer: I did not try to compile this)

  template<class F, class... Args>
  bool doesThrow(F&& f, Args&&... args)
  {
    try {
      f(std::forward<Args>(args)...);
    }
    catch(...)
    {
      return true;
    }
    return false;
  }

  [...]
  suit.check(doesThrow(foo, badInput),  "Check if bad input throws") << "...";

One could also extend this by allowing to explicitly
specify the expected exception type.

Best,
Carsten

Am 05.04.2017 um 09:55 schrieb Lasse Hinrichsen:
> Hello all,
> 
> I have a question about testing library code.
> I want to test how an object/function/... handles an input that contains
> a logic error (say, e.g., a negative value where only positive ones are
> allowed). In my understanding such an object should throw an exception.
> How do I properly test for such an behavior?
> 
> I currently use Dune::TestSuite. The following snippet
> 
> TestSuite suite;
> try {
>   foo(bad_input);
>   suite.check(false, "Check if bad input throws") << "foo(bad_input) did
> not lead to an exception";
>   }
> catch (...) {
>   suite.check(true);
> }
> 
> should work, however I don't find this very expressive (the suite itself
> doesn't check anything). Is this a proper way to deal with this?
> 
> Best,
> Lasse
> 
> _______________________________________________
> Dune mailing list
> Dune at dune-project.org
> http://lists.dune-project.org/mailman/listinfo/dune
> 


-- 
Prof. Dr. Carsten Gräser
Freie Universität Berlin
Institut für Mathematik
Arnimallee 6
14195 Berlin, Germany
phone: +49 30 838 72637
fax  : +49 30 838 472637
email: graeser at mi.fu-berlin.de
URL  : http://page.mi.fu-berlin.de/graeser

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: OpenPGP digital signature
URL: <https://lists.dune-project.org/pipermail/dune/attachments/20170406/3a37e23d/attachment.sig>


More information about the Dune mailing list