edlang/lib/edlang_driver/tests/programs.rs

101 lines
3 KiB
Rust
Raw Normal View History

2024-02-14 07:53:47 +00:00
use crate::common::{compile_program, run_program};
use test_case::test_case;
mod common;
#[test_case(include_str!("programs/simple.ed"), "simple", false, 0, &["1"] ; "simple.ed 1")]
#[test_case(include_str!("programs/simple.ed"), "simple", false, 1, &["a", "b"] ; "simple.ed 3")]
#[test_case(include_str!("programs/basic_ifs.ed"), "basic_ifs", false, 9, &[] ; "basic_ifs")]
2024-02-14 09:23:39 +00:00
#[test_case(include_str!("programs/while.ed"), "while", false, 10, &[] ; "r#while")]
2024-02-16 10:09:49 +00:00
#[test_case(include_str!("programs/factorial.ed"), "factorial", false, 24, &[] ; "factorial")]
2024-02-17 16:29:18 +00:00
#[test_case(include_str!("programs/refs.ed"), "refs", false, 2, &[] ; "refs")]
2024-02-18 09:40:19 +00:00
#[test_case(include_str!("programs/struct.ed"), "struct", false, 5, &[] ; "r#struct")]
2024-03-02 09:22:29 +00:00
#[test_case(include_str!("programs/casts.ed"), "casts", false, 2, &[] ; "casts")]
2024-03-13 11:24:40 +00:00
#[test_case(TEST_ADD, "test_add", false, 2, &[] ; "test_add")]
#[test_case(TEST_SUB, "test_sub", false, 1, &[] ; "test_sub")]
#[test_case(TEST_MUL, "test_mul", false, 4, &[] ; "TEST_MUL")]
#[test_case(TEST_DIV, "test_div", false, 2, &[] ; "TEST_DIV")]
#[test_case(TEST_REM, "test_rem", false, 0, &[] ; "TEST_REM")]
#[test_case(TEST_IF_BOTH, "test_if_both", false, 1, &[] ; "test_if_both")]
#[test_case(TEST_IF_BOTH, "test_if_both", false, 2, &["a"] ; "test_if_both_args")]
#[test_case(TEST_IF_NO_ELSE, "test_if_no_else", false, 1, &[] ; "test_if_no_else")]
#[test_case(TEST_IF_NO_ELSE, "test_if_no_else", false, 2, &["a"] ; "test_if_no_else_args")]
2024-04-13 08:38:25 +00:00
#[test_case(include_str!("programs/while_if_false.ed"), "while_if_false", false, 7, &[] ; "while_if_false")]
#[test_case(include_str!("programs/if_if_false.ed"), "if_if_false", false, 7, &[] ; "if_if_false")]
2024-02-14 07:53:47 +00:00
fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32, args: &[&str]) {
let program = compile_program(source, name, is_library).unwrap();
2024-03-13 11:24:40 +00:00
dbg!(&program);
2024-02-14 07:53:47 +00:00
assert!(program.binary_file.exists(), "program not compiled");
2024-02-16 10:48:15 +00:00
let mut result = run_program(&program.binary_file, args).unwrap();
let status = result.wait().unwrap();
2024-02-14 07:53:47 +00:00
assert_eq!(
2024-02-16 10:48:15 +00:00
status.code().unwrap(),
2024-02-14 07:53:47 +00:00
status_code,
"Program {} returned a unexpected status code",
name
);
}
2024-02-16 10:09:49 +00:00
const TEST_ADD: &str = r#"
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
pub fn main() -> i32 {
let b: i32 = 1 + 1;
return b;
}
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
"#;
const TEST_SUB: &str = r#"
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
pub fn main() -> i32 {
let b: i32 = 2 - 1;
return b;
}
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
"#;
const TEST_MUL: &str = r#"
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
pub fn main() -> i32 {
let b: i32 = 2 * 2;
return b;
}
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
"#;
const TEST_DIV: &str = r#"
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
pub fn main() -> i32 {
let b: i32 = 4 / 2;
return b;
}
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
"#;
const TEST_REM: &str = r#"
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
pub fn main() -> i32 {
let b: i32 = 4 % 2;
return b;
}
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
"#;
const TEST_IF_BOTH: &str = r#"
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
pub fn main(argc: i32) -> i32 {
if argc == 1 {
return 1;
} else {
return 2;
}
}
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
"#;
const TEST_IF_NO_ELSE: &str = r#"
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
pub fn main(argc: i32) -> i32 {
if argc == 1 {
return 1;
}
return 2;
}
2024-03-13 11:24:40 +00:00
2024-02-16 10:09:49 +00:00
"#;