File: test_change_types.py

package info (click to toggle)
python-inline-snapshot 0.29.0-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,400 kB
  • sloc: python: 8,877; makefile: 37; sh: 30
file content (56 lines) | stat: -rw-r--r-- 1,075 bytes parent folder | download | duplicates (2)
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
import pytest

from inline_snapshot.testing._example import Example

values = ["1", '"2\'"', "[5]", "{1: 2}", "F(i=5)", "F.make1('2')", "f(7)"]


@pytest.mark.parametrize("a", values)
@pytest.mark.parametrize("b", values + ["F.make2(Is(5))"])
def test_change_types(a, b):
    context = """\
from inline_snapshot import snapshot, Is
from dataclasses import dataclass

@dataclass
class F:
    i: int

    @staticmethod
    def make1(s):
        return F(i=int(s))

    @staticmethod
    def make2(s):
        return F(i=s)

def f(v):
    return v

"""

    def code_repr(v):
        g = {}
        exec(context + f"r=repr({a})", g)
        return g["r"]

    def code(a, b):
        return f"""\
{context}

def test_change():
    for _ in [1,2]:
        assert {a} == snapshot({b})
"""

    print(a, b)
    print(code_repr(a), code_repr(b))

    Example(code(a, b)).run_inline(
        ["--inline-snapshot=fix,update"],
        changed_files=(
            {"tests/test_something.py": code(a, code_repr(a))}
            if code_repr(a) != b
            else {}
        ),
    )