mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 03:44:55 +00:00 
			
		
		
		
	Convert test_contains, test_crypt, and test_select to unittest.
Patch from GHOP 294 by David Marek.
This commit is contained in:
		
							parent
							
								
									b8d37359cd
								
							
						
					
					
						commit
						2e0f9f3dd9
					
				
					 4 changed files with 60 additions and 69 deletions
				
			
		| 
						 | 
					@ -1,11 +1,16 @@
 | 
				
			||||||
#! /usr/bin/env python
 | 
					from test import test_support
 | 
				
			||||||
"""Simple test script for cryptmodule.c
 | 
					import unittest
 | 
				
			||||||
   Roger E. Masse
 | 
					 | 
				
			||||||
"""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from test.test_support import verbose
 | 
					 | 
				
			||||||
import crypt
 | 
					import crypt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
c = crypt.crypt('mypassword', 'ab')
 | 
					class CryptTestCase(unittest.TestCase):
 | 
				
			||||||
if verbose:
 | 
					
 | 
				
			||||||
 | 
					    def test_crypt(self):
 | 
				
			||||||
 | 
					        c = crypt.crypt('mypassword', 'ab')
 | 
				
			||||||
 | 
					        if test_support.verbose:
 | 
				
			||||||
            print 'Test encryption: ', c
 | 
					            print 'Test encryption: ', c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_main():
 | 
				
			||||||
 | 
					    test_support.run_unittest(CryptTestCase)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == "__main__":
 | 
				
			||||||
 | 
					    test_main()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,70 +1,53 @@
 | 
				
			||||||
# Testing select module
 | 
					from test import test_support
 | 
				
			||||||
from test.test_support import verbose, reap_children
 | 
					import unittest
 | 
				
			||||||
import select
 | 
					import select
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# test some known error conditions
 | 
					class SelectTestCase(unittest.TestCase):
 | 
				
			||||||
try:
 | 
					 | 
				
			||||||
    rfd, wfd, xfd = select.select(1, 2, 3)
 | 
					 | 
				
			||||||
except TypeError:
 | 
					 | 
				
			||||||
    pass
 | 
					 | 
				
			||||||
else:
 | 
					 | 
				
			||||||
    print 'expected TypeError exception not raised'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Nope:
 | 
					    class Nope:
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Almost:
 | 
					    class Almost:
 | 
				
			||||||
        def fileno(self):
 | 
					        def fileno(self):
 | 
				
			||||||
            return 'fileno'
 | 
					            return 'fileno'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
try:
 | 
					    def test_error_conditions(self):
 | 
				
			||||||
    rfd, wfd, xfd = select.select([Nope()], [], [])
 | 
					        self.assertRaises(TypeError, select.select, 1, 2, 3)
 | 
				
			||||||
except TypeError:
 | 
					        self.assertRaises(TypeError, select.select, [self.Nope()], [], [])
 | 
				
			||||||
    pass
 | 
					        self.assertRaises(TypeError, select.select, [self.Almost()], [], [])
 | 
				
			||||||
else:
 | 
					        self.assertRaises(TypeError, select.select, [], [], [], "not a number")
 | 
				
			||||||
    print 'expected TypeError exception not raised'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
try:
 | 
					    def test_select(self):
 | 
				
			||||||
    rfd, wfd, xfd = select.select([Almost()], [], [])
 | 
					 | 
				
			||||||
except TypeError:
 | 
					 | 
				
			||||||
    pass
 | 
					 | 
				
			||||||
else:
 | 
					 | 
				
			||||||
    print 'expected TypeError exception not raised'
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
try:
 | 
					 | 
				
			||||||
    rfd, wfd, xfd = select.select([], [], [], 'not a number')
 | 
					 | 
				
			||||||
except TypeError:
 | 
					 | 
				
			||||||
    pass
 | 
					 | 
				
			||||||
