Skip to content

Instantly share code, notes, and snippets.

@comargo
Last active August 12, 2016 22:02
Show Gist options
  • Save comargo/9d8cf4dd4f5696568227797ef5e41a6f to your computer and use it in GitHub Desktop.
Save comargo/9d8cf4dd4f5696568227797ef5e41a6f to your computer and use it in GitHub Desktop.
#include <gtest/gtest.h>
#include <system_error>
#define ASSERT_SYSTEM_ERROR(statement, error_code) \
ASSERT_THROW({\
try { \
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
} \
catch(const std::system_error &exception) {\
ASSERT_EQ(error_code, exception.code());\
throw;\
}\
}, std::system_error)
#define EXPECT_SYSTEM_ERROR(statement, error_code) \
EXPECT_THROW({\
try { \
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
} \
catch(const std::system_error &exception) {\
EXPECT_EQ(error_code, exception.code());\
throw;\
}\
}, std::system_error)
#include <gtest/gtest.h>
#include "gtest-system-error.hpp"
#include <thread>
// Example of exception took from
// http://en.cppreference.com/w/cpp/error/system_error
TEST(Test,TestPassed)
{
EXPECT_SYSTEM_ERROR(std::thread().detach(), std::make_error_code(std::errc::invalid_argument));
}
TEST(Test,TestFailed)
{
EXPECT_SYSTEM_ERROR(std::thread().detach(), std::make_error_code(std::errc::io_error));
}
@comargo
Copy link
Author

comargo commented Aug 12, 2016

On my test environment the Test.TestFailed report error twice. Why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment