I want to check that other classes meet the same specifications as my base class but, I don't want to repeat the same code. A solution that I am thinking of is passing a class as an argument to a test case and I'd be able to invoke the same methods but it doesn't seem possible.
Here is an example of what I have. It repeats the same code.
#include <iostream>
#include <catch2/catch_test_macros.hpp>
using namespace std;
class Base
{
public:
Base() = default;
virtual ~Base() = default;
virtual string do_something() {
return "Base";
}
};
class A : Base
{
public:
A() = default;
string do_something() override {
return "A";
}
};
class B : Base
{
public:
B() = default;
string do_something() override {
return "B";
}
};
TEST_CASE("some test")
{
/* THIS IS THE SPEC THAT DERIVED CLASSES SHOULD MEET.
SECTION("Base class"){
Base base_obj;
auto result = base_obj.do_something();
REQUIRE(result != "");
}
*/
SECTION("Dervied class A"){
A a_obj;
auto result = a_obj.do_something();
REQUIRE(result != "");
}
SECTION("Dervied class B"){
B b_obj;
auto result = b_obj.do_something();
REQUIRE(result != "");
}
}
REQUIRE
and SECTION
live in any function. This allows for a much cleaner solution using catch2 built in functionality.
void test_lsp(Base &obj)
{
SECTION("...") {}
SECTION("...") {}
}
TEST_CASE("some test")
{
Derived obj;
test_lsp(obj);
}