Back to index

python3.2  3.2.2
Public Member Functions
test.test_bool.BoolTest Class Reference

List of all members.

Public Member Functions

def test_subclass
def test_print
def test_repr
def test_str
def test_int
def test_float
def test_math
def test_convert
def test_format
def test_hasattr
def test_callable
def test_isinstance
def test_issubclass
def test_contains
def test_string
def test_boolean
def test_fileclosed
def test_types
def test_operator
def test_marshal
def test_pickle
def test_picklevalues
def test_convert_to_bool
def test_sane_len

Detailed Description

Definition at line 8 of file test_bool.py.


Member Function Documentation

Definition at line 222 of file test_bool.py.

00222 
00223     def test_boolean(self):
00224         self.assertEqual(True & 1, 1)
00225         self.assertNotIsInstance(True & 1, bool)
00226         self.assertIs(True & True, True)
00227 
00228         self.assertEqual(True | 1, 1)
00229         self.assertNotIsInstance(True | 1, bool)
00230         self.assertIs(True | True, True)
00231 
00232         self.assertEqual(True ^ 1, 0)
00233         self.assertNotIsInstance(True ^ 1, bool)
00234         self.assertIs(True ^ True, False)

Here is the call graph for this function:

Definition at line 176 of file test_bool.py.

00176 
00177     def test_callable(self):
00178         self.assertIs(callable(len), True)
00179         self.assertIs(callable(1), False)

Here is the call graph for this function:

Definition at line 192 of file test_bool.py.

00192 
00193     def test_contains(self):
00194         self.assertIs(1 in {}, False)
00195         self.assertIs(1 in {1:1}, True)

Here is the call graph for this function:

Definition at line 156 of file test_bool.py.

00156 
00157     def test_convert(self):
00158         self.assertRaises(TypeError, bool, 42, 42)
00159         self.assertIs(bool(10), True)
00160         self.assertIs(bool(1), True)
00161         self.assertIs(bool(-1), True)
00162         self.assertIs(bool(0), False)
00163         self.assertIs(bool("hello"), True)
00164         self.assertIs(bool(""), False)
00165         self.assertIs(bool(), False)

Here is the call graph for this function:

Definition at line 287 of file test_bool.py.

00287 
00288     def test_convert_to_bool(self):
00289         # Verify that TypeError occurs when bad things are returned
00290         # from __bool__().  This isn't really a bool test, but
00291         # it's related.
00292         check = lambda o: self.assertRaises(TypeError, bool, o)
00293         class Foo(object):
00294             def __bool__(self):
00295                 return self
00296         check(Foo())
00297 
00298         class Bar(object):
00299             def __bool__(self):
00300                 return "Yes"
00301         check(Bar())
00302 
00303         class Baz(int):
00304             def __bool__(self):
00305                 return self
00306         check(Baz())
00307 
00308         # __bool__() must return a bool not an int
00309         class Spam(int):
00310             def __bool__(self):
00311                 return 1
00312         check(Spam())
00313 
00314         class Eggs:
00315             def __len__(self):
00316                 return -1
00317         self.assertRaises(ValueError, bool, Eggs())

Here is the call graph for this function:

Definition at line 235 of file test_bool.py.

00235 
00236     def test_fileclosed(self):
00237         try:
00238             f = open(support.TESTFN, "w")
00239             self.assertIs(f.closed, False)
00240             f.close()
00241             self.assertIs(f.closed, True)
00242         finally:
00243             os.remove(support.TESTFN)

Here is the call graph for this function:

Definition at line 48 of file test_bool.py.

00048 
00049     def test_float(self):
00050         self.assertEqual(float(False), 0.0)
00051         self.assertIsNot(float(False), False)
00052         self.assertEqual(float(True), 1.0)
00053         self.assertIsNot(float(True), True)

Here is the call graph for this function:

Definition at line 166 of file test_bool.py.

00166 
00167     def test_format(self):
00168         self.assertEqual("%d" % False, "0")
00169         self.assertEqual("%d" % True, "1")
00170         self.assertEqual("%x" % False, "0")
00171         self.assertEqual("%x" % True, "1")

Here is the call graph for this function:

Definition at line 172 of file test_bool.py.

00172 
00173     def test_hasattr(self):
00174         self.assertIs(hasattr([], "append"), True)
00175         self.assertIs(hasattr([], "wobble"), False)

Here is the call graph for this function:

Definition at line 42 of file test_bool.py.

00042 
00043     def test_int(self):
00044         self.assertEqual(int(False), 0)
00045         self.assertIsNot(int(False), False)
00046         self.assertEqual(int(True), 1)
00047         self.assertIsNot(int(True), True)

Here is the call graph for this function:

Definition at line 180 of file test_bool.py.

00180 
00181     def test_isinstance(self):
00182         self.assertIs(isinstance(True, bool), True)
00183         self.assertIs(isinstance(False, bool), True)
00184         self.assertIs(isinstance(True, int), True)
00185         self.assertIs(isinstance(False, int), True)
00186         self.assertIs(isinstance(1, bool), False)
00187         self.assertIs(isinstance(0, bool), False)

Here is the call graph for this function:

Definition at line 188 of file test_bool.py.

00188 
00189     def test_issubclass(self):
00190         self.assertIs(issubclass(bool, int), True)
00191         self.assertIs(issubclass(int, bool), False)

Here is the call graph for this function:

Definition at line 265 of file test_bool.py.

00265 
00266     def test_marshal(self):
00267         import marshal
00268         self.assertIs(marshal.loads(marshal.dumps(True)), True)
00269         self.assertIs(marshal.loads(marshal.dumps(False)), False)

Here is the call graph for this function:

Definition at line 54 of file test_bool.py.

00054 
00055     def test_math(self):
00056         self.assertEqual(+False, 0)
00057         self.assertIsNot(+False, False)
00058         self.assertEqual(-False, 0)
00059         self.assertIsNot(-False, False)
00060         self.assertEqual(abs(False), 0)
00061         self.assertIsNot(abs(False), False)
00062         self.assertEqual(+True, 1)
00063         self.assertIsNot(+True, True)
00064         self.assertEqual(-True, -1)
00065         self.assertEqual(abs(True), 1)
00066         self.assertIsNot(abs(True), True)
00067         self.assertEqual(~False, -1)
00068         self.assertEqual(~True, -2)
00069 
00070         self.assertEqual(False+2, 2)
00071         self.assertEqual(True+2, 3)
00072         self.assertEqual(2+False, 2)
00073         self.assertEqual(2+True, 3)
00074 
00075         self.assertEqual(False+False, 0)
00076         self.assertIsNot(False+False, False)
00077         self.assertEqual(False+True, 1)
00078         self.assertIsNot(False+True, True)
00079         self.assertEqual(True+False, 1)
00080         self.assertIsNot(True+False, True)
00081         self.assertEqual(True+True, 2)
00082 
00083         self.assertEqual(True-True, 0)
00084         self.assertIsNot(True-True, False)
00085         self.assertEqual(False-False, 0)
00086         self.assertIsNot(False-False, False)
00087         self.assertEqual(True-False, 1)
00088         self.assertIsNot(True-False, True)
00089         self.assertEqual(False-True, -1)
00090 
00091         self.assertEqual(True*1, 1)
00092         self.assertEqual(False*1, 0)
00093         self.assertIsNot(False*1, False)
00094 
00095         self.assertEqual(True/1, 1)
00096         self.assertIsNot(True/1, True)
00097         self.assertEqual(False/1, 0)
00098         self.assertIsNot(False/1, False)
00099 
00100         for b in False, True:
00101             for i in 0, 1, 2:
00102                 self.assertEqual(b**i, int(b)**i)
00103                 self.assertIsNot(b**i, bool(int(b)**i))
00104 
00105         for a in False, True:
00106             for b in False, True:
00107                 self.assertIs(a&b, bool(int(a)&int(b)))
00108                 self.assertIs(a|b, bool(int(a)|int(b)))
00109                 self.assertIs(a^b, bool(int(a)^int(b)))
00110                 self.assertEqual(a&int(b), int(a)&int(b))
00111                 self.assertIsNot(a&int(b), bool(int(a)&int(b)))
00112                 self.assertEqual(a|int(b), int(a)|int(b))
00113                 self.assertIsNot(a|int(b), bool(int(a)|int(b)))
00114                 self.assertEqual(a^int(b), int(a)^int(b))
00115                 self.assertIsNot(a^int(b), bool(int(a)^int(b)))
00116                 self.assertEqual(int(a)&b, int(a)&int(b))
00117                 self.assertIsNot(int(a)&b, bool(int(a)&int(b)))
00118                 self.assertEqual(int(a)|b, int(a)|int(b))
00119                 self.assertIsNot(int(a)|b, bool(int(a)|int(b)))
00120                 self.assertEqual(int(a)^b, int(a)^int(b))
00121                 self.assertIsNot(int(a)^b, bool(int(a)^int(b)))
00122 
00123         self.assertIs(1==1, True)
00124         self.assertIs(1==0, False)
00125         self.assertIs(0<1, True)
00126         self.assertIs(1<0, False)
00127         self.assertIs(0<=0, True)
00128         self.assertIs(1<=0, False)
00129         self.assertIs(1>0, True)
00130         self.assertIs(1>1, False)
00131         self.assertIs(1>=1, True)
00132         self.assertIs(0>=1, False)
00133         self.assertIs(0!=1, True)
00134         self.assertIs(0!=0, False)
00135 
00136         x = [1]
00137         self.assertIs(x is x, True)
00138         self.assertIs(x is not x, False)
00139 
00140         self.assertIs(1 in x, True)
00141         self.assertIs(0 in x, False)
00142         self.assertIs(1 not in x, False)
00143         self.assertIs(0 not in x, True)
00144 
00145         x = {1: 2}
00146         self.assertIs(x is x, True)
00147         self.assertIs(x is not x, False)
00148 
00149         self.assertIs(1 in x, True)
00150         self.assertIs(0 in x, False)
00151         self.assertIs(1 not in x, False)
00152         self.assertIs(0 not in x, True)
00153 
00154         self.assertIs(not True, False)
00155         self.assertIs(not False, True)

Here is the call graph for this function:

Definition at line 250 of file test_bool.py.

00250 
00251     def test_operator(self):
00252         import operator
00253         self.assertIs(operator.truth(0), False)
00254         self.assertIs(operator.truth(1), True)
00255         self.assertIs(operator.not_(1), False)
00256         self.assertIs(operator.not_(0), True)
00257         self.assertIs(operator.contains([], 1), False)
00258         self.assertIs(operator.contains([1], 1), True)
00259         self.assertIs(operator.lt(0, 0), False)
00260         self.assertIs(operator.lt(0, 1), True)
00261         self.assertIs(operator.is_(True, True), True)
00262         self.assertIs(operator.is_(True, False), False)
00263         self.assertIs(operator.is_not(True, True), False)
00264         self.assertIs(operator.is_not(True, False), True)

Here is the call graph for this function:

Definition at line 270 of file test_bool.py.

00270 
00271     def test_pickle(self):
00272         import pickle
00273         self.assertIs(pickle.loads(pickle.dumps(True)), True)
00274         self.assertIs(pickle.loads(pickle.dumps(False)), False)
00275         self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
00276         self.assertIs(pickle.loads(pickle.dumps(False, True)), False)

Here is the call graph for this function:

Definition at line 277 of file test_bool.py.

00277 
00278     def test_picklevalues(self):
00279         # Test for specific backwards-compatible pickle values
00280         import pickle
00281         self.assertEqual(pickle.dumps(True, protocol=0), b"I01\n.")
00282         self.assertEqual(pickle.dumps(False, protocol=0), b"I00\n.")
00283         self.assertEqual(pickle.dumps(True, protocol=1), b"I01\n.")
00284         self.assertEqual(pickle.dumps(False, protocol=1), b"I00\n.")
00285         self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.')
00286         self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.')

