diff options
-rw-r--r-- | tests/libsample/str.cpp | 25 | ||||
-rw-r--r-- | tests/libsample/str.h | 2 | ||||
-rwxr-xr-x | tests/samplebinding/str_test.py | 25 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 21 |
4 files changed, 73 insertions, 0 deletions
diff --git a/tests/libsample/str.cpp b/tests/libsample/str.cpp index bd7076087..87e1145ce 100644 --- a/tests/libsample/str.cpp +++ b/tests/libsample/str.cpp @@ -93,6 +93,31 @@ Str::cstring() const return m_str.c_str(); } +int +Str::toInt(bool* ok, int base) const +{ + bool my_ok; + int result = 0; + istringstream conv(m_str); + switch (base) { + case 8: + conv >> std::oct >> result; + break; + case 10: + conv >> std::dec >> result; + break; + case 16: + conv >> std::hex >> result; + break; + } + my_ok = istringstream::eofbit & conv.rdstate(); + if (!my_ok) + result = 0; + if (ok) + *ok = my_ok; + return result; +} + void Str::show() const { diff --git a/tests/libsample/str.h b/tests/libsample/str.h index 52f46e6ba..1fe27ec8b 100644 --- a/tests/libsample/str.h +++ b/tests/libsample/str.h @@ -54,6 +54,8 @@ public: char get_char(int pos) const; bool set_char(int pos, char ch); + int toInt(bool* ok = 0, int base = 10) const; + void show() const; int size() const { return m_str.size(); } diff --git a/tests/samplebinding/str_test.py b/tests/samplebinding/str_test.py index d0ed8ce86..d8d3a75ca 100755 --- a/tests/samplebinding/str_test.py +++ b/tests/samplebinding/str_test.py @@ -49,6 +49,7 @@ class StrTest(unittest.TestCase): '''Test passing a Python class implicitly convertible to a wrapped class that is expected to be passed as reference.''' s1 = Str('This is %VAR!').arg('Athens') self.assertEqual(str(s1), 'This is Athens!') + def testSequenceOperators(self): s1 = Str("abcdef") self.assertEqual(len(s1), 6); @@ -87,6 +88,30 @@ class StrTest(unittest.TestCase): self.assertEqual(s1+2, "hello2") self.assertEqual(2+s1, "2hello") + def testToIntError(self): + self.assertEqual(Str('Z').toInt(), (0, False)) + + def testToIntWithDecimal(self): + decimal = Str('37') + val, ok = decimal.toInt() + self.assertEqual(type(val), int) + self.assertEqual(type(ok), bool) + self.assertEqual(val, int(str(decimal))) + + def testToIntWithOctal(self): + octal = Str('52') + val, ok = octal.toInt(8) + self.assertEqual(type(val), int) + self.assertEqual(type(ok), bool) + self.assertEqual(val, int(str(octal), 8)) + + def testToIntWithHexadecimal(self): + hexa = Str('2A') + val, ok = hexa.toInt(16) + self.assertEqual(type(val), int) + self.assertEqual(type(ok), bool) + self.assertEqual(val, int(str(hexa), 16)) + self.assertEqual(hexa.toInt(), (0, False)) if __name__ == '__main__': unittest.main() diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 631f5a9ee..dfb96d8c0 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -234,6 +234,13 @@ ); </template> + <template name="boolptr_at_start_and_one_arg_fix_beginning"> + bool __ok__; + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE]( + %CPPSELF.%TYPE::%FUNCTION_NAME(&__ok__, %2) + ); + </template> + <template name="boolptr_fix_end"> PyObject* _tuple_ = PyTuple_New(2); PyTuple_SET_ITEM(_tuple_, 0, %PYARG_0); @@ -765,6 +772,20 @@ return !ok ? -1 : 0; </inject-code> </add-function> + <modify-function signature="toInt(bool*, int)const"> + <modify-argument index="1"> + <remove-argument/> + </modify-argument> + <modify-argument index="return"> + <replace-type modified-type="PyObject*"/> + </modify-argument> + <inject-code class="target" position="beginning"> + <insert-template name="boolptr_at_start_and_one_arg_fix_beginning"/> + </inject-code> + <inject-code class="target" position="end"> + <insert-template name="boolptr_fix_end"/> + </inject-code> + </modify-function> </value-type> <value-type name="StrList"> |