Skip to content

Commit 77d6cb1

Browse files
zackteoTko1
authored andcommitted
Added tests and case where only filepath is specified
63b2f6a
1 parent a2c552e commit 77d6cb1

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod value;
2626
mod user_action;
2727

2828
fn main() {
29-
let cli_args: user_action::Action = user_action::parse_args();
29+
let cli_args: user_action::Action = user_action::parse_args( std::env::args().collect() );
3030

3131
// instantiate the core environment
3232
let repl = repl::Repl::default();

src/user_action.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,69 @@
1-
use std::env;
21
use std::fmt;
2+
use crate::user_action::Action::*;
33

44
pub enum Action{
55
RunScript(String),
66
Evaluate(String),
77
Nothing,
88
}
99

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 {
1122
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1223
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),
1526
Action::Nothing => write!(f, "Nothing"),
1627
}
1728
}
1829
}
1930

20-
pub fn parse_args() -> Action{
31+
pub fn parse_args(arguments: Vec<String>) -> Action{
2132

22-
let arguments: Vec<String> = env::args().collect();
23-
24-
if arguments.len() == 3 {
33+
if arguments.len() >= 2 {
2534
if arguments[1] == "-i" || arguments[1] == "--init" {
2635
return Action::RunScript(arguments[2].clone());
2736
}else if arguments[1] == "-e" || arguments[1] == "--eval" {
2837
return Action::Evaluate(arguments[2].clone());
29-
}else { return Action::Nothing; }
38+
}else { Action::RunScript(arguments[1].clone()) } // for path as argument
3039
}else {
3140
return Action::Nothing;
3241
}
3342
}
3443

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

Comments
 (0)