def test_report(self): student_tests.reset() with captured_output() as (out, err): assert_type(5, int) assert_type(5, str) assert_type("3", int) assert_type("3", str) assert_type("3", bool) self.assertEqual(student_tests.tests, 5) self.assertEqual(student_tests.failures, 3) self.assertEqual(student_tests.successes, 2)
def test_heavily_nested_example_wrong_type(self): return assert_type( { 'player': { 'status': True, 'health': 47 }, 'locations': { 'first': { 'about': 'First area', 'neighbors': ['second'] }, 'second': { 'about': 'Second area', 'neighbors': ['first'] } }, 'stuff': [{ 'name': 'Broom', 'grabbable?': True }, { 'name': 'Lava', 'grabbable?': False }] }, World)
def test_extra_keys(self): Store = {'price': int, 'fruit': str} return assert_type( { 'banana': 'fruit', 'price': 3, 'dogs': 27, 'fruit': 'banana' }, Store)
def test_traceback_shim(self): # Mock out traceback import importlib old = sys.modules.copy() sys.modules['traceback'] = None import cisc108 with captured_output() as (out, err): self.assertTrue(cisc108.assert_type(5, int)) self.assertEqual(out.getvalue().strip(), "TEST PASSED") with captured_output() as (out, err): self.assertFalse(cisc108.assert_type(5, str)) self.assertEqual( out.getvalue().strip(), "FAILURE, value was the wrong type. Expected type was 'string', but actual value was 5 ('integer')." ) # Restore traceback sys.modules['traceback'] = old['traceback']
def test_heavily_nested_example_wrong_type_in_list(self): first = {'about': 'First area', 'neighbors': []} second = {'about': 'Second area', 'neighbors': ['first']} first['neighbors'].append(second) return assert_type( { 'player': { 'status': "playing", 'health': 47 }, 'locations': { 'first': first, 'second': second }, 'stuff': [{ 'name': 'Broom', 'grabbable?': True }, { 'name': 'Lava', 'grabbable?': False }] }, World)
def test_none_is_not_int(self): return assert_type(None, int)
def test_int_is_not_str(self): return assert_type(5, str)
def test_int_is_not_bool(self): return assert_type(True, int)
def test_bool_is_not_int(self): return assert_type(1, bool)
def test_float_is_not_int(self): return assert_type(5.0, int)
def test_bad_key_value_type(self): Store = {'fruit': int} return assert_type({'fruit': 'banana'}, Store)
def test_int_is_not_list(self): return assert_type(3, list)
def test_list_ints_is_not_list_of_list_ints(self): return assert_type([1, 2, 3], [[int]])
def test_float_is_float(self): return assert_type(5.0, float)
def test_bool_is_bool(self): return assert_type(True, bool)
def test_wrong_key_type_homogenous(self): Prices = {str: int} return assert_type({'fruit': 27, 'apple': 22, 44: 'pineapple'}, Prices)
def test_str_is_str(self): return assert_type("5", str)
def test_literals_in_type(self): Store = {'fruit': 3} return assert_type({'fruit': 3}, Store)
def test_none_is_not_str(self): return assert_type(None, str)
def test_missing_keys_no_others(self): Store = {'fruit': str} return assert_type({}, Store)
def test_list_ints_is_not_int(self): return assert_type([1, 2, 3], int)
def test_int_is_float(self): return assert_type(5, float)
def test_int_is_not_list_ints(self): return assert_type(3, [int])
def test_int_is_int(self): return assert_type(5, int)
def test_wrong_element_inside_list(self): return assert_type([1, 2, "3"], [int])
def test_missing_keys(self): Store = {'fruit': str} return assert_type({'banana': 'fruit'}, Store)