use decy_parser::parser::{CParser, Statement};
#[test]
fn test_parse_printf_statement() {
let c_code = r#"
int printf(const char* format, ...);
int main() {
printf("Hello, World!\n");
return 0;
}
"#;
let parser = CParser::new().expect("Failed to create parser");
let ast = parser.parse(c_code).expect("Failed to parse");
let main_func = ast.functions().iter().find(|f| f.name == "main");
assert!(main_func.is_some(), "main function should be parsed");
let main_func = main_func.unwrap();
assert_eq!(
main_func.body.len(),
2,
"main should have 2 statements (printf + return), got {}. Statements: {:?}",
main_func.body.len(),
main_func.body
);
match &main_func.body[0] {
Statement::FunctionCall { function, arguments } => {
assert_eq!(function, "printf");
assert_eq!(arguments.len(), 1, "printf should have 1 argument");
}
other => panic!("Expected Statement::FunctionCall for printf, got {:?}", other),
}
assert!(matches!(main_func.body[1], Statement::Return(_)), "Second statement should be Return");
}
#[test]
fn test_parse_free_statement() {
let c_code = r#"
void free(void* ptr);
void cleanup(void* ptr) {
free(ptr);
}
"#;
let parser = CParser::new().expect("Failed to create parser");
let ast = parser.parse(c_code).expect("Failed to parse");
let cleanup_func = ast.functions().iter().find(|f| f.name == "cleanup");
assert!(cleanup_func.is_some(), "cleanup function should be parsed");
let cleanup_func = cleanup_func.unwrap();
assert_eq!(
cleanup_func.body.len(),
1,
"cleanup should have 1 statement (free), got {}",
cleanup_func.body.len()
);
match &cleanup_func.body[0] {
Statement::FunctionCall { function, arguments } => {
assert_eq!(function, "free");
assert_eq!(arguments.len(), 1, "free should have 1 argument");
}
other => panic!("Expected Statement::FunctionCall for free, got {:?}", other),
}
}
#[test]
fn test_parse_multiple_function_call_statements() {
let c_code = r#"
int printf(const char* format, ...);
void free(void* ptr);
void example(void* p1, void* p2) {
printf("Freeing memory\n");
free(p1);
free(p2);
printf("Done\n");
}
"#;
let parser = CParser::new().expect("Failed to create parser");
let ast = parser.parse(c_code).expect("Failed to parse");
let example_func = ast.functions().iter().find(|f| f.name == "example");
assert!(example_func.is_some(), "example function should be parsed");
let example_func = example_func.unwrap();
assert_eq!(
example_func.body.len(),
4,
"example should have 4 function call statements, got {}",
example_func.body.len()
);
for (i, stmt) in example_func.body.iter().enumerate() {
assert!(
matches!(stmt, Statement::FunctionCall { .. }),
"Statement {} should be FunctionCall, got {:?}",
i,
stmt
);
}
}