def test_trepr(): assert pstr([1, 2, "hello"]) == "[1, 2, 'hello']" assert pstr({"a": 1, "b": 2}) == "{'a': 1, 'b': 2}" assert pstr(Point(1, 2)) == "Point(x=1, y=2)" assert ( pstr(H.span["kls"](H.b("great"), "canyon")) == '<span class="kls"><b>great</b>canyon</span>' )
def test_indent(): for indent in [3, 4]: i = "\n" + indent * " " assert ( pstr([1, 2, 3, 4], max_col=10, indent=indent) == f"[{i}1, {i}2, {i}3, {i}4\n]" )
def signature(self): name = getattr(self.fn, "__qualname__", str(self.fn)) parts = [pstr(arg, max_depth=0) for arg in self.args] parts += [ f"{k}={pstr(v, max_depth=0)}" for k, v in self.kwargs.items() ] args = ", ".join(parts) return f"{name}({args})"
def test_overflow(): phrase = "the healthy fox jumps over the fancy shrub or whatever I don't remember" data = [phrase] s = pstr(data, max_col=30, overflow="allow") assert indents(s) == [0, 4, 0] assert lengths(s) == [1, 77, 1] assert sum(lengths(s)) - sum(indents(s)) == len(phrase) + 4 s = pstr(data, max_col=30, overflow="break") assert indents(s) == [0, 4, 4, 4, 0] assert lengths(s) == [1, 30, 30, 25, 1] assert sum(lengths(s)) - sum(indents(s)) == len(phrase) + 4 s = pstr(data, max_col=30, overflow="backslash") assert indents(s) == [0, 4, 4, 4, 0] assert lengths(s) == [1, 30, 30, 29, 1] assert sum(lengths(s)) - sum(indents(s)) == len(phrase) + 4 + 4
def test_sequence_max(): assert pstr(list(range(10)), sequence_max=6) == "[0, 1, 2, 3, ..., 8, 9]" # Sets don't guarantee ordering, but this is good enough for now: assert pstr(set(range(10)), sequence_max=6) == "{0, 1, 2, 3, ..., 8, 9}" assert ( pstr({i: i * i for i in range(10)}, sequence_max=3) == "{0: 0, ..., 8: 64, 9: 81}" ) # sequence_max is too small, so it does nothing: assert ( pstr(list(range(10)), sequence_max=0) == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ) assert ( pstr(list(range(10)), sequence_max=1) == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ) assert ( pstr(list(range(10)), sequence_max=-1) == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" )
def test_trepr_functions(): # Functions assert pstr(lengths) == "function lengths" assert pstr(Point.some_method) == "function some_method" assert pstr(lambda x: x) == "function <lambda>" # Generators assert pstr(_gen(3)) == "generator _gen" # Coroutines assert pstr(_coro(3)) == "coroutine _coro" assert pstr(_corogen(3)) == "async_generator _corogen" # Classes assert pstr(Point) == "class Point" assert pstr(Exception) == "class Exception" assert pstr(type) == "metaclass type" # Builtins assert pstr(pow) == "builtin pow" assert pstr(open) == "builtin io.open" assert pstr([].append) == "builtin <list>.append" # Wrappers/Descriptors assert pstr(dict.update) == "descriptor dict.update" assert pstr(list.__str__) == "descriptor object.__str__" # Methods assert pstr(Point(1, 2).some_method) == "method <Point>.some_method" assert pstr([].__str__) == "method <list>.__str__" # Modules assert pstr(dataclasses) == "module dataclasses"
def test_str_short_dup(): s = "a" assert pstr([s, s, s]) == "['a', 'a', 'a']"
def test_trepr_misc(): assert pstr({"a": 1, "b": 2}.keys()) == "dict_keys('a', 'b')" assert pstr({"a": 1, "b": 2}.keys(), max_depth=0) == "dict_keys(...)" assert pstr({"a": 1, "b": 2}.values()) == "dict_values(1, 2)" assert pstr({"a": 1, "b": 2}.values(), max_depth=0) == "dict_values(...)"
def test_max_indent(): data = [[[[[["hello"]]]]]] result = pstr(data, indent=2, max_col=10, max_indent=6) assert indents(result) == [0, 2, 4, 6, 6, 6, 6, 6, 6, 6, 4, 2, 0]
def test_nested_indent(): result = pstr([1, {"a": 2, "b": 3}, 4], max_col=15, indent=4) assert indents(result) == [0, 4, 4, 8, 8, 4, 4, 0] assert result == _expected
def test_recursive(): li = [1, 2] li.append(li) assert pstr(li) == "#1=[1, 2, #1=[...]]" assert pstr(li, shortrefs=True) == "#1=[1, 2, #1]"
def test_trepr_exception(): assert pstr(TypeError("BAH")) == "TypeError(BAH)" assert pstr(TypeError()) == "TypeError()"