use super::*; 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); } #[test] #[should_panic] fn test_undefined_opcode() { create_program_with_contents!("Hello\nWorld!", prog); prog.run() } #[test] #[should_panic] fn test_undefined_variable() { create_program_with_contents!("p$v", prog); prog.run() } #[test] #[should_panic] fn test_undefined_function() { create_program_with_contents!("p*x", prog); prog.run() } #[test] fn test_factory() { 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() { 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); } #[test] fn test_funcs() { 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); }