From 11894bef04095c37196af5ae1bfed885775d49eb Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sun, 5 Jan 2025 16:23:04 +0100 Subject: Add formatting & factories for status --- asl/tests/format_tests.cpp | 62 ++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 38 deletions(-) (limited to 'asl/tests/format_tests.cpp') diff --git a/asl/tests/format_tests.cpp b/asl/tests/format_tests.cpp index 8318757..a08d052 100644 --- a/asl/tests/format_tests.cpp +++ b/asl/tests/format_tests.cpp @@ -1,47 +1,10 @@ #include "asl/format.hpp" #include "asl/testing/testing.hpp" -#include "asl/allocator.hpp" #include "asl/float.hpp" +#include "asl/tests/test_types.hpp" static_assert(asl::formattable); -class StringSink : public asl::Writer -{ - // @Todo Use string, once we have it, or a buffer - isize_t m_current_len{}; - char* m_data{}; - -public: - ~StringSink() override - { - reset(); - } - - void write(asl::span str) override - { - m_data = reinterpret_cast(asl::GlobalHeap::realloc( - m_data, - asl::layout::array(m_current_len), - asl::layout::array(m_current_len + str.size()))); - - asl::memcpy(m_data + m_current_len, str.data(), str.size()); // NOLINT - - m_current_len += str.size(); - } - - constexpr asl::string_view str() const { return {m_data, m_current_len}; } - - void reset() - { - if (m_data != nullptr) - { - m_current_len = 0; - asl::GlobalHeap::dealloc(m_data, asl::layout::array(m_current_len)); - m_data = nullptr; - } - } -}; - ASL_TEST(format_args) { StringSink sink; @@ -154,3 +117,26 @@ ASL_TEST(format_boolean) asl::format(&sink, "{} {}", true, false); ASL_TEST_EXPECT(sink.str() == "true false"_sv); } + +struct CustomFormat +{ + int x; + friend void AslFormat(asl::Formatter&, const CustomFormat&); +}; + +void AslFormat(asl::Formatter& f, const CustomFormat& c) +{ + f.write("("_sv); + AslFormat(f, c.x); + f.write(")"_sv); +} + +static_assert(asl::formattable); + +ASL_TEST(format_custom) +{ + StringSink sink; + + asl::format(&sink, "{}", CustomFormat{37}); + ASL_TEST_EXPECT(sink.str() == "(37)"_sv); +} -- cgit