templatest¶
Templates for testing with strings.
- class templatest.BaseTemplate[source]¶
Bases:
ABCAbstract 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
Templatenamed 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
Registeredobject containingTemplateobjects 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
Registeredobject containingTemplateobjects with common prefix to their names.
- namedtuple templatest.Template(name: str, template: str, expected: str)[source]¶
Bases:
NamedTupleContains registered
BaseTemplateproperties.- 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:
ExceptionRaise if non-unique
templatest.BaseTemplatename added.- Parameters:¶
- base_template: BaseTemplate¶
templatest.BaseTemplatesubclass which could not be registered.- name: str¶
Name of
templatest.BaseTemplatesubclass that is already registered.
templatest.templates¶
Registration and usage of template subclasses.
- templatest.templates.register(base_template: type[BaseTemplate]) type[BaseTemplate][source]¶
Register
templatest.BaseTemplatesubclasses.Decorate subclass definition to register.
Registered subclasses of
templatest.BaseTemplatecan be accessed throughregistered.- Parameters:¶
- base_template: type[BaseTemplate]¶
templatest.BaseTemplateobject.
- Returns:¶
templatest.BaseTemplateobject.
-
templatest.templates.registered =
<Registered []>¶ Instantiated
templatest.Registeredobject for accessingtemplatest.TemplatepropertiesPopulated through registering
templatest.BaseTemplatesubclasses 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:
MutableStrSequenceGet 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:
objectGet 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:
MutableStrSequenceGet 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:
VarSeqGet 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.