booleans: tests
what are booleans?: tests
the code from booleans/tests/test_booleans.py from booleans
1import unittest
2
3
4class TestBooleans(unittest.TestCase):
5
6 def test_what_is_false(self):
7 self.assertIsInstance(False, bool)
8 self.assertIs(False, False)
9 self.assertEqual(bool(False), False)
10 self.assertFalse(bool(False))
11 self.assertFalse(False)
12
13 def test_what_is_true(self):
14 self.assertIsInstance(True, bool)
15 self.assertIs(True, True)
16 self.assertEqual(bool(True), True)
17 self.assertTrue(bool(True))
18 self.assertTrue(True)
19
20 def test_is_none_falsy_or_truthy(self):
21 self.assertFalse(bool(None))
22 self.assertFalse(None)
23
24 def test_is_an_integer_falsy_or_truthy(self):
25 a_negative_integer = -1
26 self.assertTrue(bool(a_negative_integer))
27 self.assertTrue(a_negative_integer)
28
29 self.assertFalse(bool(0))
30 self.assertFalse(0)
31
32 a_positive_integer = 1
33 self.assertTrue(bool(a_positive_integer))
34 self.assertTrue(a_positive_integer)
35
36 def test_is_a_float_falsy_or_truthy(self):
37 a_negative_float = -0.1
38 self.assertTrue(bool(a_negative_float))
39 self.assertTrue(a_negative_float)
40
41 self.assertFalse(bool(0.0))
42 self.assertFalse(0.0)
43
44 a_positive_float = 0.1
45 self.assertTrue(bool(a_positive_float))
46 self.assertTrue(0.1)
47
48 def test_is_a_string_falsy_or_truthy(self):
49 self.assertFalse(bool(str()))
50 self.assertFalse(str())
51
52 a_string = "a string with things"
53 self.assertTrue(bool(a_string))
54 self.assertTrue(a_string)
55
56 def test_is_a_tuple_falsy_or_truthy(self):
57 self.assertFalse(bool(tuple()))
58 self.assertFalse(tuple())
59
60 a_tuple = (1, 2, 3, 'n')
61 self.assertTrue(bool(a_tuple))
62 self.assertTrue(a_tuple)
63
64 def test_is_a_list_falsy_or_truthy(self):
65 self.assertFalse(bool(list()))
66 self.assertFalse(list())
67
68 a_list = [1, 2, 3, 'n']
69 self.assertTrue(bool(a_list))
70 self.assertTrue(a_list)
71
72 def test_is_a_set_falsy_or_truthy(self):
73 self.assertFalse(bool(set()))
74 self.assertFalse(set())
75
76 a_set = {1, 2, 3, 'n'}
77 self.assertTrue(bool(a_set))
78 self.assertTrue(a_set)
79
80 def test_is_a_dictionary_falsy_or_truthy(self):
81 self.assertFalse(bool(dict()))
82 self.assertFalse(dict())
83
84 a_dictionary = {'key': 'value'}
85 self.assertTrue(bool(a_dictionary))
86 self.assertTrue(a_dictionary)
87
88
89# NOTES
90# bool(a dictionary with things) is True
91# bool(a set with things) is True
92# bool(a list with things) is True
93# bool(a tuple with things) is True
94# bool(a string with things) is True
95# bool(a positive number) is True
96# bool(a negative number) is True
97# True is NOT False
98# True is NOT equal to False
99# True is a boolean
100# bool(the empty dictionary) is False
101# bool(the empty set) is False
102# bool(the empty list) is False
103# bool(the empty tuple) is False
104# bool(the empty string) is False
105# bool(zero) is False
106# bool(None) is False
107# False is NOT True
108# False is NOT equal to True
109# False is a boolean
110
111
112# Exceptions seen
113# AssertionError
booleans 2: tests
the tests in booleans/tests/test_booleans.py from booleans 2: test with bool
1import unittest
2
3
4class TestBooleans(unittest.TestCase):
5
6 def test_what_is_false(self):
7 self.assertIsInstance(False, bool)
8 self.assertFalse(False)
9 self.assertFalse(None)
10 self.assertFalse(bool(None))
11 self.assertFalse(0)
12 self.assertFalse(bool(0))
13 self.assertFalse(0.0)
14 self.assertFalse(bool(0.0))
15 self.assertFalse(str())
16 self.assertFalse(bool(str()))
17 self.assertFalse(tuple())
18 self.assertFalse(bool(tuple()))
19 self.assertFalse(list())
20 self.assertFalse(bool(list()))
21 self.assertFalse(set())
22 self.assertFalse(bool(set()))
23 self.assertFalse(dict())
24 self.assertFalse(bool(dict()))
25
26 def test_what_is_true(self):
27 self.assertIsInstance(True, bool)
28 self.assertTrue(True)
29 self.assertTrue(-1)
30 self.assertTrue(bool(-1))
31 self.assertTrue(1)
32 self.assertTrue(bool(1))
33 self.assertTrue(-0.1)
34 self.assertTrue(bool(-0.1))
35 self.assertTrue(0.1)
36 self.assertTrue(bool(0.1))
37 self.assertTrue("a string with things")
38 self.assertTrue(bool("a string with things"))
39 self.assertTrue((1, 2, 3, 'n'))
40 self.assertTrue(bool((1, 2, 3, 'n')))
41 self.assertTrue([1, 2, 3, 'n'])
42 self.assertTrue(bool([1, 2, 3, 'n']))
43 self.assertTrue({1, 2, 3, 'n'})
44 self.assertTrue(bool({1, 2, 3, 'n'}))
45 self.assertTrue({'key': 'value'})
46 self.assertTrue(bool({'key': 'value'}))
47
48
49# NOTES
50# a dictionary with things is True
51# a set with things is True
52# a list with things is True
53# a tuple with things is True
54# a string with things is True
55# positive and negative numbers are True
56# True is True
57# True is not false
58# True is a boolean
59# the empty dictionary is False
60# the empty set is False
61# the empty list is False
62# the empty tuple is False
63# the empty string is False
64# 0 is False
65# None is False
66# False is False
67# False is not true
68# False is a boolean
69
70
71# Exceptions seen
72# AssertionError
booleans 3: tests
the tests in booleans/tests/test_booleans.py from booleans 3: values of True and False
1import unittest
2
3
4class TestBooleans(unittest.TestCase):
5
6 def test_what_is_false(self):
7 self.assertIsInstance(False, (bool, int))
8 self.assertNotIsInstance(False, float)
9
10 for false_item in (
11 False,
12 None, bool(None),
13 0, 0.0, bool(0), bool(0.0),
14 str(), bool(str()),
15 tuple(), bool(tuple()),
16 list(), bool(list()),
17 set(), bool(set()),
18 dict(), bool(dict()),
19 ):
20 with self.subTest(item=false_item):
21 self.assertFalse(false_item)
22
23 def test_what_is_true(self):
24 self.assertIsInstance(True, (bool, int))
25 self.assertNotIsInstance(True, float)
26
27 for true_item in (
28 True,
29 -1, bool(-1), 1, bool(1),
30 -0.1, bool(-0.1), 0.1, bool(0.1),
31 "a string with things", bool("a string with things"),
32 ((1, 2, 3, 'n')), bool((1, 2, 3, 'n')),
33 [1, 2, 3, 'n'], bool([1, 2, 3, 'n']),
34 {1, 2, 3, 'n'}, bool({1, 2, 3, 'n'}),
35 {'key': 'value'}, bool({'key': 'value'}),
36 ):
37 with self.subTest(item=true_item):
38 self.assertTrue(true_item)
39
40 def test_the_value_of_false(self):
41 self.assertEqual(False+1, 1)
42 self.assertEqual(False-1, -1)
43 self.assertEqual(False*1, 0)
44 with self.assertRaises(ZeroDivisionError):
45 1 / False
46
47 def test_the_value_of_true(self):
48 self.assertEqual(True+1, 2)
49 self.assertEqual(True-1, 0)
50 self.assertEqual(True*1, 1)
51 self.assertEqual(True/1, 1)
52
53
54# NOTES
55# a dictionary with things is True
56# a set with things is True
57# a list with things is True
58# a tuple with things is True
59# a string with things is True
60# positive and negative numbers are True
61# True is True
62# True is not false
63# True is a boolean
64# True is an integer
65# True is not a float
66# True is 1
67# the empty dictionary is False
68# the empty set is False
69# the empty list is False
70# the empty tuple is False
71# the empty string is False
72# 0 is False
73# None is False
74# False is False
75# False is not true
76# False is a boolean
77# False is an integer
78# False is not a float
79# False is 0
80
81
82# Exceptions seen
83# AssertionError
booleans 4: tests
the tests in booleans/tests/test_booleans.py from booleans 4: inheritance
1import unittest
2
3
4class TestBooleans(unittest.TestCase):
5
6 def test_what_is_false(self):
7 self.assertIsInstance(False, (bool, int))
8 self.assertNotIsInstance(False, float)
9
10 for false_item in (
11 False,
12 None, bool(None),
13 0, 0.0, bool(0), bool(0.0),
14 str(), bool(str()),
15 tuple(), bool(tuple()),
16 list(), bool(list()),
17 set(), bool(set()),
18 dict(), bool(dict()),
19 ):
20 with self.subTest(item=false_item):
21 self.assertFalse(false_item)
22
23 def test_what_is_true(self):
24 self.assertIsInstance(True, (bool, int))
25 self.assertNotIsInstance(True, float)
26
27 for true_item in (
28 True,
29 -1, bool(-1), 1, bool(1),
30 -0.1, bool(-0.1), 0.1, bool(0.1),
31 "a string with things", bool("a string with things"),
32 ((1, 2, 3, 'n')), bool((1, 2, 3, 'n')),
33 [1, 2, 3, 'n'], bool([1, 2, 3, 'n']),
34 {1, 2, 3, 'n'}, bool({1, 2, 3, 'n'}),
35 {'key': 'value'}, bool({'key': 'value'}),
36 ):
37 with self.subTest(item=true_item):
38 self.assertTrue(true_item)
39
40 def test_the_value_of_false(self):
41 self.assertEqual(False+1, 1)
42 self.assertEqual(False-1, -1)
43 self.assertEqual(False*1, 0)
44 with self.assertRaises(ZeroDivisionError):
45 1 / False
46
47 def test_the_value_of_true(self):
48 self.assertEqual(True+1, 2)
49 self.assertEqual(True-1, 0)
50 self.assertEqual(True*1, 1)
51 self.assertEqual(True/1, 1)
52
53 def test_if_bool_is_an_int(self):
54 self.assertNotIsInstance(bool, int)
55
56
57# NOTES
58# a dictionary with things is True
59# a set with things is True
60# a list with things is True
61# a tuple with things is True
62# a string with things is True
63# positive and negative numbers are True
64# True is True
65# True is not false
66# True is a boolean
67# True is an integer
68# True is not a float
69# True is 1
70# the empty dictionary is False
71# the empty set is False
72# the empty list is False
73# the empty tuple is False
74# the empty string is False
75# 0 is False
76# None is False
77# False is False
78# False is not true
79# False is a boolean
80# False is an integer
81# False is not a float
82# False is 0
83
84
85# Exceptions seen
86# AssertionError