Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
8e6669ad
Commit
8e6669ad
authored
Jul 19, 2001
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test for the repr module, contributed by Nick Mathewson.
This closes SF patch #440826.
parent
1bc8fab0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
108 additions
and
0 deletions
+108
-0
Lib/test/test_repr.py
Lib/test/test_repr.py
+108
-0
No files found.
Lib/test/test_repr.py
0 → 100644
View file @
8e6669ad
"""
Test cases for the repr module
Nick Mathewson
"""
import
unittest
from
test_support
import
run_unittest
from
repr
import
repr
as
r
# Don't shadow builtin repr
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
expected
=
`s`
[:
13
]
+
"..."
+
`s`
[
-
14
:]
eq
(
r
(
s
),
expected
)
eq
(
r
(
"
\
"
'"
),
repr
(
"
\
"
'"
))
s
=
"
\
"
"
*
30
+
"'"
*
100
expected
=
`s`
[:
13
]
+
"..."
+
`s`
[
-
14
:]
eq
(
r
(
s
),
expected
)
def
test_container
(
self
):
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, ...]"
)
# 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, ...}"
)
def
test_numbers
(
self
):
eq
=
self
.
assertEquals
eq
(
r
(
123
),
repr
(
123
))
eq
(
r
(
123L
),
repr
(
123L
))
eq
(
r
(
1.0
/
3
),
repr
(
1.0
/
3
))
n
=
10L
**
100
expected
=
`n`
[:
18
]
+
"..."
+
`n`
[
-
19
:]
eq
(
r
(
n
),
expected
)
def
test_instance
(
self
):
eq
=
self
.
assertEquals
i1
=
ClassWithRepr
(
"a"
)
eq
(
r
(
i1
),
repr
(
i1
))
i2
=
ClassWithRepr
(
"x"
*
1000
)
expected
=
`i2`
[:
13
]
+
"..."
+
`i2`
[
-
14
:]
eq
(
r
(
i2
),
expected
)
i3
=
ClassWithFailingRepr
()
eq
(
r
(
i3
),
(
"<ClassWithFailingRepr instance at %x>"
%
id
(
i3
)))
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
([[[[[[[{}]]]]]]]),
"[[[[[[[...]]]]]]]"
)
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"
)
run_unittest
(
ReprTests
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment