Exemplo n.º 1
0
def test_print():
    def convert_print(target):
        print(convert(target))

    def convert_pprint(target):
        pprint(convert(target))

    for invalid_argument in [1, 'foo']:
        with pytest.raises(ValueError) as err:
            pprint(invalid_argument)
        assert str(err.value) == 'Expecting a list or a node'

    for print_func in [pprint, convert_pprint]:
        with CaptureOutput() as output:
            print_func([])
        assert output == ['']

    for print_func in [convert_print, pprint, convert_pprint]:
        with CaptureOutput() as output:
            print_func([1, 2])
        assert output == ['', '  1', ' / ', '2  ', '   ']

        with CaptureOutput() as output:
            print_func([1, None, 3])
        assert output == ['', '1  ', ' \\ ', '  3', '   ']

        with CaptureOutput() as output:
            print_func([1, 2, 3])
        assert output == ['', '  1  ', ' / \\ ', '2   3', '     ']

        with CaptureOutput() as output:
            print_func([1, 2, 3, None, 5])
        assert output == [
            '', '  __1  ', ' /   \\ ', '2     3', ' \\     ', '  5    ',
            '       '
        ]
        with CaptureOutput() as output:
            print_func([1, 2, 3, None, 5, 6])
        assert output == [
            '', '  __1__  ', ' /     \\ ', '2       3', ' \\     / ',
            '  5   6  ', '         '
        ]
        with CaptureOutput() as output:
            print_func([1, 2, 3, None, 5, 6, 7])
        assert output == [
            '', '  __1__    ', ' /     \\   ', '2       3  ', ' \\     / \\ ',
            '  5   6   7', '           '
        ]
        with CaptureOutput() as output:
            print_func([1, 2, 3, 8, 5, 6, 7])
        assert output == [
            '', '    __1__    ', '   /     \\   ', '  2       3  ',
            ' / \\     / \\ ', '8   5   6   7', '             '
        ]
    for _ in range(repetitions):
        bt = tree(height=10)
        with CaptureOutput() as output1:
            print(bt)
        assert output1 == str(bt).splitlines()
        assert output1 == stringify(bt).splitlines()
Exemplo n.º 2
0
def test_show_all():
    def convert_show_all(target):
        show_all(convert(target))

    def convert_self_show_all(target):
        convert(target).show_all()

    for invalid_argument in [1, 'foo']:
        with pytest.raises(ValueError) as err:
            show_all(invalid_argument)
        assert str(err.value) == 'Expecting a list or a node'

    for show_func in [show_all, convert_show_all]:
        with CaptureOutput() as output:
            show_func([])
        assert output == ['']

    for show_func in [show_all, convert_show_all, convert_self_show_all]:
        with CaptureOutput() as output:
            show_func([1, 2])
        assert output == ['', '   _0:1', '  /    ', '1:2    ', '       ']

        with CaptureOutput() as output:
            show_func([1, None, 3])
        assert output == ['', '0:1_   ', '    \\  ', '    1:3', '       ']

        with CaptureOutput() as output:
            show_func([1, 2, 3])
        assert output == [
            '', '   _0:1_   ', '  /     \\  ', '1:2     2:3', '           '
        ]

        with CaptureOutput() as output:
            show_func([1, 2, 3, None, 5])
        assert output == [
            '', '   _____0:1_   ', '  /         \\  ', '1:2_        2:3',
            '    \\          ', '    3:5        ', '               '
        ]

        with CaptureOutput() as output:
            show_func([1, 2, 3, None, 5, 6])
        assert output == [
            '', '   _____0:1_____   ', '  /             \\  ',
            '1:2_           _2:3', '    \\         /    ',
            '    3:5     4:6    ', '                   '
        ]
        with CaptureOutput() as output:
            show_func([1, 2, 3, None, 5, 6, 7])
        assert output == [
            '', '   _____0:1_____       ', '  /             \\      ',
            '1:2_           _2:3_   ', '    \\         /     \\  ',
            '    3:5     4:6     5:7', '                       '
        ]
        with CaptureOutput() as output:
            show_func([1, 2, 3, 8, 5, 6, 7])
        assert output == [
            '', '       _____0:1_____       ', '      /             \\      ',
            '   _1:2_           _2:3_   ', '  /     \\         /     \\  ',
            '3:8     4:5     5:6     6:7', '                           '
        ]
    for _ in range(repetitions):
        bt = tree(height=10)
        with CaptureOutput() as output:
            show_all(bt)
        assert output == stringify(bt, True, True).splitlines()
Exemplo n.º 3
0
 def __str__(self):
     return 'The tree \"%s\" is%s sorted:' % \
         (self.name, ('' if self.search_tree else ' not')) + \
         stringify(self.root)
Exemplo n.º 4
0
def test_show_ids():
    def convert_show_ids(target):
        show_ids(convert(target))

    def convert_self_show_ids(target):
        convert(target).show_ids()

    for invalid_argument in [1, 'foo']:
        with pytest.raises(ValueError) as err:
            show_ids(invalid_argument)
        assert str(err.value) == 'Expecting a list or a node'

    for show_func in [show_ids, convert_show_ids]:
        with CaptureOutput() as output:
            show_func([])
        assert output == ['']

    for show_func in [show_ids, convert_show_ids, convert_self_show_ids]:
        with CaptureOutput() as output:
            show_func([1, 2])
        assert output == ['', '  0', ' / ', '1  ', '   ']

        with CaptureOutput() as output:
            show_func([1, None, 3])
        assert output == ['', '0  ', ' \\ ', '  1', '   ']

        with CaptureOutput() as output:
            show_func([1, 2, 3])
        assert output == ['', '  0  ', ' / \\ ', '1   2', '     ']

        with CaptureOutput() as output:
            show_func([1, 2, 3, None, 5])

        assert output == [
            '', '  __0  ', ' /   \\ ', '1     2', ' \\     ', '  3    ',
            '       '
        ]
        with CaptureOutput() as output:
            show_func([1, 2, 3, None, 5, 6])
        assert output == [
            '', '  __0__  ', ' /     \\ ', '1       2', ' \\     / ',
            '  3   4  ', '         '
        ]
        with CaptureOutput() as output:
            show_func([1, 2, 3, None, 5, 6, 7])
        assert output == [
            '', '  __0__    ', ' /     \\   ', '1       2  ', ' \\     / \\ ',
            '  3   4   5', '           '
        ]
        with CaptureOutput() as output:
            show_func([1, 2, 3, 8, 5, 6, 7])
        assert output == [
            '', '    __0__    ', '   /     \\   ', '  1       2  ',
            ' / \\     / \\ ', '3   4   5   6', '             '
        ]

    for _ in range(repetitions):
        bt = tree(height=10)
        with CaptureOutput() as output:
            show_ids(bt)
        assert output == stringify(bt, True, False).splitlines()
Exemplo n.º 5
0
 def stringify(self):
     return stringify(self.root)