Skip to content

Commit

Permalink
fix: use the test node location when determining snapshot class name (#…
Browse files Browse the repository at this point in the history
…197)

* test: add subclass test

* fix: use test node location to parse classname
  • Loading branch information
iamogbz authored Apr 19, 2020
1 parent 2c2a491 commit 1010c94
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/syrupy/location.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path
from typing import (
Any,
Iterator,
Optional,
)

Expand All @@ -16,8 +17,8 @@ def __init__(self, node: Any):

@property
def classname(self) -> Optional[str]:
classes = self._node.obj.__qualname__.split(".")[:-1]
return ".".join(classes) if classes else None
_, __, qualname = self._node.location
return ".".join(list(self.__valid_ids(qualname))[:-1]) or None

@property
def filename(self) -> str:
Expand All @@ -38,9 +39,11 @@ def __valid_id(self, name: str) -> str:
valid_id = new_valid_id
return valid_id

def __valid_ids(self, name: str) -> Iterator[str]:
return filter(None, (self.__valid_id(n) for n in name.split(".")))

def __parse(self, name: str) -> str:
valid_ids = (self.__valid_id(n) for n in name.split("."))
return ".".join(valid_id for valid_id in valid_ids if valid_id)
return ".".join(self.__valid_ids(name))

def matches_snapshot_name(self, snapshot_name: str) -> bool:
return self.__parse(self.snapshot_name) == self.__parse(snapshot_name)
Expand Down
21 changes: 21 additions & 0 deletions tests/__snapshots__/test_extension_amber.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@
# name: TestClass.test_class_method_parametrized[c]
'c'
---
# name: TestSubClass.TestNestedClass.test_nested_class_method[x]
'parameterized nested class method x'
---
# name: TestSubClass.TestNestedClass.test_nested_class_method[y]
'parameterized nested class method y'
---
# name: TestSubClass.TestNestedClass.test_nested_class_method[z]
'parameterized nested class method z'
---
# name: TestSubClass.test_class_method_name
'this is in a test class'
---
# name: TestSubClass.test_class_method_parametrized[a]
'a'
---
# name: TestSubClass.test_class_method_parametrized[b]
'b'
---
# name: TestSubClass.test_class_method_parametrized[c]
'c'
---
# name: test_bool[False]
False
---
Expand Down
4 changes: 4 additions & 0 deletions tests/test_extension_amber.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,7 @@ def test_class_method_parametrized(self, snapshot, actual):
class TestNestedClass:
def test_nested_class_method(self, snapshot, actual):
assert snapshot == f"parameterized nested class method {actual}"


class TestSubClass(TestClass):
pass

0 comments on commit 1010c94

Please sign in to comment.