summaryrefslogtreecommitdiff
path: root/tests/helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/helpers.py')
-rw-r--r--tests/helpers.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 09ba485d..9c6a0ad8 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -133,3 +133,73 @@ class SuperModuleCleaner(ModuleCleaner):
shutil.rmtree("__pycache__")
invalidate_import_caches()
+
+
+# Map chars to numbers for arcz_to_arcs
+_arcz_map = {'.': -1}
+_arcz_map.update(dict((c, ord(c) - ord('0')) for c in '123456789'))
+_arcz_map.update(dict(
+ (c, 10 + ord(c) - ord('A')) for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+))
+
+def arcz_to_arcs(arcz):
+ """Convert a compact textual representation of arcs to a list of pairs.
+
+ The text has space-separated pairs of letters. Period is -1, 1-9 are
+ 1-9, A-Z are 10 through 36. The resulting list is sorted regardless of
+ the order of the input pairs.
+
+ ".1 12 2." --> [(-1,1), (1,2), (2,-1)]
+
+ Minus signs can be included in the pairs:
+
+ "-11, 12, 2-5" --> [(-1,1), (1,2), (2,-5)]
+
+ """
+ arcs = []
+ for pair in arcz.split():
+ asgn = bsgn = 1
+ if len(pair) == 2:
+ a, b = pair
+ else:
+ assert len(pair) == 3
+ if pair[0] == '-':
+ _, a, b = pair
+ asgn = -1
+ else:
+ assert pair[1] == '-'
+ a, _, b = pair
+ bsgn = -1
+ arcs.append((asgn * _arcz_map[a], bsgn * _arcz_map[b]))
+ return sorted(arcs)
+
+
+_arcz_unmap = {val: ch for ch, val in _arcz_map.items()}
+
+def _arcs_to_arcz_repr_one(num):
+ """Return an arcz form of the number `num`, or "?" if there is none."""
+ if num == -1:
+ return "."
+ z = ""
+ if num < 0:
+ z += "-"
+ num *= -1
+ z += _arcz_unmap.get(num, "?")
+ return z
+
+
+def arcs_to_arcz_repr(arcs):
+ """Convert a list of arcs to a readable multi-line form for asserting.
+
+ Each pair is on its own line, with a comment showing the arcz form,
+ to make it easier to decode when debugging test failures.
+
+ """
+ repr_list = []
+ for a, b in arcs:
+ line = repr((a, b))
+ line += " # "
+ line += _arcs_to_arcz_repr_one(a)
+ line += _arcs_to_arcz_repr_one(b)
+ repr_list.append(line)
+ return "\n".join(repr_list) + "\n"