Assert that two string arrays are equal. Display any differences in a human-readable form.
(
expected: list[str],
actual: list[str],
msg: str,
*,
traceback: bool = False,
ignore_modules_order: bool = False,
)
| 150 | |
| 151 | |
| 152 | def assert_string_arrays_equal( |
| 153 | expected: list[str], |
| 154 | actual: list[str], |
| 155 | msg: str, |
| 156 | *, |
| 157 | traceback: bool = False, |
| 158 | ignore_modules_order: bool = False, |
| 159 | ) -> None: |
| 160 | """Assert that two string arrays are equal. |
| 161 | |
| 162 | Display any differences in a human-readable form. |
| 163 | """ |
| 164 | actual = clean_up(actual) |
| 165 | if ignore_modules_order: |
| 166 | expected_module_order = module_order(expected) |
| 167 | actual = match_module_order(actual, expected_module_order) |
| 168 | if expected != actual: |
| 169 | expected_ranges, actual_ranges = diff_ranges(expected, actual) |
| 170 | sys.stderr.write("Expected:\n") |
| 171 | red = "\033[31m" if sys.platform != "win32" else None |
| 172 | render_diff_range(expected_ranges, expected, colour=red) |
| 173 | sys.stderr.write("Actual:\n") |
| 174 | green = "\033[32m" if sys.platform != "win32" else None |
| 175 | render_diff_range(actual_ranges, actual, colour=green) |
| 176 | |
| 177 | sys.stderr.write("\n") |
| 178 | first_diff = next( |
| 179 | (i for i, (a, b) in enumerate(zip(expected, actual)) if a != b), |
| 180 | max(len(expected), len(actual)), |
| 181 | ) |
| 182 | if 0 <= first_diff < len(actual) and ( |
| 183 | len(expected[first_diff]) >= MIN_LINE_LENGTH_FOR_ALIGNMENT |
| 184 | or len(actual[first_diff]) >= MIN_LINE_LENGTH_FOR_ALIGNMENT |
| 185 | ): |
| 186 | # Display message that helps visualize the differences between two |
| 187 | # long lines. |
| 188 | show_align_message(expected[first_diff], actual[first_diff]) |
| 189 | |
| 190 | sys.stderr.write( |
| 191 | "Update the test output using --update-data " |
| 192 | "(implies -n0; you can additionally use the -k selector to update only specific tests)\n" |
| 193 | ) |
| 194 | pytest.fail(msg, pytrace=traceback) |
| 195 | |
| 196 | |
| 197 | def assert_module_equivalence(name: str, expected: Iterable[str], actual: Iterable[str]) -> None: |
searching dependent graphs…