Skip to content

Commit

Permalink
Expand docstrings and names in Scenario Outline (#178, #172)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilslv authored Dec 7, 2021
1 parent e2a41ab commit 56ebadc
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 55 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ All user visible changes to `cucumber` crate will be documented in this file. Th

- Template regex in `Scenario Outline` expansion from `<(\S+)>` to `<([^>\s]+)>`. ([#163])
- Multiple `Examples` in `Scenario Outline`. ([#165], [#164])
- Docstring and name expansion in `Scenario Outline`. ([#178], [#172])

[#147]: /../../pull/147
[#151]: /../../pull/151
Expand All @@ -51,6 +52,8 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
[#165]: /../../pull/165
[#166]: /../../pull/166
[#168]: /../../pull/168
[#172]: /../../pull/172
[#178]: /../../pull/178
[cef3d480]: /../../commit/cef3d480579190425461ddb04a1248675248351e
[rev]: /../../commit/rev-full
[0110-1]: https://llg.cubic.org/docs/junit
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ impl<'p> Parameters<'p> {
// In case we encounter default parameter, we should
// assert that corresponding argument's type __doesn't__
// implement a `Parameter` trait.
// TODO: Try to use autoderef-based specialization with
// readable assertion message.
#[automatically_derived]
const _: fn() = || {
// Generic trait with a blanket impl over `()` for
Expand Down Expand Up @@ -589,8 +591,6 @@ impl<'p> Parameters<'p> {
// In case we encounter a custom parameter, we should
// assert that the corresponding type implements
// `Parameter` and has correct `Parameter::NAME`.
// TODO: Panic here, once `const_panic` is stabilized.
// https://github.com/rust-lang/rust/pull/89508
#[automatically_derived]
const _: () = ::std::assert!(
::cucumber::codegen::str_eq(
Expand Down
72 changes: 36 additions & 36 deletions src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,51 +143,51 @@ fn expand_scenario(
.zip(iter::repeat((example.position, example.tags.iter())))
})
.map(|((id, row), (position, tags))| {
let mut modified = scenario.clone();
let replace_templates = |str: &str, pos| {
let mut err = None;
let replaced = TEMPLATE_REGEX
.replace_all(str, |cap: &regex::Captures<'_>| {
let name = cap.get(1).unwrap().as_str();

row.clone()
.find_map(|(k, v)| (name == k).then(|| v.as_str()))
.unwrap_or_else(|| {
err = Some(ExpandExamplesError {
pos,
name: name.to_owned(),
path: path.cloned(),
});
""
})
})
.into_owned();

err.map_or_else(|| Ok(replaced), Err)
};

let mut expanded = scenario.clone();

// This is done to differentiate `Hash`es of
// scenario outlines with the same examples.
modified.position = position;
modified.position.line += id + 1;

modified.tags.extend(tags.cloned());
expanded.position = position;
expanded.position.line += id + 1;

for s in &mut modified.steps {
let mut err = None;
expanded.tags.extend(tags.cloned());

let to_replace = iter::once(&mut s.value).chain(
s.table.iter_mut().flat_map(|t| {
expanded.name =
replace_templates(&expanded.name, expanded.position)?;
for s in &mut expanded.steps {
for value in iter::once(&mut s.value)
.chain(s.docstring.iter_mut())
.chain(s.table.iter_mut().flat_map(|t| {
t.rows.iter_mut().flat_map(|r| r.iter_mut())
}),
);

for value in to_replace {
*value = TEMPLATE_REGEX
.replace_all(value, |c: &regex::Captures<'_>| {
let name = c.get(1).unwrap().as_str();

row.clone()
.find_map(|(k, v)| {
(name == k).then(|| v.as_str())
})
.unwrap_or_else(|| {
err = Some(ExpandExamplesError {
pos: s.position,
name: name.to_owned(),
path: path.cloned(),
});
""
})
})
.into_owned();
}

if let Some(e) = err {
return Err(e);
}))
{
*value = replace_templates(value, s.position)?;
}
}

Ok(modified)
Ok(expanded)
})
.collect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Feature: Outline
Background:
Given foo is 1

Scenario Outline: foo
Scenario Outline: foo <bar1>
Given foo is <bar1>
When foo is <bar2>
Then foo is <bar3>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Outline

Scenario Outline: foo
Given foo is <bar1>
When foo is <bar2>
"""
Nothing to replace here
"""
Then foo is <bar3>

Examples:
| bar1 | bar2 | bar3 |
| 0 | 1 | 2 |
Loading

0 comments on commit 56ebadc

Please sign in to comment.