zjx20 / googletest

Automatically exported from code.google.com/p/googletest
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

adding an expect_throw_that macro #285

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I am writing a function that can throw runtime errors with different what() 
strings for different 
errors.  Gtest cannot check the actual what string against what you expect, 
here is a bit of a 
macro I wrote to fix this.
I mainly added checking of the what string against an expected one, where if 
the strings are 
different the test will fail.

// should go in gtest.h

#define EXPECT_THROW_WHAT(statement, expected_exception, error_string) \
  GTEST_TEST_THROW_WHAT_(statement, expected_exception, error_string, 
GTEST_NONFATAL_FAILURE_)

#define ASSERT_THROW_WHAT(statement, expected_exception, error_string) \
  GTEST_TEST_THROW_WHAT_(statement, expected_exception, error_string, 
GTEST_FATAL_FAILURE_)

// should go in gtest_internal.h
#define GTEST_TEST_THROW_WHAT_(statement, expected_exception, error_string, 
fail) \
  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  if (const char* gtest_msg = "") { \
    bool gtest_caught_expected = false; \
    try { \
      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
    } \
    catch (expected_exception const& e) { \
      if(e.what() == error_string)\
      {\
        gtest_caught_expected = true; \
      }\
      else\
      {\
        gtest_msg = "Expected: " #statement ".what() to return the error string " \
                  #error_string ".\n  Actual: it throws a different error string " \
                  statement.what(); \
        goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
      }\
    } \
    catch (...) { \
      gtest_msg = "Expected: " #statement " throws an exception of type " \
                  #expected_exception ".\n  Actual: it throws a different " \
                  "type."; \
      goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
    } \
    if (!gtest_caught_expected) { \
      gtest_msg = "Expected: " #statement " throws an exception of type " \
                  #expected_exception ".\n  Actual: it throws nothing."; \
      goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
    } \
  } else \
    GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
      fail(gtest_msg)

Original issue reported on code.google.com by trox...@gmail.com on 17 May 2010 at 10:07

GoogleCodeExporter commented 9 years ago
I like the idea in general. I would like to see it a bit more generic, though. 
Some frameworks such as MFC 
define their own exception classes which do not derive from std::exception and 
do not implement what(). Since 
Google Test is a general purpose framework, I'd like those classes covered as 
well. This is possible now, with 
the universal printer ported from Google Mock. Instead of
if (e.what() == error_string) {
The code should look like this:
if (::testing::internal::PrintToString(e) == error_string) {

For further flexibility, employ regex matching rather than simple string 
comparison.

Regarding the implementation, any new code requires tests, including this one. 
Most of the test should mirror 
ones for existing constructs such as EXPECT_THROW, but some will need to be 
written specifically for this one 
(e.g., verifying that an exception of the right type with the right string 
representation is intercepted and 
logged as a failure while exceptions whose types or string representations 
don't match are not intercepted). 
And finally, before we can accept a code contribution we need to ask you to 
sign a CLA as described in 
http://code.google.com/p/googletest/wiki/GoogleTestDevGuide#Contributing_Code).

If all that doesn't daunt you, we are happy to accept your contribution.

Original comment by vladlosev on 18 May 2010 at 7:15

GoogleCodeExporter commented 9 years ago
I'd like to see this done after we port matchers to gtest.  Then we can write

  EXPECT_THROW_THAT(statement, matcher)

or something like that, which is much more flexible.

Original comment by shiq...@gmail.com on 18 May 2010 at 7:27

GoogleCodeExporter commented 9 years ago
Lowering the priority as I don't think this is needed often.

Original comment by w...@google.com on 27 Sep 2010 at 7:23