use super::*;
#[test]
fn test_create_switch_statement() {
let switch_stmt = HirStatement::Switch {
condition: HirExpression::Variable("x".to_string()),
cases: vec![SwitchCase {
value: Some(HirExpression::IntLiteral(1)),
body: vec![HirStatement::Break],
}],
default_case: Some(vec![HirStatement::Break]),
};
match switch_stmt {
HirStatement::Switch { condition, cases, default_case } => {
assert!(matches!(condition, HirExpression::Variable(_)));
assert_eq!(cases.len(), 1);
assert!(default_case.is_some());
}
_ => panic!("Expected Switch statement"),
}
}
#[test]
fn test_switch_with_multiple_cases() {
let switch_stmt = HirStatement::Switch {
condition: HirExpression::Variable("status".to_string()),
cases: vec![
SwitchCase {
value: Some(HirExpression::IntLiteral(0)),
body: vec![HirStatement::Return(Some(HirExpression::IntLiteral(1)))],
},
SwitchCase {
value: Some(HirExpression::IntLiteral(1)),
body: vec![HirStatement::Return(Some(HirExpression::IntLiteral(2)))],
},
],
default_case: Some(vec![HirStatement::Return(Some(HirExpression::IntLiteral(0)))]),
};
match switch_stmt {
HirStatement::Switch { cases, .. } => {
assert_eq!(cases.len(), 2);
}
_ => panic!("Expected Switch statement"),
}
}
#[test]
fn test_switch_without_default() {
let switch_stmt = HirStatement::Switch {
condition: HirExpression::Variable("x".to_string()),
cases: vec![SwitchCase {
value: Some(HirExpression::IntLiteral(1)),
body: vec![HirStatement::Break],
}],
default_case: None,
};
match switch_stmt {
HirStatement::Switch { default_case, .. } => {
assert!(default_case.is_none());
}
_ => panic!("Expected Switch statement"),
}
}
#[test]
fn test_switch_case_with_multiple_statements() {
let switch_stmt = HirStatement::Switch {
condition: HirExpression::Variable("x".to_string()),
cases: vec![SwitchCase {
value: Some(HirExpression::IntLiteral(1)),
body: vec![
HirStatement::Assignment {
target: "y".to_string(),
value: HirExpression::IntLiteral(1),
},
HirStatement::Assignment {
target: "z".to_string(),
value: HirExpression::IntLiteral(2),
},
HirStatement::Break,
],
}],
default_case: None,
};
match switch_stmt {
HirStatement::Switch { cases, .. } => {
assert_eq!(cases[0].body.len(), 3);
}
_ => panic!("Expected Switch statement"),
}
}
#[test]
fn test_switch_case_structure() {
let case =
SwitchCase { value: Some(HirExpression::IntLiteral(42)), body: vec![HirStatement::Break] };
assert!(case.value.is_some());
assert_eq!(case.body.len(), 1);
}
#[test]
fn test_switch_default_case_structure() {
let default = SwitchCase { value: None, body: vec![HirStatement::Break] };
assert!(default.value.is_none());
}
#[test]
fn test_switch_with_nested_if() {
let switch_stmt = HirStatement::Switch {
condition: HirExpression::Variable("x".to_string()),
cases: vec![SwitchCase {
value: Some(HirExpression::IntLiteral(1)),
body: vec![
HirStatement::If {
condition: HirExpression::Variable("y".to_string()),
then_block: vec![HirStatement::Break],
else_block: None,
},
HirStatement::Break,
],
}],
default_case: None,
};
match switch_stmt {
HirStatement::Switch { cases, .. } => {
assert!(matches!(cases[0].body[0], HirStatement::If { .. }));
}
_ => panic!("Expected Switch statement"),
}
}
#[test]
fn test_switch_clone() {
let switch_stmt = HirStatement::Switch {
condition: HirExpression::Variable("x".to_string()),
cases: vec![SwitchCase {
value: Some(HirExpression::IntLiteral(1)),
body: vec![HirStatement::Break],
}],
default_case: None,
};
let cloned = switch_stmt.clone();
assert_eq!(switch_stmt, cloned);
}
#[test]
fn test_switch_with_expression_condition() {
let switch_stmt = HirStatement::Switch {
condition: HirExpression::BinaryOp {
op: BinaryOperator::Add,
left: Box::new(HirExpression::Variable("x".to_string())),
right: Box::new(HirExpression::Variable("y".to_string())),
},
cases: vec![SwitchCase {
value: Some(HirExpression::IntLiteral(1)),
body: vec![HirStatement::Break],
}],
default_case: None,
};
match switch_stmt {
HirStatement::Switch { condition, .. } => {
assert!(matches!(condition, HirExpression::BinaryOp { .. }));
}
_ => panic!("Expected Switch statement"),
}
}
#[test]
fn test_switch_fallthrough_pattern() {
let switch_stmt = HirStatement::Switch {
condition: HirExpression::Variable("x".to_string()),
cases: vec![
SwitchCase {
value: Some(HirExpression::IntLiteral(1)),
body: vec![], },
SwitchCase {
value: Some(HirExpression::IntLiteral(2)),
body: vec![HirStatement::Break],
},
],
default_case: None,
};
match switch_stmt {
HirStatement::Switch { cases, .. } => {
assert_eq!(cases.len(), 2);
assert!(cases[0].body.is_empty()); }
_ => panic!("Expected Switch statement"),
}
}