Skip to content

Implement exception expectation #4

@zhangsu

Description

@zhangsu

We'd like to have the ability to expect the throwing of an C++ exception in CCSpec tests. We'd also like to reuse the usual expect(...).to(...) syntax:

expect([] { throw 32; }).to(throw_exception<int>());
expect([] { throw "It's dead, Jim!"; }).to(throw_exception<const char *>());
expect([] {
  std::function<int(int,int)> bar;
  bar();
}).to(throwException<std::bad_function_call>());

expect([] { 1 + 1 }).notTo(throw_exception<std::bad_function_call>());

So we'd need a new special matcher class ThrowException that copes with exceptions. Its friend constructor would be throw_exception. The constructor takes a template argument as the type of exception to match. For now the constructor can be nullary (later on it can also take a message to match std::exception::what()).

For now, if the matcher expects an exception but no exception is thrown, the error message can be:

[function object] should throw [Exception name]
OR
[function object] should not throw [Exception name]

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions