Skip to content

Commit

Permalink
fix: Handle dotted parameters in classname (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
taion authored Apr 22, 2020
1 parent d630caa commit d961f7c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/syrupy/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Optional,
)

from pytest import Class


class TestLocation:
def __init__(self, node: Any):
Expand All @@ -17,8 +19,12 @@ def __init__(self, node: Any):

@property
def classname(self) -> Optional[str]:
_, __, qualname = self._node.location
return ".".join(list(self.__valid_ids(qualname))[:-1]) or None
return (
".".join(
node.name for node in self._node.listchain() if isinstance(node, Class)
)
or None
)

@property
def filename(self) -> str:
Expand Down
3 changes: 3 additions & 0 deletions stubs/pytest.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ from typing import Any, Callable, TypeVar
ReturnType = TypeVar("ReturnType")

def fixture(func: Callable[..., ReturnType]) -> Callable[..., ReturnType]: ...

class Class:
name: str
9 changes: 9 additions & 0 deletions tests/__snapshots__/test_extension_amber.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@
],
}
---
# name: test_doubly_parametrized[bar-foo]
'foo'
---
# name: test_doubly_parametrized[bar-foo].1
'bar'
---
# name: test_empty_snapshot
None
---
Expand Down Expand Up @@ -216,6 +222,9 @@
# name: test_numbers.2
0.3333333333333333
---
# name: test_parameter_with_dot[value.with.dot]
'value.with.dot'
---
# name: test_reflection
SnapshotAssertion(name='snapshot', num_executions=0)
---
Expand Down
12 changes: 12 additions & 0 deletions tests/test_extension_amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,15 @@ def test_nested_class_method(self, snapshot, actual):

class TestSubClass(TestClass):
pass


@pytest.mark.parametrize("parameter_with_dot", ("value.with.dot",))
def test_parameter_with_dot(parameter_with_dot, snapshot):
assert parameter_with_dot == snapshot


@pytest.mark.parametrize("parameter_1", ("foo",))
@pytest.mark.parametrize("parameter_2", ("bar",))
def test_doubly_parametrized(parameter_1, parameter_2, snapshot):
assert parameter_1 == snapshot
assert parameter_2 == snapshot

0 comments on commit d961f7c

Please sign in to comment.