use decy_parser::parser::{Expression, Statement};
use decy_parser::CParser;
#[test]
fn test_assignment_with_simple_value() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int x;
x = 42;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
let assignment = func
.body
.iter()
.find(|stmt| matches!(stmt, Statement::Assignment { .. }))
.expect("Should have assignment statement");
if let Statement::Assignment { target, value } = assignment {
assert_eq!(target, "x", "Assignment target should be 'x'");
assert!(
matches!(value, Expression::IntLiteral(42)),
"Assignment value should be integer literal 42"
);
}
}
#[test]
fn test_assignment_with_variable_value() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test(int y) {
int x;
x = y;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
let assignment = func
.body
.iter()
.find(|stmt| matches!(stmt, Statement::Assignment { .. }))
.expect("Should have assignment");
if let Statement::Assignment { target, value } = assignment {
assert_eq!(target, "x");
assert!(matches!(value, Expression::Variable(_)), "Value should be variable reference");
}
}
#[test]
fn test_assignment_with_expression() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test(int a, int b) {
int x;
x = a + b;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
let assignment = func
.body
.iter()
.find(|stmt| matches!(stmt, Statement::Assignment { .. }))
.expect("Should have assignment");
if let Statement::Assignment { target, value } = assignment {
assert_eq!(target, "x");
assert!(matches!(value, Expression::BinaryOp { .. }), "Value should be binary operation");
}
}
#[test]
fn test_multiple_assignments_in_sequence() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int x;
int y;
int z;
x = 1;
y = 2;
z = 3;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
let assignments: Vec<_> =
func.body.iter().filter(|stmt| matches!(stmt, Statement::Assignment { .. })).collect();
assert_eq!(assignments.len(), 3, "Should have exactly 3 assignment statements");
let targets: Vec<String> = assignments
.iter()
.filter_map(|stmt| {
if let Statement::Assignment { target, .. } = stmt {
Some(target.clone())
} else {
None
}
})
.collect();
assert_eq!(targets, vec!["x", "y", "z"]);
}
#[test]
fn test_assignment_vs_declaration_with_initializer() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int x;
int y = 10;
x = 5;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
let declarations: Vec<_> = func
.body
.iter()
.filter(|stmt| matches!(stmt, Statement::VariableDeclaration { .. }))
.collect();
let assignments: Vec<_> =
func.body.iter().filter(|stmt| matches!(stmt, Statement::Assignment { .. })).collect();
assert_eq!(declarations.len(), 2, "Should have 2 variable declarations");
assert_eq!(assignments.len(), 1, "Should have 1 assignment statement");
if let Statement::Assignment { target, .. } = assignments[0] {
assert_eq!(target, "x", "Assignment should be to 'x'");
}
}
#[test]
fn test_assignment_with_function_call() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void* malloc(int);
void test(int size) {
void* x;
x = malloc(size);
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func =
ast.functions().iter().find(|f| f.name == "test").expect("Should find test function");
let assignment = func
.body
.iter()
.find(|stmt| matches!(stmt, Statement::Assignment { .. }))
.expect("Should have assignment");
if let Statement::Assignment { target, value } = assignment {
assert_eq!(target, "x");
assert!(matches!(value, Expression::FunctionCall { .. }), "Value should be function call");
}
}
#[test]
fn test_assignment_with_dereference() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test(int* ptr, int value) {
*ptr = value;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
let deref_assignment = func
.body
.iter()
.find(|stmt| matches!(stmt, Statement::DerefAssignment { .. }))
.expect("Should have dereference assignment");
if let Statement::DerefAssignment { target, value } = deref_assignment {
assert!(
matches!(target, Expression::Variable(_)),
"Target should be variable (the pointer being dereferenced)"
);
assert!(matches!(value, Expression::Variable(_)), "Value should be variable");
}
}
#[test]
fn test_assignment_with_array_index() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test(int* arr, int i, int value) {
arr[i] = value;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
let array_assignment = func
.body
.iter()
.find(|stmt| matches!(stmt, Statement::ArrayIndexAssignment { .. }))
.expect("Should have array index assignment");
if let Statement::ArrayIndexAssignment { array, index, value } = array_assignment {
assert!(matches!(**array, Expression::Variable(_)), "Array should be variable");
assert!(matches!(**index, Expression::Variable(_)), "Index should be variable");
assert!(matches!(value, Expression::Variable(_)), "Value should be variable");
}
}
#[test]
fn test_assignment_with_complex_lhs() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test() {
int x;
int y;
x = 42;
y = x;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
let assignments: Vec<_> =
func.body.iter().filter(|stmt| matches!(stmt, Statement::Assignment { .. })).collect();
assert_eq!(assignments.len(), 2);
if let Statement::Assignment { target, value } = assignments[0] {
assert_eq!(target, "x");
assert!(matches!(value, Expression::IntLiteral(_)));
}
if let Statement::Assignment { target, value } = assignments[1] {
assert_eq!(target, "y");
assert!(matches!(value, Expression::Variable(_)));
}
}
#[test]
fn test_assignment_validation_with_nested_expressions() {
let parser = CParser::new().expect("Parser creation failed");
let source = r#"
void test(int a, int b, int c) {
int x;
x = (a + b) * c;
}
"#;
let ast = parser.parse(source).expect("Parsing should succeed");
let func = &ast.functions()[0];
let assignment = func
.body
.iter()
.find(|stmt| matches!(stmt, Statement::Assignment { .. }))
.expect("Should have assignment");
if let Statement::Assignment { target, value } = assignment {
assert_eq!(target, "x");
match value {
Expression::BinaryOp { left, right, .. } => {
assert!(
matches!(**left, Expression::BinaryOp { .. }),
"Left operand should be binary operation"
);
assert!(
matches!(**right, Expression::Variable(_)),
"Right operand should be variable"
);
}
_ => panic!("Expected binary operation"),
}
}
}