Esempio n. 1
0
def test_override(capture, msg):
    from pybind11_tests import (ExampleVirt, runExampleVirt,
                                runExampleVirtVirtual, runExampleVirtBool)

    class ExtendedExampleVirt(ExampleVirt):
        def __init__(self, state):
            super(ExtendedExampleVirt, self).__init__(state + 1)
            self.data = "Hello world"

        def run(self, value):
            print('ExtendedExampleVirt::run(%i), calling parent..' % value)
            return super(ExtendedExampleVirt, self).run(value + 1)

        def run_bool(self):
            print('ExtendedExampleVirt::run_bool()')
            return False

        def pure_virtual(self):
            print('ExtendedExampleVirt::pure_virtual(): %s' % self.data)

    ex12 = ExampleVirt(10)
    with capture:
        assert runExampleVirt(ex12, 20) == 30
    assert capture == "Original implementation of ExampleVirt::run(state=10, value=20)"

    with pytest.raises(RuntimeError) as excinfo:
        runExampleVirtVirtual(ex12)
    assert msg(
        excinfo.value
    ) == 'Tried to call pure virtual function "ExampleVirt::pure_virtual"'

    ex12p = ExtendedExampleVirt(10)
    with capture:
        assert runExampleVirt(ex12p, 20) == 32
    assert capture == """
        ExtendedExampleVirt::run(20), calling parent..
        Original implementation of ExampleVirt::run(state=11, value=21)
    """
    with capture:
        assert runExampleVirtBool(ex12p) is False
    assert capture == "ExtendedExampleVirt::run_bool()"
    with capture:
        runExampleVirtVirtual(ex12p)
    assert capture == "ExtendedExampleVirt::pure_virtual(): Hello world"

    cstats = ConstructorStats.get(ExampleVirt)
    assert cstats.alive() == 2
    del ex12, ex12p
    assert cstats.alive() == 0
    assert cstats.values() == ['10', '11']
    assert cstats.copy_constructions == 0
    assert cstats.move_constructions >= 0
def test_override(capture, msg):
    from pybind11_tests import (ExampleVirt, runExampleVirt, runExampleVirtVirtual,
                                runExampleVirtBool)

    class ExtendedExampleVirt(ExampleVirt):
        def __init__(self, state):
            super(ExtendedExampleVirt, self).__init__(state + 1)
            self.data = "Hello world"

        def run(self, value):
            print('ExtendedExampleVirt::run(%i), calling parent..' % value)
            return super(ExtendedExampleVirt, self).run(value + 1)

        def run_bool(self):
            print('ExtendedExampleVirt::run_bool()')
            return False

        def get_string1(self):
            return "override1"

        def pure_virtual(self):
            print('ExtendedExampleVirt::pure_virtual(): %s' % self.data)

    class ExtendedExampleVirt2(ExtendedExampleVirt):
        def __init__(self, state):
            super(ExtendedExampleVirt2, self).__init__(state + 1)

        def get_string2(self):
            return "override2"

    ex12 = ExampleVirt(10)
    with capture:
        assert runExampleVirt(ex12, 20) == 30
    assert capture == """
        Original implementation of ExampleVirt::run(state=10, value=20, str1=default1, str2=default2)
    """  # noqa: E501 line too long

    with pytest.raises(RuntimeError) as excinfo:
        runExampleVirtVirtual(ex12)
    assert msg(excinfo.value) == 'Tried to call pure virtual function "ExampleVirt::pure_virtual"'

    ex12p = ExtendedExampleVirt(10)
    with capture:
        assert runExampleVirt(ex12p, 20) == 32
    assert capture == """
        ExtendedExampleVirt::run(20), calling parent..
        Original implementation of ExampleVirt::run(state=11, value=21, str1=override1, str2=default2)
    """  # noqa: E501 line too long
    with capture:
        assert runExampleVirtBool(ex12p) is False
    assert capture == "ExtendedExampleVirt::run_bool()"
    with capture:
        runExampleVirtVirtual(ex12p)
    assert capture == "ExtendedExampleVirt::pure_virtual(): Hello world"

    ex12p2 = ExtendedExampleVirt2(15)
    with capture:
        assert runExampleVirt(ex12p2, 50) == 68
    assert capture == """
        ExtendedExampleVirt::run(50), calling parent..
        Original implementation of ExampleVirt::run(state=17, value=51, str1=override1, str2=override2)
    """  # noqa: E501 line too long

    cstats = ConstructorStats.get(ExampleVirt)
    assert cstats.alive() == 3
    del ex12, ex12p, ex12p2
    assert cstats.alive() == 0
    assert cstats.values() == ['10', '11', '17']
    assert cstats.copy_constructions == 0
    assert cstats.move_constructions >= 0
