コード例 #1
0
def test_code_recoder(ballon):
    rec1 = make_recoder(ballon.module.inflate.__code__)
    rec2 = make_recoder(ballon.module.inflate)
    assert rec1.codefile is rec2.codefile
    assert rec1.focus is rec2.focus
    assert re.match(r"ballon:main__[0-9]+\.inflate", rec1.name)
    assert rec1.name == rec2.name
コード例 #2
0
def test_recoder_out_of_sync_2(ballon):
    rec1 = make_recoder(ballon.module.inflate)
    rec2 = make_recoder(ballon.module.inflate)
    assert ballon.module.inflate(4) == 8
    # Patch to what it was before
    rec1.patch(
        textwrap.dedent("""
            def inflate(x):
                return x * 2
            """))

    rec2.patch(
        textwrap.dedent("""
            def inflate(x):
                return x * 20
            """))
    assert ballon.module.inflate(4) == 80

    rec2.commit()
    with pytest.raises(OutOfSyncException):
        rec1.commit()

    assert ballon.module.inflate(4) == 80
    rec1.repatch()
    assert ballon.module.inflate(4) == 8
    rec1.commit()
コード例 #3
0
def test_function_recoder(ballon):
    rec = make_recoder(ballon.module.inflate)
    assert ballon.module.inflate(4) == 8
    rec.patch(
        textwrap.dedent("""
            def inflate(x):
                return x * 10
            """))
    assert ballon.module.inflate(4) == 40

    with pytest.raises(ValueError):
        rec.patch(
            textwrap.dedent("""
                def infloote(x):
                    return x * 10
                """))

    rec.patch_module(
        textwrap.dedent("""
            x = 10
            def infloote(x):
                return x * 100
            """))
    assert ballon.module.infloote(4) == 400
    assert ballon.module.x == 10
コード例 #4
0
def test_module_recoder(ballon):
    rec = make_recoder(ballon.module)
    assert ballon.module.inflate(4) == 8
    rec.patch(
        textwrap.dedent("""
            def inflate(x):
                return x * 10
            """))
    assert ballon.module.inflate(4) == 40
コード例 #5
0
def test_method_recoder(ballon):
    rec = make_recoder(ballon.module.FlatCircle.volume)
    assert ballon.module.FlatCircle(10).volume() == -1
    rec.patch(
        textwrap.dedent("""
            def volume(self):
                return self.radius
            """))
    assert ballon.module.FlatCircle(10).volume() == 10
コード例 #6
0
def test_recoder_patch_registry(ballon):
    rec = make_recoder(ballon.module.inflate)
    assert ballon.module.inflate(4) == 8
    rec.patch(
        textwrap.dedent("""
            def inflate(x):
                return x * 10
            """))

    # ballon.module.inflate.__code__.co_filename has changed, so this could
    # behave badly
    rec2 = make_recoder(ballon.module.inflate)
    assert rec.codefile is rec2.codefile
    assert rec.focus is rec2.focus

    # Test with __code__ directly
    rec2 = make_recoder(ballon.module.inflate.__code__)
    assert rec.codefile is rec2.codefile
    assert rec.focus is rec2.focus
コード例 #7
0
def test_recoder_registry(ballon):
    rec = make_recoder(ballon.module.inflate)
    assert ballon.module.inflate(4) == 8
    rec.patch(
        textwrap.dedent("""
            def inflate(x):
                return x * 10
            """))
    cf, defn = registry.find(ballon.module.inflate)
    assert cf is rec.codefile
    assert cf.filename == ballon.main.filename
コード例 #8
0
def test_function_recoder_delete(ballon):
    rec = make_recoder(ballon.module.inflate, deletable=True)
    assert ballon.module.inflate(4) == 8
    rec.patch("")
    assert not hasattr(ballon.module, "inflate")

    # Reinsert
    rec.patch(
        textwrap.dedent("""
            def inflate(x):
                return x * 10
            """))
    assert ballon.module.inflate(4) == 40
コード例 #9
0
def test_class_recoder(ballon):
    rec = make_recoder(ballon.module.FlatCircle)
    assert ballon.module.FlatCircle(10).volume() == -1
    rec.patch(
        textwrap.dedent("""
            class FlatCircle:
                def __init__(self, radius):
                    self.radius = radius

                def circumference(self):
                    return 2 * math.pi * self.radius

                def volume(self):
                    return self.radius
            """))
    assert ballon.module.FlatCircle(10).volume() == 10