2001-07-19 19:27:56 -03:00
"""
Test cases for the repr module
Nick Mathewson
"""
2001-08-24 15:37:32 -03:00
import sys
import os
2006-05-30 04:21:10 -03:00
import shutil
2001-07-19 19:27:56 -03:00
import unittest
2001-08-24 15:37:32 -03:00
2002-07-23 16:04:11 -03:00
from test . test_support import run_unittest
2008-05-23 02:03:59 -03:00
from repr import repr as r # Don't shadow builtin repr
from repr import Repr
2001-07-19 19:27:56 -03:00
def nestedTuple ( nesting ) :
t = ( )
for i in range ( nesting ) :
t = ( t , )
return t
class ReprTests ( unittest . TestCase ) :
def test_string ( self ) :
eq = self . assertEquals
eq ( r ( " abc " ) , " ' abc ' " )
eq ( r ( " abcdefghijklmnop " ) , " ' abcdefghijklmnop ' " )
s = " a " * 30 + " b " * 30
2004-02-12 13:35:32 -04:00
expected = repr ( s ) [ : 13 ] + " ... " + repr ( s ) [ - 14 : ]
2001-07-19 19:27:56 -03:00
eq ( r ( s ) , expected )
2001-08-09 18:40:30 -03:00
2001-07-19 19:27:56 -03:00
eq ( r ( " \" ' " ) , repr ( " \" ' " ) )
s = " \" " * 30 + " ' " * 100
2004-02-12 13:35:32 -04:00
expected = repr ( s ) [ : 13 ] + " ... " + repr ( s ) [ - 14 : ]
2001-07-19 19:27:56 -03:00
eq ( r ( s ) , expected )
2007-06-11 04:29:43 -03:00
def test_tuple ( self ) :
eq = self . assertEquals
eq ( r ( ( 1 , ) ) , " (1,) " )
t3 = ( 1 , 2 , 3 )
eq ( r ( t3 ) , " (1, 2, 3) " )
r2 = Repr ( )
r2 . maxtuple = 2
expected = repr ( t3 ) [ : - 2 ] + " ...) "
eq ( r2 . repr ( t3 ) , expected )
2001-07-19 19:27:56 -03:00
def test_container ( self ) :
2003-02-05 14:29:34 -04:00
from array import array
2004-05-21 20:01:18 -03:00
from collections import deque
2003-02-05 14:29:34 -04:00
2001-07-19 19:27:56 -03:00
eq = self . assertEquals
# Tuples give up after 6 elements
eq ( r ( ( ) ) , " () " )
eq ( r ( ( 1 , ) ) , " (1,) " )
eq ( r ( ( 1 , 2 , 3 ) ) , " (1, 2, 3) " )
eq ( r ( ( 1 , 2 , 3 , 4 , 5 , 6 ) ) , " (1, 2, 3, 4, 5, 6) " )
eq ( r ( ( 1 , 2 , 3 , 4 , 5 , 6 , 7 ) ) , " (1, 2, 3, 4, 5, 6, ...) " )
# Lists give up after 6 as well
eq ( r ( [ ] ) , " [] " )
eq ( r ( [ 1 ] ) , " [1] " )
eq ( r ( [ 1 , 2 , 3 ] ) , " [1, 2, 3] " )
eq ( r ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) , " [1, 2, 3, 4, 5, 6] " )
eq ( r ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ) , " [1, 2, 3, 4, 5, 6, ...] " )
2004-05-21 07:00:15 -03:00
# Sets give up after 6 as well
eq ( r ( set ( [ ] ) ) , " set([]) " )
eq ( r ( set ( [ 1 ] ) ) , " set([1]) " )
eq ( r ( set ( [ 1 , 2 , 3 ] ) ) , " set([1, 2, 3]) " )
eq ( r ( set ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ) , " set([1, 2, 3, 4, 5, 6]) " )
eq ( r ( set ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ) ) , " set([1, 2, 3, 4, 5, 6, ...]) " )
# Frozensets give up after 6 as well
eq ( r ( frozenset ( [ ] ) ) , " frozenset([]) " )
eq ( r ( frozenset ( [ 1 ] ) ) , " frozenset([1]) " )
eq ( r ( frozenset ( [ 1 , 2 , 3 ] ) ) , " frozenset([1, 2, 3]) " )
eq ( r ( frozenset ( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ) , " frozenset([1, 2, 3, 4, 5, 6]) " )
eq ( r ( frozenset ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ) ) , " frozenset([1, 2, 3, 4, 5, 6, ...]) " )
2004-05-21 20:01:18 -03:00
# collections.deque after 6
eq ( r ( deque ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ) ) , " deque([1, 2, 3, 4, 5, 6, ...]) " )
2001-07-19 19:27:56 -03:00
# Dictionaries give up after 4.
eq ( r ( { } ) , " {} " )
d = { ' alice ' : 1 , ' bob ' : 2 , ' charles ' : 3 , ' dave ' : 4 }
eq ( r ( d ) , " { ' alice ' : 1, ' bob ' : 2, ' charles ' : 3, ' dave ' : 4} " )
d [ ' arthur ' ] = 1
eq ( r ( d ) , " { ' alice ' : 1, ' arthur ' : 1, ' bob ' : 2, ' charles ' : 3, ...} " )
2003-02-05 14:29:34 -04:00
# array.array after 5.
eq ( r ( array ( ' i ' ) ) , " array( ' i ' , []) " )
eq ( r ( array ( ' i ' , [ 1 ] ) ) , " array( ' i ' , [1]) " )
eq ( r ( array ( ' i ' , [ 1 , 2 ] ) ) , " array( ' i ' , [1, 2]) " )
eq ( r ( array ( ' i ' , [ 1 , 2 , 3 ] ) ) , " array( ' i ' , [1, 2, 3]) " )
eq ( r ( array ( ' i ' , [ 1 , 2 , 3 , 4 ] ) ) , " array( ' i ' , [1, 2, 3, 4]) " )
eq ( r ( array ( ' i ' , [ 1 , 2 , 3 , 4 , 5 ] ) ) , " array( ' i ' , [1, 2, 3, 4, 5]) " )
eq ( r ( array ( ' i ' , [ 1 , 2 , 3 , 4 , 5 , 6 ] ) ) ,
" array( ' i ' , [1, 2, 3, 4, 5, ...]) " )
2001-07-19 19:27:56 -03:00
def test_numbers ( self ) :
eq = self . assertEquals
eq ( r ( 123 ) , repr ( 123 ) )
eq ( r ( 123 L ) , repr ( 123 L ) )
eq ( r ( 1.0 / 3 ) , repr ( 1.0 / 3 ) )
n = 10 L * * 100
2004-02-12 13:35:32 -04:00
expected = repr ( n ) [ : 18 ] + " ... " + repr ( n ) [ - 19 : ]
2001-07-19 19:27:56 -03:00
eq ( r ( n ) , expected )
def test_instance ( self ) :
eq = self . assertEquals
i1 = ClassWithRepr ( " a " )
eq ( r ( i1 ) , repr ( i1 ) )
2001-08-09 18:40:30 -03:00
2001-07-19 19:27:56 -03:00
i2 = ClassWithRepr ( " x " * 1000 )
2004-02-12 13:35:32 -04:00
expected = repr ( i2 ) [ : 13 ] + " ... " + repr ( i2 ) [ - 14 : ]
2001-07-19 19:27:56 -03:00
eq ( r ( i2 ) , expected )
i3 = ClassWithFailingRepr ( )
eq ( r ( i3 ) , ( " <ClassWithFailingRepr instance at %x > " % id ( i3 ) ) )
2001-09-04 23:26:26 -03:00
s = r ( ClassWithFailingRepr )
2009-06-30 19:57:08 -03:00
self . assertTrue ( s . startswith ( " <class " ) )
self . assertTrue ( s . endswith ( " > " ) )
self . assertTrue ( s . find ( " ... " ) == 8 )
2001-09-04 23:26:26 -03:00
2001-08-24 15:37:32 -03:00
def test_file ( self ) :
fp = open ( unittest . __file__ )
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( fp ) . startswith (
2001-08-24 15:37:32 -03:00
" <open file ' %s ' , mode ' r ' at 0x " % unittest . __file__ ) )
fp . close ( )
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( fp ) . startswith (
2001-08-24 15:37:32 -03:00
" <closed file ' %s ' , mode ' r ' at 0x " % unittest . __file__ ) )
def test_lambda ( self ) :
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( lambda x : x ) . startswith (
2005-10-21 15:11:40 -03:00
" <function <lambda " ) )
2001-08-24 15:37:32 -03:00
# XXX anonymous functions? see func_repr
def test_builtin_function ( self ) :
eq = self . assertEquals
# Functions
eq ( repr ( hash ) , ' <built-in function hash> ' )
# Methods
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( ' ' . split ) . startswith (
2001-08-24 15:37:32 -03:00
' <built-in method split of str object at 0x ' ) )
def test_xrange ( self ) :
eq = self . assertEquals
eq ( repr ( xrange ( 1 ) ) , ' xrange(1) ' )
eq ( repr ( xrange ( 1 , 2 ) ) , ' xrange(1, 2) ' )
eq ( repr ( xrange ( 1 , 2 , 3 ) ) , ' xrange(1, 4, 3) ' )
2001-07-19 19:27:56 -03:00
def test_nesting ( self ) :
eq = self . assertEquals
# everything is meant to give up after 6 levels.
eq ( r ( [ [ [ [ [ [ [ ] ] ] ] ] ] ] ) , " [[[[[[[]]]]]]] " )
eq ( r ( [ [ [ [ [ [ [ [ ] ] ] ] ] ] ] ] ) , " [[[[[[[...]]]]]]] " )
eq ( r ( nestedTuple ( 6 ) ) , " (((((((),),),),),),) " )
eq ( r ( nestedTuple ( 7 ) ) , " (((((((...),),),),),),) " )
eq ( r ( { nestedTuple ( 5 ) : nestedTuple ( 5 ) } ) ,
" { ((((((),),),),),): ((((((),),),),),)} " )
eq ( r ( { nestedTuple ( 6 ) : nestedTuple ( 6 ) } ) ,
" { ((((((...),),),),),): ((((((...),),),),),)} " )
eq ( r ( [ [ [ [ [ [ { } ] ] ] ] ] ] ) , " [[[[[[ {} ]]]]]] " )
eq ( r ( [ [ [ [ [ [ [ { } ] ] ] ] ] ] ] ) , " [[[[[[[...]]]]]]] " )
2001-08-24 15:37:32 -03:00
def test_buffer ( self ) :
# XXX doesn't test buffers with no b_base or read-write buffers (see
# bufferobject.c). The test is fairly incomplete too. Sigh.
x = buffer ( ' foo ' )
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( x ) . startswith ( ' <read-only buffer for 0x ' ) )
2001-08-24 15:37:32 -03:00
def test_cell ( self ) :
# XXX Hmm? How to get at a cell object?
pass
def test_descriptors ( self ) :
eq = self . assertEquals
# method descriptors
2001-10-29 18:25:45 -04:00
eq ( repr ( dict . items ) , " <method ' items ' of ' dict ' objects> " )
2001-08-24 15:37:32 -03:00
# XXX member descriptors
# XXX attribute descriptors
# XXX slot descriptors
# static and class methods
class C :
def foo ( cls ) : pass
x = staticmethod ( C . foo )
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( x ) . startswith ( ' <staticmethod object at 0x ' ) )
2001-08-24 15:37:32 -03:00
x = classmethod ( C . foo )
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( x ) . startswith ( ' <classmethod object at 0x ' ) )
2001-08-24 15:37:32 -03:00
2007-09-12 16:00:07 -03:00
def test_unsortable ( self ) :
# Repr.repr() used to call sorted() on sets, frozensets and dicts
# without taking into account that not all objects are comparable
x = set ( [ 1 j , 2 j , 3 j ] )
y = frozenset ( x )
z = { 1 j : 1 , 2 j : 2 }
r ( x )
r ( y )
r ( z )
2001-08-24 15:37:32 -03:00
def touch ( path , text = ' ' ) :
fp = open ( path , ' w ' )
fp . write ( text )
fp . close ( )
class LongReprTest ( unittest . TestCase ) :
def setUp ( self ) :
longname = ' areallylongpackageandmodulenametotestreprtruncation '
self . pkgname = os . path . join ( longname )
self . subpkgname = os . path . join ( longname , longname )
# Make the package and subpackage
2006-05-30 04:21:10 -03:00
shutil . rmtree ( self . pkgname , ignore_errors = True )
2001-08-24 15:37:32 -03:00
os . mkdir ( self . pkgname )
2001-10-24 17:42:55 -03:00
touch ( os . path . join ( self . pkgname , ' __init__ ' + os . extsep + ' py ' ) )
2006-05-30 04:21:10 -03:00
shutil . rmtree ( self . subpkgname , ignore_errors = True )
2001-08-24 15:37:32 -03:00
os . mkdir ( self . subpkgname )
2001-10-24 17:42:55 -03:00
touch ( os . path . join ( self . subpkgname , ' __init__ ' + os . extsep + ' py ' ) )
2001-08-24 15:37:32 -03:00
# Remember where we are
self . here = os . getcwd ( )
sys . path . insert ( 0 , self . here )
def tearDown ( self ) :
actions = [ ]
2008-05-08 19:09:54 -03:00
for dirpath , dirnames , filenames in os . walk ( self . pkgname ) :
for name in dirnames + filenames :
actions . append ( os . path . join ( dirpath , name ) )
2001-08-24 15:37:32 -03:00
actions . append ( self . pkgname )
actions . sort ( )
actions . reverse ( )
for p in actions :
if os . path . isdir ( p ) :
os . rmdir ( p )
else :
os . remove ( p )
del sys . path [ 0 ]
def test_module ( self ) :
eq = self . assertEquals
2001-10-24 17:42:55 -03:00
touch ( os . path . join ( self . subpkgname , self . pkgname + os . extsep + ' py ' ) )
2001-08-24 15:37:32 -03:00
from areallylongpackageandmodulenametotestreprtruncation . areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation
eq ( repr ( areallylongpackageandmodulenametotestreprtruncation ) ,
2003-01-16 00:56:52 -04:00
" <module ' %s ' from ' %s ' > " % ( areallylongpackageandmodulenametotestreprtruncation . __name__ , areallylongpackageandmodulenametotestreprtruncation . __file__ ) )
2001-12-28 20:25:42 -04:00
eq ( repr ( sys ) , " <module ' sys ' (built-in)> " )
2001-08-24 15:37:32 -03:00
def test_type ( self ) :
eq = self . assertEquals
2001-10-24 17:42:55 -03:00
touch ( os . path . join ( self . subpkgname , ' foo ' + os . extsep + ' py ' ) , ''' \
2001-08-24 15:37:32 -03:00
class foo ( object ) :
pass
''' )
from areallylongpackageandmodulenametotestreprtruncation . areallylongpackageandmodulenametotestreprtruncation import foo
eq ( repr ( foo . foo ) ,
2003-01-16 00:56:52 -04:00
" <class ' %s .foo ' > " % foo . __name__ )
2001-08-24 15:37:32 -03:00
def test_object ( self ) :
# XXX Test the repr of a type with a really long tp_name but with no
# tp_repr. WIBNI we had ::Inline? :)
pass
def test_class ( self ) :
2001-10-24 17:42:55 -03:00
touch ( os . path . join ( self . subpkgname , ' bar ' + os . extsep + ' py ' ) , ''' \
2001-08-24 15:37:32 -03:00
class bar :
pass
''' )
from areallylongpackageandmodulenametotestreprtruncation . areallylongpackageandmodulenametotestreprtruncation import bar
2003-01-16 00:56:52 -04:00
# Module name may be prefixed with "test.", depending on how run.
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( bar . bar ) . startswith (
2003-01-16 00:56:52 -04:00
" <class %s .bar at 0x " % bar . __name__ ) )
2001-08-24 15:37:32 -03:00
def test_instance ( self ) :
2001-10-24 17:42:55 -03:00
touch ( os . path . join ( self . subpkgname , ' baz ' + os . extsep + ' py ' ) , ''' \
2001-08-24 15:37:32 -03:00
class baz :
pass
''' )
from areallylongpackageandmodulenametotestreprtruncation . areallylongpackageandmodulenametotestreprtruncation import baz
ibaz = baz . baz ( )
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( ibaz ) . startswith (
2003-01-16 00:56:52 -04:00
" < %s .baz instance at 0x " % baz . __name__ ) )
2001-08-24 15:37:32 -03:00
def test_method ( self ) :
eq = self . assertEquals
2001-10-24 17:42:55 -03:00
touch ( os . path . join ( self . subpkgname , ' qux ' + os . extsep + ' py ' ) , ''' \
2001-08-24 15:37:32 -03:00
class aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :
def amethod ( self ) : pass
''' )
from areallylongpackageandmodulenametotestreprtruncation . areallylongpackageandmodulenametotestreprtruncation import qux
# Unbound methods first
eq ( repr ( qux . aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa . amethod ) ,
' <unbound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod> ' )
# Bound method next
iqux = qux . aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ( )
2009-06-30 19:57:08 -03:00
self . assertTrue ( repr ( iqux . amethod ) . startswith (
2003-01-16 00:56:52 -04:00
' <bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of < %s .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa instance at 0x ' \
% ( qux . __name__ , ) ) )
2001-08-24 15:37:32 -03:00
def test_builtin_function ( self ) :
# XXX test built-in functions and methods with really long names
pass
2001-07-19 19:27:56 -03:00
class ClassWithRepr :
def __init__ ( self , s ) :
self . s = s
def __repr__ ( self ) :
return " ClassWithLongRepr( %r ) " % self . s
class ClassWithFailingRepr :
def __repr__ ( self ) :
raise Exception ( " This should be caught by Repr.repr_instance " )
2001-09-20 18:33:42 -03:00
def test_main ( ) :
run_unittest ( ReprTests )
if os . name != ' mac ' :
run_unittest ( LongReprTest )
if __name__ == " __main__ " :
test_main ( )