use decy_hir::{HirExpression, HirStatement};
#[test]
fn test_hir_expression_statement_function_call() {
let printf_call = HirExpression::FunctionCall {
function: "printf".to_string(),
arguments: vec![HirExpression::StringLiteral("Hello".to_string())],
};
let stmt = HirStatement::Expression(printf_call);
match stmt {
HirStatement::Expression(expr) => {
assert!(matches!(expr, HirExpression::FunctionCall { .. }));
}
_ => panic!("Expected Expression statement"),
}
}
#[test]
fn test_hir_parser_function_call_to_expression_statement() {
use decy_parser::parser::{Expression, Statement};
let ast_stmt = Statement::FunctionCall {
function: "free".to_string(),
arguments: vec![Expression::Variable("ptr".to_string())],
};
let hir_stmt = HirStatement::from_ast_statement(&ast_stmt);
match hir_stmt {
HirStatement::Expression(HirExpression::FunctionCall { function, arguments }) => {
assert_eq!(function, "free");
assert_eq!(arguments.len(), 1);
}
_ => panic!("Expected Expression(FunctionCall), got {:?}", hir_stmt),
}
}
#[test]
fn test_hir_expression_statement_with_return_value_discarded() {
let strlen_call = HirExpression::FunctionCall {
function: "strlen".to_string(),
arguments: vec![HirExpression::Variable("s".to_string())],
};
let stmt = HirStatement::Expression(strlen_call);
assert!(matches!(stmt, HirStatement::Expression(_)));
}
#[test]
fn test_hir_post_increment_expression_preserved() {
use decy_parser::parser::Expression;
let ast_expr =
Expression::PostIncrement { operand: Box::new(Expression::Variable("x".to_string())) };
let hir_expr = HirExpression::from_ast_expression(&ast_expr);
match &hir_expr {
HirExpression::PostIncrement { operand } => {
assert!(matches!(operand.as_ref(), HirExpression::Variable(name) if name == "x"));
}
HirExpression::Variable(name) => {
panic!(
"Bug DECY-139: PostIncrement was incorrectly simplified to just Variable({})",
name
);
}
other => {
panic!("Expected PostIncrement, got {:?}", other);
}
}
}
#[test]
fn test_hir_pre_increment_expression_preserved() {
use decy_parser::parser::Expression;
let ast_expr =
Expression::PreIncrement { operand: Box::new(Expression::Variable("x".to_string())) };
let hir_expr = HirExpression::from_ast_expression(&ast_expr);
match &hir_expr {
HirExpression::PreIncrement { operand } => {
assert!(matches!(operand.as_ref(), HirExpression::Variable(name) if name == "x"));
}
HirExpression::Variable(name) => {
panic!(
"Bug DECY-139: PreIncrement was incorrectly simplified to just Variable({})",
name
);
}
other => {
panic!("Expected PreIncrement, got {:?}", other);
}
}
}
#[test]
fn test_hir_post_decrement_expression_preserved() {
use decy_parser::parser::Expression;
let ast_expr =
Expression::PostDecrement { operand: Box::new(Expression::Variable("x".to_string())) };
let hir_expr = HirExpression::from_ast_expression(&ast_expr);
match &hir_expr {
HirExpression::PostDecrement { operand } => {
assert!(matches!(operand.as_ref(), HirExpression::Variable(name) if name == "x"));
}
HirExpression::Variable(name) => {
panic!(
"Bug DECY-139: PostDecrement was incorrectly simplified to just Variable({})",
name
);
}
other => {
panic!("Expected PostDecrement, got {:?}", other);
}
}
}
#[test]
fn test_hir_pre_decrement_expression_preserved() {
use decy_parser::parser::Expression;
let ast_expr =
Expression::PreDecrement { operand: Box::new(Expression::Variable("x".to_string())) };
let hir_expr = HirExpression::from_ast_expression(&ast_expr);
match &hir_expr {
HirExpression::PreDecrement { operand } => {
assert!(matches!(operand.as_ref(), HirExpression::Variable(name) if name == "x"));
}
HirExpression::Variable(name) => {
panic!(
"Bug DECY-139: PreDecrement was incorrectly simplified to just Variable({})",
name
);
}
other => {
panic!("Expected PreDecrement, got {:?}", other);
}
}
}