|
1 |
| -use std::env; |
2 | 1 | use std::fmt;
|
| 2 | +use crate::user_action::Action::*; |
3 | 3 |
|
4 | 4 | pub enum Action{
|
5 | 5 | RunScript(String),
|
6 | 6 | Evaluate(String),
|
7 | 7 | Nothing,
|
8 | 8 | }
|
9 | 9 |
|
10 |
| -impl fmt::Display for Action { |
| 10 | +impl PartialEq for Action { |
| 11 | + fn eq(&self, other: &Action) -> bool{ |
| 12 | + match (self, other) { |
| 13 | + (RunScript(script1), RunScript(script2)) => script1 == script2, |
| 14 | + (Evaluate(expression1), Evaluate(expression2)) => expression1 == expression2, |
| 15 | + (Nothing, Nothing) => true, |
| 16 | + _ => false, |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl fmt::Debug for Action { |
11 | 22 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
12 | 23 | match &*self {
|
13 |
| - Action::RunScript(filepath) => write!(f, "RunScript"), |
14 |
| - Action::Evaluate(expression) => write!(f, "Evaluate"), |
| 24 | + Action::RunScript(filepath) => write!(f, "RunScript: {}", filepath), |
| 25 | + Action::Evaluate(expression) => write!(f, "Evaluate: {}", expression), |
15 | 26 | Action::Nothing => write!(f, "Nothing"),
|
16 | 27 | }
|
17 | 28 | }
|
18 | 29 | }
|
19 | 30 |
|
20 |
| -pub fn parse_args() -> Action{ |
| 31 | +pub fn parse_args(arguments: Vec<String>) -> Action{ |
21 | 32 |
|
22 |
| - let arguments: Vec<String> = env::args().collect(); |
23 |
| - |
24 |
| - if arguments.len() == 3 { |
| 33 | + if arguments.len() >= 2 { |
25 | 34 | if arguments[1] == "-i" || arguments[1] == "--init" {
|
26 | 35 | return Action::RunScript(arguments[2].clone());
|
27 | 36 | }else if arguments[1] == "-e" || arguments[1] == "--eval" {
|
28 | 37 | return Action::Evaluate(arguments[2].clone());
|
29 |
| - }else { return Action::Nothing; } |
| 38 | + }else { Action::RunScript(arguments[1].clone()) } // for path as argument |
30 | 39 | }else {
|
31 | 40 | return Action::Nothing;
|
32 | 41 | }
|
33 | 42 | }
|
34 | 43 |
|
| 44 | +#[cfg(test)] |
| 45 | +mod tests { |
| 46 | + use crate::user_action; |
| 47 | + |
| 48 | + #[test] |
| 49 | + fn parse_args_test() { |
| 50 | + let path = "target/debug/rust_clojure".to_string(); |
| 51 | + |
| 52 | + assert_eq!(user_action::Action::RunScript("examples/hello_world.clj".to_string()), |
| 53 | + user_action::parse_args(vec![path.clone(), "examples/hello_world.clj".to_string()])); |
| 54 | + |
| 55 | + assert_eq!(user_action::Action::RunScript("test.clj".to_string()), |
| 56 | + user_action::parse_args(vec![path.clone(), "-i".to_string(), "test.clj".to_string()])); |
| 57 | + |
| 58 | + assert_eq!(user_action::Action::RunScript("testing.clj".to_string()), |
| 59 | + user_action::parse_args(vec![path.clone(), "--init".to_string(), "testing.clj".to_string()])); |
| 60 | + |
| 61 | + assert_eq!(user_action::Action::Evaluate("(+ 1 2 3)".to_string()), |
| 62 | + user_action::parse_args(vec![path.clone(), "-e".to_string(), "(+ 1 2 3)".to_string()])); |
| 63 | + |
| 64 | + assert_eq!(user_action::Action::Evaluate("(println \"eh\")".to_string()), |
| 65 | + user_action::parse_args(vec![path.clone(), "--eval".to_string(), "(println \"eh\")".to_string()])); |
| 66 | + |
| 67 | + assert_eq!(user_action::Action::Nothing, user_action::parse_args(vec![path.clone()])); |
| 68 | + } |
| 69 | +} |
0 commit comments