Skip to content

Commit

Permalink
add: Adds templatest.Registered.getindex
Browse files Browse the repository at this point in the history
  • Loading branch information
jshwi committed Apr 22, 2022
1 parent 27b1d41 commit 6f8b94d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

[Unreleased](https://github.com/jshwi/templatest/compare/v0.1.0...HEAD)
------------------------------------------------------------------------
### Added
- Adds `templatest.Registered.getindex`

[0.1.0](https://github.com/jshwi/templatest/releases/tag/v0.1.0) - 2022-04-18
------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ Organise tests by prefixing subclasses for common tests
>>>
>>> templatest.templates.registered.getgroup('err').getids()
('err-example-template',)
>>> templatest.templates.registered.getindex('example-template')
0
>>> templatest.templates.registered.getindex('err-example-template')
1
Example usage with a parametrized test
**************************************
Expand Down
9 changes: 9 additions & 0 deletions templatest/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ def getids(self) -> _t.Tuple[str, ...]:
:return: A tuple of names of the classes within this sequence.
"""
return tuple(i.name for i in self)

def getindex(self, name: str) -> _t.Optional[int]:
"""Get the index of a template by name if it exists.
:param name: Name assigned to template.
:return: Index of the template if it exists, else None.
"""
result = next((i for i in self if i.name == name), None)
return result if result is None else self.index(result)
15 changes: 15 additions & 0 deletions tests/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,18 @@ def test_mutable_mapping_setitem() -> None:
assert str(seq) == f"<MutableSequence ['{templatest.utils.ALPHA[0]}']>"
seq[0] = templatest.utils.ALPHA[1]
assert str(seq) == f"<MutableSequence ['{templatest.utils.ALPHA[1]}']>"


def test_get_index(register_templates: RegisterTemplatesType) -> None:
"""Test :meth:`Templates.getindex` method.
:param register_templates: Register any number of test subclasses of
:class:`templatest.BaseTemplate`
"""
register_templates(
(TEST_CLASS_NAME[0],), (TEST_CLASS_NAME[1],), (TEST_CLASS_NAME[2],)
)
assert templatest.templates.registered.getindex(TEST_INST_NAME[0]) == 0
assert templatest.templates.registered.getindex(TEST_INST_NAME[1]) == 1
assert templatest.templates.registered.getindex(TEST_INST_NAME[2]) == 2
assert templatest.templates.registered.getindex(TEST_INST_NAME[3]) is None

0 comments on commit 6f8b94d

Please sign in to comment.