Add menu test, fix namespaces

This commit is contained in:
sha512sum 2024-02-27 22:35:29 +00:00
parent 5de7d757a8
commit 72ee630cd2
5 changed files with 34 additions and 9 deletions

View file

@ -42,7 +42,7 @@ inline constexpr auto Transform(Transformer&& transformer, From&& from) {
}(std::make_index_sequence<boost::pfr::tuple_size_v<To>>()); }(std::make_index_sequence<boost::pfr::tuple_size_v<To>>());
}; };
} // impl } // namespace impl
struct DefaultFieldTransformer { struct DefaultFieldTransformer {
inline constexpr auto operator()(auto&& arg) -> auto&& { inline constexpr auto operator()(auto&& arg) -> auto&& {

View file

@ -87,7 +87,7 @@ struct Menu {
return Menu<storage + Tuple{message}, Fs..., std::remove_cvref_t<F>>{.functionStorage = this->functionStorage + Tuple(std::forward<F>(f))}; return Menu<storage + Tuple{message}, Fs..., std::remove_cvref_t<F>>{.functionStorage = this->functionStorage + Tuple(std::forward<F>(f))};
}; };
template <ConstexprString fmt, ConstexprString enter = "|> "> template <ConstexprString fmt, ConstexprString enter = "|> ">
inline auto Run() const { inline constexpr auto Run(std::istream& in = std::cin, std::FILE* out = stdout) const {
using Cleared = std::remove_cvref_t<decltype(*this)>; using Cleared = std::remove_cvref_t<decltype(*this)>;
constexpr auto maxSize = Cleared::GetMaxSize(); constexpr auto maxSize = Cleared::GetMaxSize();
[&]<auto... Is>(std::index_sequence<Is...>){ [&]<auto... Is>(std::index_sequence<Is...>){
@ -116,10 +116,10 @@ struct Menu {
return ConstexprString<size>(data); return ConstexprString<size>(data);
}(Wrapper<Is>{}) + ...) + enter; }(Wrapper<Is>{}) + ...) + enter;
std::fwrite(message.begin(), 1, message.size(), stdout); std::fwrite(message.begin(), 1, message.size(), out);
std::fflush(stdout); std::fflush(out);
std::string input; std::string input;
std::getline(std::cin, input); std::getline(in, input);
([&]<auto I, impl::CallbackMessage message = Get<I>(storage)>(Wrapper<I>) { ([&]<auto I, impl::CallbackMessage message = Get<I>(storage)>(Wrapper<I>) {
if constexpr(message.need) { if constexpr(message.need) {
if(*message.need == input) { if(*message.need == input) {

View file

@ -1,6 +1,8 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <utempl/go_interface.hpp> #include <utempl/go_interface.hpp>
namespace utempl {
struct SomeInterface { struct SomeInterface {
int field; int field;
}; };
@ -17,14 +19,16 @@ struct SomeStruct2 {
}; };
TEST(GoInterface, Basic) { TEST(GoInterface, Basic) {
utempl::GoInterface<SomeInterface> obj(SomeStruct{1}); GoInterface<SomeInterface> obj(SomeStruct{1});
EXPECT_EQ(obj.field, 1); EXPECT_EQ(obj.field, 1);
}; };
TEST(GoInterface, Equal) { TEST(GoInterface, Equal) {
utempl::GoInterface<SomeInterface> obj(SomeStruct{1}); GoInterface<SomeInterface> obj(SomeStruct{1});
EXPECT_EQ(obj, utempl::GoInterface{SomeInterface{1}}); EXPECT_EQ(obj, GoInterface{SomeInterface{1}});
EXPECT_EQ(obj, SomeInterface{1}); EXPECT_EQ(obj, SomeInterface{1});
EXPECT_NE(obj, SomeStruct{1}); EXPECT_NE(obj, SomeStruct{1});
EXPECT_EQ(obj, SomeStruct2{1}); EXPECT_EQ(obj, SomeStruct2{1});
}; };
} // namespace utempl

21
tests/menu.cpp Normal file
View file

@ -0,0 +1,21 @@
#include <gtest/gtest.h>
#include <utempl/menu.hpp>
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

View file

@ -4,7 +4,7 @@
namespace utempl { namespace utempl {
TEST(Overloaded, Basic) { TEST(Overloaded, Basic) {
constexpr auto f = utempl::Overloaded([](int){ constexpr auto f = Overloaded([](int){
return 1; return 1;
}, [](auto&&){ }, [](auto&&){
return 2; return 2;