Asserts that two items are equal.
(actual, desired, err_msg='')
| 106 | |
| 107 | |
| 108 | def assert_equal(actual, desired, err_msg=''): |
| 109 | """ |
| 110 | Asserts that two items are equal. |
| 111 | |
| 112 | """ |
| 113 | # Case #1: dictionary ..... |
| 114 | if isinstance(desired, dict): |
| 115 | if not isinstance(actual, dict): |
| 116 | raise AssertionError(repr(type(actual))) |
| 117 | assert_equal(len(actual), len(desired), err_msg) |
| 118 | for k, i in desired.items(): |
| 119 | if k not in actual: |
| 120 | raise AssertionError(f"{k} not in {actual}") |
| 121 | assert_equal(actual[k], desired[k], f'key={k!r}\n{err_msg}') |
| 122 | return |
| 123 | # Case #2: lists ..... |
| 124 | if isinstance(desired, (list, tuple)) and isinstance(actual, (list, tuple)): |
| 125 | return _assert_equal_on_sequences(actual, desired, err_msg='') |
| 126 | if not (isinstance(actual, ndarray) or isinstance(desired, ndarray)): |
| 127 | msg = build_err_msg([actual, desired], err_msg,) |
| 128 | if not desired == actual: |
| 129 | raise AssertionError(msg) |
| 130 | return |
| 131 | # Case #4. arrays or equivalent |
| 132 | if ((actual is masked) and not (desired is masked)) or \ |
| 133 | ((desired is masked) and not (actual is masked)): |
| 134 | msg = build_err_msg([actual, desired], |
| 135 | err_msg, header='', names=('x', 'y')) |
| 136 | raise ValueError(msg) |
| 137 | actual = np.asanyarray(actual) |
| 138 | desired = np.asanyarray(desired) |
| 139 | (actual_dtype, desired_dtype) = (actual.dtype, desired.dtype) |
| 140 | if actual_dtype.char == "S" and desired_dtype.char == "S": |
| 141 | return _assert_equal_on_sequences(actual.tolist(), |
| 142 | desired.tolist(), |
| 143 | err_msg='') |
| 144 | return assert_array_equal(actual, desired, err_msg) |
| 145 | |
| 146 | |
| 147 | def fail_if_equal(actual, desired, err_msg='',): |
no test coverage detected