|
8 | 8 | namespace test { |
9 | 9 |
|
10 | 10 |
|
11 | | -enum Test1 { |
12 | | - VALUE1, |
| 11 | +struct Test1 { |
| 12 | + int x; |
| 13 | + int y; |
13 | 14 | }; |
14 | 15 | enum Test2 { VALUE2 }; |
15 | 16 |
|
16 | 17 |
|
17 | | -constexpr std::string_view to_string(Test1 e) { |
| 18 | + |
| 19 | +constexpr std::string_view to_string(Test2 const& e) { |
18 | 20 | switch(e) { |
19 | | - case VALUE1: return "VALUE1"; |
| 21 | + case VALUE2: return "VALUE2"; |
20 | 22 | default: return "unknown"; |
21 | 23 | } |
22 | 24 | } |
23 | 25 |
|
| 26 | +struct Test3 { |
| 27 | + Test1 a; |
| 28 | +}; |
24 | 29 |
|
25 | 30 | template<class T> |
26 | | -concept test_concept = std::is_same_v<T, Test2>; |
27 | | -constexpr std::string_view to_string(Test2 e) { |
28 | | - switch(e) { |
29 | | - case VALUE2: return "VALUE2"; |
30 | | - default: return "unknown"; |
31 | | - } |
32 | | -} |
| 31 | +concept test_concept = std::same_as<T, Test3>; |
33 | 32 | } |
34 | 33 |
|
35 | 34 |
|
36 | | -CTH_FORMAT_CLASS(test::Test1, "{}", test::to_string); |
| 35 | +CTH_FORMAT_CLASS(test::Test1, "x: {}, y: {}", ([](test::Test1 const& test) {return std::tie(test.x, test.y); })); |
| 36 | +CTH_FORMAT_CLASS_CUSTOM(test::Test2, "{}", test::to_string); |
| 37 | + |
| 38 | +namespace test { |
| 39 | +std::string to_string(Test3 const& e) { return std::format("{}", e.a); } |
| 40 | +} |
| 41 | + |
37 | 42 | CTH_FORMAT_CPT(test::test_concept, test::to_string); |
38 | 43 |
|
39 | 44 |
|
40 | 45 | namespace cth::fmt { |
41 | | -FMT_TEST(CTH_FORMAT_CLASS, main) { |
42 | | - auto const str = std::format("{}", ::test::VALUE1); |
| 46 | +::test::Test1 x{1, 2}; |
43 | 47 |
|
44 | | - ASSERT_EQ("test::Test1{VALUE1}", str); |
| 48 | +FMT_TEST(CTH_FORMAT_CLASS_CUSTOM, main) { |
| 49 | + auto const str = std::format("{}", ::test::VALUE2); |
| 50 | + ASSERT_EQ(str, "VALUE2"); |
| 51 | +} |
| 52 | + |
| 53 | +FMT_TEST(CTH_FORMAT_CLASS, main) { |
| 54 | + auto const str = std::format("{}", x); |
| 55 | + auto const expected = std::format("test::Test1{{x: {}, y: {}}}", x.x, x.y); |
| 56 | + ASSERT_EQ(str, expected); |
45 | 57 | } |
46 | 58 | FMT_TEST(CTH_FORMAT_CPT, main) { |
47 | | - auto const str = std::format("{}", ::test::VALUE2); |
48 | | - ASSERT_EQ("test::test_concept{VALUE2}", str); |
| 59 | + auto const str = std::format("{}", ::test::Test3{x}); |
| 60 | + auto const expected = std::format("test::test_concept{{{}}}", x); |
| 61 | + |
| 62 | + ASSERT_EQ(str, expected); |
49 | 63 | } |
50 | 64 | } |
0 commit comments