From e676d53e9fb0d5ef9ad130620e196b271fdd1987 Mon Sep 17 00:00:00 2001 From: Matthias Urlichs Date: Mon, 4 Feb 2019 14:15:09 +0100 Subject: [PATCH] Fix the remaining docstr tests Sets are annoying to test: (a) their repr() is different (b) they might get re-ordered. --- src/octoprint/util/__init__.py | 10 +++++++++- src/octoprint/util/comm.py | 32 ++++++++++++++++---------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 50222c779..b468c0305 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -41,10 +41,18 @@ def to_native_str(s_or_u): if sys.version_info[0] == 2: if isinstance(s_or_u, unicode): return s_or_u.encode("utf-8") + elif isinstance(s_or_u, set): # only used for doctests + if not s_or_u: + return b"set()" + return b'{'+b', '.join(repr(to_native_str(x)) for x in s_or_u)+b'}' else: if isinstance(s_or_u, bytes): return s_or_u.decode("utf-8") # only used for doctests - elif not isinstance(s_or_u, str): + elif isinstance(s_or_u, str): + pass + elif isinstance(s_or_u, set): # only used for doctests + return '{'+repr(tuple(sorted(s_or_u)))[1:-1]+'}' + else: raise RuntimeError("Please use a string here.") return s_or_u diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 05730d4ca..a891cf992 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -4587,32 +4587,32 @@ def _normalize_command_handler_result(command, command_type, gcode, subcode, tag >>> from octoprint.util import to_native_str >>> uu = lambda x: tuple(to_native_str(y) if y is not None else None for y in x) >>> u = lambda x: list(uu(y) for y in x) - >>> _normalize_command_handler_result("M105", None, "M105", None, None, None) + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, None)) [('M105', None, 'M105', None, None)] - >>> _normalize_command_handler_result("M105", None, "M105", None, None, "M110") + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, "M110")) [('M110', None, 'M110', None, None)] - >>> _normalize_command_handler_result("M105", None, "M105", None, None, ["M110"]) + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, ["M110"])) [('M110', None, 'M110', None, None)] - >>> _normalize_command_handler_result("M105", None, "M105", None, None, ["M110", "M117 Foobar"]) + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, ["M110", "M117 Foobar"])) [('M110', None, 'M110', None, None), ('M117 Foobar', None, 'M117', None, None)] - >>> _normalize_command_handler_result("M105", None, "M105", None, None, [("M110",), "M117 Foobar"]) + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, [("M110",), "M117 Foobar"])) [('M110', None, 'M110', None, None), ('M117 Foobar', None, 'M117', None, None)] - >>> _normalize_command_handler_result("M105", None, "M105", None, None, [("M110", "lineno_reset"), "M117 Foobar"]) + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, [("M110", "lineno_reset"), "M117 Foobar"])) [('M110', 'lineno_reset', 'M110', None, None), ('M117 Foobar', None, 'M117', None, None)] - >>> _normalize_command_handler_result("M105", None, "M105", None, None, []) + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, [])) [] - >>> _normalize_command_handler_result("M105", None, "M105", None, None, ["M110", None]) + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, ["M110", None])) [('M110', None, 'M110', None, None)] - >>> _normalize_command_handler_result("M105", None, "M105", None, None, [("M110",), (None, "ignored")]) + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, [("M110",), (None, "ignored")])) [('M110', None, 'M110', None, None)] - >>> _normalize_command_handler_result("M105", None, "M105", None, None, [("M110",), ("M117 Foobar", "display_message"), ("tuple", "of", "unexpected", "length"), ("M110", "lineno_reset")]) + >>> u(_normalize_command_handler_result("M105", None, "M105", None, None, [("M110",), ("M117 Foobar", "display_message"), ("tuple", "of", "unexpected", "length"), ("M110", "lineno_reset")])) [('M110', None, 'M110', None, None), ('M117 Foobar', 'display_message', 'M117', None, None), ('M110', 'lineno_reset', 'M110', None, None)] - >>> _normalize_command_handler_result("M105", None, "M105", None, {"tag1", "tag2"}, ["M110", "M117 Foobar"]) - [('M110', None, 'M110', None, set(['tag1', 'tag2'])), ('M117 Foobar', None, 'M117', None, set(['tag1', 'tag2']))] - >>> _normalize_command_handler_result("M105", None, "M105", None, {"tag1", "tag2"}, ["M110", "M105", "M117 Foobar"], tags_to_add={"tag3"}) - [('M110', None, 'M110', None, set(['tag1', 'tag2', 'tag3'])), ('M105', None, 'M105', None, set(['tag1', 'tag2'])), ('M117 Foobar', None, 'M117', None, set(['tag1', 'tag2', 'tag3']))] - >>> _normalize_command_handler_result("M105", None, "M105", None, {"tag1", "tag2"}, ["M110", ("M105", "temperature_poll"), "M117 Foobar"], tags_to_add={"tag3"}) - [('M110', None, 'M110', None, set(['tag1', 'tag2', 'tag3'])), ('M105', 'temperature_poll', 'M105', None, set(['tag1', 'tag2', 'tag3'])), ('M117 Foobar', None, 'M117', None, set(['tag1', 'tag2', 'tag3']))] + >>> u(_normalize_command_handler_result("M105", None, "M105", None, {"tag1", "tag2"}, ["M110", "M117 Foobar"])) + [('M110', None, 'M110', None, "{'tag1', 'tag2'}"), ('M117 Foobar', None, 'M117', None, "{'tag1', 'tag2'}")] + >>> u(_normalize_command_handler_result("M105", None, "M105", None, {"tag1", "tag2"}, ["M110", "M105", "M117 Foobar"], tags_to_add={"tag3"})) + [('M110', None, 'M110', None, "{'tag1', 'tag2', 'tag3'}"), ('M105', None, 'M105', None, "{'tag1', 'tag2'}"), ('M117 Foobar', None, 'M117', None, "{'tag1', 'tag2', 'tag3'}")] + >>> u(_normalize_command_handler_result("M105", None, "M105", None, {"tag1", "tag2"}, ["M110", ("M105", "temperature_poll"), "M117 Foobar"], tags_to_add={"tag3"})) + [('M110', None, 'M110', None, "{'tag1', 'tag2', 'tag3'}"), ('M105', 'temperature_poll', 'M105', None, "{'tag1', 'tag2', 'tag3'}"), ('M117 Foobar', None, 'M117', None, "{'tag1', 'tag2', 'tag3'}")] Arguments: command (str or None): The command for which the handler result was