Skip to content

Commit 747f777

Browse files
committed
Add test code from the CPython repository(Lib/test/test_bool.py).
1 parent 7a680c3 commit 747f777

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

tests/snippets/bools.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,39 @@ def __len__(self):
137137

138138

139139
with assertRaises(TypeError):
140-
bool(TestLenThrowError())
140+
bool(TestLenThrowError())
141+
142+
# Verify that TypeError occurs when bad things are returned
143+
# from __bool__(). This isn't really a bool test, but
144+
# it's related.
145+
def check(o):
146+
with assertRaises(TypeError):
147+
bool(o)
148+
149+
class Foo(object):
150+
def __bool__(self):
151+
return self
152+
check(Foo())
153+
154+
class Bar(object):
155+
def __bool__(self):
156+
return "Yes"
157+
check(Bar())
158+
159+
class Baz(int):
160+
def __bool__(self):
161+
return self
162+
check(Baz())
163+
164+
# __bool__() must return a bool not an int
165+
class Spam(int):
166+
def __bool__(self):
167+
return 1
168+
check(Spam())
169+
170+
class Eggs:
171+
def __len__(self):
172+
return -1
173+
174+
with assertRaises(ValueError):
175+
bool(Eggs())

0 commit comments

Comments
 (0)