|
23 | 23 | STEP_PARAM_RE = re.compile(r"<(.+?)>")
|
24 | 24 |
|
25 | 25 |
|
| 26 | +def render_string(input_string: str, render_context: Mapping[str, Any]) -> str: |
| 27 | + """ |
| 28 | + Render the string with the given context, |
| 29 | + but avoid replacing text inside angle brackets if context is missing. |
| 30 | +
|
| 31 | + Args: |
| 32 | + input_string (str): The string for which to render/replace params. |
| 33 | + render_context (Mapping[str, Any]): The context for rendering the string. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + str: The rendered string with parameters replaced only if they exist in the context. |
| 37 | + """ |
| 38 | + |
| 39 | + def replacer(m: re.Match) -> str: |
| 40 | + varname = m.group(1) |
| 41 | + # If the context contains the variable, replace it. Otherwise, leave it unchanged. |
| 42 | + return str(render_context.get(varname, f"<{varname}>")) |
| 43 | + |
| 44 | + return STEP_PARAM_RE.sub(replacer, input_string) |
| 45 | + |
| 46 | + |
26 | 47 | def get_tag_names(tag_data: list[GherkinTag]) -> set[str]:
|
27 | 48 | """Extract tag names from tag data.
|
28 | 49 |
|
@@ -191,13 +212,13 @@ def render(self, context: Mapping[str, Any]) -> Scenario:
|
191 | 212 | """
|
192 | 213 | scenario_steps = [
|
193 | 214 | Step(
|
194 |
| - name=step.render(context), |
| 215 | + name=step.render_step_name(context), |
195 | 216 | type=step.type,
|
196 | 217 | indent=step.indent,
|
197 | 218 | line_number=step.line_number,
|
198 | 219 | keyword=step.keyword,
|
199 |
| - datatable=step.datatable, |
200 |
| - docstring=step.docstring, |
| 220 | + datatable=step.render_datatable(context), |
| 221 | + docstring=step.render_docstring(context), |
201 | 222 | )
|
202 | 223 | for step in self._steps
|
203 | 224 | ]
|
@@ -308,22 +329,50 @@ def params(self) -> tuple[str, ...]:
|
308 | 329 | """
|
309 | 330 | return tuple(frozenset(STEP_PARAM_RE.findall(self.name)))
|
310 | 331 |
|
311 |
| - def render(self, context: Mapping[str, Any]) -> str: |
312 |
| - """Render the step name with the given context, but avoid replacing text inside angle brackets if context is missing. |
| 332 | + def render_step_name(self, context: Mapping[str, Any]) -> str: |
| 333 | + """ |
| 334 | + Render the step name with the given context, |
| 335 | + but avoid replacing text inside angle brackets if context is missing. |
313 | 336 |
|
314 | 337 | Args:
|
315 | 338 | context (Mapping[str, Any]): The context for rendering the step name.
|
316 | 339 |
|
317 | 340 | Returns:
|
318 | 341 | str: The rendered step name with parameters replaced only if they exist in the context.
|
319 | 342 | """
|
| 343 | + return render_string(self.name, context) |
| 344 | + |
| 345 | + def render_datatable(self, context: Mapping[str, Any]) -> datatable | None: |
| 346 | + """ |
| 347 | + Render the datatable with the given context, |
| 348 | + but avoid replacing text inside angle brackets if context is missing. |
| 349 | +
|
| 350 | + Args: |
| 351 | + context (Mapping[str, Any]): The context for rendering the datatable. |
| 352 | +
|
| 353 | + Returns: |
| 354 | + datatable: The rendered datatable with parameters replaced only if they exist in the context. |
| 355 | + """ |
| 356 | + if self.datatable: |
| 357 | + rendered_datatable = self.datatable |
| 358 | + for row in rendered_datatable.rows: |
| 359 | + for cell in row.cells: |
| 360 | + cell.value = render_string(cell.value, context) |
| 361 | + return rendered_datatable |
| 362 | + return None |
| 363 | + |
| 364 | + def render_docstring(self, context: Mapping[str, Any]) -> str | None: |
| 365 | + """ |
| 366 | + Render the docstring with the given context, |
| 367 | + but avoid replacing text inside angle brackets if context is missing. |
320 | 368 |
|
321 |
| - def replacer(m: re.Match) -> str: |
322 |
| - varname = m.group(1) |
323 |
| - # If the context contains the variable, replace it. Otherwise, leave it unchanged. |
324 |
| - return str(context.get(varname, f"<{varname}>")) |
| 369 | + Args: |
| 370 | + context (Mapping[str, Any]): The context for rendering the docstring. |
325 | 371 |
|
326 |
| - return STEP_PARAM_RE.sub(replacer, self.name) |
| 372 | + Returns: |
| 373 | + str: The rendered docstring with parameters replaced only if they exist in the context. |
| 374 | + """ |
| 375 | + return render_string(self.docstring, context) if self.docstring else None |
327 | 376 |
|
328 | 377 |
|
329 | 378 | @dataclass(eq=False)
|
|
0 commit comments