]> granicus.if.org Git - python/commitdiff
SF patch #736962: Port tests to unittest
authorRaymond Hettinger <python@rcn.com>
Sat, 30 Aug 2003 22:54:55 +0000 (22:54 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 30 Aug 2003 22:54:55 +0000 (22:54 +0000)
(Contributed by Walter Dörwald).

* Convert three test modules to unittest format.
* Expanded coverage in test_structseq.py.
* Raymond added a new test in test_sets.py

Lib/test/output/test_longexp [deleted file]
Lib/test/test_longexp.py
Lib/test/test_pep263.py
Lib/test/test_sets.py
Lib/test/test_structseq.py

diff --git a/Lib/test/output/test_longexp b/Lib/test/output/test_longexp
deleted file mode 100644 (file)
index bbdd70c..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-test_longexp
-65580
index c2c26125c86d4da4252a30c2fc8057a5c59723d3..dd222a19bff300bb8d9fb9537f707ccda4068e86 100644 (file)
@@ -1,5 +1,14 @@
+import unittest
+from test import test_support
 
-REPS = 65580
+class LongExpText(unittest.TestCase):
+    def test_longexp(self):
+        REPS = 65580
+        l = eval("[" + "2," * REPS + "]")
+        self.assertEqual(len(l), REPS)
 
-l = eval("[" + "2," * REPS + "]")
-print len(l)
+def test_main():
+    test_support.run_unittest(LongExpText)
+
+if __name__=="__main__":
+    test_main()
index 7cc55265f00e9bbf54d71c70e15c5428e66164bc..48b8c1310be09b3c73a7acb3b27763fa91ed08cf 100644 (file)
@@ -1,3 +1,22 @@
-#! -*- coding: koi8-r -*-
-assert u"ðÉÔÏÎ".encode("utf-8") == '\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd'
-assert u"\ð".encode("utf-8") == '\\\xd0\x9f'
+#! -*- coding: koi8-r -*-\r
+\r
+import unittest\r
+from test import test_support\r
+\r
+class PEP263Test(unittest.TestCase):\r
+\r
+    def test_pep263(self):\r
+        self.assertEqual(\r
+            u"ðÉÔÏÎ".encode("utf-8"),\r
+            '\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd'\r
+        )\r
+        self.assertEqual(\r
+            u"\ð".encode("utf-8"),\r
+            '\\\xd0\x9f'\r
+        )\r
+\r
+def test_main():\r
+    test_support.run_unittest(PEP263Test)\r
+\r
+if __name__=="__main__":\r
+    test_main()\r
index f0a1925d4e01463dfb9f7051e9e88fe97d74b291..416b7cac3a025e47a094274e4a1d218a4501355d 100644 (file)
@@ -596,6 +596,7 @@ class TestOnlySetsInBinaryOps(unittest.TestCase):
             self.set.difference(self.other)
         else:
             self.assertRaises(TypeError, self.set.difference, self.other)
+
 #------------------------------------------------------------------------------
 
 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps):
@@ -647,6 +648,14 @@ class TestOnlySetsGenerator(TestOnlySetsInBinaryOps):
         self.other = gen()
         self.otherIsIterable = True
 
+#------------------------------------------------------------------------------
+
+class TestOnlySetsofSets(TestOnlySetsInBinaryOps):
+    def setUp(self):
+        self.set   = Set((1, 2, 3))
+        self.other = [Set('ab'), ImmutableSet('cd')]
+        self.otherIsIterable = True
+
 #==============================================================================
 
 class TestCopying(unittest.TestCase):
@@ -801,6 +810,7 @@ def test_main(verbose=None):
         TestOnlySetsTuple,
         TestOnlySetsString,
         TestOnlySetsGenerator,
+        TestOnlySetsofSets,
         TestCopyingEmpty,
         TestCopyingSingleton,
         TestCopyingTriple,
index 2760d722b4e724830570dc035afb74453979de3a..eb6d58104fcc3c61b64bcfb10f54d07ad0f75f45 100644 (file)
-from test.test_support import vereq
+import unittest
+from test import test_support
 
 import time
 
