Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test exception with message #131

Merged
merged 7 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,23 @@ UTEST(foo, bar) {
}
```

### EXPECT_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message)

Expects that exception_type will be thrown with message exception_message when code x is executed.

```cpp
void foo(int bar) {
if (bar == 1)
throw std::range_error("bad bar");
}

UTEST(foo, bar) {
EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar"); // pass!
EXPECT_EXCEPTION_WITH_MESSAGE(foo(2), std::range_error, "bad bar2"); // fail!
EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::exception, "bad bar"); // fail!
}
```

### UTEST_SKIP(msg)

This macro lets you mark a test case as being skipped - eg. that the test case
Expand Down
13 changes: 12 additions & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,12 @@ UTEST(cpp, Near) {
// GCC stdlib has a sanitizer bug in exceptions!
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
__attribute__((no_sanitize("memory")))
#define MEMORY_SANITIZER
#endif
#endif

#if defined(MEMORY_SANITIZER)
__attribute__((no_sanitize("memory")))
#endif
static int
foo(int bar) {
Expand All @@ -255,6 +259,13 @@ UTEST(cpp, Exception) {
ASSERT_EXCEPTION(foo(1), std::range_error);
}

#if !defined(MEMORY_SANITIZER)
UTEST(cpp, ExceptionWithMessage) {
EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar");
ASSERT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar");
}
#endif

UTEST(cpp, Todo) { UTEST_SKIP("Not yet implemented!"); }

enum SomeEnum { SomeEnumFoo, SomeEnumBar };
Expand Down
14 changes: 14 additions & 0 deletions test/test11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ UTEST(cpp11, Near) {
ASSERT_NEAR(a, b, 0.01f);
}

// GCC stdlib has a sanitizer bug in exceptions!
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define MEMORY_SANITIZER
#endif
#endif

static int foo(int bar) {
if (bar == 1)
throw std::range_error("bad bar");
Expand All @@ -276,6 +283,13 @@ UTEST(cpp11, Exception) {
ASSERT_EXCEPTION(foo(1), std::range_error);
}

#if !defined(MEMORY_SANITIZER)
UTEST(cpp11, ExceptionWithMessage) {
EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar");
ASSERT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar");
}
#endif

UTEST(cpp11, Todo) { UTEST_SKIP("Not yet implemented!"); }

enum SomeEnum { SomeEnumFoo, SomeEnumBar };
Expand Down
14 changes: 14 additions & 0 deletions test/test14.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ UTEST(cpp14, Near) {
ASSERT_NEAR(a, b, 0.01f);
}

// GCC stdlib has a sanitizer bug in exceptions!
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define MEMORY_SANITIZER
#endif
#endif

static int foo(int bar) {
if (bar == 1)
throw std::range_error("bad bar");
Expand All @@ -276,6 +283,13 @@ UTEST(cpp14, Exception) {
ASSERT_EXCEPTION(foo(1), std::range_error);
}

#if !defined(MEMORY_SANITIZER)
UTEST(cpp14, ExceptionWithMessage) {
EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar");
ASSERT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar");
}
#endif

UTEST(cpp14, Todo) { UTEST_SKIP("Not yet implemented!"); }

enum SomeEnum { SomeEnumFoo, SomeEnumBar };
Expand Down
14 changes: 14 additions & 0 deletions test/test17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ UTEST(cpp17, Near) {
ASSERT_NEAR(a, b, 0.01f);
}

// GCC stdlib has a sanitizer bug in exceptions!
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define MEMORY_SANITIZER
#endif
#endif

static int foo(int bar) {
if (bar == 1)
throw std::range_error("bad bar");
Expand All @@ -276,6 +283,13 @@ UTEST(cpp17, Exception) {
ASSERT_EXCEPTION(foo(1), std::range_error);
}

#if !defined(MEMORY_SANITIZER)
UTEST(cpp17, ExceptionWithMessage) {
EXPECT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar");
ASSERT_EXCEPTION_WITH_MESSAGE(foo(1), std::range_error, "bad bar");
}
#endif

UTEST(cpp17, Todo) { UTEST_SKIP("Not yet implemented!"); }

enum SomeEnum { SomeEnumFoo, SomeEnumBar };
Expand Down
78 changes: 78 additions & 0 deletions utest.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ static UTEST_INLINE void *utest_realloc(void *const pointer, size_t new_size) {
return new_pointer;
}

#if defined(_MSC_VER)
#define UTEST_STRCPY(dest, dest_size, src) strcpy_s(dest, dest_size, src)
#else
#define UTEST_STRCPY(dest, dest_size, src) strcpy(dest, src)
#endif

static UTEST_INLINE utest_int64_t utest_ns(void) {
#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
utest_large_integer counter;
Expand Down Expand Up @@ -887,6 +893,41 @@ utest_type_printer(long long unsigned int i) {
} \
while (0) \
UTEST_SURPRESS_WARNING_END

#define EXPECT_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message) \
UTEST_SURPRESS_WARNING_BEGIN do { \
int exception_caught = 0; \
char* message_caught = UTEST_NULL; \
try { \
x; \
} catch (const exception_type & e) { \
exception_caught = 1; \
if (strcmp(e.what(), exception_message) != 0) { \
const size_t message_size = strlen(e.what()) + 1; \
message_caught = UTEST_PTR_CAST(char *, malloc(message_size)); \
UTEST_STRCPY(message_caught, message_size, e.what()); \
} \
} catch (...) { \
exception_caught = 2; \
} \
if (exception_caught != 1) { \
UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : %s exception\n", #exception_type); \
UTEST_PRINTF(" Actual : %s\n", (exception_caught == 2) \
? "Unexpected exception" \
: "No exception"); \
*utest_result = UTEST_TEST_FAILURE; \
} else if (exception_caught == 1 && message_caught != UTEST_NULL) { \
UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : %s exception with message %s\n", \
#exception_type, exception_message); \
UTEST_PRINTF(" Actual message : %s\n", message_caught); \
*utest_result = UTEST_TEST_FAILURE; \
free(message_caught); \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#endif

#if defined(__clang__)
Expand Down Expand Up @@ -1089,6 +1130,43 @@ utest_type_printer(long long unsigned int i) {
} \
while (0) \
UTEST_SURPRESS_WARNING_END

#define ASSERT_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message) \
UTEST_SURPRESS_WARNING_BEGIN do { \
int exception_caught = 0; \
char* message_caught = UTEST_NULL; \
try { \
x; \
} catch (const exception_type & e) { \
exception_caught = 1; \
if (strcmp(e.what(), exception_message) != 0) { \
const size_t message_size = strlen(e.what()) + 1; \
message_caught = UTEST_PTR_CAST(char *, malloc(message_size)); \
UTEST_STRCPY(message_caught, message_size, e.what()); \
} \
} catch (...) { \
exception_caught = 2; \
} \
if (exception_caught != 1) { \
UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : %s exception\n", #exception_type); \
UTEST_PRINTF(" Actual : %s\n", (exception_caught == 2) \
? "Unexpected exception" \
: "No exception"); \
*utest_result = UTEST_TEST_FAILURE; \
return; \
} else if (exception_caught == 1 && message_caught != UTEST_NULL) { \
UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \
UTEST_PRINTF(" Expected : %s exception with message %s\n", \
#exception_type, exception_message); \
UTEST_PRINTF(" Actual message : %s\n", message_caught); \
*utest_result = UTEST_TEST_FAILURE; \
free(message_caught); \
return; \
} \
} \
while (0) \
UTEST_SURPRESS_WARNING_END
#endif

#define UTEST(SET, NAME) \
Expand Down
Loading