def test_field_injector_correctly_injects_class_after_field():
    @inject(target=cls54, injector=FieldInjector('y', insert='after'))
    def handler():
        y = 25

    assert cls54.x == 10
    assert cls54().x == 10
    assert cls54.y == 25
    assert cls54().y == 25
    assert cls54.z == 30
    assert cls54().z == 30
def test_field_injector_correctly_injects_class_before_field():
    @inject(target=cls37, injector=FieldInjector('y', insert='before'))
    def handler():
        x = 15

    assert cls37.x == 15
    assert cls37().x == 15
    assert cls37.y == 20
    assert cls37().y == 20
    assert cls37.z == 30
    assert cls37().z == 30
def test_field_injector_correctly_injects_async_function_after_all_fields():
    async def target(x):
        if x > 100:
            y = x * 2
        else:
            y = x + 2
        return y

    @inject(target=target, injector=FieldInjector('y', insert='after'))
    def handler():
        y -= 1

    assert run(target(13)) == 14
    assert run(target(101)) == 201
def test_field_injector_correctly_injects_function_before_all_fields():
    def target(x):
        if x > 100:
            y = x * 2
        else:
            y = x + 2
        return y

    @inject(target=target, injector=FieldInjector('y', insert='before'))
    def handler():
        x += 1

    assert target(13) == 16
    assert target(101) == 204
def test_field_injector_correctly_injects_function_after_ordinal_field():
    def target(x):
        if x > 100:
            y = x * 2
        else:
            y = x + 2
        return y

    @inject(target=target,
            injector=FieldInjector('y', ordinal=0, insert='after'))
    def handler():
        y -= 1

    assert target(13) == 15
    assert target(101) == 201
def test_field_injector_correctly_injects_method_after_all_fields():
    class Target:
        def target(self, x):
            if x > 100:
                y = x * 2
            else:
                y = x + 2
            return y

    @inject(target=Target.target, injector=FieldInjector('y', insert='after'))
    def handler():
        y -= 1

    target = Target()
    assert target.target(13) == 14
    assert target.target(101) == 201
def test_field_injector_correctly_injects_static_method_before_all_fields():
    class Target:
        @staticmethod
        def target(x):
            if x > 100:
                y = x * 2
            else:
                y = x + 2
            return y

    @inject(target=Target.target, injector=FieldInjector('y', insert='before'))
    def handler():
        x += 1

    target = Target()
    assert target.target(13) == 16
    assert target.target(101) == 204
def test_field_injector_correctly_injects_async_function_before_ordinal_field(
):
    async def target(x):
        if x > 100:
            y = x * 2
        else:
            y = x + 2
        return y

    @inject(
        target=target,
        injector=FieldInjector('y', ordinal=1, insert='before'),
    )
    def handler():
        x += 1

    assert run(target(13)) == 16
    assert run(target(101)) == 202
def test_field_injector_correctly_injects_static_method_after_ordinal_field():
    class Target:
        @staticmethod
        def target(x):
            if x > 100:
                y = x * 2
            else:
                y = x + 2
            return y

    @inject(target=Target.target,
            injector=FieldInjector('y', ordinal=0, insert='after'))
    def handler():
        y -= 1

    target = Target()
    assert target.target(13) == 15
    assert target.target(101) == 201
def test_field_injector_correctly_injects_method_before_ordinal_field():
    class Target:
        def target(self, x):
            if x > 100:
                y = x * 2
            else:
                y = x + 2
            return y

    @inject(
        target=Target.target,
        injector=FieldInjector('y', ordinal=1, insert='before'),
    )
    def handler():
        x += 1

    target = Target()
    assert target.target(13) == 16
    assert target.target(101) == 202