11use std:: fmt;
2- use crate :: user_action:: Action :: * ;
32
3+ #[ derive( PartialEq , Eq ) ]
44pub enum Action {
55 RunScript ( String ) ,
66 Evaluate ( String ) ,
77 Nothing ,
88}
99
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-
2110impl fmt:: Debug for Action {
2211 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
2312 match & * self {
@@ -32,38 +21,64 @@ pub fn parse_args(arguments: Vec<String>) -> Action{
3221
3322 if arguments. len ( ) >= 2 {
3423 if arguments[ 1 ] == "-i" || arguments[ 1 ] == "--init" {
35- return Action :: RunScript ( arguments[ 2 ] . clone ( ) ) ;
24+ return Action :: RunScript ( String :: from ( & arguments[ 2 ] ) ) ;
3625 } else if arguments[ 1 ] == "-e" || arguments[ 1 ] == "--eval" {
37- return Action :: Evaluate ( arguments[ 2 ] . clone ( ) ) ;
38- } else { Action :: RunScript ( arguments[ 1 ] . clone ( ) ) } // for path as argument
26+ return Action :: Evaluate ( String :: from ( & arguments[ 2 ] ) ) ;
27+ } else { Action :: RunScript ( String :: from ( & arguments[ 1 ] ) ) } // for path as argument
3928 } else {
4029 return Action :: Nothing ;
4130 }
4231}
4332
4433#[ cfg( test) ]
4534mod tests {
46- use crate :: user_action;
35+ mod parse_args_test {
36+ use crate :: user_action;
4737
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( ) ] ) ) ;
38+ #[ test]
39+ fn parses_args_given_path ( ) {
40+ let arguments = vec ! [ "target/debug/rust_clojure" . to_string( ) , "examples/hello_world.clj" . to_string( ) ] ;
41+
42+ assert_eq ! ( user_action:: Action :: RunScript ( "examples/hello_world.clj" . to_string( ) ) ,
43+ user_action:: parse_args( arguments) ) ;
44+ }
5445
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( ) ] ) ) ;
46+ #[ test]
47+ fn parses_args_given_i ( ) {
48+ let arguments = vec ! [ "target/debug/rust_clojure" . to_string( ) , "-i" . to_string( ) , "test.clj" . to_string( ) ] ;
49+
50+ assert_eq ! ( user_action:: Action :: RunScript ( "test.clj" . to_string( ) ) ,
51+ user_action:: parse_args( arguments) ) ;
52+ }
5753
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( ) ] ) ) ;
54+ #[ test]
55+ fn parses_args_given_init ( ) {
56+ let arguments = vec ! [ "target/debug/rust_clojure" . to_string( ) , "--init" . to_string( ) , "testing.clj" . to_string( ) ] ;
57+
58+ assert_eq ! ( user_action:: Action :: RunScript ( "testing.clj" . to_string( ) ) ,
59+ user_action:: parse_args( arguments) ) ;
60+ }
6061
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( ) ] ) ) ;
62+ #[ test]
63+ fn parses_args_given_e ( ) {
64+ let arguments = vec ! [ "target/debug/rust_clojure" . to_string( ) , "-e" . to_string( ) , "(+ 1 2 3)" . to_string( ) ] ;
65+
66+ assert_eq ! ( user_action:: Action :: Evaluate ( "(+ 1 2 3)" . to_string( ) ) ,
67+ user_action:: parse_args( arguments) ) ;
68+ }
6369
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( ) ] ) ) ;
70+ #[ test]
71+ fn parses_args_given_eval ( ) {
72+ let arguments = vec ! [ "target/debug/rust_clojure" . to_string( ) , "--eval" . to_string( ) , "(println \" eh\" )" . to_string( ) ] ;
73+
74+ assert_eq ! ( user_action:: Action :: Evaluate ( "(println \" eh\" )" . to_string( ) ) ,
75+ user_action:: parse_args( arguments) ) ;
76+ }
6677
67- assert_eq ! ( user_action:: Action :: Nothing , user_action:: parse_args( vec![ path. clone( ) ] ) ) ;
78+ #[ test]
79+ fn parses_args_given_nil ( ) {
80+ assert_eq ! ( user_action:: Action :: Nothing , user_action:: parse_args( vec![ "target/debug/rust_clojure" . to_string( ) ] ) ) ;
81+ }
6882 }
6983}
84+
0 commit comments