what is None?: tests


None (the simplest object): tests

The code in none/tests/test_none.py from what is None?

none/tests/test_none.py
 1import unittest
 2
 3
 4class TestNone(unittest.TestCase):
 5
 6    def test_what_is_none(self):
 7        self.assertIs(None, None)
 8        self.assertIsNone(None)
 9
10    def test_is_none_a_boolean(self):
11        self.assertIsNotNone(False)
12        self.assertIsNotNone(True)
13        self.assertIsInstance(False, bool)
14        self.assertIsInstance(True, bool)
15        self.assertNotIsInstance(None, bool)
none/tests/test_none.py
17    def test_is_none_an_integer(self):
18        self.assertIsNotNone(-1)
19        self.assertIsNotNone(0)
20        self.assertIsNotNone(1)
21        self.assertIsInstance(-1, int)
22        self.assertIsInstance(0, int)
23        self.assertIsInstance(1, int)
24        self.assertNotIsInstance(None, int)
25
26    def test_is_none_a_float(self):
27        self.assertIsNotNone(-0.1)
28        self.assertIsNotNone(0.0)
29        self.assertIsNotNone(0.1)
30        self.assertIsInstance(-0.1, float)
31        self.assertIsInstance(0.0, float)
32        self.assertIsInstance(0.1, float)
33        self.assertNotIsInstance(None, float)
none/tests/test_none.py
35    def test_is_none_a_string(self):
36        self.assertIsNotNone('')
37        self.assertIsNotNone("characters")
38        self.assertIsInstance("", str)
39        self.assertIsInstance('characters', str)
40        self.assertNotIsInstance(None, str)
41
42    def test_is_none_a_tuple(self):
43        self.assertIsNotNone(())
44        self.assertIsNotNone((0, 1, 2, 'n'))
45        self.assertIsInstance((), tuple)
46        self.assertIsInstance((0, 1, 2, 'n'), tuple)
47        self.assertNotIsInstance(None, tuple)
none/tests/test_none.py
49    def test_is_none_a_list(self):
50        self.assertIsNotNone([])
51        self.assertIsNotNone([0, 1, 2, 'n'])
52        self.assertIsInstance([], list)
53        self.assertIsInstance([0, 1, 2, 'n'], list)
54        self.assertNotIsInstance(None, list)
55
56    def test_is_none_a_set(self):
57        self.assertIsNotNone(set())
58        self.assertIsNotNone({0, 1, 2, 'n'})
59        self.assertIsInstance({0, 1, 2, 'n'}, set)
60        self.assertNotIsInstance(None, set)
none/tests/test_none.py
62    def test_is_none_a_dictionary(self):
63        self.assertIsNotNone(dict())
64        self.assertIsNotNone({'key': 'value'})
65        self.assertIsInstance({}, dict)
66        self.assertIsInstance(
67            {'key': 'value'}, dict
68        )
69        self.assertNotIsInstance(None, dict)
70
71
72# NOTES
73# None is NOT a dictionary
74# None is NOT a set
75# None is NOT a list
76# None is NOT a tuple
77# None is NOT a string
78# None is NOT a float
79# None is NOT an integer
80# None is NOT a boolean
81# None is None
82
83
84# Exceptions seen
85# AssertionError

test AssertionError with None: tests

The code in assertion_error/tests/test_assertion_error.py from test AssertionError with None

  1import unittest
  2
  3
  4def assert_is_not(x, y):
  5    assert x is not y
  6
  7
  8def assert_is_not_none(x):
  9    assert_is_not(x, None)
 10
 11
 12def assert_is_not_false(x):
 13    assert_is_not(x, False)
 14
 15
 16def assert_is_not_true(x):
 17    assert_is_not(x, True)
 18
 19
 20def assert_not_equal(x, y):
 21    assert x != y
 22
 23
 24def assert_equal(x, y):
 25    assert x == y
 26
 27
 28class TestAssertionError(unittest.TestCase):
 29
 30    an_integer = 0
 31    a_float = 0.0
 32    a_string = ''
 33    a_tuple = ()
 34    a_list = []
 35    a_set = set()
 36    a_dictionary = {}
 37
 38    def test_assert_keyword(self):
 39        reality = 1 + 1
 40        my_expectation = 2
 41        assert_equal(reality, my_expectation)
 42        self.assertEqual(reality, my_expectation)
 43
 44        reality = '1' + '1'
 45        my_expectation = '11'
 46        assert_equal(reality, my_expectation)
 47        self.assertEqual(reality, my_expectation)
 48
 49        reality = 'I am' + ' alive'
 50        my_expectation = 'I am alive'
 51        assert_equal(reality, my_expectation)
 52        self.assertEqual(reality, my_expectation)
 53
 54    def test_assertion_error_w_none(self):
 55        assert None is None
 56        self.assertIs(None, None)
 57        self.assertIsNone(None)
 58
 59        assert_is_not_none(False)
 60        self.assertIsNot(False, None)
 61        self.assertIsNotNone(False)
 62
 63        assert_is_not_none(True)
 64        self.assertIsNot(True, None)
 65        self.assertIsNotNone(True)
 66
 67        assert_is_not_none(self.an_integer)
 68        self.assertIsNot(self.an_integer, None)
 69        self.assertIsNotNone(self.an_integer)
 70
 71        assert_is_not_none(self.a_float)
 72        self.assertIsNot(self.a_float, None)
 73        self.assertIsNotNone(self.a_float)
 74
 75        assert_is_not_none(self.a_string)
 76        self.assertIsNot(self.a_string, None)
 77        self.assertIsNotNone(self.a_string)
 78
 79        assert_is_not_none(self.a_tuple)
 80        self.assertIsNot(self.a_tuple, None)
 81        self.assertIsNotNone(self.a_tuple)
 82
 83        assert_is_not_none(self.a_list)
 84        self.assertIsNot(self.a_list, None)
 85        self.assertIsNotNone(self.a_list)
 86
 87        assert_is_not_none(self.a_set)
 88        self.assertIsNot(self.a_set, None)
 89        self.assertIsNotNone(self.a_set)
 90
 91        assert_is_not_none(self.a_dictionary)
 92        self.assertIsNot(self.a_dictionary, None)
 93        self.assertIsNotNone(self.a_dictionary)
 94
 95    def test_assertion_error_w_false(self):
 96        assert False is False
 97        self.assertIs(False, False)
 98
 99        assert_is_not_false(None)
100        self.assertIsNot(None, False)
101
102        assert_is_not_false(True)
103        self.assertIsNot(True, False)
104
105        assert_is_not_false(self.an_integer)
106        self.assertIsNot(self.an_integer, False)
107
108        assert_is_not_false(self.a_float)
109        self.assertIsNot(self.a_float, False)
110
111        assert_is_not_false(self.a_string)
112        self.assertIsNot(self.a_string, False)
113
114        assert_is_not_false(self.a_tuple)
115        self.assertIsNot(self.a_tuple, False)
116
117        assert_is_not_false(self.a_list)
118        self.assertIsNot(self.a_list, False)
119
120        assert_is_not_false(self.a_set)
121        self.assertIsNot(self.a_set, False)
122
123        assert_is_not_false(self.a_dictionary)
124        self.assertIsNot(self.a_dictionary, False)
125
126    def test_assertion_error_w_true(self):
127        assert True is True
128        self.assertIs(True, True)
129
130        assert_is_not_true(None)
131        self.assertIsNot(None, True)
132
133        assert_is_not_true(False)
134        self.assertIsNot(False, True)
135
136        assert_is_not_true(self.an_integer)
137        self.assertIsNot(self.an_integer, True)
138
139        assert_is_not_true(self.a_float)
140        self.assertIsNot(self.a_float, True)
141
142        assert_is_not_true(self.a_string)
143        self.assertIsNot(self.a_string, True)
144
145        assert_is_not_true(self.a_tuple)
146        self.assertIsNot(self.a_tuple, True)
147
148        assert_is_not_true(self.a_list)
149        self.assertIsNot(self.a_list, True)
150
151        assert_is_not_true(self.a_set)
152        self.assertIsNot(self.a_set, True)
153
154        assert_is_not_true(self.a_dictionary)
155        self.assertIsNot(self.a_dictionary, True)
156
157    def test_assertion_error_w_equality(self):
158        assert_equal(None, None)
159        self.assertEqual(None, None)
160
161        assert_equal(False, False)
162        self.assertEqual(False, False)
163
164        assert_equal(True, True)
165        self.assertEqual(True, True)
166
167        assert_not_equal(False, None)
168        self.assertNotEqual(False, None)
169
170        assert_not_equal(False, True)
171        self.assertNotEqual(False, True)
172
173        assert_not_equal(True, None)
174        self.assertNotEqual(True, None)
175
176    def test_assertion_error_w_is_vs_equal(self):
177        assert_is_not(0, 0.0)
178        self.assertIsNot(0, 0.0)
179
180        assert_equal(0, 0.0)
181        self.assertEqual(0, 0.0)
182
183    def will_not_run():
184        # will not run because
185        # the name does not start with test
186        assert False == True
187        self.assertEqual(False, True)
188
189    def test_failure(self):
190        assert_not_equal(False, True)
191        self.assertEqual(False, False)
192
193
194# NOTES
195# a dictionary is not the same object as True
196# a dictionary is not the same object as False
197# a dictionary is not the same object as None
198# a set is not the same object as True
199# a set is not the same object as False
200# a set is not the same object as None
201# a list is not the same object as True
202# a list is not the same object as False
203# a list is not the same object as None
204# a tuple is not the same object as True
205# a tuple is not the same object as False
206# a tuple is not the same object as None
207# a string is not the same object as True
208# a string is not the same object as False
209# a string is not the same object as None
210# a float is not the same object as True
211# a float is not the same object as False
212# a float is not the same object as None
213# an integer is not the same object as True
214# an integer is not the same object as False
215# an integer is not the same object as None
216# True is True and equal to True
217# True is not equal to False
218# True is not the same object as False
219# True is not equal to None
220# True is not the same object as None
221# False is not equal to True
222# False is not the same object as True
223# False is False and equal to False
224# False is not equal to None
225# False is not the same object as None
226# None is not equal to True
227# None is not the same object as True
228# None is not equal to False
229# None is not the same object as False
230# None is None and equal to None
231
232
233# Exceptions seen
234# AssertionError
235# IndentationError
236# AttributeError
237# NameError
238# TypeError

test functions with None

