diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2011-12-29 17:02:26 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:18:36 -0300 |
commit | 7ca708a09839e4a9a9bb2f1fb600bf683d42c1e9 (patch) | |
tree | 44ced148ddfaf0ab7a037961d64cb36101e1ff5d /tests/samplebinding | |
parent | 3991d3b23bcce3f3ac5f468d35a1510a50ec8cba (diff) |
Updated documentation for type converters.
Expanded the Complex type conversion unit test.
Reviewed by Hugo Parente <hugo.lima@openbossa.org>
Reviewed by Paulo Alcantara <pcacjr@gmail.com>
Diffstat (limited to 'tests/samplebinding')
-rw-r--r-- | tests/samplebinding/complex_test.py | 13 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 20 |
2 files changed, 33 insertions, 0 deletions
diff --git a/tests/samplebinding/complex_test.py b/tests/samplebinding/complex_test.py index 84bea25f8..c12f063f3 100644 --- a/tests/samplebinding/complex_test.py +++ b/tests/samplebinding/complex_test.py @@ -61,6 +61,19 @@ class ComplexTest(unittest.TestCase): cpx2 = complex(5.6, 7.8) self.assertEqual(sample.sumComplexPair((cpx1, cpx2)), cpx1 + cpx2) + def testUsingTuples(self): + cpx1, cpx2 = (1.2, 3.4), (5.6, 7.8) + self.assertEqual(sample.sumComplexPair((cpx1, cpx2)), sample.sumComplexPair((complex(*cpx1), complex(*cpx2)))) + cpx1, cpx2 = (1, 3), (5, 7) + self.assertEqual(sample.sumComplexPair((cpx1, cpx2)), sample.sumComplexPair((complex(*cpx1), complex(*cpx2)))) + cpx1, cpx2 = (1.2, 3), (5.6, 7) + self.assertEqual(sample.sumComplexPair((cpx1, cpx2)), sample.sumComplexPair((complex(*cpx1), complex(*cpx2)))) + cpx1, cpx2 = (1, 2, 3), (4, 5, 7) + self.assertRaises(TypeError, sample.sumComplexPair, (cpx1, cpx2)) + cpx1, cpx2 = ('1', '2'), ('4', '5') + self.assertRaises(TypeError, sample.sumComplexPair, (cpx1, cpx2)) + + if __name__ == '__main__': unittest.main() diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 6ab2d154e..0389d8520 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -37,6 +37,19 @@ </conversion-rule> </primitive-type> + <inject-code class="native" position="beginning"> + static bool Check2TupleOfNumbers(PyObject* pyIn) { + if (!PySequence_Check(pyIn) || !(PySequence_Size(pyIn) == 2)) + return false; + Shiboken::AutoDecRef pyReal(PySequence_GetItem(pyIn, 0)); + if (!SbkNumber_Check(pyReal)) + return false; + Shiboken::AutoDecRef pyImag(PySequence_GetItem(pyIn, 1)); + if (!SbkNumber_Check(pyImag)) + return false; + return true; + } + </inject-code> <primitive-type name="Complex" target-lang-api-name="PyComplex"> <include file-name="complex.h" location="global"/> <conversion-rule> @@ -51,6 +64,13 @@ double imag = PyComplex_ImagAsDouble(%in); %out = %OUTTYPE(real, imag); </add-conversion> + <add-conversion type="PySequence" check="Check2TupleOfNumbers(%in)"> + Shiboken::AutoDecRef pyReal(PySequence_GetItem(%in, 0)); + Shiboken::AutoDecRef pyImag(PySequence_GetItem(%in, 1)); + double real = %CONVERTTOCPP[double](pyReal); + double imag = %CONVERTTOCPP[double](pyImag); + %out = %OUTTYPE(real, imag); + </add-conversion> </target-to-native> </conversion-rule> </primitive-type> |