summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2009-12-29 10:09:58 -0300
committerMarcelo Lira <marcelo.lira@openbossa.org>2009-12-29 15:42:18 -0300
commit9f79c3de8192c4ce5f0ae86687d3ece2196323ba (patch)
tree742281d9a54b01c1e9888b311878c6a8604c57ef /tests
parent731a68999274ab58fb35f3d3238f713fbf91c878 (diff)
downloadshiboken-9f79c3de8192c4ce5f0ae86687d3ece2196323ba.tar.gz
shiboken-9f79c3de8192c4ce5f0ae86687d3ece2196323ba.tar.xz
shiboken-9f79c3de8192c4ce5f0ae86687d3ece2196323ba.zip
Adds test for modified function using type system template.
The new test adds a "toInt(bool* ok = 0, int base = 10)" method to Str class. The modification uses a type system template function that uses the variable "%2" to reference the "base" argument; in the case when the thing is called without parameters "%2" should be replaced by "10" instead of a converted C++ variable name. Reviewed by Hugo Parente <hugo.lima@openbossa.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/libsample/str.cpp25
-rw-r--r--tests/libsample/str.h2
-rwxr-xr-xtests/samplebinding/str_test.py25
-rw-r--r--tests/samplebinding/typesystem_sample.xml21
4 files changed, 73 insertions, 0 deletions
diff --git a/tests/libsample/str.cpp b/tests/libsample/str.cpp
index bd707608..87e1145c 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 52f46e6b..1fe27ec8 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 d0ed8ce8..d8d3a75c 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 631f5a9e..dfb96d8c 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(&amp;__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">