コード例 #1
0
    def test_super_repr():
        output = pretty.pretty(super(SA))
        assert_in('SA', output)

        sb = SB()
        output = pretty.pretty(super(SA, sb))
        assert_in('SA', output)
コード例 #2
0
def test_unicode_repr():
    u = u"üniçodé"
    ustr = unicode_to_str(u)

    class C(object):
        def __repr__(self):
            return ustr

    c = C()
    p = pretty.pretty(c)
    assert_equal(p, u)
    p = pretty.pretty([c])
    assert_equal(p, u"[%s]" % u)
コード例 #3
0
def nicerepr(v):
    if inspect.isfunction(v):
        return get_pretty_function_description(v)
    elif isinstance(v, type):
        return v.__name__
    else:
        return to_str(pretty(v))
コード例 #4
0
def test_callability_checking():
    """Test that the _repr_pretty_ method is tested for callability and skipped
    if not."""
    gotoutput = pretty.pretty(Dummy2())
    expectedoutput = 'Dummy1(...)'

    assert_equal(gotoutput, expectedoutput)
コード例 #5
0
def test_collections_deque():
    # Create deque with cycle
    a = deque()
    a.append(a)

    cases = [
        (deque(), 'deque([])'),
        (deque(i for i in range(1000, 1020)),
         'deque([1000,\n'
         '       1001,\n'
         '       1002,\n'
         '       1003,\n'
         '       1004,\n'
         '       1005,\n'
         '       1006,\n'
         '       1007,\n'
         '       1008,\n'
         '       1009,\n'
         '       1010,\n'
         '       1011,\n'
         '       1012,\n'
         '       1013,\n'
         '       1014,\n'
         '       1015,\n'
         '       1016,\n'
         '       1017,\n'
         '       1018,\n'
         '       1019])'),
        (a, 'deque([deque(...)])'),
    ]
    for obj, expected in cases:
        assert_equal(pretty.pretty(obj), expected)
コード例 #6
0
def test_dispatch():
    """Test correct dispatching: The _repr_pretty_ method for MyDict must be
    found before the registered printer for dict."""
    gotoutput = pretty.pretty(MyDict())
    expectedoutput = "MyDict(...)"

    assert_equal(gotoutput, expectedoutput)
コード例 #7
0
def test_indentation():
    """Test correct indentation in groups."""
    count = 40
    gotoutput = pretty.pretty(MyList(range(count)))
    expectedoutput = "MyList(\n" + ",\n".join("   %d" % i for i in range(count)) + ")"

    assert_equal(gotoutput, expectedoutput)
コード例 #8
0
def test_re_evals():
    for r in [
        re.compile(r'hi'), re.compile(r'b\nc', re.MULTILINE),
        re.compile(br'hi', 0), re.compile(u'foo', re.MULTILINE | re.UNICODE),
    ]:
        r2 = eval(pretty.pretty(r), globals())
        assert r.pattern == r2.pattern and r.flags == r2.flags
コード例 #9
0
def test_indentation():
    """Test correct indentation in groups."""
    count = 40
    gotoutput = pretty.pretty(MyList(range(count)))
    expectedoutput = 'MyList(\n' + ',\n'.join('   %d' %
                                              i for i in range(count)) + ')'

    assert_equal(gotoutput, expectedoutput)
コード例 #10
0
def test_sets():
    """Test that set and frozenset use Python 3 formatting."""
    objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]),
               frozenset([1, 2]), set([-1, -2, -3])]
    expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}',
                'frozenset({1, 2})', '{-3, -2, -1}']
    for obj, expected_output in zip(objects, expected):
        got_output = pretty.pretty(obj)
        yield assert_equal, got_output, expected_output
コード例 #11
0
def test_re_evals():
    for r in [
        re.compile(r"hi"),
        re.compile(r"b\nc", re.MULTILINE),
        re.compile(br"hi", 0),
        re.compile(u"foo", re.MULTILINE | re.UNICODE),
    ]:
        r2 = eval(pretty.pretty(r), globals())
        assert r.pattern == r2.pattern and r.flags == r2.flags
コード例 #12
0
def test_collections_counter():
    class MyCounter(Counter):
        pass
    cases = [
        (Counter(), 'Counter()'),
        (Counter(a=1), "Counter({'a': 1})"),
        (MyCounter(a=1), "MyCounter({'a': 1})"),
    ]
    for obj, expected in cases:
        assert_equal(pretty.pretty(obj), expected)
コード例 #13
0
def test_collections_defaultdict():
    # Create defaultdicts with cycles
    a = defaultdict()
    a.default_factory = a
    b = defaultdict(list)
    b['key'] = b

    # Dictionary order cannot be relied on, test against single keys.
    cases = [
        (defaultdict(list), 'defaultdict(list, {})'),
        (defaultdict(list, {'key': '-' * 50}),
         'defaultdict(list,\n'
         "            {'key': '-----------------------------------------"
         "---------'})"),
        (a, 'defaultdict(defaultdict(...), {})'),
        (b, "defaultdict(list, {'key': defaultdict(...)})"),
    ]
    for obj, expected in cases:
        assert_equal(pretty.pretty(obj), expected)
コード例 #14
0
def test_collections_ordereddict():
    # Create OrderedDict with cycle
    a = OrderedDict()
    a['key'] = a

    cases = [
        (OrderedDict(), 'OrderedDict()'),
        (OrderedDict((i, i) for i in range(1000, 1010)),
         'OrderedDict([(1000, 1000),\n'
         '             (1001, 1001),\n'
         '             (1002, 1002),\n'
         '             (1003, 1003),\n'
         '             (1004, 1004),\n'
         '             (1005, 1005),\n'
         '             (1006, 1006),\n'
         '             (1007, 1007),\n'
         '             (1008, 1008),\n'
         '             (1009, 1009)])'),
        (a, "OrderedDict([('key', OrderedDict(...))])"),
    ]
    for obj, expected in cases:
        assert_equal(pretty.pretty(obj), expected)
コード例 #15
0
def test_sets():
    """Test that set and frozenset use Python 3 formatting."""
    objects = [
        set(),
        frozenset(),
        {1},
        frozenset([1]),
        {1, 2},
        frozenset([1, 2]),
        {-1, -2, -3},
    ]
    expected = [
        "set()",
        "frozenset()",
        "{1}",
        "frozenset({1})",
        "{1, 2}",
        "frozenset({1, 2})",
        "{-3, -2, -1}",
    ]
    for obj, expected_output in zip(objects, expected):
        got_output = pretty.pretty(obj)
        assert_equal(got_output, expected_output)
コード例 #16
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_long_tuple():
    tup = tuple(range(10000))
    p = pretty.pretty(tup)
    last2 = p.rsplit("\n", 2)[-2:]
    assert_equal(last2, [" 999,", " ...)"])
コード例 #17
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_unbound_method():
    output = pretty.pretty(MyObj.somemethod)
    assert_in("MyObj.somemethod", output)
コード例 #18
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_cyclic_list():
    x = []
    x.append(x)
    assert pretty.pretty(x) == "[[...]]"
コード例 #19
0
def test_pprint_break_repr():
    """Test that p.break_ is used in repr."""
    output = pretty.pretty(BreakingReprParent())
    expected = "TG: Breaking(\n    ):"
    assert_equal(output, expected)
コード例 #20
0
def test_pprint_nomod():
    """Test that pprint works for classes with no __module__."""
    output = pretty.pretty(NoModule)
    assert_equal(output, "NoModule")
コード例 #21
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_cyclic_dequeue():
    x = deque()
    x.append(x)
    assert pretty.pretty(x) == "deque([deque(...)])"
コード例 #22
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_print_builtin_function():
    assert pretty.pretty(abs) == "<function abs>"
コード例 #23
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_custom():
    assert "bye" not in pretty.pretty(CustomStuff())
    assert "bye=" in pretty.pretty(CustomStuff(), verbose=True)
    assert "squirrels" not in pretty.pretty(CustomStuff(), verbose=True)
コード例 #24
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_exception():
    assert pretty.pretty(ValueError("hi")) == "ValueError('hi')"
    assert pretty.pretty(ValueError("hi", "there")) == "ValueError('hi', 'there')"
    assert "test_pretty." in pretty.pretty(MyException())
コード例 #25
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_print_with_indent():
    pretty.pretty(BigList([1, 2, 3]))
コード例 #26
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_pprint():
    t = {"hi": 1}
    with capture_out() as o:
        pretty.pprint(t)
    assert o.getvalue().strip() == pretty.pretty(t)
コード例 #27
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_cyclic_set():
    x = set()
    x.add(HashItAnyway(x))
    assert pretty.pretty(x) == "{{...}}"
コード例 #28
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_cyclic_dict():
    x = {}
    k = HashItAnyway(x)
    x[k] = x
    assert pretty.pretty(x) == "{{...}: {...}}"
コード例 #29
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_cyclic_counter():
    c = Counter()
    k = HashItAnyway(c)
    c[k] = 1
    assert pretty.pretty(c) == "Counter({Counter(...): 1})"
コード例 #30
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_pretty_function():
    assert "." in pretty.pretty(test_pretty_function)
コード例 #31
0
def test_pprint_heap_allocated_type():
    """Test that pprint works for heap allocated types."""
    import xxlimited

    output = pretty.pretty(xxlimited.Null)
    assert_equal(output, "xxlimited.Null")
コード例 #32
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_breakable_at_group_boundary():
    assert "\n" in pretty.pretty([[], "000000"], max_width=5)
コード例 #33
0
def test_pprint_break():
    """Test that p.break_ produces expected output."""
    output = pretty.pretty(Breaking())
    expected = "TG: Breaking(\n    ):"
    assert_equal(output, expected)
