File size: 4,456 Bytes
81ec5d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100


class BaseTest:

    # Custom assert methods implementation
    def assertIsNotNone(self, value, msg=None):
        """Assert that value is not None"""
        if value is None:
            raise AssertionError(msg or f"Expected not None, but got None")
    
    def assertEqual(self, first, second, msg=None):
        """Assert that first equals second"""
        if first != second:
            raise AssertionError(msg or f"Expected {first} == {second}, but {first} != {second}")
    
    def assertTrue(self, expr, msg=None):
        """Assert that expr is True"""
        if not expr:
            raise AssertionError(msg or f"Expected True, but got {expr}")
    
    def assertFalse(self, expr, msg=None):
        """Assert that expr is False"""
        if expr:
            raise AssertionError(msg or f"Expected False, but got {expr}")
    
    def assertAlmostEqual(self, first, second, places=7, msg=None):
        """Assert that first and second are approximately equal"""
        if round(abs(second - first), places) != 0:
            raise AssertionError(msg or f"Expected {first} ~= {second} (within {places} decimal places)")
    
    def assertIs(self, first, second, msg=None):
        """Assert that first is second (same object identity)"""
        if first is not second:
            raise AssertionError(msg or f"Expected {first} is {second}, but they are different objects")
    
    def assertIn(self, member, container, msg=None):
        """Assert that member is in container"""
        if member not in container:
            raise AssertionError(msg or f"Expected {member} in {container}")
    
    def assertIsInstance(self, obj, cls, msg=None):
        """Assert that obj is an instance of cls"""
        if not isinstance(obj, cls):
            raise AssertionError(msg or f"Expected {obj} to be instance of {cls}, but got {type(obj)}")
    
    def assertIsNone(self, value, msg=None):
        """Assert that value is None"""
        if value is not None:
            raise AssertionError(msg or f"Expected None, but got {value}")
    
    def assertNotEqual(self, first, second, msg=None):
        """Assert that first does not equal second"""
        if first == second:
            raise AssertionError(msg or f"Expected {first} != {second}, but they are equal")
    
    def assertGreater(self, first, second, msg=None):
        """Assert that first is greater than second"""
        if not first > second:
            raise AssertionError(msg or f"Expected {first} > {second}")
    
    def assertLess(self, first, second, msg=None):
        """Assert that first is less than second"""
        if not first < second:
            raise AssertionError(msg or f"Expected {first} < {second}")
    
    def assertGreaterEqual(self, first, second, msg=None):
        """Assert that first is greater than or equal to second"""
        if not first >= second:
            raise AssertionError(msg or f"Expected {first} >= {second}")
    
    def assertLessEqual(self, first, second, msg=None):
        """Assert that first is less than or equal to second"""
        if not first <= second:
            raise AssertionError(msg or f"Expected {first} <= {second}")
    
    def assertNotIn(self, member, container, msg=None):
        """Assert that member is not in container"""
        if member in container:
            raise AssertionError(msg or f"Expected {member} not in {container}")
    
    def assertIsNot(self, first, second, msg=None):
        """Assert that first is not second (different object identity)"""
        if first is second:
            raise AssertionError(msg or f"Expected {first} is not {second}, but they are the same object")
    
    def assertRaises(self, exception_class, callable_obj=None, *args, **kwargs):
        """Assert that calling callable_obj raises exception_class"""
        if callable_obj is None:
            # Return a context manager for use with 'with' statement
            return self._AssertRaisesContext(exception_class)
        else:
            try:
                callable_obj(*args, **kwargs)
                raise AssertionError(f"Expected {exception_class.__name__} to be raised, but no exception was raised")
            except exception_class:
                pass  # Expected exception was raised
            except Exception as e:
                raise AssertionError(f"Expected {exception_class.__name__} to be raised, but got {type(e).__name__}: {e}")