diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-01-24 19:48:19 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-01-26 19:08:00 +0200 |
commit | f285f652c39db1b8e51eee9a499fadec7b0bec91 (patch) | |
tree | 554a73ac2f112c8a34f58b3f8bae8dcfb1000345 | |
parent | 43f72bf660ade91c08e09d62923c2d4635fb8e16 (diff) | |
download | subplot-f285f652c39db1b8e51eee9a499fadec7b0bec91.tar.gz |
feat: automatically use "and" on output
This changes typesetting of scenarios so that when two adjacent
scenario steps have the same keyword, "and" is used on the second one.
This means that when input has
when I do foo
when I do bar
the output will have
when I do foo
and I do bar
I didn't bother to make this configurable. I don't feel that level of
configuration is good.
The "keyword aliases" scenario is repurposed to verify this.
Sponsored-by: author
-rw-r--r-- | src/typeset.rs | 26 | ||||
-rw-r--r-- | subplot.md | 26 |
2 files changed, 33 insertions, 19 deletions
diff --git a/src/typeset.rs b/src/typeset.rs index 2bd1fd2..c6301e4 100644 --- a/src/typeset.rs +++ b/src/typeset.rs @@ -70,13 +70,13 @@ pub fn scenario_snippet(bindings: &Bindings, snippet: &str) -> Block { fn step( bindings: &Bindings, text: &str, - defkind: Option<StepKind>, + prevkind: Option<StepKind>, ) -> (Vec<Inline>, Option<StepKind>) { - let step = ScenarioStep::new_from_str(text, defkind); + let step = ScenarioStep::new_from_str(text, prevkind); if step.is_err() { return ( error_msg(&format!("Could not parse step: {}", text)), - defkind, + prevkind, ); } let step = step.unwrap(); @@ -87,12 +87,12 @@ fn step( eprintln!("Could not select binding: {:?}", e); return ( error_msg(&format!("Could not select binding for: {}", text)), - defkind, + prevkind, ); } }; - let mut inlines = vec![keyword(&step), space()]; + let mut inlines = vec![keyword(&step, prevkind), space()]; for part in m.parts() { match part { @@ -106,9 +106,19 @@ fn step( // Typeset first word, which is assumed to be a keyword, of a scenario // step. -fn keyword(step: &ScenarioStep) -> Inline { - let word = inlinestr(step.keyword()); - Inline::Emph(vec![word]) +fn keyword(step: &ScenarioStep, prevkind: Option<StepKind>) -> Inline { + let actual = inlinestr(&format!("{}", step.kind())); + let and = inlinestr("and"); + let keyword = if let Some(prevkind) = prevkind { + if prevkind == step.kind() { + and + } else { + actual + } + } else { + actual + }; + Inline::Emph(vec![keyword]) } // Typeset a space between words. @@ -1144,20 +1144,24 @@ but foobar was done ``` ~~~ -### Keyword aliases +### Keyword aliases in output + +We support **and** and **but** in input lines, and we always render +scenarios in output so they are used when they are allowed. This +scenario verifies that this happens. ~~~scenario given file aliases.md -and file b.yaml -and file f.py -and an installed subplot +given file b.yaml +given file f.py +given an installed subplot when I run subplot docgen aliases.md -o aliases.html +then command is successful then file aliases.html matches regex /given<[^>]*> precondition foo/ -and file aliases.html matches regex /when<[^>]*> I do bar/ -and file aliases.html matches regex /and<[^>]*> I do foobar/ -and file aliases.html matches regex /then<[^>]*> bar was done/ -and file aliases.html matches regex /but<[^>]*> foobar was done/ -and command is successful +then file aliases.html matches regex /when<[^>]*> I do bar/ +then file aliases.html matches regex /and<[^>]*> I do foobar/ +then file aliases.html matches regex /then<[^>]*> bar was done/ +then file aliases.html matches regex /and<[^>]*> foobar was done/ ~~~ ~~~{#aliases.md .file .markdown .numberLines} @@ -1172,9 +1176,9 @@ functions: [f.py] ```scenario given precondition foo when I do bar -and I do foobar +when I do foobar then bar was done -but foobar was done +then foobar was done ``` ~~~ |