templatest¶
Templates for testing with strings.
- class templatest.BaseTemplate[source]¶
Bases:
ABC
Abstract base class for string template and expected tests.
Override the
template()
abstract property method with a string that the test will be working with.Override the
expected()
abstract property method with the expected result that the test will produce.- abstract property expected : str¶
Expected result.
- property name : str¶
The name of the inherited class, parsed for test ID.
- abstract property template : str¶
Template to test.
- class templatest.Registered(*args: Template)[source]¶
Bases:
MutableSequence
[Template
]Mutable sequence of
Template
named tuples.- filtergroup(*prefix: str) Registered [source]¶
Get new object excluding templates sharing a prefix.
- Parameters:¶
- *prefix: str¶
Common prefix(s) to registered subclasses.
- Returns:¶
New
Registered
object containingTemplate
objects that do not have the provided prefix in their names.
- getgroup(*prefix: str) Registered [source]¶
Get new object containing templates sharing a prefix.
- Parameters:¶
- *prefix: str¶
Common prefix(s) to registered subclasses.
- Returns:¶
New
Registered
object containingTemplate
objects with common prefix to their names.
- namedtuple templatest.Template(name: str, template: str, expected: str)[source]¶
Bases:
NamedTuple
Contains registered
BaseTemplate
properties.- Fields:¶
name (
str
) – The name of the inherited class, parsed for test ID.template (
str
) – Template to test.expected (
str
) – Expected result.
templatest.exceptions¶
- exception templatest.exceptions.NameConflictError(base_template: BaseTemplate, name: str)[source]¶
Bases:
Exception
Raise if non-unique
templatest.BaseTemplate
name added.- Parameters:¶
- base_template: BaseTemplate¶
templatest.BaseTemplate
subclass which could not be registered.- name: str¶
Name of
templatest.BaseTemplate
subclass that is already registered.
templatest.templates¶
Registration and usage of template subclasses.
- templatest.templates.register(base_template: type[BaseTemplate]) type[BaseTemplate] [source]¶
Register
templatest.BaseTemplate
subclasses.Decorate subclass definition to register.
Registered subclasses of
templatest.BaseTemplate
can be accessed throughregistered
.- Parameters:¶
- base_template: type[BaseTemplate]¶
templatest.BaseTemplate
object.
- Returns:¶
templatest.BaseTemplate
object.
-
templatest.templates.registered =
<Registered []>
¶ Instantiated
templatest.Registered
object for accessingtemplatest.Template
propertiesPopulated through registering
templatest.BaseTemplate
subclasses withregister()
.
templatest.utils¶
Additional tools for working with string variables.
-
class templatest.utils.PosArgs(args: Any | None =
None
)[source]¶ Bases:
List
[str
]Get chain of strings as a list to represent positional arguments.
- Example:¶
>>> from pathlib import Path >>> from templatest.utils import PosArgs >>> args = PosArgs('arg') Traceback (most recent call last): ... NotImplementedError: constructor cannot take args directly >>> other_args = PosArgs() >>> args = PosArgs(other_args) Traceback (most recent call last): ... NotImplementedError: constructor cannot take args directly >>> args = PosArgs() >>> args.path ['path'] >>> args("") [] >>> args("path") ['path'] >>> args.path.to ['path', 'to'] >>> args.path.to.another ['path', 'to', 'another'] >>> args.path.to.another.path ['path', 'to', 'another', 'path'] >>> args.path.to.another ['path', 'to', 'another'] >>> args.path.to ['path', 'to'] >>> args.path ['path'] >>> args.path.another ['path', 'another'] >>> args.path.another.new ['path', 'another', 'new'] >>> args.path.another.new.path ['path', 'another', 'new', 'path'] >>> args.src ['src'] >>> args.src.to ['src', 'to'] >>> args.src.to.another ['src', 'to', 'another'] >>> args.src.to.another.path ['src', 'to', 'another', 'path'] >>> args.src.to.another.path("") ['src', 'to', 'another', 'path'] >>> args.src.to.another.path("opt") ['src', 'to', 'another', 'path', 'opt'] >>> args.src.to.another.path("opt").another ['src', 'to', 'another', 'path', 'opt', 'another'] >>> path = Path("/tmp") >>> args(path) ['/tmp']
- Parameters:¶
- args: Any | None =
None
¶ Args passed to child classes, not to be used to directly.
- args: Any | None =
- class templatest.utils.RandStrLenSeq(length: int)[source]¶
Bases:
MutableStrSequence
Get random string of varying length.
The instantiated object will always return the same string as per the index. If index does not exist, strings will be generated up to the selected index.
-
class templatest.utils.VarPrefix(prefix: str, slug: str =
'_'
)[source]¶ Bases:
object
Get string with prefix.
- Parameters:¶
- Example:¶
>>> from templatest.utils import VarPrefix >>> flag = VarPrefix("--") >>> slugify = VarPrefix("--", slug="-") >>> flag.src '--src' >>> flag.dst '--dst' >>> flag.exclude '--exclude' >>> flag <VarPrefix ['--src', '--dst', '--exclude']> >>> flag.force_remove '--force_remove' >>> slugify.force_remove '--force-remove'
-
class templatest.utils.VarSeq(name: str, suffix: object | None =
None
)[source]¶ Bases:
MutableStrSequence
Get string as instantiated name with the index as the suffix.
The instantiated object will always return the same string as per the index. If index does not exist, strings will be generated up to the selected index.
- Parameters:¶
- Example:¶
>>> from templatest.utils import VarSeq >>> CONST = VarSeq("CONST") >>> CONST[0] 'CONST_0' >>> CONST <VarSeq ['CONST_0']> >>> CONST[1] 'CONST_1' >>> CONST <VarSeq ['CONST_0', 'CONST_1']> >>> CONST[4] 'CONST_4' >>> CONST <VarSeq ['CONST_0', 'CONST_1', 'CONST_2', 'CONST_3', 'CONST_4']> >>> name = VarSeq("package-name", suffix="-") >>> name[0] 'package-name-0'
-
class templatest.utils.VarSeqSuffix(name: str, suffix: str, separator: object | None =
None
)[source]¶ Bases:
VarSeq
Get string as instantiated name with the index before suffix.
The instantiated object will always return the same string as per the index. If index does not exist, strings will be generated up to the selected index.