Skip to content

Commit 89b95cf

Browse files
committed
Add test for issue #211 (thanks to Tadeusz Sznuk)
1 parent 5e81a4f commit 89b95cf

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/test_future/test_object.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,47 @@ class MetaClass(type):
230230
class TestClass(with_metaclass(MetaClass, object)):
231231
pass
232232

233+
def test_bool(self):
234+
"""
235+
Issue #211
236+
"""
237+
from builtins import object
238+
239+
class ResultSet(object):
240+
def __len__(self):
241+
return 0
242+
243+
self.assertTrue(bool(ResultSet()) is False)
244+
245+
class ResultSet(object):
246+
def __len__(self):
247+
return 2
248+
249+
self.assertTrue(bool(ResultSet()) is True)
250+
251+
def test_bool2(self):
252+
"""
253+
If __bool__ is defined, the presence or absence of __len__ should
254+
be irrelevant.
255+
"""
256+
from builtins import object
257+
258+
class TrueThing(object):
259+
def __bool__(self):
260+
return True
261+
def __len__(self):
262+
raise RuntimeError('__len__ should not be called')
263+
264+
self.assertTrue(bool(TrueThing()))
265+
266+
class FalseThing(object):
267+
def __bool__(self):
268+
return False
269+
def __len__(self):
270+
raise RuntimeError('__len__ should not be called')
271+
272+
self.assertFalse(bool(FalseThing()))
273+
233274

234275
if __name__ == '__main__':
235276
unittest.main()

0 commit comments

Comments
 (0)