Fix core tests for Python 3 (actually imported + fixes).

This commit is contained in:
Valentin Lorentz 2013-08-10 15:18:04 +02:00
parent 34b0e5faad
commit c23f25545f
7 changed files with 55 additions and 39 deletions

View File

@ -204,7 +204,11 @@ class IterableMap(object):
"""Define .iteritems() in a class and subclass this to get the other iters. """Define .iteritems() in a class and subclass this to get the other iters.
""" """
def iteritems(self): def iteritems(self):
raise NotImplementedError if sys.version_info[0] >= 3 and hasattr(self, 'iteritems'):
# For old plugins
return getattr(self, 'iteritems')() # avoid 2to3
else:
raise NotImplementedError()
def iterkeys(self): def iterkeys(self):
for (key, _) in self.iteritems(): for (key, _) in self.iteritems():

View File

@ -28,6 +28,6 @@
### ###
# We're just masquerading as a plugin :) # We're just masquerading as a plugin :)
import test from . import test
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -513,7 +513,7 @@ class PrivmsgTestCase(ChannelPluginTestCase):
class BadInvalidCommand(callbacks.Plugin): class BadInvalidCommand(callbacks.Plugin):
def invalidCommand(self, irc, msg, tokens): def invalidCommand(self, irc, msg, tokens):
s = 'This shouldn\'t keep Misc.invalidCommand from being called' s = 'This shouldn\'t keep Misc.invalidCommand from being called'
raise Exception, s raise Exception(s)
def testBadInvalidCommandDoesNotKillAll(self): def testBadInvalidCommandDoesNotKillAll(self):
try: try:

View File

@ -27,6 +27,8 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
import sys
from supybot.test import * from supybot.test import *
from supybot.commands import * from supybot.commands import *
@ -83,10 +85,11 @@ class GeneralContextTestCase(CommandsTestCase):
finally: finally:
conf.supybot.protocols.irc.strictRfc.setValue(strict) conf.supybot.protocols.irc.strictRfc.setValue(strict)
if sys.version_info[0] < 3:
def testSpecLong(self): def testSpecLong(self):
self.assertState(['long'], ['1'], [1L]) self.assertState(['long'], ['1'], [long(1)])
self.assertState(['long', 'long', 'long'], ['1', '2', '3'], self.assertState(['long', 'long', 'long'], ['1', '2', '3'],
[1L, 2L, 3L]) [long(1), long(2), long(3)])
def testRestHandling(self): def testRestHandling(self):
self.assertState([rest(None)], ['foo', 'bar', 'baz'], ['foo bar baz']) self.assertState([rest(None)], ['foo', 'bar', 'baz'], ['foo bar baz'])

View File

@ -37,13 +37,17 @@ class FirewallTestCase(SupyTestCase):
def tearDown(self): def tearDown(self):
log.testing = True log.testing = True
class C(object): # Python 3's syntax for metaclasses is incompatible with Python 3 so
__metaclass__ = log.MetaFirewall # using Python 3's syntax directly will raise a SyntaxError on Python 2.
exec("""
class C(%s
__firewalled__ = {'foo': None} __firewalled__ = {'foo': None}
class MyException(Exception): class MyException(Exception):
pass pass
def foo(self): def foo(self):
raise self.MyException() raise self.MyException()""" %
('metaclass=log.MetaFirewall):\n' if sys.version_info[0] >= 3 else
'object):\n__metaclass__ = log.MetaFirewall'))
def testCFooDoesNotRaise(self): def testCFooDoesNotRaise(self):
c = self.C() c = self.C()

View File

@ -95,7 +95,7 @@ class IrcMsgQueueTestCase(SupyTestCase):
q.enqueue(self.msg) q.enqueue(self.msg)
try: try:
repr(q) repr(q)
except Exception, e: except Exception as e:
self.fail('repr(q) raised an exception: %s' % self.fail('repr(q) raised an exception: %s' %
utils.exnToString(e)) utils.exnToString(e))
@ -466,7 +466,7 @@ class IrcCallbackTestCase(SupyTestCase):
doCommandCatcher = DoCommandCatcher() doCommandCatcher = DoCommandCatcher()
for msg in msgs: for msg in msgs:
doCommandCatcher(self.irc, msg) doCommandCatcher(self.irc, msg)
commands = map(makeCommand, msgs) commands = list(map(makeCommand, msgs))
self.assertEqual(doCommandCatcher.L, commands) self.assertEqual(doCommandCatcher.L, commands)
def testFirstCommands(self): def testFirstCommands(self):

View File