コード例 #34
0
def test_cyclic_list():
    x = []
    x.append(x)
    assert pretty.pretty(x) == '[[...]]'
コード例 #35
0
def test_bad_repr():
    """Don't catch bad repr errors."""
    with assert_raises(ZeroDivisionError):
        pretty.pretty(BadRepr())
コード例 #36
0
 def _repr_pretty_(self, pretty, cycle):
     pretty.pretty(self.value)
コード例 #37
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_metaclass_repr():
    output = pretty.pretty(ClassWithMeta)
    assert_equal(output, "[CUSTOM REPR FOR CLASS ClassWithMeta]")
コード例 #38
0
def test_cyclic_dict():
    x = {}
    k = HashItAnyway(x)
    x[k] = x
    assert pretty.pretty(x) == '{{...}: {...}}'
コード例 #39
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
def test_long_dict():
    d = {n: n for n in range(10000)}
    p = pretty.pretty(d)
    last2 = p.rsplit("\n", 2)[-2:]
    assert_equal(last2, [" 999: 999,", " ...}"])
コード例 #40
0
def test_pprint():
    t = {'hi': 1}
    with capture_out() as o:
        pretty.pprint(t)
    assert o.getvalue().strip() == pretty.pretty(t)
コード例 #41
0
def test_metaclass_repr():
    output = pretty.pretty(ClassWithMeta)
    assert_equal(output, '[CUSTOM REPR FOR CLASS ClassWithMeta]')
コード例 #42
0
def test_exception():
    assert pretty.pretty(ValueError('hi')) == "ValueError('hi')"
    assert pretty.pretty(ValueError('hi', 'there')) == \
        "ValueError('hi', 'there')"
    assert 'test_pretty.' in pretty.pretty(MyException())
コード例 #43
0
def test_custom():
    assert 'bye' not in pretty.pretty(CustomStuff())
    assert 'bye=' in pretty.pretty(CustomStuff(), verbose=True)
    assert 'squirrels' not in pretty.pretty(CustomStuff(), verbose=True)
コード例 #44
0
def test_pretty_function():
    assert '.' in pretty.pretty(test_pretty_function)
コード例 #45
0
def test_list():
    assert pretty.pretty([]) == "[]"
    assert pretty.pretty([1]) == "[1]"
コード例 #46
0
ファイル: test_pretty.py プロジェクト: yssource/hypothesis
 def _repr_pretty_(self, pretty, cycle):
     pretty.pretty(self.value)
コード例 #47
0
def test_cyclic_dequeue():
    x = deque()
    x.append(x)
    assert pretty.pretty(x) == 'deque([deque(...)])'
コード例 #48
0
def test_dict():
    assert pretty.pretty({}) == "{}"
    assert pretty.pretty({1: 1}) == "{1: 1}"
コード例 #49
0
def test_cyclic_counter():
    c = Counter()
    k = HashItAnyway(c)
    c[k] = 1
    assert pretty.pretty(c) == 'Counter({Counter(...): 1})'
コード例 #50
0
def test_tuple():
    assert pretty.pretty(()) == "()"
    assert pretty.pretty((1, )) == "(1,)"
    assert pretty.pretty((1, 2)) == "(1, 2)"
コード例 #51
0
def test_cyclic_set():
    x = set()
    x.add(HashItAnyway(x))
    assert pretty.pretty(x) == '{{...}}'
コード例 #52
0
def test_dict_with_custom_repr():
    assert pretty.pretty(ReprDict()) == "hi"
コード例 #53
0
def test_print_with_indent():
    pretty.pretty(BigList([1, 2, 3]))
コード例 #54
0
def test_set_with_custom_repr():
    assert pretty.pretty(ReprSet()) == "cat"
コード例 #55
0
def test_re_evals():
    for r in [
        re.compile(r'hi'), re.compile(r'b\nc', re.MULTILINE),
        re.compile(br'hi', 0), re.compile(u'foo', re.MULTILINE | re.UNICODE),
    ]:
        assert repr(eval(pretty.pretty(r), globals())) == repr(r)
コード例 #56
0
def test_list_with_custom_repr():
    assert pretty.pretty(ReprList()) == "bye"
コード例 #57
0
def test_print_builtin_function():
    assert pretty.pretty(abs) == '<function abs>'
コード例 #58
0
def test_unsortable_set():
    xs = {1, 2, 3, "foo", "bar", "baz", object()}
    p = pretty.pretty(xs)
    for x in xs:
        assert pretty.pretty(x) in p
コード例 #59
0
def test_breakable_at_group_boundary():
    assert '\n' in pretty.pretty([[], '000000'], max_width=5)
コード例 #60
0
def test_unsortable_dict():
    xs = {k: 1 for k in [1, 2, 3, "foo", "bar", "baz", object()]}
    p = pretty.pretty(xs)
    for x in xs:
        assert pretty.pretty(x) in p