diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-11-12 09:48:17 -0300 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-11-18 09:22:50 -0300 |
commit | 80282277d0845a65739c33675aa9a91b10faa4b1 (patch) | |
tree | f4dd29f1e781aedae883e9e96e958fd213072b98 /tests | |
parent | 75988e117f22d6bd228dc3fa54887bfcf3773b00 (diff) |
improved multiple inheritance tests with a myriad of new cases
Diffstat (limited to 'tests')
-rw-r--r-- | tests/libsample/multiple_derived.cpp | 37 | ||||
-rw-r--r-- | tests/libsample/multiple_derived.h | 152 | ||||
-rw-r--r-- | tests/samplebinding/CMakeLists.txt | 15 | ||||
-rwxr-xr-x | tests/samplebinding/multiple_derived_test.py | 82 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 15 |
5 files changed, 220 insertions, 81 deletions
diff --git a/tests/libsample/multiple_derived.cpp b/tests/libsample/multiple_derived.cpp index c6ce9f8a8..e8e4c4c3d 100644 --- a/tests/libsample/multiple_derived.cpp +++ b/tests/libsample/multiple_derived.cpp @@ -34,52 +34,37 @@ #include "multiple_derived.h" -MDerived::MDerived() +MDerived1::MDerived1() : m_value(100) { } -MDerived::~MDerived() +MDerived2::MDerived2() : m_value(200) { } -const char* -MDerived::name() +MDerived3::MDerived3() : m_value(3000) { - return "MDerived"; } -// Base2 methods -const char* -MDerived::funcName() +MDerived4::MDerived4() { - return "MDerived.funcName"; } -MBase1* -MDerived::castToMBase1() +MDerived5::MDerived5() { - MBase1* ptr = (MBase1*) this; - return ptr; -} - -MBase2* -MDerived::castToMBase2() -{ - MBase2* ptr = (MBase2*) this; - return ptr; } -MDerived* -MDerived::transformFromBase1(MBase1* self) +MDerived1* +MDerived1::transformFromBase1(Base1* self) { - MDerived* ptr = dynamic_cast<MDerived*>(self); + MDerived1* ptr = dynamic_cast<MDerived1*>(self); return ptr; } -MDerived* -MDerived::transformFromBase2(MBase2* self) +MDerived1* +MDerived1::transformFromBase2(Base2* self) { - MDerived* ptr = dynamic_cast<MDerived*>(self); + MDerived1* ptr = dynamic_cast<MDerived1*>(self); return ptr; } diff --git a/tests/libsample/multiple_derived.h b/tests/libsample/multiple_derived.h index 531cdb28d..a798cc566 100644 --- a/tests/libsample/multiple_derived.h +++ b/tests/libsample/multiple_derived.h @@ -35,37 +35,155 @@ #ifndef MDERIVED_H #define MDERIVED_H -class MBase1 +class Base1 { public: - ~MBase1() {} - virtual const char* name() { return "MBase"; } + Base1() : m_value(1) {} + ~Base1() {} + virtual int base1Method() { return m_value; } +private: + int m_value; }; -class MBase2 +class Base2 { public: - ~MBase2() {} - virtual const char* funcName() { return "MBase2.funcName"; } + Base2() : m_value(2) {} + ~Base2() {} + virtual int base2Method() { return m_value; } +private: + int m_value; }; -class MDerived : public MBase1, public MBase2 +class MDerived1 : public Base1, public Base2 { public: - MDerived(); - virtual ~MDerived(); + MDerived1(); + virtual ~MDerived1() {} - // MBase1 methods - const char* name(); + virtual int mderived1Method() { return m_value; } + virtual int base1Method() { return Base1::base1Method() * 10; } + virtual int base2Method() { return Base2::base2Method() * 10; } - // MBase2 methods - const char* funcName(); + Base1* castToBase1() { return (Base1*) this; } + Base2* castToBase2() { return (Base2*) this; } - MBase1* castToMBase1(); - MBase2* castToMBase2(); + static MDerived1* transformFromBase1(Base1 *self); + static MDerived1* transformFromBase2(Base2 *self); - static MDerived* transformFromBase1(MBase1 *self); - static MDerived* transformFromBase2(MBase2 *self); +private: + int m_value; }; + +class SonOfMDerived1 : public MDerived1 +{ +public: + SonOfMDerived1() : m_value(0) {} + ~SonOfMDerived1() {} + int sonOfMDerived1Method() { return m_value; } +private: + int m_value; +}; + +class Base3 +{ +public: + explicit Base3(int val = 3) : m_value(val) {} + ~Base3() {} + int base3Method() { return m_value; } +private: + int m_value; +}; + +class Base4 +{ +public: + Base4() : m_value(4) {} + ~Base4() {} + int base4Method() { return m_value; } +private: + int m_value; +}; + +class Base5 +{ +public: + Base5() : m_value(5) {} + ~Base5() {} + virtual int base5Method() { return m_value; } +private: + int m_value; +}; + +class Base6 +{ +public: + Base6() : m_value(6) {} + ~Base6() {} + virtual int base6Method() { return m_value; } +private: + int m_value; +}; + + +class MDerived2 : public Base3, public Base4, public Base5, public Base6 +{ +public: + MDerived2(); + virtual ~MDerived2() {} + + int base4Method() { return Base3::base3Method() * 10; } + int mderived2Method() { return m_value; } + + Base3* castToBase3() { return (Base3*) this; } + Base4* castToBase4() { return (Base4*) this; } + Base5* castToBase5() { return (Base5*) this; } + Base6* castToBase6() { return (Base6*) this; } + +private: + int m_value; +}; + +class MDerived3 : public MDerived1, public MDerived2 +{ +public: + MDerived3(); + virtual ~MDerived3() {} + + virtual int mderived3Method() { return m_value; } + + MDerived1* castToMDerived1() { return (MDerived1*) this; } + MDerived2* castToMDerived2() { return (MDerived2*) this; } + +private: + int m_value; +}; + +class MDerived4 : public Base3, public Base4 +{ +public: + MDerived4(); + ~MDerived4() {} + + int mderived4Method() { return 0; } + + Base3* castToBase3() { return (Base3*) this; } + Base4* castToBase4() { return (Base4*) this; } +private: + int m_value; +}; + +class MDerived5 : public Base3, public Base4 +{ +public: + MDerived5(); + ~MDerived5() {} + + virtual int mderived5Method() { return 0; } + + Base3* castToBase3() { return (Base3*) this; } + Base4* castToBase4() { return (Base4*) this; } +}; + #endif // MDERIVED_H diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt index 9ba100523..7685d9b47 100644 --- a/tests/samplebinding/CMakeLists.txt +++ b/tests/samplebinding/CMakeLists.txt @@ -7,6 +7,12 @@ ${CMAKE_CURRENT_SOURCE_DIR}/typesystem_sample.xml set(sample_SRC ${CMAKE_CURRENT_BINARY_DIR}/sample/abstractmodifications_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/abstract_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base1_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base2_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base3_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base4_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base5_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/base6_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/collector_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/derived_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/echo_wrapper.cpp @@ -17,9 +23,11 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/injectcode_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/kindergarten_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/listuser_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/mapuser_wrapper.cpp -${CMAKE_CURRENT_BINARY_DIR}/sample/mbase1_wrapper.cpp -${CMAKE_CURRENT_BINARY_DIR}/sample/mbase2_wrapper.cpp -${CMAKE_CURRENT_BINARY_DIR}/sample/mderived_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived1_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived2_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived3_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived4_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mderived5_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/modifications_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/nondefaultctor_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/objecttype_wrapper.cpp @@ -33,6 +41,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/sample_module_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/samplenamespace_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/simplefile_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/size_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/sonofmderived1_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/str_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/time_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/virtualmethods_wrapper.cpp diff --git a/tests/samplebinding/multiple_derived_test.py b/tests/samplebinding/multiple_derived_test.py index 473cf8da0..f42ede30f 100755 --- a/tests/samplebinding/multiple_derived_test.py +++ b/tests/samplebinding/multiple_derived_test.py @@ -29,55 +29,73 @@ import sys import unittest -from sample import MBase1, MBase2, MDerived +from sample import Base1, Base2, Base3, Base4, Base5, MDerived1, MDerived2, MDerived3 class MultipleDerivedTest(unittest.TestCase): '''Test cases for multiple inheritance''' def testIsInstance(self): - '''MDerived is instance of its parents MBase1 and MBase2.''' - a = MDerived() - self.assert_(isinstance(a, MDerived)) - self.assert_(isinstance(a, MBase1)) - self.assert_(isinstance(a, MBase2)) + '''MDerived1 is instance of its parents Base1 and Base2.''' + a = MDerived1() + self.assert_(isinstance(a, MDerived1)) + self.assert_(isinstance(a, Base1)) + self.assert_(isinstance(a, Base2)) def testIsSubclass(self): - '''MDerived is subclass of its parents MBase1 and MBase2.''' - self.assert_(issubclass(MDerived, MBase1)) - self.assert_(issubclass(MDerived, MBase2)) + '''MDerived1 is subclass of its parents Base1 and Base2.''' + self.assert_(issubclass(MDerived1, Base1)) + self.assert_(issubclass(MDerived1, Base2)) - def testCallToFunctionWithMBase1ArgumentThatCastsBackToMDerived(self): - '''MDerived is passed as an MBase1 argument to method that returns it casted back to MDerived.''' - a = MDerived() - b = MDerived.transformFromBase1(a) + def testCallToFunctionWithBase1ArgumentThatCastsBackToMDerived1(self): + '''MDerived1 is passed as an Base1 argument to method that returns it casted back to MDerived1.''' + a = MDerived1() + b = MDerived1.transformFromBase1(a) self.assertEqual(a, b) - def testCallToFunctionWithMBase2ArgumentThatCastsBackToMDerived(self): - '''MDerived is passed as an MBase2 argument to method that returns it casted back to MDerived.''' - a = MDerived() - b = MDerived.transformFromBase2(a) + def testCallToFunctionWithBase2ArgumentThatCastsBackToMDerived1(self): + '''MDerived1 is passed as an Base2 argument to method that returns it casted back to MDerived1.''' + a = MDerived1() + b = MDerived1.transformFromBase2(a) self.assertEqual(a, b) - def testCastFromMDerivedToMBase1(self): - '''MDerived is casted by C++ to its first parent MBase2 and the binding must return the MDerived wrapper.''' - a = MDerived() + def testCastFromMDerived1ToBase1(self): + '''MDerived1 is casted by C++ to its first parent Base2 and the binding must return the MDerived1 wrapper.''' + a = MDerived1() refcnt = sys.getrefcount(a) - b = a.castToMBase1() - self.assert_(isinstance(b, MDerived)) + b = a.castToBase1() + self.assert_(isinstance(b, MDerived1)) self.assertEqual(a, b) self.assertEqual(sys.getrefcount(a), refcnt + 1) - """ - # This method must be commented since it will break the test flow until the problem is fixed. - def testCastFromMDerivedToMBase2(self): - '''MDerived is casted by C++ to its second parent MBase2 and the binding must return the MDerived wrapper.''' - a = MDerived() + def testCastFromMDerived1ToBases(self): + '''MDerived1 is casted by C++ to its parents and the binding must return the MDerived1 wrapper.''' + a = MDerived1() refcnt = sys.getrefcount(a) - b = a.castToMBase2() - self.assert_(isinstance(b, MDerived)) - self.assertEqual(a, b) - self.assertEqual(sys.getrefcount(a), refcnt + 1) - """ + b1 = a.castToBase1() + b2 = a.castToBase2() + self.assert_(isinstance(b1, MDerived1)) + self.assert_(isinstance(b2, MDerived1)) + self.assertEqual(a, b1) + self.assertEqual(a, b2) + self.assertEqual(sys.getrefcount(a), refcnt + 2) + + def testCastFromMDerived2ToBases(self): + '''MDerived2 is casted by C++ to its parents and the binding must return the MDerived2 wrapper.''' + a = MDerived2() + refcnt = sys.getrefcount(a) + b3 = a.castToBase3() + b4 = a.castToBase4() + b5 = a.castToBase5() + b6 = a.castToBase6() + self.assert_(isinstance(b3, MDerived2)) + self.assert_(isinstance(b4, MDerived2)) + self.assert_(isinstance(b5, MDerived2)) + self.assert_(isinstance(b6, MDerived2)) + self.assertEqual(a, b3) + self.assertEqual(a, b4) + self.assertEqual(a, b5) + self.assertEqual(a, b6) + self.assertEqual(sys.getrefcount(a), refcnt + 4) if __name__ == '__main__': unittest.main() diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index b999aa500..582d87e32 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -408,9 +408,18 @@ <object-type name="PrivateDtor" /> - <interface-type name="MBase1"/> - <object-type name="MBase2"/> - <object-type name="MDerived"/> + <object-type name="Base1"/> + <interface-type name="Base2"/> + <object-type name="Base3"/> + <interface-type name="Base4"/> + <interface-type name="Base5"/> + <interface-type name="Base6"/> + <object-type name="MDerived1"/> + <object-type name="MDerived2"/> + <object-type name="MDerived3"/> + <object-type name="MDerived4"/> + <object-type name="MDerived5"/> + <object-type name="SonOfMDerived1"/> <value-type name="Echo"> <add-function signature="echo(const char *)" return-type="PyObject*"> |