else:
 | 
					 | 
				
			||||||
    print 'expected TypeError exception not raised'
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test():
 | 
					 | 
				
			||||||
    import sys
 | 
					 | 
				
			||||||
        if sys.platform[:3] in ('win', 'mac', 'os2', 'riscos'):
 | 
					        if sys.platform[:3] in ('win', 'mac', 'os2', 'riscos'):
 | 
				
			||||||
        if verbose:
 | 
					            if test_support.verbose:
 | 
				
			||||||
                print "Can't test select easily on", sys.platform
 | 
					                print "Can't test select easily on", sys.platform
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
 | 
					        cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
 | 
				
			||||||
        p = os.popen(cmd, 'r')
 | 
					        p = os.popen(cmd, 'r')
 | 
				
			||||||
        for tout in (0, 1, 2, 4, 8, 16) + (None,)*10:
 | 
					        for tout in (0, 1, 2, 4, 8, 16) + (None,)*10:
 | 
				
			||||||
        if verbose:
 | 
					            if test_support.verbose:
 | 
				
			||||||
                print 'timeout =', tout
 | 
					                print 'timeout =', tout
 | 
				
			||||||
            rfd, wfd, xfd = select.select([p], [], [], tout)
 | 
					            rfd, wfd, xfd = select.select([p], [], [], tout)
 | 
				
			||||||
            if (rfd, wfd, xfd) == ([], [], []):
 | 
					            if (rfd, wfd, xfd) == ([], [], []):
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
            if (rfd, wfd, xfd) == ([p], [], []):
 | 
					            if (rfd, wfd, xfd) == ([p], [], []):
 | 
				
			||||||
                line = p.readline()
 | 
					                line = p.readline()
 | 
				
			||||||
            if verbose:
 | 
					                if test_support.verbose:
 | 
				
			||||||
                    print repr(line)
 | 
					                    print repr(line)
 | 
				
			||||||
                if not line:
 | 
					                if not line:
 | 
				
			||||||
                if verbose:
 | 
					                    if test_support.verbose:
 | 
				
			||||||
                        print 'EOF'
 | 
					                        print 'EOF'
 | 
				
			||||||
                    break
 | 
					                    break
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
        print 'Unexpected return values from select():', rfd, wfd, xfd
 | 
					            self.fail('Unexpected return values from select():', rfd, wfd, xfd)
 | 
				
			||||||
        p.close()
 | 
					        p.close()
 | 
				
			||||||
    reap_children()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
test()
 | 
					
 | 
				
			||||||
 | 
					def test_main():
 | 
				
			||||||
 | 
					    test_support.run_unittest(SelectTestCase)
 | 
				
			||||||
 | 
					    test_support.reap_children()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == "__main__":
 | 
				
			||||||
 | 
					    test_main()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -428,6 +428,7 @@ Steve Majewski
 | 
				
			||||||
Grzegorz Makarewicz
 | 
					Grzegorz Makarewicz
 | 
				
			||||||
Ken Manheimer
 | 
					Ken Manheimer
 | 
				
			||||||
Vladimir Marangozov
 | 
					Vladimir Marangozov
 | 
				
			||||||
 | 
					David Marek
 | 
				
			||||||
Doug Marien
 | 
					Doug Marien
 | 
				
			||||||
Alex Martelli
 | 
					Alex Martelli
 | 
				
			||||||
Anthony Martin
 | 
					Anthony Martin
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,6 +50,8 @@ Library
 | 
				
			||||||
Tests
 | 
					Tests
 | 
				
			||||||
-----
 | 
					-----
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- GHOP 294: Convert test_contains, test_crypt, and test_select to unittest.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- GHOP 238: Convert test_tokenize to use doctest.
 | 
					- GHOP 238: Convert test_tokenize to use doctest.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- GHOP 237: Rewrite test_thread using unittest.
 | 
					- GHOP 237: Rewrite test_thread using unittest.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue