#include <gtest/gtest.h>
#include <concepts>
#include <string>
template <typename T>
concept Animal = requires(T a) {
{ a.eats() } -> std::same_as<std::string>;
};
struct GrassEater {
std::string eats() const { return "grass"; }
};
struct Cow : public GrassEater {};
struct Duck : public GrassEater {};
template <Animal T>
class GrassEaterTest : public ::testing::Test {
public:
T grass_eater;
};
typedef ::testing::Types<Cow, Duck> GrassEaterTypes;
TYPED_TEST_SUITE(GrassEaterTest, GrassEaterTypes);
TYPED_TEST(GrassEaterTest, EatsGrass) {
EXPECT_EQ(this->grass_eater.eats(), "grass");
}
$ apt install libgtest-dev
$ clang++ main.cpp --std=c++23 -lgtest -lgtest_main -pthread
$ ./a.out
[==========] Running 2 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 1 test from GrassEaterTest/0, where TypeParam = Cow
[ RUN ] GrassEaterTest/0.EatsGrass
[ OK ] GrassEaterTest/0.EatsGrass (0 ms)
[----------] 1 test from GrassEaterTest/0 (0 ms total)
[----------] 1 test from GrassEaterTest/1, where TypeParam = Duck
[ RUN ] GrassEaterTest/1.EatsGrass
[ OK ] GrassEaterTest/1.EatsGrass (0 ms)
[----------] 1 test from GrassEaterTest/1 (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 2 test suites ran. (0 ms total)
[ PASSED ] 2 tests.