Пример #1
0
def test_fail_gracefully_on_bogus__qualname__and__name__():
    # Test that we correctly repr types that have non-string values for both
    # __qualname__ and __name__

    class Meta(type):
        __name__ = 5

    class Type(object):
        __metaclass__ = Meta
        __qualname__ = 5

    stream = StringIO()
    printer = pretty.RepresentationPrinter(stream)

    printer.pretty(Type)
    printer.flush()
    output = stream.getvalue()

    # If we can't find __name__ or __qualname__ just use a sentinel string.
    expected = '.'.join([__name__, '<unknown type>'])
    assert_equal(output, expected)

    # Clear stream buffer.
    stream.buf = ''

    # Test repring of an instance of the type.
    instance = Type()
    printer.pretty(instance)
    printer.flush()
    output = stream.getvalue()

    # Should look like:
    # <IPython.lib.tests.test_pretty.<unknown type> at 0x7f7658ae07d0>
    prefix = '<' + '.'.join([__name__, '<unknown type>']) + ' at 0x'
    assert_true(output.startswith(prefix))
Пример #2
0
def test_fallback_to__name__on_type():
    # Test that we correctly repr types that have non-string values for
    # __qualname__ by falling back to __name__

    class Type(object):
        __qualname__ = 5

    # Test repring of the type.
    stream = StringIO()
    printer = pretty.RepresentationPrinter(stream)

    printer.pretty(Type)
    printer.flush()
    output = stream.getvalue()

    # If __qualname__ is malformed, we should fall back to __name__.
    expected = '.'.join([__name__, Type.__name__])
    assert_equal(output, expected)

    # Clear stream buffer.
    stream.buf = ''

    # Test repring of an instance of the type.
    instance = Type()
    printer.pretty(instance)
    printer.flush()
    output = stream.getvalue()

    # Should look like:
    # <IPython.lib.tests.test_pretty.Type at 0x7f7658ae07d0>
    prefix = '<' + '.'.join([__name__, Type.__name__]) + ' at 0x'
    assert_true(output.startswith(prefix))
Пример #3
0
def test_empty_printer():
    printer = pretty.RepresentationPrinter(
        pretty.CUnicodeIO(),
        singleton_pprinters={},
        type_pprinters={int: pretty._repr_pprint, list: pretty._repr_pprint},
        deferred_pprinters={},
    )
    printer.pretty([1, 2, 3])
    assert printer.output.getvalue() == u"[1, 2, 3]"
Пример #4
0
def test_basic_class():
    def type_pprint_wrapper(obj, p, cycle):
        if obj is MyObj:
            type_pprint_wrapper.called = True
        return pretty._type_pprint(obj, p, cycle)
    type_pprint_wrapper.called = False

    stream = StringIO()
    printer = pretty.RepresentationPrinter(stream)
    printer.type_pprinters[type] = type_pprint_wrapper
    printer.pretty(MyObj)
    printer.flush()
    output = stream.getvalue()

    assert_equal(output, '%s.MyObj' % __name__)
    assert_true(type_pprint_wrapper.called)