From 747f777a2441f3b5192eb5d7b84d5b5f14186c78 Mon Sep 17 00:00:00 2001 From: ChJR Date: Fri, 16 Aug 2019 18:01:39 +0900 Subject: [PATCH] Add test code from the CPython repository(Lib/test/test_bool.py). --- tests/snippets/bools.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/snippets/bools.py b/tests/snippets/bools.py index c33fcff0f..5becc1645 100644 --- a/tests/snippets/bools.py +++ b/tests/snippets/bools.py @@ -137,4 +137,39 @@ class TestLenThrowError: with assertRaises(TypeError): - bool(TestLenThrowError()) \ No newline at end of file + bool(TestLenThrowError()) + +# Verify that TypeError occurs when bad things are returned +# from __bool__(). This isn't really a bool test, but +# it's related. +def check(o): + with assertRaises(TypeError): + bool(o) + +class Foo(object): + def __bool__(self): + return self +check(Foo()) + +class Bar(object): + def __bool__(self): + return "Yes" +check(Bar()) + +class Baz(int): + def __bool__(self): + return self +check(Baz()) + +# __bool__() must return a bool not an int +class Spam(int): + def __bool__(self): + return 1 +check(Spam()) + +class Eggs: + def __len__(self): + return -1 + +with assertRaises(ValueError): + bool(Eggs()) \ No newline at end of file