From c23f25545f50a0e92dd384417b852b6517e98ddc Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 10 Aug 2013 15:18:04 +0200 Subject: [PATCH] Fix core tests for Python 3 (actually imported + fixes). --- src/utils/gen.py | 6 ++++- test/__init__.py | 2 +- test/test_callbacks.py | 2 +- test/test_commands.py | 11 +++++---- test/test_firewall.py | 18 +++++++++------ test/test_irclib.py | 4 ++-- test/test_utils.py | 51 +++++++++++++++++++++++------------------- 7 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/utils/gen.py b/src/utils/gen.py index 494006330..049d05e90 100644 --- a/src/utils/gen.py +++ b/src/utils/gen.py @@ -204,7 +204,11 @@ class IterableMap(object): """Define .iteritems() in a class and subclass this to get the other iters. """ 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): for (key, _) in self.iteritems(): diff --git a/test/__init__.py b/test/__init__.py index bd36ce052..be6bbb8f7 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -28,6 +28,6 @@ ### # We're just masquerading as a plugin :) -import test +from . import test # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/test/test_callbacks.py b/test/test_callbacks.py index 76aa97424..ab9c758ee 100644 --- a/test/test_callbacks.py +++ b/test/test_callbacks.py @@ -513,7 +513,7 @@ class PrivmsgTestCase(ChannelPluginTestCase): class BadInvalidCommand(callbacks.Plugin): def invalidCommand(self, irc, msg, tokens): s = 'This shouldn\'t keep Misc.invalidCommand from being called' - raise Exception, s + raise Exception(s) def testBadInvalidCommandDoesNotKillAll(self): try: diff --git a/test/test_commands.py b/test/test_commands.py index 290ae5c93..2a7bdc7b9 100644 --- a/test/test_commands.py +++ b/test/test_commands.py @@ -27,6 +27,8 @@ # POSSIBILITY OF SUCH DAMAGE. ### +import sys + from supybot.test import * from supybot.commands import * @@ -83,10 +85,11 @@ class GeneralContextTestCase(CommandsTestCase): finally: conf.supybot.protocols.irc.strictRfc.setValue(strict) - def testSpecLong(self): - self.assertState(['long'], ['1'], [1L]) - self.assertState(['long', 'long', 'long'], ['1', '2', '3'], - [1L, 2L, 3L]) + if sys.version_info[0] < 3: + def testSpecLong(self): + self.assertState(['long'], ['1'], [long(1)]) + self.assertState(['long', 'long', 'long'], ['1', '2', '3'], + [long(1), long(2), long(3)]) def testRestHandling(self): self.assertState([rest(None)], ['foo', 'bar', 'baz'], ['foo bar baz']) diff --git a/test/test_firewall.py b/test/test_firewall.py index 9429478f8..f4019ecaf 100644 --- a/test/test_firewall.py +++ b/test/test_firewall.py @@ -37,13 +37,17 @@ class FirewallTestCase(SupyTestCase): def tearDown(self): log.testing = True - class C(object): - __metaclass__ = log.MetaFirewall - __firewalled__ = {'foo': None} - class MyException(Exception): - pass - def foo(self): - raise self.MyException() + # Python 3's syntax for metaclasses is incompatible with Python 3 so + # using Python 3's syntax directly will raise a SyntaxError on Python 2. + exec(""" +class C(%s + __firewalled__ = {'foo': None} + class MyException(Exception): + pass + def foo(self): + raise self.MyException()""" % + ('metaclass=log.MetaFirewall):\n' if sys.version_info[0] >= 3 else + 'object):\n__metaclass__ = log.MetaFirewall')) def testCFooDoesNotRaise(self): c = self.C() diff --git a/test/test_irclib.py b/test/test_irclib.py index 428e26898..190a46557 100644 --- a/test/test_irclib.py +++ b/test/test_irclib.py @@ -95,7 +95,7 @@ class IrcMsgQueueTestCase(SupyTestCase): q.enqueue(self.msg) try: repr(q) - except Exception, e: + except Exception as e: self.fail('repr(q) raised an exception: %s' % utils.exnToString(e)) @@ -466,7 +466,7 @@ class IrcCallbackTestCase(SupyTestCase): doCommandCatcher = DoCommandCatcher() for msg in msgs: doCommandCatcher(self.irc, msg) - commands = map(makeCommand, msgs) + commands = list(map(makeCommand, msgs)) self.assertEqual(doCommandCatcher.L, commands) def testFirstCommands(self): diff --git a/test/test_utils.py b/test/test_utils.py index dde9af6cd..1919c1e09 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -30,14 +30,18 @@ from supybot.test import * +import sys import time import pickle import supybot.utils as utils from supybot.utils.structures import * +if sys.version_info[0] >= 0: + xrange = range + class UtilsTest(SupyTestCase): def testReversed(self): - L = range(10) + L = list(range(10)) revL = list(reversed(L)) L.reverse() self.assertEqual(L, revL, 'reversed didn\'t return reversed list') @@ -53,7 +57,7 @@ class SeqTest(SupyTestCase): self.assertEqual(L, LL[::-1]) def testWindow(self): - L = range(10) + L = list(range(10)) def wwindow(*args): return list(utils.seq.window(*args)) self.assertEqual(wwindow([], 1), [], 'Empty sequence, empty window') @@ -82,12 +86,12 @@ class GenTest(SupyTestCase): def testExnToString(self): try: - raise KeyError, 1 - except Exception, e: + raise KeyError(1) + except Exception as e: self.assertEqual(utils.exnToString(e), 'KeyError: 1') try: - raise EOFError - except Exception, e: + raise EOFError() + except Exception as e: self.assertEqual(utils.exnToString(e), 'EOFError') def testSaltHash(self): @@ -123,12 +127,13 @@ class GenTest(SupyTestCase): AL[2] = 3 AL[3] = 4 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(AL.keys(), [1, 2, 3]) - self.assertEqual(list(AL.iterkeys()), [1, 2, 3]) - self.assertEqual(AL.values(), [2, 3, 4]) - self.assertEqual(list(AL.itervalues()), [2, 3, 4]) + self.assertEqual(list(AL.keys()), [1, 2, 3]) + if sys.version_info[0] < 3: + self.assertEqual(list(AL.iterkeys()), [1, 2, 3]) + self.assertEqual(AL.values(), [2, 3, 4]) + self.assertEqual(list(AL.itervalues()), [2, 3, 4]) self.assertEqual(len(AL), 3) def testSortBy(self): @@ -391,7 +396,7 @@ class IterTest(SupyTestCase): self.assertEqual([], list(utils.iter.limited(L, 0))) self.assertEqual([0], list(utils.iter.limited(L, 1))) 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)) def testRandomChoice(self): @@ -459,7 +464,7 @@ class IterTest(SupyTestCase): [[1, 2], [1, 2], [1, 2], []]) self.assertEqual(list(itersplit(lambda x: x, [])), []) 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'])), [['foo'], ['bar']]) self.assertEqual(list(itersplit('for'.__eq__, @@ -471,8 +476,8 @@ class IterTest(SupyTestCase): return list(utils.iter.flatten(seq)) self.assertEqual(lflatten([]), []) self.assertEqual(lflatten([1]), [1]) - self.assertEqual(lflatten(range(10)), range(10)) - twoRanges = range(10)*2 + self.assertEqual(lflatten(range(10)), list(range(10))) + twoRanges = list(range(10))*2 twoRanges.sort() self.assertEqual(lflatten(zip(range(10), range(10))), twoRanges) self.assertEqual(lflatten([1, [2, 3], 4]), [1, 2, 3, 4]) @@ -538,7 +543,7 @@ class RingBufferTestCase(SupyTestCase): def testInit(self): self.assertRaises(ValueError, RingBuffer, -1) 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): b = RingBuffer(3) @@ -598,7 +603,7 @@ class RingBufferTestCase(SupyTestCase): self.assertEqual(list(b), list(b[:i]) + list(b[i:])) def testSliceGetitem(self): - L = range(10) + L = list(range(10)) b = RingBuffer(len(L), L) for i in range(len(b)): self.assertEqual(L[:i], b[:i]) @@ -632,7 +637,7 @@ class RingBufferTestCase(SupyTestCase): self.assertEqual(b[i], i) def testSliceSetitem(self): - L = range(10) + L = list(range(10)) b = RingBuffer(len(L), [0]*len(L)) self.assertRaises(ValueError, b.__setitem__, slice(0, 10), []) b[2:4] = L[2:4] @@ -644,9 +649,9 @@ class RingBufferTestCase(SupyTestCase): def testExtend(self): b = RingBuffer(3, range(3)) - self.assertEqual(list(b), range(3)) + self.assertEqual(list(b), list(range(3))) b.extend(range(6)) - self.assertEqual(list(b), range(6)[3:]) + self.assertEqual(list(b), list(range(6)[3:])) def testRepr(self): b = RingBuffer(3) @@ -670,7 +675,7 @@ class RingBufferTestCase(SupyTestCase): def testEq(self): b = RingBuffer(3, range(3)) - self.failIf(b == range(3)) + self.failIf(b == list(range(3))) b1 = RingBuffer(3) self.failIf(b == b1) b1.append(0) @@ -688,13 +693,13 @@ class RingBufferTestCase(SupyTestCase): L = [] for elt in b: L.append(elt) - self.assertEqual(L, range(3)) + self.assertEqual(L, list(range(3))) for elt in range(3): b.append(elt) del L[:] for elt in b: L.append(elt) - self.assertEqual(L, range(3)) + self.assertEqual(L, list(range(3))) class QueueTest(SupyTestCase):