Skip to content

Commit 69d05a4

Browse files
authored
Output Table in writer::Basic (#135)
Additionally: - expand Scenario Outline Examples in Step Table
1 parent 9e4f24a commit 69d05a4

File tree

6 files changed

+197
-67
lines changed

6 files changed

+197
-67
lines changed

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
1919
- Provide reference implementations for those abstractions;
2020
- Enable `macros` feature by default.
2121
- Replaced `#[given(step)]`, `#[when(step)]` and `#[then(step)]` function argument attributes with a single `#[step]`. ([#128])
22-
- Made test callbacks arguments consistent with proc macros: ([#128])
23-
- `&mut World` instead of `World` as a first parameter;
24-
- `Step` instead of `StepContext` as a second one.
22+
- Made test callbacks first argument `&mut World` instead of `World`. ([#128])
23+
- Made `#[step]` argument of step functions `Step` instead of `StepContext` again, while test callbacks still receive `StepContext` as a second parameter. ([#128])
2524
- CLI and [hooks](https://cucumber.io/docs/cucumber/api/#hooks) were removed, but are planned to be re-implemented with some changes in `0.11` release. ([#128])
2625

2726
### Added
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
Feature: Example feature
22

3-
Scenario: An example scenario
3+
Scenario Outline: An example scenario
44
Given foo is 0
5+
| sample | val1 | val2 |
6+
| longer value | <val1> | <val2> |
57
When foo is 0
68

9+
Examples:
10+
| val1 | val2 |
11+
| 1 | 4 |
12+
| 2 | 5 |
13+
| 3 | 6 |
14+
715
Scenario: An example sync scenario
816
Given foo is sync 0

src/feature.rs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! [`gherkin::Feature`] extension.
1212
1313
use std::{
14-
mem,
14+
iter, mem,
1515
path::{Path, PathBuf},
1616
};
1717

@@ -32,11 +32,14 @@ pub trait Ext: Sized {
3232
/// Given there are <start> cucumbers
3333
/// When I eat <eat> cucumbers
3434
/// Then I should have <left> cucumbers
35+
/// And substitution in tables works too
36+
/// | cucumbers left |
37+
/// | <left> |
3538
///
3639
/// Examples:
3740
/// | start | eat | left |
3841
/// | 12 | 5 | 7 |
39-
/// | 20 | 5 | 15 |
42+
/// | 20 | 4 | 16 |
4043
/// ```
4144
///
4245
/// Will be expanded as:
@@ -46,15 +49,21 @@ pub trait Ext: Sized {
4649
/// Given there are 12 cucumbers
4750
/// When I eat 5 cucumbers
4851
/// Then I should have 7 cucumbers
52+
/// And substitution in tables works too
53+
/// | cucumbers left |
54+
/// | 7 |
4955
/// Scenario Outline: eating
5056
/// Given there are 20 cucumbers
51-
/// When I eat 5 cucumbers
52-
/// Then I should have 15 cucumbers
57+
/// When I eat 4 cucumbers
58+
/// Then I should have 16 cucumbers
59+
/// And substitution in tables works too
60+
/// | cucumbers left |
61+
/// | 7 |
5362
///
5463
/// Examples:
5564
/// | start | eat | left |
5665
/// | 12 | 5 | 7 |
57-
/// | 20 | 5 | 15 |
66+
/// | 20 | 4 | 16 |
5867
/// ```
5968
///
6069
/// # Errors
@@ -141,22 +150,33 @@ fn expand_scenario(
141150
let mut err = None;
142151

143152
for s in &mut modified.steps {
144-
s.value = TEMPLATE_REGEX
145-
.replace_all(&s.value, |c: &regex::Captures<'_>| {
146-
let name = c.get(1).unwrap().as_str();
147-
148-
row.clone()
149-
.find_map(|(k, v)| (name == k).then(|| v.as_str()))
150-
.unwrap_or_else(|| {
151-
err = Some(ExpandExamplesError {
152-
pos: s.position,
153-
name: name.to_owned(),
154-
path: path.cloned(),
155-
});
156-
""
157-
})
158-
})
159-
.into_owned();
153+
let pos = s.position;
154+
let to_replace = iter::once(&mut s.value).chain(
155+
s.table.iter_mut().flat_map(|t| {
156+
t.rows.iter_mut().flat_map(|r| r.iter_mut())
157+
}),
158+
);
159+
160+
for value in to_replace {
161+
*value = TEMPLATE_REGEX
162+
.replace_all(value, |c: &regex::Captures<'_>| {
163+
let name = c.get(1).unwrap().as_str();
164+
165+
row.clone()
166+
.find_map(|(k, v)| {
167+
(name == k).then(|| v.as_str())
168+
})
169+
.unwrap_or_else(|| {
170+
err = Some(ExpandExamplesError {
171+
pos,
172+
name: name.to_owned(),
173+
path: path.cloned(),
174+
});
175+
""
176+
})
177+
})
178+
.into_owned();
179+
}
160180

161181
if let Some(e) = err {
162182
return Err(e);

0 commit comments

Comments
 (0)