1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
use crate::ScenarioStep;
use serde::{Deserialize, Serialize};
/// An acceptance test scenario.
///
/// A scenario consists of a title, by which it can be identified, and
/// a sequence of steps. The Scenario struct assumes the steps are
/// valid and make sense; the struct does not try to validate the
/// sequence.
#[derive(Debug, Serialize, Deserialize)]
pub struct Scenario {
title: String,
name: Option<String>,
steps: Vec<ScenarioStep>,
}
impl Scenario {
/// Construct a new scenario.
///
/// The new scenario will have a title, but no steps.
pub fn new(title: &str) -> Scenario {
Scenario {
title: title.to_string(),
name: None,
steps: vec![],
}
}
/// Return the title of a scenario.
pub fn title(&self) -> &str {
&self.title
}
/// Set name of scenario.
pub fn set_name(&mut self, name: &str) {
self.name = Some(name.to_string());
}
/// Return name of scenario.
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
/// Does the scenario have steps?
pub fn has_steps(&self) -> bool {
!self.steps.is_empty()
}
/// Return slice with all the steps.
pub fn steps(&self) -> &[ScenarioStep] {
&self.steps
}
/// Add a step to a scenario.
pub fn add(&mut self, step: &ScenarioStep) {
self.steps.push(step.clone());
}
}
#[cfg(test)]
mod test {
use super::Scenario;
use crate::ScenarioStep;
use crate::StepKind;
#[test]
fn has_title() {
let scen = Scenario::new("title");
assert_eq!(scen.title(), "title");
}
#[test]
fn has_no_name_initially() {
let scen = Scenario::new("title");
assert_eq!(scen.name(), None);
}
#[test]
fn sets_name() {
let mut scen = Scenario::new("title");
scen.set_name("Alfred");
assert_eq!(scen.name(), Some("Alfred"));
}
#[test]
fn has_no_steps_initially() {
let scen = Scenario::new("title");
assert_eq!(scen.steps().len(), 0);
}
#[test]
fn adds_step() {
let mut scen = Scenario::new("title");
let step = ScenarioStep::new(StepKind::Given, "and", "foo");
scen.add(&step);
assert_eq!(scen.steps(), &[step]);
}
}
|