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
d8472a45
Commit
d8472a45
authored
Mar 02, 2010
by
Florent Xicluna
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor test_dict using assertRaises.
parent
abe6331c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
68 deletions
+47
-68
Lib/test/test_dict.py
Lib/test/test_dict.py
+47
-68
No files found.
Lib/test/test_dict.py
View file @
d8472a45
...
...
@@ -9,24 +9,24 @@ class DictTest(unittest.TestCase):
def
test_constructor
(
self
):
# calling built-in types without argument must return empty
self
.
assertEqual
(
dict
(),
{})
self
.
assert
True
(
dict
()
is
not
{})
self
.
assert
IsNot
(
dict
(),
{})
def
test_literal_constructor
(
self
):
# check literal constructor for different sized dicts (to exercise the BUILD_MAP oparg
# check literal constructor for different sized dicts
# (to exercise the BUILD_MAP oparg).
for
n
in
(
0
,
1
,
6
,
256
,
400
):
items
=
[(
''
.
join
([
random
.
choice
(
string
.
letters
)
for
j
in
range
(
8
)]),
i
)
items
=
[(
''
.
join
(
random
.
sample
(
string
.
letters
,
8
)),
i
)
for
i
in
range
(
n
)]
random
.
shuffle
(
items
)
dictliteral
=
'{'
+
', '
.
join
(
'%r: %d'
%
item
for
item
in
items
)
+
'}'
formatted_items
=
(
'{!r}: {:d}'
.
format
(
k
,
v
)
for
k
,
v
in
items
)
dictliteral
=
'{'
+
', '
.
join
(
formatted_items
)
+
'}'
self
.
assertEqual
(
eval
(
dictliteral
),
dict
(
items
))
def
test_bool
(
self
):
self
.
assert
True
(
not
{}
)
self
.
assert
Is
(
not
{},
True
)
self
.
assertTrue
({
1
:
2
})
self
.
assert
True
(
bool
({})
is
False
)
self
.
assert
True
(
bool
({
1
:
2
})
is
True
)
self
.
assert
Is
(
bool
({}),
False
)
self
.
assert
Is
(
bool
({
1
:
2
}),
True
)
def
test_keys
(
self
):
d
=
{}
...
...
@@ -57,7 +57,7 @@ class DictTest(unittest.TestCase):
def
test_has_key
(
self
):
d
=
{}
self
.
assert
True
(
not
d
.
has_key
(
'a'
))
self
.
assert
False
(
d
.
has_key
(
'a'
))
d
=
{
'a'
:
1
,
'b'
:
2
}
k
=
d
.
keys
()
k
.
sort
()
...
...
@@ -68,7 +68,7 @@ class DictTest(unittest.TestCase):
def
test_contains
(
self
):
d
=
{}
self
.
assertNotIn
(
'a'
,
d
)
self
.
assert
True
(
not
(
'a'
in
d
)
)
self
.
assert
False
(
'a'
in
d
)
self
.
assertTrue
(
'a'
not
in
d
)
d
=
{
'a'
:
1
,
'b'
:
2
}
self
.
assertIn
(
'a'
,
d
)
...
...
@@ -207,7 +207,7 @@ class DictTest(unittest.TestCase):
def
test_fromkeys
(
self
):
self
.
assertEqual
(
dict
.
fromkeys
(
'abc'
),
{
'a'
:
None
,
'b'
:
None
,
'c'
:
None
})
d
=
{}
self
.
assert
True
(
not
(
d
.
fromkeys
(
'abc'
)
is
d
)
)
self
.
assert
IsNot
(
d
.
fromkeys
(
'abc'
),
d
)
self
.
assertEqual
(
d
.
fromkeys
(
'abc'
),
{
'a'
:
None
,
'b'
:
None
,
'c'
:
None
})
self
.
assertEqual
(
d
.
fromkeys
((
4
,
5
),
0
),
{
4
:
0
,
5
:
0
})
self
.
assertEqual
(
d
.
fromkeys
([]),
{})
...
...
@@ -218,8 +218,8 @@ class DictTest(unittest.TestCase):
class
dictlike
(
dict
):
pass
self
.
assertEqual
(
dictlike
.
fromkeys
(
'a'
),
{
'a'
:
None
})
self
.
assertEqual
(
dictlike
().
fromkeys
(
'a'
),
{
'a'
:
None
})
self
.
assert
True
(
type
(
dictlike
.
fromkeys
(
'a'
))
is
dictlike
)
self
.
assert
True
(
type
(
dictlike
().
fromkeys
(
'a'
))
is
dictlike
)
self
.
assert
IsInstance
(
dictlike
.
fromkeys
(
'a'
),
dictlike
)
self
.
assert
IsInstance
(
dictlike
().
fromkeys
(
'a'
),
dictlike
)
class
mydict
(
dict
):
def
__new__
(
cls
):
return
UserDict
.
UserDict
()
...
...
@@ -262,10 +262,10 @@ class DictTest(unittest.TestCase):
def
test_get
(
self
):
d
=
{}
self
.
assert
True
(
d
.
get
(
'c'
)
is
None
)
self
.
assert
Is
(
d
.
get
(
'c'
),
None
)
self
.
assertEqual
(
d
.
get
(
'c'
,
3
),
3
)
d
=
{
'a'
:
1
,
'b'
:
2
}
self
.
assert
True
(
d
.
get
(
'c'
)
is
None
)
d
=
{
'a'
:
1
,
'b'
:
2
}
self
.
assert
Is
(
d
.
get
(
'c'
),
None
)
self
.
assertEqual
(
d
.
get
(
'c'
,
3
),
3
)
self
.
assertEqual
(
d
.
get
(
'a'
),
1
)
self
.
assertEqual
(
d
.
get
(
'a'
,
3
),
1
)
...
...
@@ -275,9 +275,9 @@ class DictTest(unittest.TestCase):
def
test_setdefault
(
self
):
# dict.setdefault()
d
=
{}
self
.
assert
True
(
d
.
setdefault
(
'key0'
)
is
None
)
self
.
assert
Is
(
d
.
setdefault
(
'key0'
),
None
)
d
.
setdefault
(
'key0'
,
[])
self
.
assert
True
(
d
.
setdefault
(
'key0'
)
is
None
)
self
.
assert
Is
(
d
.
setdefault
(
'key0'
),
None
)
d
.
setdefault
(
'key'
,
[]).
append
(
3
)
self
.
assertEqual
(
d
[
'key'
][
0
],
3
)
d
.
setdefault
(
'key'
,
[]).
append
(
4
)
...
...
@@ -319,9 +319,9 @@ class DictTest(unittest.TestCase):
self
.
assertEqual
(
va
,
int
(
ka
))
kb
,
vb
=
tb
=
b
.
popitem
()
self
.
assertEqual
(
vb
,
int
(
kb
))
self
.
assert
True
(
not
(
copymode
<
0
and
ta
!=
tb
)
)
self
.
assert
True
(
not
a
)
self
.
assert
True
(
not
b
)
self
.
assert
False
(
copymode
<
0
and
ta
!=
tb
)
self
.
assert
False
(
a
)
self
.
assert
False
(
b
)
d
=
{}
self
.
assertRaises
(
KeyError
,
d
.
popitem
)
...
...
@@ -338,8 +338,8 @@ class DictTest(unittest.TestCase):
self
.
assertRaises
(
KeyError
,
d
.
pop
,
k
)
# verify longs/ints get same value when key > 32 bits
(for 64-bit archs)
#
see SF bug #689659
# verify longs/ints get same value when key > 32 bits
#
(for 64-bit archs). See SF bug #689659.
x
=
4503599627370496L
y
=
4503599627370496
h
=
{
x
:
'anything'
,
y
:
'something else'
}
...
...
@@ -367,15 +367,12 @@ class DictTest(unittest.TestCase):
self
.
assertRaises
(
Exc
,
d
.
pop
,
x
)
def
test_mutatingiteration
(
self
):
# changing dict size during iteration
d
=
{}
d
[
1
]
=
1
try
:
with
self
.
assertRaises
(
RuntimeError
)
:
for
i
in
d
:
d
[
i
+
1
]
=
1
except
RuntimeError
:
pass
else
:
self
.
fail
(
"changing dict size during iteration doesn't raise Error"
)
def
test_repr
(
self
):
d
=
{}
...
...
@@ -396,8 +393,8 @@ class DictTest(unittest.TestCase):
self
.
assertRaises
(
Exc
,
repr
,
d
)
def
test_le
(
self
):
self
.
assert
True
(
not
({}
<
{})
)
self
.
assert
True
(
not
({
1
:
2
}
<
{
1L
:
2L
})
)
self
.
assert
False
({}
<
{}
)
self
.
assert
False
({
1
:
2
}
<
{
1L
:
2L
}
)
class
Exc
(
Exception
):
pass
...
...
@@ -409,17 +406,14 @@ class DictTest(unittest.TestCase):
d1
=
{
BadCmp
():
1
}
d2
=
{
1
:
1
}
try
:
with
self
.
assertRaises
(
Exc
):
d1
<
d2
except
Exc
:
pass
else
:
self
.
fail
(
"< didn't raise Exc"
)
def
test_missing
(
self
):
# Make sure dict doesn't have a __missing__ method
self
.
assert
Equal
(
hasattr
(
dict
,
"__missing__"
),
False
)
self
.
assert
Equal
(
hasattr
({},
"__missing__"
),
False
)
self
.
assert
False
(
hasattr
(
dict
,
"__missing__"
)
)
self
.
assert
False
(
hasattr
({},
"__missing__"
)
)
# Test several cases:
# (D) subclass defines __missing__ method returning a value
# (E) subclass defines __missing__ method raising RuntimeError
...
...
@@ -434,46 +428,37 @@ class DictTest(unittest.TestCase):
self
.
assertNotIn
(
2
,
d
)
self
.
assertNotIn
(
2
,
d
.
keys
())
self
.
assertEqual
(
d
[
2
],
42
)
class
E
(
dict
):
def
__missing__
(
self
,
key
):
raise
RuntimeError
(
key
)
e
=
E
()
try
:
with
self
.
assertRaises
(
RuntimeError
)
as
c
:
e
[
42
]
except
RuntimeError
,
err
:
self
.
assertEqual
(
err
.
args
,
(
42
,))
else
:
self
.
fail
(
"e[42] didn't raise RuntimeError"
)
self
.
assertEqual
(
c
.
exception
.
args
,
(
42
,))
class
F
(
dict
):
def
__init__
(
self
):
# An instance variable __missing__ should have no effect
self
.
__missing__
=
lambda
key
:
None
f
=
F
()
try
:
with
self
.
assertRaises
(
KeyError
)
as
c
:
f
[
42
]
except
KeyError
,
err
:
self
.
assertEqual
(
err
.
args
,
(
42
,))
else
:
self
.
fail
(
"f[42] didn't raise KeyError"
)
self
.
assertEqual
(
c
.
exception
.
args
,
(
42
,))
class
G
(
dict
):
pass
g
=
G
()
try
:
with
self
.
assertRaises
(
KeyError
)
as
c
:
g
[
42
]
except
KeyError
,
err
:
self
.
assertEqual
(
err
.
args
,
(
42
,))
else
:
self
.
fail
(
"g[42] didn't raise KeyError"
)
self
.
assertEqual
(
c
.
exception
.
args
,
(
42
,))
def
test_tuple_keyerror
(
self
):
# SF #1576657
d
=
{}
try
:
with
self
.
assertRaises
(
KeyError
)
as
c
:
d
[(
1
,)]
except
KeyError
,
e
:
self
.
assertEqual
(
e
.
args
,
((
1
,),))
else
:
self
.
fail
(
"missing KeyError"
)
self
.
assertEqual
(
c
.
exception
.
args
,
((
1
,),))
def
test_bad_key
(
self
):
# Dictionary lookups should fail if __cmp__() raises an exception.
...
...
@@ -501,12 +486,8 @@ class DictTest(unittest.TestCase):
'd.setdefault(x2, 42)'
,
'd.pop(x2)'
,
'd.update({x2: 2})'
]:
try
:
with
self
.
assertRaises
(
CustomException
)
:
exec
stmt
in
locals
()
except
CustomException
:
pass
else
:
self
.
fail
(
"Statement didn't raise exception"
)
def
test_resize1
(
self
):
# Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
...
...
@@ -549,11 +530,9 @@ class DictTest(unittest.TestCase):
def
test_empty_presized_dict_in_freelist
(
self
):
# Bug #3537: if an empty but presized dict with a size larger
# than 7 was in the freelist, it triggered an assertion failure
try
:
d
=
{
'a'
:
1
/
0
,
'b'
:
None
,
'c'
:
None
,
'd'
:
None
,
'e'
:
None
,
with
self
.
assertRaises
(
ZeroDivisionError
)
:
d
=
{
'a'
:
1
//
0
,
'b'
:
None
,
'c'
:
None
,
'd'
:
None
,
'e'
:
None
,
'f'
:
None
,
'g'
:
None
,
'h'
:
None
}
except
ZeroDivisionError
:
pass
d
=
{}
def
test_container_iterator
(
self
):
...
...
@@ -568,7 +547,7 @@ class DictTest(unittest.TestCase):
obj
.
x
=
i
(
container
)
del
obj
,
container
gc
.
collect
()
self
.
assert
True
(
ref
()
is
None
,
"Cycle was not collected"
)
self
.
assert
Is
(
ref
(),
None
,
"Cycle was not collected"
)
def
_not_tracked
(
self
,
t
):
# Nested containers can take several collections to untrack
...
...
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