Here is the call graph for this function:

Definition at line 21 of file test_bool.py.

00021 
00022     def test_print(self):
00023         try:
00024             fo = open(support.TESTFN, "w")
00025             print(False, True, file=fo)
00026             fo.close()
00027             fo = open(support.TESTFN, "r")
00028             self.assertEqual(fo.read(), 'False True\n')
00029         finally:
00030             fo.close()
00031             os.remove(support.TESTFN)

Here is the call graph for this function:

Definition at line 32 of file test_bool.py.

00032 
00033     def test_repr(self):
00034         self.assertEqual(repr(False), 'False')
00035         self.assertEqual(repr(True), 'True')
00036         self.assertEqual(eval(repr(False)), False)
00037         self.assertEqual(eval(repr(True)), True)

Here is the call graph for this function:

Definition at line 318 of file test_bool.py.

00318 
00319     def test_sane_len(self):
00320         # this test just tests our assumptions about __len__
00321         # this will start failing if __len__ changes assertions
00322         for badval in ['illegal', -1, 1 << 32]:
00323             class A:
00324                 def __len__(self):
00325                     return badval
00326             try:
00327                 bool(A())
00328             except (Exception) as e_bool:
00329                 try:
00330                     len(A())
00331                 except (Exception) as e_len:
00332                     self.assertEqual(str(e_bool), str(e_len))

Here is the call graph for this function:

Definition at line 38 of file test_bool.py.

00038 
00039     def test_str(self):
00040         self.assertEqual(str(False), 'False')
00041         self.assertEqual(str(True), 'True')

Here is the call graph for this function:

Definition at line 196 of file test_bool.py.

00196 
00197     def test_string(self):
00198         self.assertIs("xyz".endswith("z"), True)
00199         self.assertIs("xyz".endswith("x"), False)
00200         self.assertIs("xyz0123".isalnum(), True)
00201         self.assertIs("@#$%".isalnum(), False)
00202         self.assertIs("xyz".isalpha(), True)
00203         self.assertIs("@#$%".isalpha(), False)
00204         self.assertIs("0123".isdigit(), True)
00205         self.assertIs("xyz".isdigit(), False)
00206         self.assertIs("xyz".islower(), True)
00207         self.assertIs("XYZ".islower(), False)
00208         self.assertIs("0123".isdecimal(), True)
00209         self.assertIs("xyz".isdecimal(), False)
00210         self.assertIs("0123".isnumeric(), True)
00211         self.assertIs("xyz".isnumeric(), False)
00212         self.assertIs(" ".isspace(), True)
00213         self.assertIs("\xa0".isspace(), True)
00214         self.assertIs("\u3000".isspace(), True)
00215         self.assertIs("XYZ".isspace(), False)
00216         self.assertIs("X".istitle(), True)
00217         self.assertIs("x".istitle(), False)
00218         self.assertIs("XYZ".isupper(), True)
00219         self.assertIs("xyz".isupper(), False)
00220         self.assertIs("xyz".startswith("x"), True)
00221         self.assertIs("xyz".startswith("z"), False)

Here is the call graph for this function:

Definition at line 10 of file test_bool.py.

00010 
00011     def test_subclass(self):
00012         try:
00013             class C(bool):
00014                 pass
00015         except TypeError:
00016             pass
00017         else:
00018             self.fail("bool should not be subclassable")
00019 
00020         self.assertRaises(TypeError, int.__new__, bool, 0)

Definition at line 244 of file test_bool.py.

00244 
00245     def test_types(self):
00246         # types are always true.
00247         for t in [bool, complex, dict, float, int, list, object,
00248                   set, str, tuple, type]:
00249             self.assertIs(bool(t), True)

Here is the call graph for this function:


The documentation for this class was generated from the following file: