Issue #8248: Add some tests for the bool type. Patch by Gregory Nofi.

This commit is contained in:
Antoine Pitrou 2010-03-30 18:49:45 +00:00
parent 7e213255ce
commit 6032c25063
5 changed files with 35 additions and 0 deletions

View File

@ -45,6 +45,18 @@ class BoolTest(unittest.TestCase):
self.assertEqual(int(True), 1) self.assertEqual(int(True), 1)
self.assertIsNot(int(True), True) 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): def test_math(self):
self.assertEqual(+False, 0) self.assertEqual(+False, 0)
self.assertIsNot(+False, False) self.assertIsNot(+False, False)
@ -157,6 +169,12 @@ class BoolTest(unittest.TestCase):
self.assertIs(bool(""), False) self.assertIs(bool(""), False)
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): def test_hasattr(self):
self.assertIs(hasattr([], "append"), True) self.assertIs(hasattr([], "append"), True)
self.assertIs(hasattr([], "wobble"), False) self.assertIs(hasattr([], "wobble"), False)
@ -251,6 +269,12 @@ class BoolTest(unittest.TestCase):
finally: finally:
os.remove(test_support.TESTFN) 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): def test_operator(self):
import operator import operator
self.assertIs(operator.truth(0), False) self.assertIs(operator.truth(0), False)

View File

@ -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, 10, 4, 9, 1), 2) )
self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 'a', 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): def test_explicit_from_Decimal(self):
#positive #positive

View File

@ -49,6 +49,8 @@ class BaseTestCase(unittest.TestCase):
self.assertEqual(-7L.__index__(), -7) self.assertEqual(-7L.__index__(), -7)
self.assertEqual(self.o.__index__(), 4) self.assertEqual(self.o.__index__(), 4)
self.assertEqual(self.n.__index__(), 5) self.assertEqual(self.n.__index__(), 5)
self.assertEqual(True.__index__(), 1)
self.assertEqual(False.__index__(), 0)
def test_subclasses(self): def test_subclasses(self):
r = range(10) r = range(10)

View File

@ -545,6 +545,7 @@ Samuel Nicolary
Gustavo Niemeyer Gustavo Niemeyer
Oscar Nierstrasz Oscar Nierstrasz
Hrvoje Niksic Hrvoje Niksic
Gregory Nofi
Jesse Noller Jesse Noller
Bill Noon Bill Noon
Stefan Norberg Stefan Norberg

View File

@ -164,6 +164,8 @@ C-API
Tests 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 - Issue #8263: Now regrtest.py will report a failure if it receives a
KeyboardInterrupt (SIGINT). KeyboardInterrupt (SIGINT).