Commits

Armin Rigo committed fb7b52c

Checking in the test for PEP 357.

This is from the SF tracker as well; for some reason the
content of test_index.py was lost and an empty file was
checked in instead.

Comments (0)

Files changed (1)

Lib/test/test_index.py

+import unittest
+from test import test_support
+import operator
+
+class oldstyle:
+    def __index__(self):
+        return self.ind
+
+class newstyle(object):
+    def __index__(self):
+        return self.ind
+
+class ListTestCase(unittest.TestCase):
+    def setUp(self):
+        self.seq = [0,10,20,30,40,50]
+        self.o = oldstyle()
+        self.n = newstyle()
+        self.o2 = oldstyle()
+        self.n2 = newstyle()
+    
+    def test_basic(self):
+        self.o.ind = -2
+        self.n.ind = 2
+        assert(self.seq[self.n] == 20)
+        assert(self.seq[self.o] == 40)
+        assert(operator.index(self.o) == -2)
+        assert(operator.index(self.n) == 2)
+        
+    def test_error(self):
+        self.o.ind = 'dumb'
+        self.n.ind = 'bad'
+        myfunc = lambda x, obj: obj.seq[x]
+        self.failUnlessRaises(TypeError, operator.index, self.o)
+        self.failUnlessRaises(TypeError, operator.index, self.n)
+        self.failUnlessRaises(TypeError, myfunc, self.o, self)
+        self.failUnlessRaises(TypeError, myfunc, self.n, self)
+
+    def test_slice(self):
+        self.o.ind = 1
+        self.o2.ind = 3
+        self.n.ind = 2
+        self.n2.ind = 4
+        assert(self.seq[self.o:self.o2] == self.seq[1:3])
+        assert(self.seq[self.n:self.n2] == self.seq[2:4])
+
+class TupleTestCase(unittest.TestCase):
+    def setUp(self):
+        self.seq = (0,10,20,30,40,50)
+        self.o = oldstyle()
+        self.n = newstyle()
+        self.o2 = oldstyle()
+        self.n2 = newstyle()
+        
+    
+    def test_basic(self):
+        self.o.ind = -2
+        self.n.ind = 2
+        assert(self.seq[self.n] == 20)
+        assert(self.seq[self.o] == 40)
+        assert(operator.index(self.o) == -2)
+        assert(operator.index(self.n) == 2)
+        
+    def test_error(self):
+        self.o.ind = 'dumb'
+        self.n.ind = 'bad'
+        myfunc = lambda x, obj: obj.seq[x]
+        self.failUnlessRaises(TypeError, operator.index, self.o)
+        self.failUnlessRaises(TypeError, operator.index, self.n)
+        self.failUnlessRaises(TypeError, myfunc, self.o, self)
+        self.failUnlessRaises(TypeError, myfunc, self.n, self)
+
+    def test_slice(self):
+        self.o.ind = 1
+        self.o2.ind = 3
+        self.n.ind = 2
+        self.n2.ind = 4
+        assert(self.seq[self.o:self.o2] == self.seq[1:3])
+        assert(self.seq[self.n:self.n2] == self.seq[2:4])
+
+class StringTestCase(unittest.TestCase):
+    def setUp(self):
+        self.seq = "this is a test"
+        self.o = oldstyle()
+        self.n = newstyle()
+        self.o2 = oldstyle()
+        self.n2 = newstyle()
+        
+    
+    def test_basic(self):
+        self.o.ind = -2
+        self.n.ind = 2
+        assert(self.seq[self.n] == self.seq[2])
+        assert(self.seq[self.o] == self.seq[-2])
+        assert(operator.index(self.o) == -2)
+        assert(operator.index(self.n) == 2)
+        
+    def test_error(self):
+        self.o.ind = 'dumb'
+        self.n.ind = 'bad'
+        myfunc = lambda x, obj: obj.seq[x]
+        self.failUnlessRaises(TypeError, operator.index, self.o)
+        self.failUnlessRaises(TypeError, operator.index, self.n)
+        self.failUnlessRaises(TypeError, myfunc, self.o, self)
+        self.failUnlessRaises(TypeError, myfunc, self.n, self)
+
+    def test_slice(self):
+        self.o.ind = 1
+        self.o2.ind = 3
+        self.n.ind = 2
+        self.n2.ind = 4
+        assert(self.seq[self.o:self.o2] == self.seq[1:3])
+        assert(self.seq[self.n:self.n2] == self.seq[2:4])
+
+
+class UnicodeTestCase(unittest.TestCase):
+    def setUp(self):
+        self.seq = u"this is a test"
+        self.o = oldstyle()
+        self.n = newstyle()
+        self.o2 = oldstyle()
+        self.n2 = newstyle()
+        
+    
+    def test_basic(self):
+        self.o.ind = -2
+        self.n.ind = 2
+        assert(self.seq[self.n] == self.seq[2])
+        assert(self.seq[self.o] == self.seq[-2])
+        assert(operator.index(self.o) == -2)
+        assert(operator.index(self.n) == 2)
+        
+    def test_error(self):
+        self.o.ind = 'dumb'
+        self.n.ind = 'bad'
+        myfunc = lambda x, obj: obj.seq[x]
+        self.failUnlessRaises(TypeError, operator.index, self.o)
+        self.failUnlessRaises(TypeError, operator.index, self.n)
+        self.failUnlessRaises(TypeError, myfunc, self.o, self)
+        self.failUnlessRaises(TypeError, myfunc, self.n, self)
+
+    def test_slice(self):
+        self.o.ind = 1
+        self.o2.ind = 3
+        self.n.ind = 2
+        self.n2.ind = 4
+        assert(self.seq[self.o:self.o2] == self.seq[1:3])
+        assert(self.seq[self.n:self.n2] == self.seq[2:4])
+
+
+def test_main():
+    test_support.run_unittest(
+        ListTestCase,
+        TupleTestCase,
+        StringTestCase,
+        UnicodeTestCase
+    )
+
+if __name__ == "__main__":
+    test_main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.