edlang/lib/edlang_driver/tests/programs.rs

98 lines
2.8 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-02-16 10:09:49 +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-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();
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#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 1 + 1;
return b;
}
}
"#;
const TEST_SUB: &str = r#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 2 - 1;
return b;
}
}
"#;
const TEST_MUL: &str = r#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 2 * 2;
return b;
}
}
"#;
const TEST_DIV: &str = r#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 4 / 2;
return b;
}
}
"#;
const TEST_REM: &str = r#"
mod Main {
pub fn main() -> i32 {
let b: i32 = 4 % 2;
return b;
}
}
"#;
const TEST_IF_BOTH: &str = r#"
mod Main {
pub fn main(argc: i32) -> i32 {
if argc == 1 {
return 1;
} else {
return 2;
}
}
}
"#;
const TEST_IF_NO_ELSE: &str = r#"
mod Main {
pub fn main(argc: i32) -> i32 {
if argc == 1 {
return 1;
}
return 2;
}
}
"#;