from inline_snapshot import snapshot
from inline_snapshot.extra import raises
from inline_snapshot.testing import Example


def test_example():

    e = Example(
        {
            "test_a.py": """
from inline_snapshot import snapshot

def test_a():
    assert 1==snapshot(2)
    """,
            "test_b.py": "1+1",
        },
    )

    e.run_pytest(
        ["--inline-snapshot=create,fix"],
        returncode=1,
        changed_files={"test_a.py": """\

from inline_snapshot import snapshot

def test_a():
    assert 1==snapshot(1)
    \
"""},
        outcomes={"passed": 1, "errors": 1},
    )

    e.run_inline(
        ["--inline-snapshot=fix"],
        reported_categories=snapshot(None),
        changed_files={"test_a.py": """\

from inline_snapshot import snapshot

def test_a():
    assert 1==snapshot(1)
    \
"""},
    ).run_inline(
        ["--inline-snapshot=fix"], changed_files=snapshot({}), reported_categories=set()
    )


def test_no_tests():

    with raises(snapshot("UsageError: no test_*() functions in the example")):
        Example("").run_inline()


def test_throws_exception():

    Example("""\
def test_a():
    raise Exception("test")

        """).run_inline(raises="Exception: test")


def test_throws_exception_in_conftest():

    Example(
        {
            "conftest.py": """
assert False , "some error"
                 """,
            "test_a.py": """\
def test_a():
    pass
        """,
        }
    ).run_inline(raises=snapshot("AssertionError: some error"))
