diff --git a/demo.cpp b/demo.cpp index 71f4576..192d656 100644 --- a/demo.cpp +++ b/demo.cpp @@ -114,3 +114,29 @@ void Four::three_method() assert(check4_ == 0xbadfeed); } +void Base1::method1() +{ + std::cout << "One::One" << std::endl; + assert(check1_ == 12345678); +} + +void Base2::method2() +{ + std::cout << "Two::Two" << std::endl; + assert(check2_ == 87654321); +} + +void Multiple::method1() +{ + std::cout << "Multiple::One" << std::endl; + assert(check1_ == 12345678); + assert(check2_ == 87654321); +} + +void Multiple::method2() +{ + std::cout << "Multiple::Two" << std::endl; + assert(check1_ == 12345678); + assert(check2_ == 87654321); +} + diff --git a/demo.h b/demo.h index d4844bb..b4a5d58 100644 --- a/demo.h +++ b/demo.h @@ -64,4 +64,31 @@ protected: long check4_ = 0xbadfeed; }; +class Base1 +{ +public: + virtual ~Base1() = default; + virtual void method1(); + +protected: + long check1_ = 12345678; +}; + +class Base2 +{ +public: + virtual ~Base2() = default; + virtual void method2(); + +protected: + long check2_ = 87654321; +}; + +class Multiple : public Base1, public Base2 +{ +public: + virtual void method1() override; + virtual void method2() override; +}; + #endif diff --git a/example7.cpp b/example7.cpp index 99c8d82..c276d05 100644 --- a/example7.cpp +++ b/example7.cpp @@ -75,9 +75,36 @@ void three_method4(Four* four) assert(four->check4_ == 0xbadfeed); } +void Base1__method1(Base1* base1) +{ + std::cout << "1::1" << std::endl; + assert(base1->check1_ == 12345678); +} + +void Base2__method2(Base2* base2) +{ + std::cout << "2::2" << std::endl; + assert(base2->check2_ == 87654321); +} + +void Multiple__method1(Multiple* multiple) +{ + std::cout << "M::1" << std::endl; + assert(multiple->check1_ == 12345678); + assert(multiple->check2_ == 87654321); +} + +void Multiple__method2(Multiple* multiple) +{ + std::cout << "M::2" << std::endl; + assert(multiple->check1_ == 12345678); + assert(multiple->check2_ == 87654321); +} + int main(int argc, char** argv) { spy::initialise(argc, argv); + // single inheritance test auto method_spy = SPY(&MyClass::virtual_method); MyClass my_object; auto method_this = spy::arg<0>(method_spy); @@ -90,6 +117,23 @@ int main(int argc, char** argv) MyClass* my_heap_object = new MyClass; my_heap_object->virtual_method(); assert(method_this.value(1) == my_heap_object); + // multiple inheritance test + auto bspy1 = SPY(&Base1::method1); + auto bspy2 = SPY(&Base2::method2); + auto mspy1 = SPY(&Multiple::method1); + auto mspy2 = SPY(&Multiple::method2); + auto bfake1 = spy::fake(bspy1, &Base1__method1); + auto bfake2 = spy::fake(bspy2, &Base2__method2); + auto mfake1 = spy::fake(mspy1, &Multiple__method1); + auto mfake2 = spy::fake(mspy2, &Multiple__method2); + Base1* base1 = new Base1; + Base2* base2 = new Base2; + Multiple* multiple = new Multiple; + base1->method1(); + base2->method2(); + multiple->method1(); + multiple->method2(); + // virtual inheritance test auto spy11 = SPY(&One::one_method); auto spy21 = SPY(&Two::one_method); auto spy22 = SPY(&Two::two_method);