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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! Contains requests made by Gherkin interpreter (or Wire Protocol).
//!
//! Consumers will interact with [InvokeArgument](./enum.InvokeArgument.html)
//! if using
//! [Cucumber's](../../state/struct.Cucumber.html) direct invoke capability
//! with tables or
//! docstrings.

#[cfg(feature = "serde_macros")]
include!("request.rs.in");

#[cfg(not(feature = "serde_macros"))]
include!(concat!(env!("OUT_DIR"), "/event/request.rs"));

use std::ascii::AsciiExt;

use serde::Deserializer;
use serde::de::{SeqVisitor, Visitor};
use serde::de::impls::VecVisitor;
use serde::Error as SerdeError;

use event::response::StepArg;

// NOTE: These defined in request.rs.in (as they need to derive Deserialize)
// pub struct StepMatchesRequest
// pub struct InvokeRequest
// pub struct BeginScenarioRequest
// pub struct EndScenarioRequest
// pub struct SnippetTextRequest

/// Types of requests handled by [runners](../../runner/struct.WorldRunner.html)
#[derive(Eq, PartialEq, Clone, Debug)]
pub enum Request {
  StepMatches(StepMatchesRequest),
  Invoke(InvokeRequest),
  BeginScenario(BeginScenarioRequest),
  EndScenario(EndScenarioRequest),
  SnippetText(SnippetTextRequest),
}

impl Deserialize for Request {
  fn deserialize<D: Deserializer>(d: &mut D) -> Result<Self, D::Error> {
    d.deserialize(RequestVisitor)
  }
}


struct RequestVisitor;

impl Visitor for RequestVisitor {
  type Value = Request;

  fn visit_seq<V: SeqVisitor>(&mut self, mut _visitor: V) -> Result<Request, V::Error> {
    let cmd_type = try!(_visitor.visit()).map(|val: String| val.to_ascii_lowercase());

    match cmd_type {
      None => Err(V::Error::invalid_length(0)),
      Some(command) => {
        match command.as_ref() {
          "step_matches" => {
            let payload = try!(_visitor.visit::<StepMatchesRequest>());
            try!(_visitor.end());
            match payload {
              None => Err(V::Error::invalid_length(1)),
              Some(payload) => Ok(Request::StepMatches(payload)),
            }
          },
          "invoke" => {
            let payload = try!(_visitor.visit::<InvokeRequest>());
            try!(_visitor.end());
            match payload {
              None => Err(V::Error::invalid_length(1)),
              Some(payload) => Ok(Request::Invoke(payload)),
            }
          },
          "begin_scenario" => {
            let payload = try!(_visitor.visit::<BeginScenarioRequest>());
            try!(_visitor.end());
            match payload {
              None => Ok(Request::BeginScenario(BeginScenarioRequest { tags: Vec::new() })),
              Some(payload) => Ok(Request::BeginScenario(payload)),
            }
          },
          "end_scenario" => {
            let payload = try!(_visitor.visit::<EndScenarioRequest>());
            try!(_visitor.end());
            match payload {
              None => Ok(Request::EndScenario(EndScenarioRequest { tags: Vec::new() })),
              Some(payload) => Ok(Request::EndScenario(payload)),
            }
          },
          "snippet_text" => {
            let payload = try!(_visitor.visit::<SnippetTextRequest>());
            try!(_visitor.end());
            match payload {
              None => Err(V::Error::invalid_length(1)),
              Some(payload) => Ok(Request::SnippetText(payload)),
            }
          },
          _ => Err(V::Error::custom("Unknown command type as first value")),
        }
      },
    }
  }
}

/// The low level type capturing the possible values a step may provide.
///
/// Normal regex arguments as well as docstrings come in the form of the String
/// variant. Conversion
/// to other types is done at later stages. Tables are represented as
/// `Vec<Vec<String>>`
#[derive(Eq, PartialEq, Clone, Debug)]
pub enum InvokeArgument {
  String(String),
  None,
  Table(Vec<Vec<String>>),
}

impl InvokeArgument {
  pub fn from_str(arg: &str) -> InvokeArgument {
    InvokeArgument::String(arg.to_owned())
  }

  pub fn from_step_arg(arg: StepArg) -> InvokeArgument {
    match arg.val {
      Some(v) => InvokeArgument::String(v),
      None => InvokeArgument::None,
    }
  }
}

impl Deserialize for InvokeArgument {
  fn deserialize<D: Deserializer>(d: &mut D) -> Result<Self, D::Error> {
    d.deserialize(InvokeArgumentVisitor)
  }
}

struct InvokeArgumentVisitor;

impl Visitor for InvokeArgumentVisitor {
  type Value = InvokeArgument;

  fn visit_str<E: SerdeError>(&mut self, v: &str) -> Result<InvokeArgument, E> {
    Ok(InvokeArgument::from_str(v))
  }

  fn visit_unit<E: SerdeError>(&mut self) -> Result<InvokeArgument, E> {
    Ok(InvokeArgument::None)
  }

  fn visit_seq<V: SeqVisitor>(&mut self, _visitor: V) -> Result<InvokeArgument, V::Error> {
    VecVisitor::new().visit_seq(_visitor).map(|res| InvokeArgument::Table(res))
  }
}

#[cfg(test)]
mod test {
  use super::*;
  use serde_json;

  #[test]
  fn read_step_matches() {
    let json = "[\"step_matches\", {\"name_to_match\": \"we're all wired\"}]";
    let res = serde_json::from_str(json);
    match res.unwrap() {
      Request::StepMatches(payload) => {
        assert_eq!(payload,
                   StepMatchesRequest { name_to_match: "we're all wired".to_owned() })
      },
      _ => panic!("result was not StepMatches type"),
    }
  }

  #[test]
  fn read_invoke_no_args() {
    let json = "[\"invoke\", {\"id\":\"1\", \"args\": []}]";
    let res = serde_json::from_str(json);
    match res.unwrap() {
      Request::Invoke(payload) => {
        assert_eq!(payload,
                   InvokeRequest {
                     id: "1".to_owned(),
                     args: Vec::new(),
                   })
      },
      _ => panic!("result was not Invoke type"),
    }
  }

  #[test]
  fn read_invoke_string_arg() {
    let json = "[\"invoke\", {\"id\":\"1\", \"args\": [\"wired\"]}]";
    let res = serde_json::from_str(json);
    println!("{:?}", res);
    match res.unwrap() {
      Request::Invoke(payload) => {
        assert_eq!(payload,
                   InvokeRequest {
                     id: "1".to_owned(),
                     args: vec![InvokeArgument::from_str("wired")],
                   })
      },
      _ => panic!("result was not Invoke type"),
    }
  }

  #[test]
  fn read_invoke_complicated_args() {
    let json = "[\"invoke\", {\"id\":\"1\", \"args\": [\"we're\", \
                [[\"wired\"],[\"high\"],[\"happy\"]]]}]";
    let res = serde_json::from_str(json);
    println!("{:?}", res);
    match res.unwrap() {
      Request::Invoke(payload) => {
        assert_eq!(payload,
                   InvokeRequest {
                     id: "1".to_owned(),
                     args: vec![InvokeArgument::from_str("we're"),
                                InvokeArgument::Table(vec![vec!["wired".to_owned()],
                                                           vec!["high".to_owned()],
                                                           vec!["happy".to_owned()]])],
                   })
      },
      _ => panic!("result was not Invoke type"),
    }
  }

  #[test]
  fn read_begin_scenario_empty() {
    let json = "[\"begin_scenario\"]";
    let res = serde_json::from_str(json);
    match res.unwrap() {
      Request::BeginScenario(payload) => {
        assert_eq!(payload, BeginScenarioRequest { tags: Vec::new() })
      },
      _ => panic!("result was not BeginScenario type"),
    }
  }

  #[test]
  fn read_begin_scenario() {
    let json = "[\"begin_scenario\", { \"tags\": [\"hello\"] }]";
    let res = serde_json::from_str(json);
    match res.unwrap() {
      Request::BeginScenario(payload) => {
        assert_eq!(payload,
                   BeginScenarioRequest { tags: vec!["hello".to_owned()] })
      },
      _ => panic!("result was not BeginScenario type"),
    }
  }

  #[test]
  fn read_end_scenario_empty() {
    let json = "[\"end_scenario\"]";
    let res = serde_json::from_str(json);
    match res.unwrap() {
      Request::EndScenario(payload) => assert_eq!(payload, EndScenarioRequest { tags: Vec::new() }),
      _ => panic!("result was not EndScenario type"),
    }
  }

  #[test]
  fn read_end_scenario() {
    let json = "[\"end_scenario\", { \"tags\": [\"hello\"]}]";
    let res = serde_json::from_str(json);
    match res.unwrap() {
      Request::EndScenario(payload) => {
        assert_eq!(payload,
                   EndScenarioRequest { tags: vec!["hello".to_owned()] })
      },
      _ => panic!("result was not EndScenario type"),
    }
  }

  #[test]
  fn read_snippet_text() {
    let json = "[\"snippet_text\", {\"step_keyword\": \"Given\", \"multiline_arg_class\":\"\", \
                \"step_name\":\"we're all wired\"}]";
    let res = serde_json::from_str(json);
    match res.unwrap() {
      Request::SnippetText(payload) => {
        assert_eq!(payload,
                   SnippetTextRequest {
                     step_keyword: "Given".to_owned(),
                     multiline_arg_class: "".to_owned(),
                     step_name: "we're all wired".to_owned(),
                   })
      },
      _ => panic!("result was not SnippetText type"),
    }
  }
}