summaryrefslogtreecommitdiff
path: root/src/scenarios.rs
blob: 17549d23cb73dcfcdc030cb3686bf9bb145d45f2 (plain)
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
use crate::{html::Location, 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,
    origin: Location,
    steps: Vec<ScenarioStep>,
}

impl Scenario {
    /// Construct a new scenario.
    ///
    /// The new scenario will have a title, but no steps.
    pub fn new(title: &str, origin: Location) -> Scenario {
        Scenario {
            title: title.to_string(),
            origin,
            steps: vec![],
        }
    }

    /// Return the title of a scenario.
    pub fn title(&self) -> &str {
        &self.title
    }

    /// 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());
    }

    pub(crate) fn origin(&self) -> &Location {
        &self.origin
    }
}

#[cfg(test)]
mod test {
    use super::Scenario;
    use crate::html::Location;
    use crate::ScenarioStep;
    use crate::StepKind;

    #[test]
    fn has_title() {
        let scen = Scenario::new("title", Location::Unknown);
        assert_eq!(scen.title(), "title");
    }

    #[test]
    fn has_no_steps_initially() {
        let scen = Scenario::new("title", Location::Unknown);
        assert_eq!(scen.steps().len(), 0);
    }

    #[test]
    fn adds_step() {
        let mut scen = Scenario::new("title", Location::Unknown);
        let step = ScenarioStep::new(StepKind::Given, "and", "foo", Location::Unknown);
        scen.add(&step);
        assert_eq!(scen.steps(), &[step]);
    }
}