diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-05-09 09:34:36 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-05-09 11:07:37 +0300 |
commit | a21ba1f5fcc9b03f7657c5b00c4c215286b06341 (patch) | |
tree | a64b77067ca53d8cf32482b8ab1b64ad7687f711 /src/steps.rs | |
parent | aefd93fb14b4781deca481d2a21953c9fd30f361 (diff) | |
download | subplot-a21ba1f5fcc9b03f7657c5b00c4c215286b06341.tar.gz |
Change: keep actual text of keyword from scenario
The typesetting should preserve the actual keyword or alias in the
source. Previously, if source had this:
```
given foo
and bar
```
it got typeset as if were:
```
given foo
given bar
```
Also, change subplot.md to use alias when possible.
Diffstat (limited to 'src/steps.rs')
-rw-r--r-- | src/steps.rs | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/src/steps.rs b/src/steps.rs index 2c6f34f..1cbdfe5 100644 --- a/src/steps.rs +++ b/src/steps.rs @@ -14,14 +14,16 @@ use std::fmt; #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct ScenarioStep { kind: StepKind, + keyword: String, text: String, } impl ScenarioStep { /// Construct a new step. - pub fn new(kind: StepKind, text: &str) -> ScenarioStep { + pub fn new(kind: StepKind, keyword: &str, text: &str) -> ScenarioStep { ScenarioStep { kind, + keyword: keyword.to_owned(), text: text.to_owned(), } } @@ -31,6 +33,11 @@ impl ScenarioStep { self.kind } + /// Return the actual textual keyword of a step. + pub fn keyword(&self) -> &str { + &self.keyword + } + /// Return the text of a step. pub fn text(&self) -> &str { &self.text @@ -43,13 +50,18 @@ impl ScenarioStep { pub fn new_from_str(text: &str, default: Option<StepKind>) -> Result<ScenarioStep> { let mut words = text.split_whitespace(); - let kind = match words.next() { - Some("given") => StepKind::Given, - Some("when") => StepKind::When, - Some("then") => StepKind::Then, - Some("and") => default.ok_or(SubplotError::ContinuationTooEarly)?, - Some("but") => default.ok_or(SubplotError::ContinuationTooEarly)?, - _ => return Err(SubplotError::UnknownStepKind), + let keyword = match words.next() { + Some(s) => s, + _ => return Err(SubplotError::NoStepKeyword(text.to_string())), + }; + + let kind = match keyword { + "given" => StepKind::Given, + "when" => StepKind::When, + "then" => StepKind::Then, + "and" => default.ok_or(SubplotError::ContinuationTooEarly)?, + "but" => default.ok_or(SubplotError::ContinuationTooEarly)?, + _ => return Err(SubplotError::UnknownStepKind(keyword.to_string())), }; let mut joined = String::new(); @@ -60,13 +72,13 @@ impl ScenarioStep { if joined.len() > 1 { joined.pop(); } - Ok(ScenarioStep::new(kind, &joined)) + Ok(ScenarioStep::new(kind, keyword, &joined)) } } impl fmt::Display for ScenarioStep { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{} {}", self.kind(), self.text()) + write!(f, "{} {}", self.keyword(), self.text()) } } |