The code in functions/tests/test_functions.py from test functions with None

  1import src.functions
  2import unittest
  3
  4
  5class TestFunctions(unittest.TestCase):
  6
  7    first = 'first'
  8    last = 'last'
  9    a_tuple = (0, 1, 2, 'n')
 10    a_list = [0, 1, 2, 'n']
 11    a_set = {0, 1, 2, 'n'}
 12    a_dictionary = {'key': 'value'}
 13
 14    def test_making_a_function_w_pass(self):
 15        self.assertIsNone(src.functions.w_pass())
 16
 17    def test_making_a_function_w_return(self):
 18        self.assertIsNone(src.functions.w_return())
 19
 20    def test_making_a_function_w_return_none(self):
 21        self.assertIsNone(src.functions.w_return_none())
 22
 23    def test_what_happens_after_functions_return(self):
 24        self.assertIsNone(
 25            src.functions.return_leaves_the_function()
 26        )
 27
 28    def test_constant_function(self):
 29        self.assertEqual(
 30            src.functions.constant(), 'the same thing'
 31        )
 32
 33    def test_identity_function(self):
 34        self.assertIsNone(src.functions.identity(None))
 35        self.assertEqual(
 36            src.functions.identity(object), object
 37        )
 38
 39    def test_why_use_a_function(self):
 40        def add_x(number):
 41            return 3 + number
 42
 43        self.assertEqual(add_x(0), 3)
 44        self.assertEqual(add_x(1), 4)
 45        self.assertEqual(add_x(2), 5)
 46        self.assertEqual(add_x(3), 6)
 47        self.assertEqual(add_x(4), 7)
 48        self.assertEqual(add_x(5), 8)
 49        self.assertEqual(add_x(6), 9)
 50        self.assertEqual(add_x(7), 10)
 51        self.assertEqual(add_x(8), 11)
 52        self.assertEqual(add_x(9), 12)
 53
 54    def test_positional_arguments(self):
 55        positional_arguments = (
 56            src.functions.positional_arguments
 57        )
 58
 59        self.assertEqual(
 60            positional_arguments(self.first, self.last),
 61            (self.first, self.last)
 62        )
 63        self.assertEqual(
 64            positional_arguments(self.last, self.first),
 65            (self.last, self.first)
 66        )
 67
 68        self.assertEqual(
 69            positional_arguments(0, 1), (0, 1)
 70        )
 71
 72        self.assertEqual(
 73            positional_arguments(
 74                self.a_tuple, self.a_list
 75            ),
 76            (self.a_tuple, self.a_list)
 77        )
 78
 79        self.assertEqual(
 80            src.functions.keyword_arguments(
 81                self.a_set, self.a_dictionary
 82            ),
 83            (self.a_set, self.a_dictionary)
 84        )
 85
 86    def test_keyword_arguments(self):
 87        keyword_arguments = (
 88            src.functions.keyword_arguments
 89        )
 90
 91        self.assertEqual(
 92            keyword_arguments(
 93                first_input=self.first,
 94                last_input=self.last,
 95            ),
 96            (self.first, self.last)
 97        )
 98        self.assertEqual(
 99            keyword_arguments(
100                last_input=self.last,
101                first_input=self.first,
102            ),
103            (self.first, self.last)
104        )
105
106        self.assertEqual(
107            keyword_arguments(
108                last_input=0, first_input=1,
109            ),
110            (1, 0)
111        )
112
113        self.assertEqual(
114            keyword_arguments(
115                first_input=self.a_tuple,
116                last_input=self.a_list,
117            ),
118            (self.a_tuple, self.a_list)
119        )
120
121        self.assertEqual(
122            src.functions.positional_arguments(
123                last_input=self.a_dictionary,
124                first_input=self.a_set,
125            ),
126            (self.a_set, self.a_dictionary)
127        )
128
129    def test_args_and_kwargs(self):
130        self.assertEqual(
131            src.functions.args_and_kwargs(
132                self.first, last_input=self.last
133            ),
134            (self.first, self.last)
135        )
136
137    def test_optional_arguments(self):
138        optional_arguments = (
139            src.functions.optional_arguments
140        )
141
142        first_name, last_name = 'jane', 'doe'
143        self.assertEqual(
144            optional_arguments(
145                first_name,
146            ),
147            (first_name, last_name)
148        )
149
150        first_name, blow = 'joe', 'blow'
151        self.assertEqual(
152            optional_arguments(
153                first_name, blow
154            ),
155            (first_name, blow)
156        )
157
158        first_name = 'john'
159        self.assertEqual(
160            optional_arguments(
161                first_input=first_name,
162            ),
163            (first_name, last_name)
164        )
165
166        last_name = 'smith'
167        self.assertEqual(
168            optional_arguments(
169                last_input=last_name,
170                first_input=first_name,
171            ),
172            (first_name, last_name)
173        )
174
175    def test_unknown_number_of_arguments(self):
176        unknown_number_of_arguments = (
177            src.functions.unknown_number_of_arguments
178        )
179
180        a_tuple = (0, 1)
181        a_dictionary = {'a': 2, 'b': 3}
182        self.assertEqual(
183            unknown_number_of_arguments(
184                *a_tuple, **a_dictionary
185            ),
186            (a_tuple, a_dictionary)
187        )
188
189        a_dictionary = {'a': 2, 'b': 3, 'c': 4}
190        self.assertEqual(
191            unknown_number_of_arguments(
192                *a_tuple, **a_dictionary
193            ),
194            (a_tuple, a_dictionary)
195        )
196
197        a_tuple = (0, 1, 2)
198        a_dictionary = {'a': 3, 'b': 4, 'c': 5}
199        self.assertEqual(
200            unknown_number_of_arguments(
201                *a_tuple, **a_dictionary
202            ),
203            (a_tuple, a_dictionary)
204        )
205
206        a_tuple = (0, 1, 2, 'n')
207        self.assertEqual(
208            unknown_number_of_arguments(*a_tuple),
209            (a_tuple, {})
210        )
211
212        a_dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 'n'}
213        self.assertEqual(
214            unknown_number_of_arguments(**a_dictionary),
215            ((), a_dictionary)
216        )
217
218        self.assertEqual(
219            unknown_number_of_arguments(), ((), {})
220        )
221
222
223# Exceptions seen
224# AssertionError
225# NameError
226# TypeError
227# SyntaxError
228# AttributeError