Skip to content

Commit 34b229e

Browse files
committed
Workaround missing <print>
1 parent b1b91c4 commit 34b229e

4 files changed

Lines changed: 60 additions & 14 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ target_sources(
4040
include/eggs/test.hpp
4141
include/eggs/test/detail/registry.hpp
4242
include/eggs/test/detail/checks.hpp
43+
include/eggs/test/detail/print.hpp
4344
include/eggs/test/detail/runner.hpp
4445
)
4546
set_target_properties(

include/eggs/test/detail/checks.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
#pragma once
99

10+
#include <eggs/test/detail/print.hpp>
1011
#include <eggs/test/detail/registry.hpp>
1112

12-
#include <print>
13+
#include <cstdio>
1314
#include <source_location>
1415
#include <stacktrace>
1516

@@ -20,15 +21,17 @@ inline void do_check_failed(
2021
)
2122
{
2223
++s.assertions_failed;
23-
std::println(" FAILED: {} [{}:{}]", expr, loc.file_name(), loc.line());
24+
detail::println(
25+
stdout, " FAILED: {} [{}:{}]", expr, loc.file_name(), loc.line()
26+
);
2427

2528
// Directly in test body: st.size() == s.entry_depth + 1
2629
// Inside a helper: st.size() > s.entry_depth + 1
2730
if (st.size() > s.entry_depth + 1) {
28-
std::println(" Stacktrace (innermost first):");
31+
detail::println(stdout, " Stacktrace (innermost first):");
2932
std::size_t const user_frames = st.size() - s.entry_depth - 1;
3033
for (std::size_t i = 0; i < user_frames; ++i)
31-
std::println(" #{} {}", i, st[i].description());
34+
detail::println(stdout, " #{} {}", i, st[i].description());
3235
}
3336
}
3437

include/eggs/test/detail/print.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Eggs.Test
2+
//
3+
// Copyright Agustin K-ballo Berge, Fusion Fenix 2026
4+
//
5+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6+
// file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7+
8+
#pragma once
9+
10+
#include <version>
11+
12+
#ifdef __cpp_lib_print
13+
# include <print>
14+
#else
15+
# include <cstdio>
16+
# include <format>
17+
#endif
18+
19+
namespace eggs::test::detail {
20+
21+
#ifdef __cpp_lib_print
22+
using std::print;
23+
using std::println;
24+
#else
25+
template <typename... Args>
26+
void print(std::FILE* f, std::format_string<Args...> fmt, Args&&... args)
27+
{
28+
std::fputs(std::format(fmt, std::forward<Args>(args)...).c_str(), f);
29+
}
30+
31+
template <typename... Args>
32+
void println(std::FILE* f, std::format_string<Args...> fmt, Args&&... args)
33+
{
34+
print(f, fmt, std::forward<Args>(args)...);
35+
std::fputc('\n', f);
36+
}
37+
#endif
38+
39+
} // namespace eggs::test::detail

include/eggs/test/detail/runner.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#pragma once
99

1010
#include <eggs/test/detail/checks.hpp>
11+
#include <eggs/test/detail/print.hpp>
1112
#include <eggs/test/detail/registry.hpp>
1213

1314
#include <exception>
14-
#include <print>
1515
#include <stacktrace>
1616
#include <string_view>
1717
#include <vector>
@@ -30,7 +30,7 @@ inline int registry::run_all()
3030
std::vector<std::string_view> cases_failed;
3131

3232
for (test_entry& e : cases()) {
33-
std::println("[ RUN ] {} -- {}", e.name, e.desc);
33+
detail::println(stdout, "[ RUN ] {} -- {}", e.name, e.desc);
3434

3535
e.state.entry_depth = entry_depth;
3636

@@ -41,35 +41,38 @@ inline int registry::run_all()
4141
passed = !e.state.assertions_failed;
4242
} catch (require_failed const&) {
4343
} catch (std::exception const& ex) {
44-
std::println(" EXCEPTION: {}", ex.what());
44+
detail::println(stdout, " EXCEPTION: {}", ex.what());
4545
} catch (...) {
46-
std::println(" UNKNOWN EXCEPTION");
46+
detail::println(stdout, " UNKNOWN EXCEPTION");
4747
}
4848
tl_current_state = nullptr;
4949

5050
if (passed) {
51-
std::println("[ PASS ] {}", e.name);
51+
detail::println(stdout, "[ PASS ] {}", e.name);
5252
++cases_passed;
5353
} else {
54-
std::println("[ FAIL ] {}", e.name);
54+
detail::println(stdout, "[ FAIL ] {}", e.name);
5555
cases_failed.push_back(e.name);
5656
}
5757

5858
auto const assertions_total =
5959
e.state.assertions_passed + e.state.assertions_failed;
60-
std::println(
60+
detail::println(
61+
stdout,
6162
"{} of {} assertions passed",
6263
e.state.assertions_passed,
6364
assertions_total
6465
);
6566
}
6667

6768
auto const cases_total = cases_passed + cases_failed.size();
68-
std::println("\n{} of {} test cases passed", cases_passed, cases_total);
69+
detail::println(
70+
stdout, "\n{} of {} test cases passed", cases_passed, cases_total
71+
);
6972
if (!cases_failed.empty()) {
70-
std::println("Failed test cases:");
73+
detail::println(stdout, "Failed test cases:");
7174
for (const auto& e : cases_failed) {
72-
std::println("- {}", e);
75+
detail::println(stdout, "- {}", e);
7376
}
7477
}
7578

0 commit comments

Comments
 (0)