From 72ee630cd24fe49c8abe0dcc6bac3b784b5bc13a Mon Sep 17 00:00:00 2001 From: sha512sum Date: Tue, 27 Feb 2024 22:35:29 +0000 Subject: [PATCH] Add menu test, fix namespaces --- include/utempl/go_interface.hpp | 2 +- include/utempl/menu.hpp | 8 ++++---- tests/go_interface.cpp | 10 +++++++--- tests/menu.cpp | 21 +++++++++++++++++++++ tests/overloaded.cpp | 2 +- 5 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 tests/menu.cpp diff --git a/include/utempl/go_interface.hpp b/include/utempl/go_interface.hpp index 11c3301..c2551f4 100644 --- a/include/utempl/go_interface.hpp +++ b/include/utempl/go_interface.hpp @@ -42,7 +42,7 @@ inline constexpr auto Transform(Transformer&& transformer, From&& from) { }(std::make_index_sequence>()); }; -} // impl +} // namespace impl struct DefaultFieldTransformer { inline constexpr auto operator()(auto&& arg) -> auto&& { diff --git a/include/utempl/menu.hpp b/include/utempl/menu.hpp index 8e707da..1eac333 100644 --- a/include/utempl/menu.hpp +++ b/include/utempl/menu.hpp @@ -87,7 +87,7 @@ struct Menu { return Menu>{.functionStorage = this->functionStorage + Tuple(std::forward(f))}; }; template - inline auto Run() const { + inline constexpr auto Run(std::istream& in = std::cin, std::FILE* out = stdout) const { using Cleared = std::remove_cvref_t; constexpr auto maxSize = Cleared::GetMaxSize(); [&](std::index_sequence){ @@ -116,10 +116,10 @@ struct Menu { return ConstexprString(data); }(Wrapper{}) + ...) + enter; - std::fwrite(message.begin(), 1, message.size(), stdout); - std::fflush(stdout); + std::fwrite(message.begin(), 1, message.size(), out); + std::fflush(out); std::string input; - std::getline(std::cin, input); + std::getline(in, input); ([&](storage)>(Wrapper) { if constexpr(message.need) { if(*message.need == input) { diff --git a/tests/go_interface.cpp b/tests/go_interface.cpp index b1f0436..abb5440 100644 --- a/tests/go_interface.cpp +++ b/tests/go_interface.cpp @@ -1,6 +1,8 @@ #include #include +namespace utempl { + struct SomeInterface { int field; }; @@ -17,14 +19,16 @@ struct SomeStruct2 { }; TEST(GoInterface, Basic) { - utempl::GoInterface obj(SomeStruct{1}); + GoInterface obj(SomeStruct{1}); EXPECT_EQ(obj.field, 1); }; TEST(GoInterface, Equal) { - utempl::GoInterface obj(SomeStruct{1}); - EXPECT_EQ(obj, utempl::GoInterface{SomeInterface{1}}); + GoInterface obj(SomeStruct{1}); + EXPECT_EQ(obj, GoInterface{SomeInterface{1}}); EXPECT_EQ(obj, SomeInterface{1}); EXPECT_NE(obj, SomeStruct{1}); EXPECT_EQ(obj, SomeStruct2{1}); }; +} // namespace utempl + diff --git a/tests/menu.cpp b/tests/menu.cpp new file mode 100644 index 0000000..31c078f --- /dev/null +++ b/tests/menu.cpp @@ -0,0 +1,21 @@ +#include +#include + +namespace utempl { + +TEST(Menu, Basic) { + testing::internal::CaptureStdout(); + std::istringstream stream("t\n"); + int value = 0; + menu::Menu{} + .With<{"t", "This is t"}>([&]{ + std::cout << "Success!" << std::endl; + value = 1; + }) + .Run<"[{0}]{2} - ({1})\n">(stream); + auto captured = testing::internal::GetCapturedStdout(); + EXPECT_EQ(captured, "[t] - (This is t)\n|> Success!\n"); + EXPECT_EQ(value, 1); +}; + +} // namespace utempl diff --git a/tests/overloaded.cpp b/tests/overloaded.cpp index 93e0f87..26b0f7a 100644 --- a/tests/overloaded.cpp +++ b/tests/overloaded.cpp @@ -4,7 +4,7 @@ namespace utempl { TEST(Overloaded, Basic) { - constexpr auto f = utempl::Overloaded([](int){ + constexpr auto f = Overloaded([](int){ return 1; }, [](auto&&){ return 2;