Add Tests, Add auto operator== in GoInterface

This commit is contained in:
sha512sum 2024-02-27 21:27:05 +00:00
parent 3683200ebf
commit dcd081e6d3
4 changed files with 65 additions and 4 deletions

View file

@ -2,10 +2,20 @@ cmake_minimum_required(VERSION 3.27)
project(utempl) project(utempl)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(fmt REQUIRED) find_package(fmt REQUIRED)
find_package(Boost 1.84.0 REQUIRED)
set(CMAKE_CXX_STANDART 20) set(CMAKE_CXX_STANDART 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CXX_EXTENSIONS NO) set(CXX_EXTENSIONS NO)
FIND_PACKAGE(Boost 1.84.0 REQUIRED)
add_library(utempl INTERFACE) add_library(utempl INTERFACE)
target_link_libraries(utempl INTERFACE fmt::fmt-header-only ${Boost_LIBRARIES}) target_link_libraries(utempl INTERFACE fmt::fmt-header-only ${Boost_LIBRARIES})
if(ENABLE_TESTS)
find_package(GTest REQUIRED)
enable_testing()
file(GLOB SOURCES tests/* tests/*/* tests/*/*/*)
add_executable(utempl_tests ${SOURCES})
target_link_libraries(utempl_tests GTest::gtest_main utempl)
set_property(TARGET utempl_tests PROPERTY CXX_STANDARD 20)
include(GoogleTest)
gtest_discover_tests(utempl_tests)
endif()
target_include_directories(utempl INTERFACE include) target_include_directories(utempl INTERFACE include)

View file

@ -68,9 +68,22 @@ struct GoInterface : Value {
Value(impl::Transform<Value>(Transformer{}, std::forward<T>(value))) {}; Value(impl::Transform<Value>(Transformer{}, std::forward<T>(value))) {};
inline constexpr auto operator=(const GoInterface&) -> GoInterface& = default; inline constexpr auto operator=(const GoInterface&) -> GoInterface& = default;
inline constexpr auto operator=(GoInterface&&) -> GoInterface& = default; inline constexpr auto operator=(GoInterface&&) -> GoInterface& = default;
inline constexpr auto operator==(const GoInterface& other) const -> bool inline constexpr auto operator==(const Value& other) const -> bool
requires requires {static_cast<const Value&>(*this) == static_cast<const Value&>(other);} { requires requires(const Value& a){a == a;} {
return static_cast<const Value&>(*this) == static_cast<const Value&>(other); return static_cast<const Value&>(*this) == other;
};
inline constexpr auto operator==(const GoInterface& other) const -> bool {
return *this == static_cast<const Value&>(other);
};
template <typename T>
inline constexpr auto operator==(T&& other) const -> bool {
return [&]<auto... Is>(std::index_sequence<Is...>){
using Type = std::remove_cvref_t<T>;
Transformer transformer;
// + 1 - NULL Terminator
return ((boost::pfr::get<Is>(static_cast<const Value&>(*this)) ==
transformer(impl::TryGet<ConstexprString<boost::pfr::get_name<Is, Type>().size() + 1>{boost::pfr::get_name<Is, Type>().begin()}>(other))) && ...);
}(std::make_index_sequence<boost::pfr::tuple_size_v<Value>>());
}; };
}; };

22
tests/go_interface.cpp Normal file
View file

@ -0,0 +1,22 @@
#include <gtest/gtest.h>
#include <utempl/go_interface.hpp>
struct SomeInterface {
int field;
};
struct SomeStruct {
int field;
};
TEST(GoInterface, Basic) {
utempl::GoInterface<SomeInterface> obj(SomeStruct{1});
EXPECT_EQ(obj.field, 1);
};
TEST(GoInterface, Equal) {
utempl::GoInterface<SomeInterface> obj(SomeStruct{1});
EXPECT_EQ(obj, utempl::GoInterface{SomeInterface{1}});
EXPECT_EQ(obj, SomeInterface{1});
EXPECT_EQ(obj, SomeStruct{1});
};

16
tests/overloaded.cpp Normal file
View file

@ -0,0 +1,16 @@
#include <gtest/gtest.h>
#include <utempl/overloaded.hpp>
namespace utempl {
TEST(Overloaded, Basic) {
constexpr auto f = utempl::Overloaded([](int){
return 1;
}, [](auto&&){
return 2;
});
EXPECT_EQ(f(1), 1);
EXPECT_EQ(f(""), 2);
};
} // namespace utempl