forked from test_framework/pytest-bdd
Merge remote-tracking branch 'origin/master' into updates
This commit is contained in:
commit
84153493b2
|
@ -6,6 +6,7 @@ import textwrap
|
|||
import typing
|
||||
from collections import OrderedDict
|
||||
from dataclasses import dataclass, field
|
||||
from functools import cached_property
|
||||
from typing import cast
|
||||
|
||||
from . import exceptions, types
|
||||
|
@ -318,9 +319,10 @@ class Step:
|
|||
:param str line: Line of text - the continuation of the step name.
|
||||
"""
|
||||
self.lines.append(line)
|
||||
self._invalidate_full_name_cache()
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
@cached_property
|
||||
def full_name(self) -> str:
|
||||
multilines_content = textwrap.dedent("\n".join(self.lines)) if self.lines else ""
|
||||
|
||||
# Remove the multiline quotes, if present.
|
||||
|
@ -334,9 +336,19 @@ class Step:
|
|||
lines = [self._name] + [multilines_content]
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
def _invalidate_full_name_cache(self) -> None:
|
||||
"""Invalidate the full_name cache."""
|
||||
if "full_name" in self.__dict__:
|
||||
del self.full_name
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self.full_name
|
||||
|
||||
@name.setter
|
||||
def name(self, value: str) -> None:
|
||||
self._name = value
|
||||
self._invalidate_full_name_cache()
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""Full step name including the type."""
|
||||
|
|
|
@ -4,7 +4,7 @@ from unittest import mock
|
|||
|
||||
import pytest
|
||||
|
||||
from pytest_bdd import given, parsers, then, when
|
||||
from pytest_bdd import given, parser, parsers, then, when
|
||||
from pytest_bdd.utils import collect_dumped_objects
|
||||
|
||||
|
||||
|
@ -316,3 +316,25 @@ def test_step_catches_all(pytester):
|
|||
|
||||
objects = collect_dumped_objects(result)
|
||||
assert objects == ["foo", ("foo parametrized", 1), "foo", ("foo parametrized", 2), "foo", ("foo parametrized", 3)]
|
||||
|
||||
|
||||
def test_step_name_is_cached():
|
||||
"""Test that the step name is cached and not re-computed eache time."""
|
||||
step = parser.Step(name="step name", type="given", indent=8, line_number=3, keyword="Given")
|
||||
assert step.name == "step name"
|
||||
|
||||
# manipulate the step name directly and validate the cache value is still returned
|
||||
step._name = "incorrect step name"
|
||||
assert step.name == "step name"
|
||||
|
||||
# change the step name using the property and validate the cache has been invalidated
|
||||
step.name = "new step name"
|
||||
assert step.name == "new step name"
|
||||
|
||||
# manipulate the step lines and validate the cache value is still returned
|
||||
step.lines.append("step line 1")
|
||||
assert step.name == "new step name"
|
||||
|
||||
# add a step line and validate the cache has been invalidated
|
||||
step.add_line("step line 2")
|
||||
assert step.name == "new step name\nstep line 1\nstep line 2"
|
||||
|
|
Loading…
Reference in New Issue