File: test_change.py

package info (click to toggle)
python-inline-snapshot 0.23.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,116 kB
  • sloc: python: 6,888; makefile: 34; sh: 28
file content (91 lines) | stat: -rw-r--r-- 2,039 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import ast

import pytest
from executing import Source

from inline_snapshot._change import CallArg
from inline_snapshot._change import Delete
from inline_snapshot._change import Replace
from inline_snapshot._change import apply_all
from inline_snapshot._inline_snapshot import snapshot
from inline_snapshot._rewrite_code import ChangeRecorder
from inline_snapshot._source_file import SourceFile


@pytest.fixture
def check_change(tmp_path):
    i = 0

    def w(source, changes, new_code):
        nonlocal i

        filename = tmp_path / f"test_{i}.py"
        i += 1

        filename.write_text(source)
        print(f"\ntest: {source}")

        source = Source.for_filename(filename)
        module = source.tree
        context = SourceFile(source)

        call = module.body[0].value
        assert isinstance(call, ast.Call)

        cr = ChangeRecorder()
        apply_all(changes(context, call), cr)

        cr.virtual_write()

        cr.dump()

        assert list(cr.files())[0].source == new_code

    return w


def test_change_function_args(check_change):

    check_change(
        "f(a,b=2)",
        lambda source, call: [
            Replace(
                flag="fix",
                file=source,
                node=call.args[0],
                new_code="22",
                old_value=0,
                new_value=0,
            )
        ],
        snapshot("f(22,b=2)"),
    )

    check_change(
        "f(a,b=2)",
        lambda source, call: [
            Delete(
                flag="fix",
                file=source,
                node=call.args[0],
                old_value=0,
            )
        ],
        snapshot("f(b=2)"),
    )

    check_change(
        "f(a,b=2)",
        lambda source, call: [
            CallArg(
                flag="fix",
                file=source,
                node=call,
                arg_pos=0,
                arg_name=None,
                new_code="22",
                new_value=22,
            )
        ],
        snapshot("f(22, a,b=2)"),
    )