Skip to content

Commit

Permalink
Add another test case
Browse files Browse the repository at this point in the history
  • Loading branch information
mingxwa committed May 12, 2024
1 parent 51af4a8 commit 2139acf
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/proxy_integration_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

#include <gtest/gtest.h>
#include <format>
#include <iomanip>
#include <memory>
#include <memory_resource>
Expand All @@ -19,6 +20,9 @@ PRO_DEF_MEMBER_DISPATCH(Draw, void(std::ostream&));
PRO_DEF_MEMBER_DISPATCH(Area, double() noexcept);
PRO_DEF_FACADE(Drawable, PRO_MAKE_DISPATCH_PACK(Draw, Area));

PRO_DEF_MEMBER_DISPATCH(Log, void(const char*), void(const char*, const std::exception&));
PRO_DEF_FACADE(Logger, Log);

} // namespace spec

class Rectangle {
Expand Down Expand Up @@ -106,6 +110,22 @@ pro::proxy<spec::Drawable> MakeDrawableFromCommand(const std::string& s) {
throw std::runtime_error{"Invalid command"};
}

class StreamLogger {
public:
explicit StreamLogger(std::ostream& out) : out_(&out) {}
StreamLogger(const StreamLogger&) = default;

void Log(const char* s) {
*out_ << std::format("[INFO] {}\n", s);
}
void Log(const char* s, const std::exception& e) {
*out_ << std::format("[ERROR] {} (exception info: {})\n", s, e.what());
}

private:
std::ostream* out_;
};

} // namespace

TEST(ProxyIntegrationTests, TestDrawable) {
Expand All @@ -127,3 +147,17 @@ TEST(ProxyIntegrationTests, TestDrawable) {
ASSERT_STREQ(e.what(), "Invalid command");
}
}

TEST(ProxyIntegrationTests, TestLogger) {
std::ostringstream out;
auto logger = pro::make_proxy<spec::Logger, StreamLogger>(out);
logger.Log("hello");
try {
throw std::runtime_error{"runtime error!"};
} catch (const std::exception& e) {
logger.Log("world", e);
}
auto content = std::move(out).str();
ASSERT_EQ(content, "[INFO] hello\n\
[ERROR] world (exception info: runtime error!)\n");
}

0 comments on commit 2139acf

Please sign in to comment.