diff options
-rw-r--r-- | src/command.rs | 162 |
1 files changed, 133 insertions, 29 deletions
diff --git a/src/command.rs b/src/command.rs index ae5be66..d474e35 100644 --- a/src/command.rs +++ b/src/command.rs | |||
@@ -104,7 +104,7 @@ fn call_on_app(s: &mut Cursive, input: &str) { | |||
104 | } | 104 | } |
105 | } | 105 | } |
106 | 106 | ||
107 | #[derive(PartialEq)] | 107 | #[derive(PartialEq, Debug)] |
108 | pub enum Command { | 108 | pub enum Command { |
109 | Add(String, Option<u32>, bool), | 109 | Add(String, Option<u32>, bool), |
110 | MonthPrev, | 110 | MonthPrev, |
@@ -219,50 +219,154 @@ mod tests { | |||
219 | use super::*; | 219 | use super::*; |
220 | 220 | ||
221 | #[test] | 221 | #[test] |
222 | fn parse_add_command_with_goal() { | 222 | fn parse_add_command() { |
223 | let command: Vec<String> = "eat healthy 3" | 223 | let result = Command::from_string("add eat 2"); |
224 | .trim() | ||
225 | .split(' ') | ||
226 | .into_iter() | ||
227 | .map(|s| s.to_string()) | ||
228 | .collect(); | ||
229 | 224 | ||
230 | let verb = "add".to_owned(); | 225 | assert!(result.is_ok()); |
231 | let auto = false; | 226 | match result.unwrap() { |
227 | Command::Add(name, goal, auto) => { | ||
228 | assert_eq!(name, "eat"); | ||
229 | assert_eq!(goal.unwrap(), 2); | ||
230 | assert_eq!(auto, false); | ||
231 | } | ||
232 | _ => panic!(), | ||
233 | } | ||
234 | } | ||
232 | 235 | ||
233 | let result = parse_add(verb, command, auto); | 236 | #[test] |
237 | fn parse_add_command_without_goal() { | ||
238 | let result = Command::from_string("add eat"); | ||
234 | 239 | ||
240 | assert!(result.is_ok()); | ||
235 | match result.unwrap() { | 241 | match result.unwrap() { |
236 | Command::Add(name, goal, a) => { | 242 | Command::Add(name, goal, auto) => { |
237 | assert_eq!(name, "eat healthy".to_owned()); | 243 | assert_eq!(name, "eat"); |
238 | assert_eq!(goal.unwrap(), 3); | 244 | assert!(goal.is_none()); |
239 | assert_eq!(a, auto); | 245 | assert_eq!(auto, false); |
246 | } | ||
247 | _ => panic!(), | ||
248 | } | ||
249 | } | ||
250 | |||
251 | // #[test] | ||
252 | fn parse_add_command_with_long_name() { | ||
253 | let result = Command::from_string("add \"eat healthy\" 5"); | ||
254 | |||
255 | assert!(result.is_ok()); | ||
256 | match result.unwrap() { | ||
257 | Command::Add(name, goal, auto) => { | ||
258 | assert_eq!(name, "eat healthy"); | ||
259 | assert_eq!(goal.unwrap(), 5); | ||
260 | assert_eq!(auto, false); | ||
240 | } | 261 | } |
241 | _ => panic!(), | 262 | _ => panic!(), |
242 | } | 263 | } |
243 | } | 264 | } |
244 | 265 | ||
245 | #[test] | 266 | #[test] |
246 | fn parse_add_command_without_goal() { | 267 | fn parse_add_auto_command() { |
247 | let command: Vec<String> = "eat healthy" | 268 | let result = Command::from_string("add-auto eat 2"); |
248 | .trim() | 269 | |
249 | .split(' ') | 270 | assert!(result.is_ok()); |
250 | .into_iter() | 271 | match result.unwrap() { |
251 | .map(|s| s.to_string()) | 272 | Command::Add(name, goal, auto) => { |
252 | .collect(); | 273 | assert_eq!(name, "eat"); |
274 | assert_eq!(goal.unwrap(), 2); | ||
275 | assert_eq!(auto, true); | ||
276 | } | ||
277 | _ => panic!(), | ||
278 | } | ||
279 | } | ||
253 | 280 | ||
254 | let verb = "add".to_owned(); | 281 | #[test] |
255 | let auto = false; | 282 | fn parse_delete_command() { |
283 | let result = Command::from_string("delete eat"); | ||
256 | 284 | ||
257 | let result = parse_add(verb, command, auto); | 285 | assert!(result.is_ok()); |
286 | match result.unwrap() { | ||
287 | Command::Delete(name) => { | ||
288 | assert_eq!(name, "eat"); | ||
289 | } | ||
290 | _ => panic!(), | ||
291 | } | ||
292 | } | ||
293 | |||
294 | #[test] | ||
295 | fn parse_track_up_command() { | ||
296 | let result = Command::from_string("track-up eat"); | ||
258 | 297 | ||
298 | assert!(result.is_ok()); | ||
259 | match result.unwrap() { | 299 | match result.unwrap() { |
260 | Command::Add(name, goal, a) => { | 300 | Command::TrackUp(name) => { |
261 | assert_eq!(name, "eat healthy".to_owned()); | 301 | assert_eq!(name, "eat"); |
262 | assert!(goal.is_none()); | ||
263 | assert_eq!(a, auto); | ||
264 | } | 302 | } |
265 | _ => panic!(), | 303 | _ => panic!(), |
266 | } | 304 | } |
267 | } | 305 | } |
306 | |||
307 | #[test] | ||
308 | fn parse_track_down_command() { | ||
309 | let result = Command::from_string("track-down eat"); | ||
310 | |||
311 | assert!(result.is_ok()); | ||
312 | match result.unwrap() { | ||
313 | Command::TrackDown(name) => { | ||
314 | assert_eq!(name, "eat"); | ||
315 | } | ||
316 | _ => panic!(), | ||
317 | } | ||
318 | } | ||
319 | |||
320 | #[test] | ||
321 | fn parse_help_command() { | ||
322 | let result = Command::from_string("help add"); | ||
323 | |||
324 | assert!(result.is_ok()); | ||
325 | match result.unwrap() { | ||
326 | Command::Help(name) => { | ||
327 | assert_eq!(name.unwrap(), "add"); | ||
328 | } | ||
329 | _ => panic!(), | ||
330 | } | ||
331 | } | ||
332 | |||
333 | #[test] | ||
334 | fn parse_month_prev_command() { | ||
335 | let result = Command::from_string("mprev"); | ||
336 | |||
337 | assert!(result.is_ok()); | ||
338 | assert_eq!(result.unwrap(), Command::MonthPrev); | ||
339 | } | ||
340 | |||
341 | #[test] | ||
342 | fn parse_month_next_command() { | ||
343 | let result = Command::from_string("mnext"); | ||
344 | |||
345 | assert!(result.is_ok()); | ||
346 | assert_eq!(result.unwrap(), Command::MonthNext); | ||
347 | } | ||
348 | |||
349 | #[test] | ||
350 | fn parse_quit_command() { | ||
351 | let result = Command::from_string("q"); | ||
352 | |||
353 | assert!(result.is_ok()); | ||
354 | assert_eq!(result.unwrap(), Command::Quit); | ||
355 | } | ||
356 | |||
357 | #[test] | ||
358 | fn parse_write_command() { | ||
359 | let result = Command::from_string("w"); | ||
360 | |||
361 | assert!(result.is_ok()); | ||
362 | assert_eq!(result.unwrap(), Command::Write); | ||
363 | } | ||
364 | |||
365 | #[test] | ||
366 | fn parse_no_command() { | ||
367 | let result = Command::from_string(""); | ||
368 | |||
369 | assert!(result.is_ok()); | ||
370 | assert_eq!(result.unwrap(), Command::Blank); | ||
371 | } | ||
268 | } | 372 | } |