diff options
author | Renato Filho <renato.filho@openbossa.org> | 2011-09-28 17:55:54 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:18:18 -0300 |
commit | b9ab302dd7aff3ecbbbc7ffe1ef2ef0bf4956a5a (patch) | |
tree | b2c62e3ae790b7c5a70f7308282ec2975aa168a2 /tests/samplebinding | |
parent | 6b21c2fa5eeb6a7c7b6533f1c966a47d8ef07e69 (diff) |
Fixed tests to work with python3.x and python2.x
Diffstat (limited to 'tests/samplebinding')
20 files changed, 91 insertions, 51 deletions
diff --git a/tests/samplebinding/bug_704_test.py b/tests/samplebinding/bug_704_test.py index f2b978c52..214c0dda5 100644 --- a/tests/samplebinding/bug_704_test.py +++ b/tests/samplebinding/bug_704_test.py @@ -24,8 +24,8 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA # 02110-1301 USA -import sys import unittest +from py3kcompat import IS_PY3K from sample import ObjectType @@ -53,7 +53,9 @@ class ObjectTypeTest(unittest.TestCase): defineNewStyle() def testObjectTypeOldStype(self): - self.assertRaises(TypeError, defineOldStyle) + # Py 3k doesn't have old style classes + if not IS_PY3K: + self.assertRaises(TypeError, defineOldStyle) if __name__ == '__main__': diff --git a/tests/samplebinding/bytearray_test.py b/tests/samplebinding/bytearray_test.py index 2eb99601f..d2b345989 100644 --- a/tests/samplebinding/bytearray_test.py +++ b/tests/samplebinding/bytearray_test.py @@ -27,6 +27,7 @@ import unittest from os.path import isdir from sample import ByteArray +from py3kcompat import b class ByteArrayBufferProtocolTest(unittest.TestCase): @@ -35,7 +36,7 @@ class ByteArrayBufferProtocolTest(unittest.TestCase): def testByteArrayBufferProtocol(self): # Tests ByteArray implementation of Python buffer protocol using the os.path.isdir # function which an unicode object or other object implementing the Python buffer protocol. - isdir(ByteArray('/tmp')) + isdir(str(ByteArray('/tmp'))) class ByteArrayConcatenationOperatorTest(unittest.TestCase): @@ -90,14 +91,14 @@ class ByteArrayOperatorAt(unittest.TestCase): string = 'abcdefgh' obj = ByteArray(string) for i in range(len(string)): - self.assertEqual(obj[i], string[i]) + self.assertEqual(obj[i], b(string[i])) def testInRangeReverse(self): # ByteArray[x] where x is a valid index (reverse order). string = 'abcdefgh' obj = ByteArray(string) for i in range(len(string)-1, 0, -1): - self.assertEqual(obj[i], string[i]) + self.assertEqual(obj[i], b(string[i])) def testOutOfRange(self): # ByteArray[x] where x is out of index. @@ -107,8 +108,8 @@ class ByteArrayOperatorAt(unittest.TestCase): def testNullStrings(self): ba = ByteArray('\x00') - self.assertEqual(ba.at(0), '\x00') - self.assertEqual(ba[0], '\x00') + self.assertEqual(ba.at(0), b('\x00')) + self.assertEqual(ba[0], b('\x00')) class ByteArrayOperatorLen(unittest.TestCase): @@ -132,7 +133,7 @@ class ByteArrayAndPythonStr(unittest.TestCase): self.assertEqual(ByteArray('aaa').__str__(), 'aaa') def testPythonStrAndNull(self): - s1 = "123\000321" + s1 = b('123\000321') ba = ByteArray(s1) s2 = ba.data() self.assertEqual(s1, s2) diff --git a/tests/samplebinding/duck_punching_test.py b/tests/samplebinding/duck_punching_test.py index b48693f31..aa266da9d 100644 --- a/tests/samplebinding/duck_punching_test.py +++ b/tests/samplebinding/duck_punching_test.py @@ -28,6 +28,7 @@ import types import unittest +from py3kcompat import IS_PY3K from sample import VirtualMethods, SimpleFile, Point diff --git a/tests/samplebinding/enum_test.py b/tests/samplebinding/enum_test.py index f81f8b0a8..fefe75ce3 100644 --- a/tests/samplebinding/enum_test.py +++ b/tests/samplebinding/enum_test.py @@ -70,12 +70,12 @@ class EnumTest(unittest.TestCase): def testBuildingEnumWithDefaultValue(self): '''Enum constructor with default value''' enum = SampleNamespace.Option() - self.assertEqual(enum, SampleNamespace.None) + self.assertEqual(enum, SampleNamespace.None_) def testEnumConversionToAndFromPython(self): '''Conversion of enum objects from Python to C++ back again.''' enumout = SampleNamespace.enumInEnumOut(SampleNamespace.TwoIn) - self.assert_(enumout, SampleNamespace.TwoOut) + self.assertTrue(enumout, SampleNamespace.TwoOut) self.assertEqual(repr(enumout), repr(SampleNamespace.TwoOut)) def testEnumConstructorWithTooManyParameters(self): @@ -105,7 +105,13 @@ class EnumTest(unittest.TestCase): def testEnumTpPrintImplementation(self): '''Without SbkEnum.tp_print 'print' returns the enum represented as an int.''' tmpfile = createTempFile() - print(Event.ANY_EVENT, file=tmpfile) + if IS_PY3K: + from py3k import printToFile + printToFile(tmpfile, Event.ANY_EVENT) + else: + sys.stdout = tmpfile + print(Event.ANY_EVENT) + sys.stdout = sys.__stdout__ tmpfile.seek(0) text = tmpfile.read().strip() tmpfile.close() diff --git a/tests/samplebinding/handle_conversions.h b/tests/samplebinding/handle_conversions.h index a6e561359..6e1093f6b 100644 --- a/tests/samplebinding/handle_conversions.h +++ b/tests/samplebinding/handle_conversions.h @@ -13,6 +13,8 @@ struct Converter<HANDLE> static inline bool isConvertible(PyObject* pyObj) { + if (pyObj == Py_None) + return true; #ifdef IS_PY3K return PyCapsule_CheckExact(pyObj); #else @@ -28,6 +30,8 @@ struct Converter<HANDLE> static inline PyObject* toPython(HANDLE cppobj) { + if (!cppobj) + Py_RETURN_NONE; #ifdef IS_PY3K return PyCapsule_New(cppobj, 0, 0); #else @@ -37,6 +41,9 @@ struct Converter<HANDLE> static inline HANDLE toCpp(PyObject* pyobj) { + if (pyobj == Py_None) + return (HANDLE) 0; + #ifdef IS_PY3K return (HANDLE) PyCapsule_GetPointer(pyobj, 0); #else diff --git a/tests/samplebinding/handleholder_test.py b/tests/samplebinding/handleholder_test.py index e1969dff3..65f3d0cce 100644 --- a/tests/samplebinding/handleholder_test.py +++ b/tests/samplebinding/handleholder_test.py @@ -35,13 +35,12 @@ class HandleHolderTest(unittest.TestCase): def testCreation(self): holder = HandleHolder(HandleHolder.createHandle()) holder2 = HandleHolder(HandleHolder.createHandle()) - self.assertEquals(holder.compare(holder2), False) + self.assertEqual(holder.compare(holder2), False) def testTransfer(self): holder = HandleHolder() holder2 = HandleHolder(holder.get()) - self.assert_(holder.compare(holder2)) - + self.assertTrue(holder.compare(holder2)) if __name__ == '__main__': unittest.main() diff --git a/tests/samplebinding/implicitconv_numerical_test.py b/tests/samplebinding/implicitconv_numerical_test.py index 06a7c2c27..c5c082ccb 100644 --- a/tests/samplebinding/implicitconv_numerical_test.py +++ b/tests/samplebinding/implicitconv_numerical_test.py @@ -29,6 +29,10 @@ import unittest import sys import sample +from py3kcompat import IS_PY3K, l, long + +if IS_PY3K: + sys.maxint = sys.maxsize class NumericTester(unittest.TestCase): @@ -107,27 +111,27 @@ class LongImplicitConvert(NumericTester): def testLongAsInt(self): '''Long as Int''' - self.check_value(24224l, 24224, sample.acceptInt, int) + self.check_value(l(24224), 24224, sample.acceptInt, int) self.assertRaises(OverflowError, sample.acceptInt, sys.maxint + 20) def testLongAsLong(self): '''Long as Long''' - self.check_value(2405l, 2405, sample.acceptLong, int) + self.check_value(l(2405), 2405, sample.acceptLong, int) self.assertRaises(OverflowError, sample.acceptLong, sys.maxint + 20) def testLongAsUInt(self): '''Long as unsigned Int''' - self.check_value(260l, 260, sample.acceptUInt, long) + self.check_value(l(260), 260, sample.acceptUInt, long) self.assertRaises(OverflowError, sample.acceptUInt, -42) def testLongAsULong(self): '''Long as unsigned Long''' - self.check_value(128l, 128, sample.acceptULong, long) - self.assertRaises(OverflowError, sample.acceptULong, -334l) + self.check_value(l(128), 128, sample.acceptULong, long) + self.assertRaises(OverflowError, sample.acceptULong, l(-334)) def testLongAsDouble(self): '''Float as double''' - self.check_value(42l, 42, sample.acceptDouble, float) + self.check_value(l(42), 42, sample.acceptDouble, float) if __name__ == '__main__': diff --git a/tests/samplebinding/invalid_virtual_return_test.py b/tests/samplebinding/invalid_virtual_return_test.py index bf0a6e1f6..ff6c85d3c 100644 --- a/tests/samplebinding/invalid_virtual_return_test.py +++ b/tests/samplebinding/invalid_virtual_return_test.py @@ -30,7 +30,6 @@ import unittest from sample import ObjectModel, ObjectType, ObjectView import warnings -warnings.simplefilter('error') class MyObject(ObjectType): @@ -44,6 +43,7 @@ class ListModelWrong(ObjectModel): self.obj = 0 def data(self): + warnings.simplefilter('error') # Shouldn't segfault. Must set TypeError return self.obj diff --git a/tests/samplebinding/namedarg_test.py b/tests/samplebinding/namedarg_test.py index f510b372a..20f1908b6 100644 --- a/tests/samplebinding/namedarg_test.py +++ b/tests/samplebinding/namedarg_test.py @@ -30,14 +30,23 @@ import unittest from sample import Echo +from py3kcompat import IS_PY3K class TestNamedArg(unittest.TestCase): def testRegularCall(self): + # Python 3 support unicode as string + if IS_PY3K: + return + echo = Echo() self.assertRaises(TypeError, echo.methodWithNamedArg, unicode('foo')) def testNamedArgumentCall(self): + # Python 3 support unicode as string + if IS_PY3K: + return + echo = Echo() self.assertRaises(TypeError, echo.methodWithNamedArg, string=unicode('foo')) diff --git a/tests/samplebinding/namespace_test.py b/tests/samplebinding/namespace_test.py index 703333e0f..310f64ade 100644 --- a/tests/samplebinding/namespace_test.py +++ b/tests/samplebinding/namespace_test.py @@ -28,6 +28,12 @@ import unittest from sample import * +from py3kcompat import IS_PY3K + +if IS_PY3K: + TYPE_STR = "class" +else: + TYPE_STR = "type" class TestEnumUnderNamespace(unittest.TestCase): def testInvisibleNamespace(self): @@ -46,9 +52,9 @@ class TestClassesUnderNamespace(unittest.TestCase): self.assertEquals(res, 4) def testTpNames(self): - self.assertEquals(str(SampleNamespace.SomeClass), "<type 'sample.SampleNamespace.SomeClass'>") - self.assertEquals(str(SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough), "<type 'sample.SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough'>") - self.assertEquals(str(SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough.NiceEnum), "<type 'sample.SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough.NiceEnum'>") + self.assertEquals(str(SampleNamespace.SomeClass), "<%s 'sample.SampleNamespace.SomeClass'>"%TYPE_STR) + self.assertEquals(str(SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough), "<%s 'sample.SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough'>"%TYPE_STR) + self.assertEquals(str(SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough.NiceEnum), "<%s 'sample.SampleNamespace.SomeClass.SomeInnerClass.OkThisIsRecursiveEnough.NiceEnum'>"%TYPE_STR) if __name__ == '__main__': unittest.main() diff --git a/tests/samplebinding/oldstyleclass_as_number_test.py b/tests/samplebinding/oldstyleclass_as_number_test.py index 4e4be3342..4045bac8d 100644 --- a/tests/samplebinding/oldstyleclass_as_number_test.py +++ b/tests/samplebinding/oldstyleclass_as_number_test.py @@ -26,6 +26,7 @@ import unittest import sample +from py3kcompat import IS_PY3K class OldStyle: pass @@ -71,6 +72,6 @@ class TestOldStyleClassAsNumber(unittest.TestCase): obj = NewStyleNumber(123) self.assertEqual(sample.acceptInt(obj), obj.value) -if __name__ == "__main__": +if __name__ == "__main__" and not IS_PY3K: unittest.main() diff --git a/tests/samplebinding/overloadwithdefault_test.py b/tests/samplebinding/overloadwithdefault_test.py index d7d23c50d..5c418d8b9 100644 --- a/tests/samplebinding/overloadwithdefault_test.py +++ b/tests/samplebinding/overloadwithdefault_test.py @@ -27,6 +27,7 @@ import unittest from sample import Overload, Str +from py3kcompat import b class OverloadTest(unittest.TestCase): @@ -47,11 +48,11 @@ class OverloadTest(unittest.TestCase): def testStringArgumentAsBuffer(self): overload = Overload() - self.assertEqual(overload.strBufferOverloads('', 0), Overload.Function1) + self.assertEqual(overload.strBufferOverloads(b'', 0), Overload.Function1) def testBufferArgument(self): overload = Overload() - self.assertEqual(overload.strBufferOverloads(buffer(''), 0), Overload.Function1) + self.assertEqual(overload.strBufferOverloads(b(''), 0), Overload.Function1) if __name__ == '__main__': unittest.main() diff --git a/tests/samplebinding/ownership_delete_child_in_python_test.py b/tests/samplebinding/ownership_delete_child_in_python_test.py index cb8637b40..9aa1f9aba 100644 --- a/tests/samplebinding/ownership_delete_child_in_python_test.py +++ b/tests/samplebinding/ownership_delete_child_in_python_test.py @@ -31,6 +31,10 @@ import random import string from sample import ObjectType +from py3kcompat import IS_PY3K + +if IS_PY3K: + string.letters = string.ascii_letters class DeleteChildInPython(unittest.TestCase): diff --git a/tests/samplebinding/point_test.py b/tests/samplebinding/point_test.py index 6d9fc8fca..4c015171f 100644 --- a/tests/samplebinding/point_test.py +++ b/tests/samplebinding/point_test.py @@ -30,6 +30,7 @@ import sys import unittest from sample import Point +from py3kcompat import unicode class PointTest(unittest.TestCase): '''Test case for Point class, including operator overloads.''' @@ -68,7 +69,7 @@ class PointTest(unittest.TestCase): pt2 = pt1.copy() self.assertEqual(pt1, pt2) pt2 += pt1 - self.assert_(not pt1 == pt2) + self.assertFalse(pt1 == pt2) def testReturnConstPointer(self): '''Point returns a const pointer for itself.''' @@ -86,12 +87,12 @@ class PointTest(unittest.TestCase): def testAddedOperator(self): p = Point(0.0, 0.0) - r = p - u'Hi' - self.assertEqual(r, u'Hi') + r = p - unicode('Hi') + self.assertEqual(r, unicode('Hi')) # now the reverse op. - r = u'Hi' - p - self.assertEqual(r, u'Hi') + r = unicode('Hi') - p + self.assertEqual(r, unicode('Hi')) def testModifiedMethod(self): pt1 = Point(0.0, 0.0) diff --git a/tests/samplebinding/referencetopointer_test.py b/tests/samplebinding/referencetopointer_test.py index 88e901e20..2f213b0d4 100644 --- a/tests/samplebinding/referencetopointer_test.py +++ b/tests/samplebinding/referencetopointer_test.py @@ -39,7 +39,6 @@ class ExtendedVirtualMethods(VirtualMethods): ext_text = text if text is not None: ext_text = self.prefix + text - print ext_text return VirtualMethods.createStr(self, ext_text) @@ -71,7 +70,7 @@ class ReferenceToPointerTest(unittest.TestCase): '''Calls createStr method from C++ with a Python string argument.''' obj = VirtualMethods() ok, string = obj.callCreateStr('foo') - self.assert_(ok) + self.assertTrue(ok) self.assertEqual(string, Str('foo')) def testCallReimplementedMethodWithNone(self): diff --git a/tests/samplebinding/sample_test.py b/tests/samplebinding/sample_test.py index 785efaac3..16e652667 100644 --- a/tests/samplebinding/sample_test.py +++ b/tests/samplebinding/sample_test.py @@ -44,18 +44,18 @@ class ModuleTest(unittest.TestCase): 'transmutePointIntoComplex', 'sumComplexPair', 'FirstThing', 'SecondThing', 'ThirdThing', 'GlobalEnum', 'NoThing']) - self.assert_(expected_members.issubset(dir(sample))) + self.assertTrue(expected_members.issubset(dir(sample))) def testAbstractPrintFormatEnum(self): '''Test availability of PrintFormat enum from Abstract class''' enum_members = set(['PrintFormat', 'Short', 'Verbose', 'OnlyId', 'ClassNameAndId']) - self.assert_(enum_members.issubset(dir(sample.Abstract))) + self.assertTrue(enum_members.issubset(dir(sample.Abstract))) def testSampleNamespaceOptionEnum(self): '''Test availability of Option enum from SampleNamespace namespace''' - enum_members = set(['Option', 'None', 'RandomNumber', 'UnixTime']) - self.assert_(enum_members.issubset(dir(sample.SampleNamespace))) + enum_members = set(['Option', 'None_', 'RandomNumber', 'UnixTime']) + self.assertTrue(enum_members.issubset(dir(sample.SampleNamespace))) def testAddedFunctionAtModuleLevel(self): '''Calls function added to module from type system description.''' diff --git a/tests/samplebinding/str_test.py b/tests/samplebinding/str_test.py index d8d3a75ca..8fe112626 100644 --- a/tests/samplebinding/str_test.py +++ b/tests/samplebinding/str_test.py @@ -66,7 +66,7 @@ class StrTest(unittest.TestCase): self.assertEqual(s1[-2], "e"); try: - print s1[6] + print(s1[6]) self.assertFalse(true); except: pass diff --git a/tests/samplebinding/time_test.py b/tests/samplebinding/time_test.py index 5c0270d7a..d1ffc268e 100644 --- a/tests/samplebinding/time_test.py +++ b/tests/samplebinding/time_test.py @@ -30,7 +30,7 @@ import sys import unittest import datetime -from sample import Time, ImplicitConv, ObjectType, Str +from sample import Time, ImplicitConv, ObjectType class TimeTest(unittest.TestCase): '''Test cases for constructor and method signature decisor on Time class. @@ -43,7 +43,7 @@ class TimeTest(unittest.TestCase): def testConstructorWithoutParamers(self): '''Constructor without parameters: Time()''' time = Time() - self.assert_(time.isNull()) + self.assertTrue(time.isNull()) def testConstructorWithAllParamers(self): '''Constructor with all parameters: Time(int h, int m, int s = 0, int ms = 0)''' @@ -64,7 +64,7 @@ class TimeTest(unittest.TestCase): '''Constructor without parameters: Time.setTime()''' time = Time(1, 2, 3, 4) time.setTime() - self.assert_(time.isNull()) + self.assertTrue(time.isNull()) def testSimpleMethodWithAllParamers(self): '''Simple method with all parameters: Time.setTime(int h, int m, int s = 0, int ms = 0)''' @@ -120,13 +120,13 @@ class TimeTest(unittest.TestCase): self.assertEqual(result, Time.ThreeArgs) def testCompareWithPythonTime(self): - time = Time(12, 32, 05) - py = datetime.time(12, 32, 05) + time = Time(12, 32, 5) + py = datetime.time(12, 32, 5) self.assertEqual(time, py) def testNotEqual(self): - time = Time(12, 32, 06) - py = datetime.time(12, 32, 05) + time = Time(12, 32, 6) + py = datetime.time(12, 32, 5) self.assertNotEqual(time, py) if __name__ == '__main__': diff --git a/tests/samplebinding/virtualmethods_test.py b/tests/samplebinding/virtualmethods_test.py index f2895af64..376ab8495 100644 --- a/tests/samplebinding/virtualmethods_test.py +++ b/tests/samplebinding/virtualmethods_test.py @@ -32,7 +32,6 @@ import unittest from sample import * import warnings -warnings.simplefilter('error') class ExtendedVirtualMethods(VirtualMethods): def __init__(self): @@ -44,6 +43,7 @@ class ExtendedVirtualMethods(VirtualMethods): return VirtualMethods.virtualMethod0(self, pt, val, cpx, b) * -1.0 def strListToStdList(self, arg): + warnings.simplefilter('error') # returning wrong type for test purposes. return True @@ -105,7 +105,7 @@ class VirtualMethodsTest(unittest.TestCase): self.assertFalse(evd.grand_daughter_name_called) name = evd.callName() - self.assert_(evd.grand_daughter_name_called) + self.assertTrue(evd.grand_daughter_name_called) self.assertEqual(evd.name().prepend(self.prefix_from_codeinjection), name) def testReimplementedVirtualMethodInheritedFromGrandGrandParent(self): @@ -119,8 +119,8 @@ class VirtualMethodsTest(unittest.TestCase): self.assertFalse(eevd.grand_grand_daughter_name_called) name = eevd.callName() - self.assert_(eevd.grand_daughter_name_called) - self.assert_(eevd.grand_grand_daughter_name_called) + self.assertTrue(eevd.grand_daughter_name_called) + self.assertTrue(eevd.grand_grand_daughter_name_called) self.assertEqual(eevd.name().prepend(self.prefix_from_codeinjection), name) class PrettyErrorMessageTest(unittest.TestCase): diff --git a/tests/samplebinding/weakref_test.py b/tests/samplebinding/weakref_test.py index 390252d46..a4ea9e66e 100644 --- a/tests/samplebinding/weakref_test.py +++ b/tests/samplebinding/weakref_test.py @@ -51,7 +51,6 @@ class WeakrefBasicTest(unittest.TestCase): def testPrivateDtor(self): '''PrivateDtor weakref''' obj = PrivateDtor.instance() - print obj ref = weakref.ref(obj, self.cb) del obj self.assert_(self.called) |