summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPauli Virtanen <pav@iki.fi>2015-11-14 14:28:25 +0200
committerPauli Virtanen <pav@iki.fi>2015-11-14 14:28:25 +0200
commiteadc135a5f2ab577748188770af66feafe87859d (patch)
tree252fdf346b3cd5c211a2209e3176cb515afb010f
parentf83d68bdac7bf0843f1601da25ac51f97983b1ef (diff)
downloadnumpy-eadc135a5f2ab577748188770af66feafe87859d.tar.gz
BUG: testing: fix a bug in assert_string_equal
-rw-r--r--numpy/testing/tests/test_utils.py19
-rw-r--r--numpy/testing/utils.py11
2 files changed, 24 insertions, 6 deletions
diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py
index a31fce4af..13aeffe02 100644
--- a/numpy/testing/tests/test_utils.py
+++ b/numpy/testing/tests/test_utils.py
@@ -9,7 +9,8 @@ from numpy.testing import (
assert_array_almost_equal, build_err_msg, raises, assert_raises,
assert_warns, assert_no_warnings, assert_allclose, assert_approx_equal,
assert_array_almost_equal_nulp, assert_array_max_ulp,
- clear_and_catch_warnings, run_module_suite
+ clear_and_catch_warnings, run_module_suite,
+ assert_string_equal
)
import unittest
@@ -715,6 +716,22 @@ class TestULP(unittest.TestCase):
lambda: assert_array_max_ulp(nan, nzero,
maxulp=maxulp))
+class TestStringEqual(unittest.TestCase):
+ def test_simple(self):
+ assert_string_equal("hello", "hello")
+ assert_string_equal("hello\nmultiline", "hello\nmultiline")
+
+ try:
+ assert_string_equal("foo\nbar", "hello\nbar")
+ except AssertionError as exc:
+ assert_equal(str(exc), "Differences in strings:\n- foo\n+ hello")
+ else:
+ raise AssertionError("exception not raised")
+
+ self.assertRaises(AssertionError,
+ lambda: assert_string_equal("foo", "hello"))
+
+
def assert_warn_len_equal(mod, n_in_context):
mod_warns = mod.__warningregistry__
# Python 3.4 appears to clear any pre-existing warnings of the same type,
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index 099b75bdf..8a282ff3c 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -1018,11 +1018,12 @@ def assert_string_equal(actual, desired):
if not d2.startswith('+ '):
raise AssertionError(repr(d2))
l.append(d2)
- d3 = diff.pop(0)
- if d3.startswith('? '):
- l.append(d3)
- else:
- diff.insert(0, d3)
+ if diff:
+ d3 = diff.pop(0)
+ if d3.startswith('? '):
+ l.append(d3)
+ else:
+ diff.insert(0, d3)
if re.match(r'\A'+d2[2:]+r'\Z', d1[2:]):
continue
diff_list.extend(l)