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