Skip to content

Commit 2dfe831

Browse files
authored
Merge pull request #3 from sbash64/bug-fix/catch-std-exceptions
Bug fix/catch std exceptions
2 parents 94aebc2 + 32ad50b commit 2dfe831

3 files changed

Lines changed: 41 additions & 7 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.10.0)
2-
project(TestCppLite VERSION 1.1.2 LANGUAGES CXX)
2+
project(TestCppLite VERSION 1.1.3 LANGUAGES CXX)
33
add_subdirectory(src)
44
if (${WITH_TESTS})
55
enable_testing()

src/testcpplite.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "testcpplite.hpp"
2+
#include <exception>
23

34
namespace testcpplite {
45
struct TestResult {
@@ -22,18 +23,33 @@ static auto quoted(const std::string &s) -> std::string {
2223
return mark + s + mark;
2324
}
2425

25-
auto test(const std::vector<Test> &tests, std::ostream &stream) -> int {
26+
static void writeFailure(
27+
std::ostream &stream, const Test &test, const std::string &what) {
28+
stream << "fail " << test.name << '\n';
29+
stream << " " << what << '\n';
30+
}
31+
32+
static auto test(const Test &test, std::ostream &stream) -> bool {
2633
bool passed{true};
27-
for (const auto &test : tests) {
34+
try {
2835
TestResult result{};
2936
test.f(result);
3037
if (result.failed) {
3138
passed = false;
32-
stream << "fail " << test.name << '\n';
33-
stream << " expected " << result.expected << ", actual "
34-
<< result.actual << '\n';
39+
writeFailure(stream, test,
40+
"expected " + result.expected + ", actual " + result.actual);
3541
}
42+
} catch (const std::exception &e) {
43+
passed = false;
44+
writeFailure(stream, test, e.what());
3645
}
46+
return passed;
47+
}
48+
49+
auto test(const std::vector<Test> &tests, std::ostream &stream) -> int {
50+
bool passed{true};
51+
for (const auto &t : tests)
52+
passed &= test(t, stream);
3753
if (passed)
3854
stream << "pass\n";
3955
return passed ? 0 : 1;

test/main.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <testcpplite/testcpplite.hpp>
22
#include <sstream>
33
#include <iostream>
4+
#include <exception>
5+
#include <string>
46

57
namespace testcpplite {
68
namespace {
@@ -28,6 +30,16 @@ void passesNegativeBooleanAssertion(TestResult &result) {
2830

2931
void expectsFalseActualTrue(TestResult &result) { assertFalse(result, true); }
3032

33+
class StandardException : public std::exception {
34+
public:
35+
explicit StandardException(std::string s) : s{std::move(s)} {}
36+
37+
auto what() const noexcept -> const char * override { return s.c_str(); }
38+
39+
private:
40+
std::string s;
41+
};
42+
3143
auto testStream(const std::vector<Test> &tests) -> std::stringstream {
3244
std::stringstream stream;
3345
test(tests, stream);
@@ -140,6 +152,11 @@ void failedUnsignedIntegerComparisonShowsFailedMessage(TestResult &result) {
140152
{expects2147483647Actual2147483648, "myTest"});
141153
}
142154

155+
void catchesStandardExceptions(TestResult &result) {
156+
assertEqual(result, failMessage("myTest") + " error\n",
157+
{[](TestResult &) { throw StandardException{"error"}; }, "myTest"});
158+
}
159+
143160
int main() {
144161
return testcpplite::test(
145162
{{passedOnlyTestShowsPassedMessage, "passedOnlyTestShowsPassedMessage"},
@@ -166,7 +183,8 @@ int main() {
166183
{failedNegativeBooleanAssertionShowsFailedMessage,
167184
"failedNegativeBooleanAssertionShowsFailedMessage"},
168185
{failedUnsignedIntegerComparisonShowsFailedMessage,
169-
"failedUnsignedIntegerComparisonShowsFailedMessage"}},
186+
"failedUnsignedIntegerComparisonShowsFailedMessage"},
187+
{catchesStandardExceptions, "catchesStandardExceptions"}},
170188
std::cout);
171189
}
172190
}

0 commit comments

Comments
 (0)