diff --git a/src/main.rs b/src/main.rs index 38b05df..2695dfa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,13 @@ struct Program { funcs: HashMap } +macro_rules! create_program { + ($filename:expr, $prog_name:ident) => { + let contents = fs::read_to_string($filename).expect("Something went wrong reading the file"); + let mut $prog_name = Program::from_string(contents); + }; +} + impl Program { fn from_string(program: String) -> Program { let mut op_list: Vec = Vec::new(); @@ -148,8 +155,7 @@ impl Program { let filename = argument_vec[0]; // Read contents of the provided file and construct a symbolic Program from it - let contents = fs::read_to_string(filename).expect("Something went wrong reading the file"); - let mut prog = Program::from_string(contents); + create_program!(filename, prog); prog.run(); if argument_vec.len() > 1 { @@ -243,8 +249,7 @@ fn main() { let filename = &args[1]; // Read contents of the provided file and construct a symbolic Program from it - let contents = fs::read_to_string(filename).expect("Something went wrong reading the file"); - let mut prog = Program::from_string(contents); + create_program!(filename, prog); prog.run(); } diff --git a/src/tests.rs b/src/tests.rs index 3fbdbeb..0064aa1 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,8 +1,11 @@ use super::*; -fn make_program(contents: &str) -> Program { - Program::from_string(contents.to_string()) +macro_rules! create_program_with_contents { + ($contents:expr, $prog_name:ident) => { + let mut $prog_name = Program::from_string($contents.to_string()); + }; } + #[test] fn test_math() { assert_eq!(eval::do_math("2-2".to_string(), '+'), 4); @@ -10,27 +13,31 @@ fn test_math() { #[test] #[should_panic] fn test_undefined_opcode() { - make_program("Hello\nWorld!").run(); + create_program_with_contents!("Hello\nWorld!", prog); + prog.run() } #[test] #[should_panic] fn test_undefined_variable() { - make_program("p$v").run(); + create_program_with_contents!("p$v", prog); + prog.run() } #[test] #[should_panic] fn test_undefined_function() { - make_program("p*x").run(); + create_program_with_contents!("p*x", prog); + prog.run() } #[test] fn test_factory() { - let prog = make_program("lhHello\nlwWorld\np$h $w"); + create_program_with_contents!("lhHello\nlwWorld\np$h $w", prog); + prog.run(); let vec_to_check: Vec = vec!["lhHello", "lwWorld", "p$h $w"].into_iter().map(|s| s.to_string()).collect(); assert_eq!(prog.data, vec_to_check); } #[test] fn test_args() { - let mut prog = make_program("lhHello\nlwWorld\np$h $w"); + create_program_with_contents!("lhHello\nlwWorld\np$h $w", prog); prog.run(); let args_to_check: HashMap = [('h', String::from("Hello")), ('w', String::from("World"))].iter().cloned().collect(); assert_eq!(prog.vars, args_to_check); @@ -38,7 +45,7 @@ fn test_args() { #[test] fn test_funcs() { - let mut prog = make_program("fxa10-10\nfys10-5\np*x *y"); + create_program_with_contents!("fxa10-10\nfys10-5\np*x *y", prog); prog.run(); let funcs_to_check: HashMap = [('x', String::from("a10-10")), ('y', String::from("s10-5"))].iter().cloned().collect(); assert_eq!(prog.funcs, funcs_to_check);