From: Antoine Pitrou Date: Tue, 30 Mar 2010 18:49:45 +0000 (+0000) Subject: Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi. X-Git-Tag: v2.7b1~182 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6032c2506376c76ae15064e64e077349c00e1c78;p=thirdparty%2FPython%2Fcpython.git Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi. --- diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py index 7464c7152fa8..26c0d583c4a0 100644 --- a/Lib/test/test_bool.py +++ b/Lib/test/test_bool.py @@ -45,6 +45,18 @@ class BoolTest(unittest.TestCase): self.assertEqual(int(True), 1) self.assertIsNot(int(True), True) + def test_float(self): + self.assertEqual(float(False), 0.0) + self.assertIsNot(float(False), False) + self.assertEqual(float(True), 1.0) + self.assertIsNot(float(True), True) + + def test_long(self): + self.assertEqual(int(False), 0L) + self.assertIsNot(int(False), False) + self.assertEqual(int(True), 1L) + self.assertIsNot(int(True), True) + def test_math(self): self.assertEqual(+False, 0) self.assertIsNot(+False, False) @@ -157,6 +169,12 @@ class BoolTest(unittest.TestCase): self.assertIs(bool(""), False) self.assertIs(bool(), False) + def test_format(self): + self.assertEqual("%d" % False, "0") + self.assertEqual("%d" % True, "1") + self.assertEqual("%x" % False, "0") + self.assertEqual("%x" % True, "1") + def test_hasattr(self): self.assertIs(hasattr([], "append"), True) self.assertIs(hasattr([], "wobble"), False) @@ -251,6 +269,12 @@ class BoolTest(unittest.TestCase): finally: os.remove(test_support.TESTFN) + def test_types(self): + # types are always true. + for t in [bool, complex, dict, file, float, int, list, long, object, + set, str, tuple, type]: + self.assertIs(bool(t), True) + def test_operator(self): import operator self.assertIs(operator.truth(0), False) diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 3d55bc15ab68..35d74056c950 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -476,6 +476,12 @@ class DecimalExplicitConstructionTest(unittest.TestCase): self.assertRaises(ValueError, Decimal, (1, (4, 10, 4, 9, 1), 2) ) self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 'a', 1), 2) ) + def test_explicit_from_bool(self): + self.assertIs(bool(Decimal(0)), False) + self.assertIs(bool(Decimal(1)), True) + self.assertEqual(Decimal(False), Decimal(0)) + self.assertEqual(Decimal(True), Decimal(1)) + def test_explicit_from_Decimal(self): #positive diff --git a/Lib/test/test_index.py b/Lib/test/test_index.py index abc80418a4d9..a92b4428a32a 100644 --- a/Lib/test/test_index.py +++ b/Lib/test/test_index.py @@ -49,6 +49,8 @@ class BaseTestCase(unittest.TestCase): self.assertEqual(-7L.__index__(), -7) self.assertEqual(self.o.__index__(), 4) self.assertEqual(self.n.__index__(), 5) + self.assertEqual(True.__index__(), 1) + self.assertEqual(False.__index__(), 0) def test_subclasses(self): r = range(10) diff --git a/Misc/ACKS b/Misc/ACKS index 6cfdd5a16434..60644970d776 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -545,6 +545,7 @@ Samuel Nicolary Gustavo Niemeyer Oscar Nierstrasz Hrvoje Niksic +Gregory Nofi Jesse Noller Bill Noon Stefan Norberg diff --git a/Misc/NEWS b/Misc/NEWS index 5752318007cd..e74d4acfdf25 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -164,6 +164,8 @@ C-API Tests ----- +- Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi. + - Issue #8263: Now regrtest.py will report a failure if it receives a KeyboardInterrupt (SIGINT).