-t = time.gmtime()
-astuple = tuple(t)
-vereq(len(t), len(astuple))
-vereq(t, astuple)
-
-# Check that slicing works the same way; at one point, slicing t[i:j] with
-# 0 < i < j could produce NULLs in the result.
-for i in range(-len(t), len(t)):
-    for j in range(-len(t), len(t)):
-        vereq(t[i:j], astuple[i:j])
-
-# Devious code could crash structseqs' contructors
-class C:
-    def __getitem__(self, i):
-        raise IndexError
-    def __len__(self):
-        return 9
-
-try:
-    repr(time.struct_time(C()))
-except:
-    pass
-
-# XXX more needed
+class StructSeqTest(unittest.TestCase):
+
+    def test_tuple(self):
+        t = time.gmtime()
+        astuple = tuple(t)
+        self.assertEqual(len(t), len(astuple))
+        self.assertEqual(t, astuple)
+
+        # Check that slicing works the same way; at one point, slicing t[i:j] with
+        # 0 < i < j could produce NULLs in the result.
+        for i in xrange(-len(t), len(t)):
+            self.assertEqual(t[i:], astuple[i:])
+            for j in xrange(-len(t), len(t)):
+                self.assertEqual(t[i:j], astuple[i:j])
+
+        for j in xrange(-len(t), len(t)):
+            self.assertEqual(t[:j], astuple[:j])
+
+        self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
+        self.assertRaises(IndexError, t.__getitem__, len(t))
+        for i in xrange(-len(t), len(t)-1):
+            self.assertEqual(t[i], astuple[i])
+
+    def test_repr(self):
+        t = time.gmtime()
+        repr(t)
+
+    def test_concat(self):
+        t1 = time.gmtime()
+        t2 = t1 + tuple(t1)
+        for i in xrange(len(t1)):
+            self.assertEqual(t2[i], t2[i+len(t1)])
+
+    def test_repeat(self):
+        t1 = time.gmtime()
+        t2 = 3 * t1
+        for i in xrange(len(t1)):
+            self.assertEqual(t2[i], t2[i+len(t1)])
+            self.assertEqual(t2[i], t2[i+2*len(t1)])
+
+    def test_contains(self):
+        t1 = time.gmtime()
+        for item in t1:
+            self.assert_(item in t1)
+        self.assert_(-42 not in t1)
+
+    def test_hash(self):
+        t1 = time.gmtime()
+        self.assertEqual(hash(t1), hash(tuple(t1)))
+
+    def test_cmp(self):
+        t1 = time.gmtime()
+        t2 = type(t1)(t1)
+        self.assertEqual(t1, t2)
+        self.assert_(not (t1 < t2))
+        self.assert_(t1 <= t2)
+        self.assert_(not (t1 > t2))
+        self.assert_(t1 >= t2)
+        self.assert_(not (t1 != t2))
+
+    def test_fields(self):
+        t = time.gmtime()
+        self.assertEqual(len(t), t.n_fields)
+        self.assertEqual(t.n_fields, t.n_sequence_fields+t.n_unnamed_fields)
+
+    def test_constructor(self):
+        t = time.struct_time
+
+        self.assertRaises(TypeError, t)
+        self.assertRaises(TypeError, t, None)
+        self.assertRaises(TypeError, t, "123")
+        self.assertRaises(TypeError, t, "123", dict={})
+        self.assertRaises(TypeError, t, "123456789", dict=None)
+
+        s = "123456789"
+        self.assertEqual("".join(t(s)), s)
+
+    def test_eviltuple(self):
+        class Exc(Exception):
+            pass
+
+        # Devious code could crash structseqs' contructors
+        class C:
+            def __getitem__(self, i):
+                raise Exc
+            def __len__(self):
+                return 9
+
+        self.assertRaises(Exc, time.struct_time, C())
+
+    def test_reduce(self):
+        t = time.gmtime()
+        x = t.__reduce__()
+
+def test_main():
+    test_support.run_unittest(StructSeqTest)
+
+if __name__ == "__main__":
+    test_main()