MCPcopy Create free account
hub / github.com/numpy/numpy / assert_string_equal

Function assert_string_equal

numpy/testing/_private/utils.py:1122–1190  ·  view source on GitHub ↗

Test if two strings are equal. If the given strings are equal, `assert_string_equal` does nothing. If they are not equal, an AssertionError is raised, and the diff between the strings is shown. Parameters ---------- actual : str The string to test for equality

(actual, desired)

Source from the content-addressed store, hash-verified

1120
1121
1122def assert_string_equal(actual, desired):
1123 """
1124 Test if two strings are equal.
1125
1126 If the given strings are equal, `assert_string_equal` does nothing.
1127 If they are not equal, an AssertionError is raised, and the diff
1128 between the strings is shown.
1129
1130 Parameters
1131 ----------
1132 actual : str
1133 The string to test for equality against the expected string.
1134 desired : str
1135 The expected string.
1136
1137 Examples
1138 --------
1139 >>> np.testing.assert_string_equal('abc', 'abc')
1140 >>> np.testing.assert_string_equal('abc', 'abcd')
1141 Traceback (most recent call last):
1142 File "<stdin>", line 1, in <module>
1143 ...
1144 AssertionError: Differences in strings:
1145 - abc+ abcd? +
1146
1147 """
1148 # delay import of difflib to reduce startup time
1149 __tracebackhide__ = True # Hide traceback for py.test
1150 import difflib
1151
1152 if not isinstance(actual, str):
1153 raise AssertionError(repr(type(actual)))
1154 if not isinstance(desired, str):
1155 raise AssertionError(repr(type(desired)))
1156 if desired == actual:
1157 return
1158
1159 diff = list(difflib.Differ().compare(actual.splitlines(True),
1160 desired.splitlines(True)))
1161 diff_list = []
1162 while diff:
1163 d1 = diff.pop(0)
1164 if d1.startswith(' '):
1165 continue
1166 if d1.startswith('- '):
1167 l = [d1]
1168 d2 = diff.pop(0)
1169 if d2.startswith('? '):
1170 l.append(d2)
1171 d2 = diff.pop(0)
1172 if not d2.startswith('+ '):
1173 raise AssertionError(repr(d2))
1174 l.append(d2)
1175 if diff:
1176 d3 = diff.pop(0)
1177 if d3.startswith('? '):
1178 l.append(d3)
1179 else:

Callers 2

test_simpleMethod · 0.90
test_regexMethod · 0.90

Calls 4

splitlinesMethod · 0.80
startswithMethod · 0.80
rstripMethod · 0.80
joinMethod · 0.45

Tested by 2

test_simpleMethod · 0.72
test_regexMethod · 0.72