diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-01-05 16:23:04 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-01-05 16:23:04 +0100 |
commit | 11894bef04095c37196af5ae1bfed885775d49eb (patch) | |
tree | 42c776a4caf24559d2e7e3aeedcafa73674b455b /asl/tests | |
parent | 8607772d4f9e21f53c1abfd9379737403b97f430 (diff) |
Add formatting & factories for status
Diffstat (limited to 'asl/tests')
-rw-r--r-- | asl/tests/format_tests.cpp | 62 | ||||
-rw-r--r-- | asl/tests/status_tests.cpp | 39 | ||||
-rw-r--r-- | asl/tests/test_types.hpp | 39 |
3 files changed, 95 insertions, 45 deletions
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<decltype("Hello")>);
-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<const asl::byte> str) override
- {
- m_data = reinterpret_cast<char*>(asl::GlobalHeap::realloc(
- m_data,
- asl::layout::array<char>(m_current_len),
- asl::layout::array<char>(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<char>(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<CustomFormat>);
+
+ASL_TEST(format_custom)
+{
+ StringSink sink;
+
+ asl::format(&sink, "{}", CustomFormat{37});
+ ASL_TEST_EXPECT(sink.str() == "(37)"_sv);
+}
diff --git a/asl/tests/status_tests.cpp b/asl/tests/status_tests.cpp index aa7dad1..8ac6373 100644 --- a/asl/tests/status_tests.cpp +++ b/asl/tests/status_tests.cpp @@ -1,9 +1,11 @@ #include "asl/status.hpp" #include "asl/testing/testing.hpp" +#include "asl/format.hpp" +#include "asl/tests/test_types.hpp" ASL_TEST(simple_ok) { - asl::status s; + asl::status s = asl::ok(); ASL_TEST_ASSERT(s); ASL_TEST_ASSERT(s.ok()); ASL_TEST_ASSERT(s.code() == asl::status_code::ok); @@ -11,7 +13,7 @@ ASL_TEST(simple_ok) ASL_TEST(simple_code) { - asl::status s{asl::status_code::runtime}; + asl::status s = asl::runtime_error(); ASL_TEST_ASSERT(!s); ASL_TEST_ASSERT(!s.ok()); ASL_TEST_ASSERT(s.code() == asl::status_code::runtime); @@ -20,7 +22,7 @@ ASL_TEST(simple_code) ASL_TEST(with_message) { - asl::status s{asl::status_code::internal, "We done goofed"}; + asl::status s = asl::internal_error("We done goofed"); ASL_TEST_ASSERT(!s); ASL_TEST_ASSERT(!s.ok()); ASL_TEST_ASSERT(s.code() == asl::status_code::internal); @@ -29,8 +31,8 @@ ASL_TEST(with_message) ASL_TEST(copy_inline) { - asl::status s{asl::status_code::ok}; - asl::status s2{asl::status_code::internal}; + asl::status s = asl::ok(); + asl::status s2 = asl::internal_error(); asl::status s3 = s; ASL_TEST_ASSERT(s3.code() == asl::status_code::ok); @@ -41,10 +43,10 @@ ASL_TEST(copy_inline) ASL_TEST(copy_message) { - asl::status s2; + asl::status s2 = asl::ok(); { - asl::status s{asl::status_code::internal, "Oh no!"}; + asl::status s = asl::internal_error("Oh no!"); ASL_TEST_ASSERT(!s); ASL_TEST_ASSERT(!s.ok()); ASL_TEST_ASSERT(s.code() == asl::status_code::internal); @@ -68,3 +70,26 @@ ASL_TEST(copy_message) ASL_TEST_ASSERT(s2.code() == asl::status_code::internal); ASL_TEST_ASSERT(s2.message() == "Oh no!"_sv); } + +static_assert(asl::formattable<asl::status>); + +ASL_TEST(format) +{ + StringSink sink; + + asl::format(&sink, "-{}-", asl::ok()); + ASL_TEST_EXPECT(sink.str() == "-[ok]-"_sv); + + sink.reset(); + asl::format(&sink, "-{}-", asl::internal_error("hello")); + ASL_TEST_EXPECT(sink.str() == "-[internal: hello]-"_sv); +} + +ASL_TEST(make_with_format) +{ + StringSink sink; + + sink.reset(); + asl::format(&sink, "-{}-", asl::internal_error("hello, {}, {}", 45, "world")); + ASL_TEST_EXPECT(sink.str() == "-[internal: hello, 45, world]-"_sv); +} diff --git a/asl/tests/test_types.hpp b/asl/tests/test_types.hpp index 1bcf72f..3c95d95 100644 --- a/asl/tests/test_types.hpp +++ b/asl/tests/test_types.hpp @@ -1,6 +1,9 @@ #pragma once
#include "asl/utility.hpp"
+#include "asl/io.hpp"
+#include "asl/allocator.hpp"
+#include "asl/string_view.hpp"
struct TrivialType
{
@@ -88,3 +91,39 @@ struct DestructorObserver } };
+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<const asl::byte> str) override
+ {
+ m_data = reinterpret_cast<char*>(asl::GlobalHeap::realloc(
+ m_data,
+ asl::layout::array<char>(m_current_len),
+ asl::layout::array<char>(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<char>(m_current_len));
+ m_data = nullptr;
+ }
+ }
+};
|