@ -30,14 +30,18 @@
from supybot.test import * from supybot.test import *
import sys
import time import time
import pickle import pickle
import supybot.utils as utils import supybot.utils as utils
from supybot.utils.structures import * from supybot.utils.structures import *
if sys.version_info[0] >= 0:
xrange = range
class UtilsTest(SupyTestCase): class UtilsTest(SupyTestCase):
def testReversed(self): def testReversed(self):
L = range(10) L = list(range(10))
revL = list(reversed(L)) revL = list(reversed(L))
L.reverse() L.reverse()
self.assertEqual(L, revL, 'reversed didn\'t return reversed list') self.assertEqual(L, revL, 'reversed didn\'t return reversed list')
@ -53,7 +57,7 @@ class SeqTest(SupyTestCase):
self.assertEqual(L, LL[::-1]) self.assertEqual(L, LL[::-1])
def testWindow(self): def testWindow(self):
L = range(10) L = list(range(10))
def wwindow(*args): def wwindow(*args):
return list(utils.seq.window(*args)) return list(utils.seq.window(*args))
self.assertEqual(wwindow([], 1), [], 'Empty sequence, empty window') self.assertEqual(wwindow([], 1), [], 'Empty sequence, empty window')
@ -82,12 +86,12 @@ class GenTest(SupyTestCase):
def testExnToString(self): def testExnToString(self):
try: try:
raise KeyError, 1 raise KeyError(1)
except Exception, e: except Exception as e:
self.assertEqual(utils.exnToString(e), 'KeyError: 1') self.assertEqual(utils.exnToString(e), 'KeyError: 1')
try: try:
raise EOFError raise EOFError()
except Exception, e: except Exception as e:
self.assertEqual(utils.exnToString(e), 'EOFError') self.assertEqual(utils.exnToString(e), 'EOFError')
def testSaltHash(self): def testSaltHash(self):
@ -123,9 +127,10 @@ class GenTest(SupyTestCase):
AL[2] = 3 AL[2] = 3
AL[3] = 4 AL[3] = 4
self.failUnless(AL) self.failUnless(AL)
self.assertEqual(AL.items(), [(1, 2), (2, 3), (3, 4)]) self.assertEqual(list(AL.items()), [(1, 2), (2, 3), (3, 4)])
self.assertEqual(list(AL.iteritems()), [(1, 2), (2, 3), (3, 4)]) self.assertEqual(list(AL.iteritems()), [(1, 2), (2, 3), (3, 4)])
self.assertEqual(AL.keys(), [1, 2, 3]) self.assertEqual(list(AL.keys()), [1, 2, 3])
if sys.version_info[0] < 3:
self.assertEqual(list(AL.iterkeys()), [1, 2, 3]) self.assertEqual(list(AL.iterkeys()), [1, 2, 3])
self.assertEqual(AL.values(), [2, 3, 4]) self.assertEqual(AL.values(), [2, 3, 4])
self.assertEqual(list(AL.itervalues()), [2, 3, 4]) self.assertEqual(list(AL.itervalues()), [2, 3, 4])
@ -391,7 +396,7 @@ class IterTest(SupyTestCase):
self.assertEqual([], list(utils.iter.limited(L, 0))) self.assertEqual([], list(utils.iter.limited(L, 0)))
self.assertEqual([0], list(utils.iter.limited(L, 1))) self.assertEqual([0], list(utils.iter.limited(L, 1)))
self.assertEqual([0, 1], list(utils.iter.limited(L, 2))) self.assertEqual([0, 1], list(utils.iter.limited(L, 2)))
self.assertEqual(range(10), list(utils.iter.limited(L, 10))) self.assertEqual(list(range(10)), list(utils.iter.limited(L, 10)))
self.assertRaises(ValueError, list, utils.iter.limited(L, 11)) self.assertRaises(ValueError, list, utils.iter.limited(L, 11))
def testRandomChoice(self): def testRandomChoice(self):
@ -459,7 +464,7 @@ class IterTest(SupyTestCase):
[[1, 2], [1, 2], [1, 2], []]) [[1, 2], [1, 2], [1, 2], []])
self.assertEqual(list(itersplit(lambda x: x, [])), []) self.assertEqual(list(itersplit(lambda x: x, [])), [])
self.assertEqual(list(itersplit(lambda c: c.isspace(), s)), self.assertEqual(list(itersplit(lambda c: c.isspace(), s)),
map(list, s.split())) list(map(list, s.split())))
self.assertEqual(list(itersplit('for'.__eq__, ['foo', 'for', 'bar'])), self.assertEqual(list(itersplit('for'.__eq__, ['foo', 'for', 'bar'])),
[['foo'], ['bar']]) [['foo'], ['bar']])
self.assertEqual(list(itersplit('for'.__eq__, self.assertEqual(list(itersplit('for'.__eq__,
@ -471,8 +476,8 @@ class IterTest(SupyTestCase):
return list(utils.iter.flatten(seq)) return list(utils.iter.flatten(seq))
self.assertEqual(lflatten([]), []) self.assertEqual(lflatten([]), [])
self.assertEqual(lflatten([1]), [1]) self.assertEqual(lflatten([1]), [1])
self.assertEqual(lflatten(range(10)), range(10)) self.assertEqual(lflatten(range(10)), list(range(10)))
twoRanges = range(10)*2 twoRanges = list(range(10))*2
twoRanges.sort() twoRanges.sort()
self.assertEqual(lflatten(zip(range(10), range(10))), twoRanges) self.assertEqual(lflatten(zip(range(10), range(10))), twoRanges)
self.assertEqual(lflatten([1, [2, 3], 4]), [1, 2, 3, 4]) self.assertEqual(lflatten([1, [2, 3], 4]), [1, 2, 3, 4])
@ -538,7 +543,7 @@ class RingBufferTestCase(SupyTestCase):
def testInit(self): def testInit(self):
self.assertRaises(ValueError, RingBuffer, -1) self.assertRaises(ValueError, RingBuffer, -1)
self.assertRaises(ValueError, RingBuffer, 0) self.assertRaises(ValueError, RingBuffer, 0)
self.assertEqual(range(10), list(RingBuffer(10, range(10)))) self.assertEqual(list(range(10)), list(RingBuffer(10, range(10))))
def testLen(self): def testLen(self):
b = RingBuffer(3) b = RingBuffer(3)
@ -598,7 +603,7 @@ class RingBufferTestCase(SupyTestCase):
self.assertEqual(list(b), list(b[:i]) + list(b[i:])) self.assertEqual(list(b), list(b[:i]) + list(b[i:]))
def testSliceGetitem(self): def testSliceGetitem(self):
L = range(10) L = list(range(10))
b = RingBuffer(len(L), L) b = RingBuffer(len(L), L)
for i in range(len(b)): for i in range(len(b)):
self.assertEqual(L[:i], b[:i]) self.assertEqual(L[:i], b[:i])
@ -632,7 +637,7 @@ class RingBufferTestCase(SupyTestCase):
self.assertEqual(b[i], i) self.assertEqual(b[i], i)
def testSliceSetitem(self): def testSliceSetitem(self):
L = range(10) L = list(range(10))
b = RingBuffer(len(L), [0]*len(L)) b = RingBuffer(len(L), [0]*len(L))
self.assertRaises(ValueError, b.__setitem__, slice(0, 10), []) self.assertRaises(ValueError, b.__setitem__, slice(0, 10), [])
b[2:4] = L[2:4] b[2:4] = L[2:4]
@ -644,9 +649,9 @@ class RingBufferTestCase(SupyTestCase):
def testExtend(self): def testExtend(self):
b = RingBuffer(3, range(3)) b = RingBuffer(3, range(3))
self.assertEqual(list(b), range(3)) self.assertEqual(list(b), list(range(3)))
b.extend(range(6)) b.extend(range(6))
self.assertEqual(list(b), range(6)[3:]) self.assertEqual(list(b), list(range(6)[3:]))
def testRepr(self): def testRepr(self):
b = RingBuffer(3) b = RingBuffer(3)
@ -670,7 +675,7 @@ class RingBufferTestCase(SupyTestCase):
def testEq(self): def testEq(self):
b = RingBuffer(3, range(3)) b = RingBuffer(3, range(3))
self.failIf(b == range(3)) self.failIf(b == list(range(3)))
b1 = RingBuffer(3) b1 = RingBuffer(3)
self.failIf(b == b1) self.failIf(b == b1)
b1.append(0) b1.append(0)
@ -688,13 +693,13 @@ class RingBufferTestCase(SupyTestCase):
L = [] L = []
for elt in b: for elt in b:
L.append(elt) L.append(elt)
self.assertEqual(L, range(3)) self.assertEqual(L, list(range(3)))
for elt in range(3): for elt in range(3):
b.append(elt) b.append(elt)
del L[:] del L[:]
for elt in b: for elt in b:
L.append(elt) L.append(elt)
self.assertEqual(L, range(3)) self.assertEqual(L, list(range(3)))
class QueueTest(SupyTestCase): class QueueTest(SupyTestCase):