44#include < libassert/assert.hpp>
55#include < rsl/testing/test.hpp>
66#include < rsl/testing/output.hpp>
7+ #include < rsl/testing/_testing_impl/discovery.hpp>
8+
9+ #include " capture.hpp"
710
8- template <bool Colorize>
911void failure_handler (libassert::assertion_info const & info) {
1012 // libassert::enable_virtual_terminal_processing_if_needed(); // for terminal colors on windows
11- auto width = libassert::terminal_width (libassert::stderr_fileno);
13+ constexpr bool Colorize = true ;
14+ auto width = libassert::terminal_width (libassert::stderr_fileno);
1215 const auto & scheme = Colorize ? libassert::get_color_scheme () : libassert::color_scheme::blank;
1316 std::string message = std::string (info.action ()) + " at " + info.location () + " :" ;
1417 if (info.message ) {
@@ -21,12 +24,57 @@ void failure_handler(libassert::assertion_info const& info) {
2124}
2225
2326namespace rsl ::testing {
24- namespace _testing_impl {
25- std::set<TestDef>& registry ();
27+ std::set<TestDef>& _testing_impl::registry () {
28+ static std::set<TestDef> data;
29+ return data;
30+ }
31+
32+ bool TestRoot::run (Reporter* reporter) {
33+ libassert::set_failure_handler (failure_handler);
34+ std::println (" failure handler set" );
35+ reporter->before_run (*this );
36+ bool status = TestNamespace::run (reporter);
37+ libassert::set_failure_handler (libassert::default_failure_handler);
38+ // TODO after_run
39+ reporter->after_run ({});
40+ return status;
41+ }
42+
43+ bool TestNamespace::run (Reporter* reporter) {
44+ if (!name.empty ()) {
45+ reporter->enter_namespace (name);
46+ }
47+ bool status = true ;
48+ for (auto & ns : children) {
49+ status &= ns.run (reporter);
50+ }
51+
52+ for (auto & test : tests) {
53+ auto runs = test.get_tests ();
54+ reporter->before_test_group (test);
55+
56+ std::vector<TestResult> results;
57+ for (auto const & test_run : test.get_tests ()) {
58+ reporter->before_test (test_run);
59+ auto result = test_run.run ();
60+ reporter->after_test (result);
61+ results.push_back (result);
62+ }
63+
64+ reporter->after_test_group (results);
65+ }
66+ if (!name.empty ()) {
67+ reporter->exit_namespace (name);
68+ }
69+ return status;
2670}
71+
2772TestResult TestCase::run () const {
2873 auto ret = TestResult{.test = test, .name = name};
2974 try {
75+ Capture _out (stdout, ret.stdout );
76+ Capture _err (stderr, ret.stderr );
77+
3078 auto t0 = std::chrono::steady_clock::now ();
3179 fnc ();
3280 auto t1 = std::chrono::steady_clock::now ();
@@ -35,11 +83,16 @@ TestResult TestCase::run() const {
3583 ret.duration_ms = std::chrono::duration<double , std::milli>(t1 - t0).count ();
3684 return ret;
3785 } catch (assertion_failure const & failure) {
38- ret.error = failure.what ();
39- } catch (std::exception const & exc) {
40- ret.error = exc.what ();
41- } //
42- catch (...) {}
86+ ret.error += failure.what ();
87+ } catch (std::exception const & exc) { //
88+ ret.error += exc.what ();
89+ } catch (std::string const & msg) { //
90+ ret.error += msg;
91+ } catch (std::string_view msg) { //
92+ ret.error += msg;
93+ } catch (char const * msg) { //
94+ ret.error += msg;
95+ } catch (...) { ret.error += " unknown exception thrown" ; }
4396
4497 ret.passed = test->expect_failure ;
4598 return ret;
@@ -111,45 +164,6 @@ std::size_t TestNamespace::count() const {
111164 return total;
112165}
113166
114- bool TestNamespace::run (Reporter* reporter) {
115- if (!name.empty ()) {
116- reporter->enter_namespace (name);
117- }
118- bool status = true ;
119- for (auto & ns : children) {
120- status &= ns.run (reporter);
121- }
122-
123- for (auto & test : tests) {
124- auto runs = test.get_tests ();
125- reporter->before_test_group (test);
126-
127- std::vector<TestResult> results;
128- for (auto const & test_run : test.get_tests ()) {
129- reporter->before_test (test_run);
130- auto result = test_run.run ();
131- reporter->after_test (result);
132- results.push_back (result);
133- }
134-
135- reporter->after_test_group (results);
136- }
137- if (!name.empty ()) {
138- reporter->exit_namespace (name);
139- }
140- return status;
141- }
142-
143- bool TestRoot::run (Reporter* reporter) {
144- libassert::set_failure_handler (failure_handler<true >);
145- reporter->before_run (*this );
146- bool status = TestNamespace::run (reporter);
147- libassert::set_failure_handler (libassert::default_failure_handler);
148- // TODO after_run
149- reporter->after_run ({});
150- return status;
151- }
152-
153167TestRoot get_tests () {
154168 TestRoot root;
155169 for (auto test_def : rsl::testing::_testing_impl::registry ()) {
0 commit comments