From 709c6bb01d903f1f59c861dff403bbd565239bdc Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sat, 30 Dec 2023 00:32:30 +0100 Subject: [PATCH] refactor: make os_access and file_access settable in test_iolayer --- test/test_helpers.h | 11 +++++++---- test/test_iolayer.cpp | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/test/test_helpers.h b/test/test_helpers.h index f8e410ff..a5093ac4 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -180,12 +180,12 @@ class test_file_access : public file_access { class test_iolayer { public: test_iolayer(); - test_iolayer(std::shared_ptr os); - test_iolayer(std::shared_ptr os, + test_iolayer(std::shared_ptr os); + test_iolayer(std::shared_ptr os, std::shared_ptr fa); ~test_iolayer(); - iolayer const& get() const; + iolayer const& get(); std::string out() const; std::string err() const; @@ -194,8 +194,11 @@ class test_iolayer { void set_terminal_fancy(bool fancy); void set_terminal_width(size_t width); + void set_os_access(std::shared_ptr os); + void set_file_access(std::shared_ptr fa); + private: - std::shared_ptr os_; + std::shared_ptr os_; std::shared_ptr term_; std::shared_ptr fa_; std::istringstream in_; diff --git a/test/test_iolayer.cpp b/test/test_iolayer.cpp index 7bf97250..4f59d046 100644 --- a/test/test_iolayer.cpp +++ b/test/test_iolayer.cpp @@ -202,26 +202,30 @@ std::string test_terminal::colored(std::string text, termcolor color, test_iolayer::test_iolayer() : test_iolayer{os_access_mock::create_test_instance()} {} -test_iolayer::test_iolayer(std::shared_ptr os) +test_iolayer::test_iolayer(std::shared_ptr os) : test_iolayer{std::move(os), create_file_access_generic()} {} -test_iolayer::test_iolayer(std::shared_ptr os, +test_iolayer::test_iolayer(std::shared_ptr os, std::shared_ptr fa) : os_{std::move(os)} , term_{std::make_shared(out_, err_)} - , fa_{std::move(fa)} - , iol_{std::make_unique(iolayer{ - .os = os_, - .term = term_, - .file = fa_, - .in = in_, - .out = out_, - .err = err_, - })} {} + , fa_{std::move(fa)} {} test_iolayer::~test_iolayer() = default; -iolayer const& test_iolayer::get() const { return *iol_; } +iolayer const& test_iolayer::get() { + if (!iol_) { + iol_ = std::make_unique(iolayer{ + .os = os_, + .term = term_, + .file = fa_, + .in = in_, + .out = out_, + .err = err_, + }); + } + return *iol_; +} void test_iolayer::set_terminal_fancy(bool fancy) { term_->set_fancy(fancy); } void test_iolayer::set_terminal_width(size_t width) { term_->set_width(width); } @@ -230,4 +234,18 @@ void test_iolayer::set_in(std::string in) { in_.str(std::move(in)); } std::string test_iolayer::out() const { return out_.str(); } std::string test_iolayer::err() const { return err_.str(); } +void test_iolayer::set_os_access(std::shared_ptr os) { + if (iol_) { + throw std::runtime_error("iolayer already created"); + } + os_ = std::move(os); +} + +void test_iolayer::set_file_access(std::shared_ptr fa) { + if (iol_) { + throw std::runtime_error("iolayer already created"); + } + fa_ = std::move(fa); +} + } // namespace dwarfs::test