def test_override(capture, msg):
    from pybind11_tests import (ExampleVirt, runExampleVirt,
                                runExampleVirtVirtual, runExampleVirtBool)

    class ExtendedExampleVirt(ExampleVirt):
        def __init__(self, state):
            super(ExtendedExampleVirt, self).__init__(state + 1)
            self.data = "Hello world"

        def run(self, value):
            print('ExtendedExampleVirt::run(%i), calling parent..' % value)
            return super(ExtendedExampleVirt, self).run(value + 1)

        @staticmethod
        def run_bool():
            print('ExtendedExampleVirt::run_bool()')
            return False

        @staticmethod
        def get_string1():
            return "override1"

        def pure_virtual(self):
            print('ExtendedExampleVirt::pure_virtual(): %s' % self.data)

    class ExtendedExampleVirt2(ExtendedExampleVirt):
        def __init__(self, state):
            super(ExtendedExampleVirt2, self).__init__(state + 1)

        @staticmethod
        def get_string2():
            return "override2"

    ex12 = ExampleVirt(10)
    with capture:
        if runExampleVirt(ex12, 20) != 30:
            raise AssertionError
    if capture != """
        Original implementation of ExampleVirt::run(state=10, value=20, str1=default1, str2=default2)
    """:
        raise AssertionError

    with pytest.raises(RuntimeError) as excinfo:
        runExampleVirtVirtual(ex12)
    if msg(
            excinfo.value
    ) != 'Tried to call pure virtual function "ExampleVirt::pure_virtual"':
        raise AssertionError

    ex12p = ExtendedExampleVirt(10)
    with capture:
        if runExampleVirt(ex12p, 20) != 32:
            raise AssertionError
    if capture != """
        ExtendedExampleVirt::run(20), calling parent..
        Original implementation of ExampleVirt::run(state=11, value=21, str1=override1, str2=default2)
    """:
        raise AssertionError
    with capture:
        if runExampleVirtBool(ex12p) is not False:
            raise AssertionError
    if capture != "ExtendedExampleVirt::run_bool()":
        raise AssertionError
    with capture:
        runExampleVirtVirtual(ex12p)
    if capture != "ExtendedExampleVirt::pure_virtual(): Hello world":
        raise AssertionError

    ex12p2 = ExtendedExampleVirt2(15)
    with capture:
        if runExampleVirt(ex12p2, 50) != 68:
            raise AssertionError
    if capture != """
        ExtendedExampleVirt::run(50), calling parent..
        Original implementation of ExampleVirt::run(state=17, value=51, str1=override1, str2=override2)
    """:
        raise AssertionError

    cstats = ConstructorStats.get(ExampleVirt)
    if cstats.alive() != 3:
        raise AssertionError
    del ex12, ex12p, ex12p2
    if cstats.alive() != 0:
        raise AssertionError
    if cstats.values() != ['10', '11', '17']:
        raise AssertionError
    if cstats.copy_constructions != 0:
        raise AssertionError
    if cstats.move_constructions < 0:
        raise AssertionError