Пример #1
0
def test_patch_instancemethod_mangled_tabs(tmpdir):
    tmpdir.join("tabs_mangled.py").write(
        dedent("""\
        class Artist:
        \tdef __mangled_name(self, v):
        \t\treturn v + ' on toast'

        \tdef method(self):
        \t\tfilling = 'Chalk'
        \t\treturn self.__mangled_name(filling)
    """))
    sys.path.insert(0, str(tmpdir))

    try:
        from tabs_mangled import Artist
    finally:
        sys.path.pop(0)

    patchy.patch(
        Artist.method,
        """\
        @@ -1,2 +1,2 @@
         def method(self):
        -\tfilling = 'Chalk'
        +\tfilling = 'Cheese'
        \treturn __mangled_name(filling)
        """,
    )

    assert Artist().method() == "Cheese on toast"
Пример #2
0
def test_patch_instancemethod_twice():
    class Artist:
        def method(self):
            return "Chalk"

    patchy.patch(
        Artist.method,
        """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    return "Chalk"
        +    return "Cheese"
        """,
    )

    patchy.patch(
        Artist.method,
        """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    return "Cheese"
        +    return "Crackers"
        """,
    )

    assert Artist().method() == "Crackers"
Пример #3
0
def test_patch_init_module_level(tmpdir):
    """
    Module level classes do not have a freevar for their class name, whilst
    classes defined in a scope do...
    """
    example_py = tmpdir.join('example.py')
    example_py.write(
        dedent('''\
        class Person(object):
            def __init__(self):
                self.base_prop = 'yo'


        class Artist(Person):
            def __init__(self):
                super(Artist, self).__init__()
                self.prop = 'old'
    '''))
    mod = example_py.pyimport()
    Artist = mod.Artist

    patchy.patch(
        Artist.__init__, """\
        @@ -1,3 +1,3 @@
         def __init__(self):
             super(Artist, self).__init__()
        -    self.prop = 'old'
        +    self.prop = 'new'
    """)

    a = Artist()
    assert a.base_prop == 'yo'
    assert a.prop == 'new'
Пример #4
0
def test_patch_init():
    class Artist(object):
        def __init__(self):
            self.prop = 'old'

    patchy.patch(
        Artist.__init__, """\
        @@ -1,2 +1,2 @@
         def __init__(self):
        -    self.prop = 'old'
        +    self.prop = 'new'""")

    a = Artist()
    assert a.prop == 'new'
Пример #5
0
def test_patch_instancemethod():
    class Artist(object):
        def method(self):
            return 'Chalk'

    patchy.patch(
        Artist.method, """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    return 'Chalk'
        +    return 'Cheese'
        """)

    assert Artist().method() == "Cheese"
Пример #6
0
def test_patch_init_change_arg():
    class Artist(object):
        def __init__(self):
            self.prop = 'old'

    patchy.patch(
        Artist.__init__, """\
        @@ -1,2 +1,2 @@
        -def __init__(self):
        -    self.prop = 'old'
        +def __init__(self, arg):
        +    self.prop = arg""")

    a = Artist('new')
    assert a.prop == 'new'
Пример #7
0
def test_patch_init():
    class Artist:
        def __init__(self):
            self.prop = "old"

    patchy.patch(
        Artist.__init__,
        """\
        @@ -1,2 +1,2 @@
         def __init__(self):
        -    self.prop = "old"
        +    self.prop = "new"
        """,
    )

    a = Artist()
    assert a.prop == "new"
Пример #8
0
def test_patch_init_change_arg():
    class Artist:
        def __init__(self):
            self.prop = "old"

    patchy.patch(
        Artist.__init__,
        """\
        @@ -1,2 +1,2 @@
        -def __init__(self):
        -    self.prop = "old"
        +def __init__(self, arg):
        +    self.prop = arg
        """,
    )

    a = Artist("new")
    assert a.prop == "new"
Пример #9
0
def test_patch_old_class_instancemethod_mangled():
    class Artist:
        def __mangled_name(self, v):
            return v + ' on toast'

        def method(self):
            filling = 'Chalk'
            return self.__mangled_name(filling)

    patchy.patch(
        Artist.method, """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    filling = 'Chalk'
        +    filling = 'Cheese'
             return self.__mangled_name(filling)
        """)

    assert Artist().method() == "Cheese on toast"
Пример #10
0
def test_patch_instancemethod_freevars():
    def free_func(v):
        return v + ' on toast'

    class Artist:
        def method(self):
            filling = 'Chalk'
            return free_func(filling)

    patchy.patch(
        Artist.method, """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    filling = 'Chalk'
        +    filling = 'Cheese'
             return free_func(filling)
        """)

    assert Artist().method() == "Cheese on toast"
Пример #11
0
def test_patch_instancemethod_mangled():
    class Artist:
        def __mangled_name(self, v):
            return v + " on toast"

        def method(self):
            filling = "Chalk"
            return self.__mangled_name(filling)

    patchy.patch(
        Artist.method,
        """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    filling = "Chalk"
        +    filling = "Cheese"
             return self.__mangled_name(filling)
        """,
    )

    assert Artist().method() == "Cheese on toast"
Пример #12
0
def test_patch_init_super_new():
    class Person(object):
        def __init__(self):
            self.base_prop = 'yo'

    class Artist(Person):
        def __init__(self):
            super().__init__()
            self.prop = 'old'

    patchy.patch(
        Artist.__init__, """\
        @@ -1,3 +1,3 @@
         def __init__(self):
             super().__init__()
        -    self.prop = 'old'
        +    self.prop = 'new'""")

    a = Artist()
    assert a.base_prop == 'yo'
    assert a.prop == 'new'
Пример #13
0
def test_patch_instancemethod_mangled_freevars():
    def _Artist__mangled_name(v):
        return v + ' on '

    def plain_name(v):
        return v + 'toast'

    class Artist:
        def method(self):
            filling = 'Chalk'
            return plain_name(__mangled_name(filling))  # noqa: F821

    patchy.patch(
        Artist.method, """\
        @@ -1,2 +1,2 @@
         def method(self):
        -    filling = 'Chalk'
        +    filling = 'Cheese'
             return plain_name(__mangled_name(filling))  # noqa: F821
        """)

    assert Artist().method() == "Cheese on toast"
Пример #14
0
def test_patch_init_super_new():
    class Person:
        def __init__(self):
            self.base_prop = "yo"

    class Artist(Person):
        def __init__(self):
            super().__init__()
            self.prop = "old"

    patchy.patch(
        Artist.__init__,
        """\
        @@ -1,3 +1,3 @@
         def __init__(self):
             super().__init__()
        -    self.prop = "old"
        +    self.prop = "new"
        """,
    )

    a = Artist()
    assert a.base_prop == "yo"
    assert a.prop == "new"