2021-03-09 17:25:47 +00:00
|
|
|
use super::*;
|
|
|
|
|
2021-04-08 19:09:44 +00:00
|
|
|
macro_rules! create_program_with_contents {
|
|
|
|
($contents:expr, $prog_name:ident) => {
|
|
|
|
let mut $prog_name = Program::from_string($contents.to_string());
|
|
|
|
};
|
2021-03-09 17:25:47 +00:00
|
|
|
}
|
2021-04-08 19:09:44 +00:00
|
|
|
|
2021-03-09 17:25:47 +00:00
|
|
|
#[test]
|
|
|
|
fn test_math() {
|
|
|
|
assert_eq!(eval::do_math("2-2".to_string(), '+'), 4);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_undefined_opcode() {
|
2021-04-08 19:09:44 +00:00
|
|
|
create_program_with_contents!("Hello\nWorld!", prog);
|
|
|
|
prog.run()
|
2021-03-09 17:25:47 +00:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_undefined_variable() {
|
2021-04-08 19:09:44 +00:00
|
|
|
create_program_with_contents!("p$v", prog);
|
|
|
|
prog.run()
|
2021-03-09 17:25:47 +00:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_undefined_function() {
|
2021-04-08 19:09:44 +00:00
|
|
|
create_program_with_contents!("p*x", prog);
|
|
|
|
prog.run()
|
2021-03-09 17:25:47 +00:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_factory() {
|
2021-04-08 19:09:44 +00:00
|
|
|
create_program_with_contents!("lhHello\nlwWorld\np$h $w", prog);
|
|
|
|
prog.run();
|
2021-03-09 17:25:47 +00:00
|
|
|
let vec_to_check: Vec<String> = 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() {
|
2021-04-08 19:09:44 +00:00
|
|
|
create_program_with_contents!("lhHello\nlwWorld\np$h $w", prog);
|
2021-03-09 17:25:47 +00:00
|
|
|
prog.run();
|
|
|
|
let args_to_check: HashMap<char, String> = [('h', String::from("Hello")), ('w', String::from("World"))].iter().cloned().collect();
|
|
|
|
assert_eq!(prog.vars, args_to_check);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_funcs() {
|
2021-04-08 19:09:44 +00:00
|
|
|
create_program_with_contents!("fxa10-10\nfys10-5\np*x *y", prog);
|
2021-03-09 17:25:47 +00:00
|
|
|
prog.run();
|
|
|
|
let funcs_to_check: HashMap<char, String> = [('x', String::from("a10-10")), ('y', String::from("s10-5"))].iter().cloned().collect();
|
|
|
|
assert_eq!(prog.funcs, funcs_to_check);
|
|
|
|
}
|