#[cfg(test)]
mod tests {
use finx::Finx;
fn run_and_capture(source: &str) -> Vec<String> {
let mut engine = Finx::new();
engine.execute(source).expect("Execution failed");
engine.get_output()
}
#[test]
fn test_simple_while_loop() {
let src = r#"
let i = 0;
while i < 3 {
print(i);
i = i + 1;
}
print("done");
"#;
let out = run_and_capture(src);
assert_eq!(out, ["0", "1", "2", "done"]);
}
#[test]
fn test_simple_for_loop() {
let src = r#"
for i 0..3 {
print(i);
}
print("done");
"#;
let out = run_and_capture(src);
assert_eq!(out, ["0", "1", "2", "done"]);
}
#[test]
fn test_nested_loops() {
let src = r#"
for i 0..2 {
for j 0..2 {
print(i * 2 + j);
}
}
"#;
let out = run_and_capture(src);
assert_eq!(out, ["0", "1", "2", "3"]);
}
#[test]
fn test_while_loop_with_zero_iterations() {
let src = r#"
let i = 5;
while i < 3 {
print(i);
i = i + 1;
}
print("after");
"#;
let out = run_and_capture(src);
assert_eq!(out, ["after"]);
}
#[test]
fn test_for_loop_with_zero_iterations() {
let src = r#"
for i 5..3 {
print(i);
}
print("after");
"#;
let out = run_and_capture(src);
assert_eq!(out, ["after"]);
}
#[test]
fn test_for_loop_variable_scope() {
let src = r#"
let i = 100;
for i 0..2 {
print(i);
}
print(i); // Should print 100, not the loop variable
"#;
let out = run_and_capture(src);
assert_eq!(out, ["0", "1", "100"]);
}
#[test]
fn test_while_loop_with_complex_condition() {
let src = r#"
let x = 1;
let y = 10;
while x * 2 < y {
print(x);
x = x * 2;
}
"#;
let out = run_and_capture(src);
assert_eq!(out, ["1", "2", "4"]);
}
#[test]
fn test_for_loop_with_expressions() {
let src = r#"
for i (1 + 1)..(3 + 2) {
print(i);
}
"#;
let out = run_and_capture(src);
assert_eq!(out, ["2", "3", "4"]);
}
#[test]
fn test_loop_with_function_calls() {
let src = r#"
fn double(x) {
return x * 2;
}
for i 0..3 {
print(double(i));
}
"#;
let out = run_and_capture(src);
assert_eq!(out, ["0", "2", "4"]);
}
#[test]
fn test_while_loop_modifying_external_variable() {
let src = r#"
let sum = 0;
let i = 1;
while i <= 3 {
sum = sum + i;
i = i + 1;
}
print(sum);
"#;
let out = run_and_capture(src);
assert_eq!(out, ["6"]); }
}