diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2011-11-04 20:01:33 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:18:33 -0300 |
commit | df0ca8b2d02af3673960004e2ea547972ddf2b75 (patch) | |
tree | e0c46186686f93bfd3e3691463a2ce8c808a4f1c /tests/samplebinding | |
parent | f189875436c5024fff6819aa9f2a651f48118599 (diff) |
Added adapter class SpecificConverter to provide type conversion based on a given string.
Also added code to register a couple of type conversions by name,
a bunch of related tests, and some fixes to the converter functions.
Diffstat (limited to 'tests/samplebinding')
-rw-r--r-- | tests/samplebinding/typeconverters_test.py | 70 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 61 |
2 files changed, 126 insertions, 5 deletions
diff --git a/tests/samplebinding/typeconverters_test.py b/tests/samplebinding/typeconverters_test.py index 4261030a6..6b40573eb 100644 --- a/tests/samplebinding/typeconverters_test.py +++ b/tests/samplebinding/typeconverters_test.py @@ -65,15 +65,15 @@ class GetPythonTypeByNameTest(unittest.TestCase): self.assertEqual(pyType, pyTypedef) def testPairContainerType(self): - pyType = sample.getPythonType('std::pair<Complex, int >') + pyType = sample.getPythonType('std::pair<Complex,int>') self.assertEqual(pyType, list) def testListContainerType(self): - pyType = sample.getPythonType('std::list<int >') + pyType = sample.getPythonType('std::list<int>') self.assertEqual(pyType, list) def testMapContainerType(self): - pyType = sample.getPythonType('std::map<std::string, int >') + pyType = sample.getPythonType('std::map<std::string,int>') self.assertEqual(pyType, dict) def testGlobalEnumType(self): @@ -116,8 +116,68 @@ class CheckValueAndObjectTypeByNameTest(unittest.TestCase): self.assertFalse(sample.cppTypeIsObjectType('Complex')) def testContainerType(self): - self.assertFalse(sample.cppTypeIsValueType('std::list<int >')) - self.assertFalse(sample.cppTypeIsObjectType('std::list<int >')) + self.assertFalse(sample.cppTypeIsValueType('std::list<int>')) + self.assertFalse(sample.cppTypeIsObjectType('std::list<int>')) + + +class SpecificConverterTest(unittest.TestCase): + + '''Uses an added function with inject code that uses the libshiboken + adapter class "Shiboken::Conversions::SpecificConverter".''' + + def testNotExistentType(self): + conversion = sample.getConversionTypeString('NotExistentType') + self.assertEqual(conversion, 'Invalid conversion') + + def testObjectType(self): + conversion = sample.getConversionTypeString('ObjectType') + self.assertEqual(conversion, 'Pointer conversion') + conversion = sample.getConversionTypeString('ObjectType*') + self.assertEqual(conversion, 'Pointer conversion') + conversion = sample.getConversionTypeString('ObjectType&') + self.assertEqual(conversion, 'Reference conversion') + + def testValueType(self): + conversion = sample.getConversionTypeString('Point') + self.assertEqual(conversion, 'Copy conversion') + conversion = sample.getConversionTypeString('Point*') + self.assertEqual(conversion, 'Pointer conversion') + conversion = sample.getConversionTypeString('Point&') + self.assertEqual(conversion, 'Reference conversion') + + +class StringBasedConversionTest(unittest.TestCase): + + def testValueType(self): + pts = (sample.Point(1, 1), sample.Point(2, 2), sample.Point(3, 3)) + result = sample.convertValueTypeToCppAndThenToPython(pts[0], pts[1], pts[2]) + for orig, new in zip(pts, result): + self.assertEqual(orig, new) + self.assertFalse(pts[0] is result[0]) + self.assertTrue(pts[1] is result[1]) + self.assertTrue(pts[2] is result[2]) + + def testObjectType(self): + objs = (sample.ObjectType(), sample.ObjectType()) + objs[0].setObjectName('obj0') + objs[1].setObjectName('obj1') + result = sample.convertObjectTypeToCppAndThenToPython(objs[0], objs[1]) + for orig, new in zip(objs, result): + self.assertEqual(orig, new) + self.assertEqual(orig.objectName(), new.objectName()) + self.assertTrue(orig is new) + + def testContainerType(self): + lst = range(4) + result = sample.convertListOfIntegersToCppAndThenToPython(lst) + self.assertTrue(len(result), 1) + self.assertTrue(lst, result[0]) + + def testCppPrimitiveType(self): + integers = (12, 34) + result = sample.convertIntegersToCppAndThenToPython(integers[0], integers[1]) + for orig, new in zip(integers, result): + self.assertEqual(orig, new) if __name__ == '__main__': diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 0c5ee7cdd..b2b795aa8 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -285,6 +285,67 @@ </inject-code> </add-function> + <add-function signature="getConversionTypeString(const char*)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + Shiboken::Conversions::SpecificConverter converter(%1); + const char* %0 = 0; + switch (converter.conversionType()) { + case Shiboken::Conversions::SpecificConverter::CopyConversion: + %0 = "Copy conversion"; + break; + case Shiboken::Conversions::SpecificConverter::PointerConversion: + %0 = "Pointer conversion"; + break; + case Shiboken::Conversions::SpecificConverter::ReferenceConversion: + %0 = "Reference conversion"; + break; + default: + %0 = "Invalid conversion"; + } + %PYARG_0 = %CONVERTTOPYTHON[const char*](%0); + </inject-code> + </add-function> + + <inject-code class="native" position="beginning"> + static PyObject* __convertCppValuesToPython(const char** typeName, void** values, int size) + { + PyObject* result = PyTuple_New(size); + for (int i = 0; i < size; ++i) { + Shiboken::Conversions::SpecificConverter converter(typeName[i]); + PyTuple_SET_ITEM(result, i, converter.toPython(values[i])); + } + return result; + } + </inject-code> + <add-function signature="convertValueTypeToCppAndThenToPython(Point,Point*,Point&)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + const char* typeNames[] = { "Point", "Point*", "Point&" }; + void* values[] = { &%1, &%2, &(%3) }; + %PYARG_0 = __convertCppValuesToPython(typeNames, values, 3); + </inject-code> + </add-function> + <add-function signature="convertObjectTypeToCppAndThenToPython(ObjectType*,ObjectType&)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + const char* typeNames[] = { "ObjectType*", "ObjectType&" }; + void* values[] = { &%1, &(%2) }; + %PYARG_0 = __convertCppValuesToPython(typeNames, values, 2); + </inject-code> + </add-function> + <add-function signature="convertListOfIntegersToCppAndThenToPython(std::list<int>)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + const char* typeNames[] = { "std::list<int>" }; + void* values[] = { &%1 }; + %PYARG_0 = __convertCppValuesToPython(typeNames, values, 1); + </inject-code> + </add-function> + <add-function signature="convertIntegersToCppAndThenToPython(int,int)" return-type="PyObject"> + <inject-code class="target" position="beginning"> + const char* typeNames[] = { "int", "int" }; + void* values[] = { &%1, &%2 }; + %PYARG_0 = __convertCppValuesToPython(typeNames, values, 2); + </inject-code> + </add-function> + <container-type name="std::pair" type="pair"> <include file-name="utility" location="global"/> <conversion-rule file="pair_conversions.h"> |