#![allow(deprecated)]
#![allow(clippy::ignore_without_reason)]
use assert_cmd::Command;
use predicates::prelude::*;
use std::path::PathBuf;
fn ruchy_cmd() -> Command {
Command::cargo_bin("ruchy").expect("Failed to find ruchy binary")
}
fn example_path(relative_path: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("examples/lang_comp/03-control-flow")
.join(relative_path)
}
fn validate_with_15_tools(example: &PathBuf) {
ruchy_cmd().arg("check").arg(example).assert().success();
ruchy_cmd().arg("transpile").arg(example).assert().success();
ruchy_cmd().arg("lint").arg(example).assert().success();
let compile_output = std::env::temp_dir().join(format!(
"compile_test_{}_{}",
example.file_stem().unwrap().to_string_lossy(),
std::process::id()
));
ruchy_cmd()
.arg("compile")
.arg(example)
.arg("-o")
.arg(&compile_output)
.assert()
.success();
std::fs::remove_file(&compile_output).ok();
ruchy_cmd().arg("run").arg(example).assert().success();
ruchy_cmd().arg("coverage").arg(example).assert().success();
ruchy_cmd()
.arg("runtime")
.arg(example)
.arg("--bigo")
.assert()
.success();
ruchy_cmd().arg("ast").arg(example).assert().success();
ruchy_cmd().arg("wasm").arg(example).assert().success();
ruchy_cmd()
.arg("provability")
.arg(example)
.assert()
.success();
ruchy_cmd()
.arg("property-tests")
.arg(example)
.arg("--cases")
.arg("100")
.assert()
.success();
ruchy_cmd()
.arg("mutations")
.arg(example)
.arg("--min-coverage")
.arg("0")
.arg("--timeout")
.arg("60")
.assert()
.success();
ruchy_cmd()
.arg("fuzz")
.arg(example)
.arg("--iterations")
.arg("10")
.assert()
.success();
}
#[test]
fn test_langcomp_003_01_if_expression_true_branch() {
let temp_file = std::env::temp_dir().join("langcomp_003_01_if_true.ruchy");
std::fs::write(&temp_file, "println(if true { 1 } else { 2 })").unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("1"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_003_01_if_expression_false_branch() {
let temp_file = std::env::temp_dir().join("langcomp_003_01_if_false.ruchy");
std::fs::write(&temp_file, "println(if false { 1 } else { 2 })").unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("2"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_003_01_if_expression_example_file() {
let example = example_path("01_if.ruchy");
validate_with_15_tools(&example);
}
#[test]
fn test_langcomp_003_02_match_literal_pattern() {
let temp_file = std::env::temp_dir().join("langcomp_003_02_match_literal.ruchy");
std::fs::write(
&temp_file,
"println(match 1 { 1 => 100, 2 => 200, _ => 999 })",
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("100"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_003_02_match_wildcard_pattern() {
let temp_file = std::env::temp_dir().join("langcomp_003_02_match_wildcard.ruchy");
std::fs::write(
&temp_file,
"println(match 99 { 1 => 100, 2 => 200, _ => 999 })",
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("999"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_003_02_match_expression_example_file() {
let example = example_path("02_match.ruchy");
validate_with_15_tools(&example);
}
#[test]
fn test_langcomp_003_03_for_loop_range() {
let temp_file = std::env::temp_dir().join("langcomp_003_03_for_range.ruchy");
std::fs::write(
&temp_file,
r"
let sum = 0
for i in 0..3 {
sum = sum + i
}
println(sum)
",
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("3"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_003_03_for_loop_example_file() {
let example = example_path("03_for.ruchy");
validate_with_15_tools(&example);
}
#[test]
fn test_langcomp_003_04_while_loop_condition() {
let temp_file = std::env::temp_dir().join("langcomp_003_04_while.ruchy");
std::fs::write(
&temp_file,
r"
let count = 0
while count < 3 {
count = count + 1
}
println(count)
",
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("3"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_003_04_while_loop_example_file() {
let example = example_path("04_while.ruchy");
validate_with_15_tools(&example);
}
#[test]
fn test_langcomp_003_05_break_exits_loop() {
let temp_file = std::env::temp_dir().join("langcomp_003_05_break.ruchy");
std::fs::write(
&temp_file,
r"
let i = 0
while true {
if i == 3 {
break
}
i = i + 1
}
println(i)
",
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("3"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_003_05_loop_control_example_file() {
let example = example_path("05_break_continue.ruchy");
validate_with_15_tools(&example);
}
#[cfg(test)]
mod property_tests {
use super::*;
#[test]
#[ignore = "Test disabled - run with --ignored"]
fn test_langcomp_003_property_if_else_always_returns_value() {
for i in 0..100 {
let code = format!("if {i} > 50 {{ 1 }} else {{ 0 }}");
let temp_file = std::env::temp_dir().join(format!("langcomp_003_prop_if_{i}.ruchy"));
std::fs::write(&temp_file, &code).unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::is_match(r"^(0|1)\s*$").unwrap());
std::fs::remove_file(&temp_file).ok();
}
}
#[test]
#[ignore = "Test disabled - run with --ignored"]
fn test_langcomp_003_property_match_wildcard_never_fails() {
for i in 0..100 {
let code = format!("match {i} {{ 1 => 100, _ => 999 }}");
let temp_file = std::env::temp_dir().join(format!("langcomp_003_prop_match_{i}.ruchy"));
std::fs::write(&temp_file, &code).unwrap();
ruchy_cmd().arg("run").arg(&temp_file).assert().success();
std::fs::remove_file(&temp_file).ok();
}
}
#[test]
#[ignore = "Test disabled - run with --ignored"]
fn test_langcomp_003_property_for_loop_iterations_equal_range_size() {
for n in 1..10 {
let code = format!(
r"
let count = 0
for i in 0..{n} {{
count = count + 1
}}
count
"
);
let temp_file = std::env::temp_dir().join(format!("langcomp_003_prop_for_{n}.ruchy"));
std::fs::write(&temp_file, &code).unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains(n.to_string()));
std::fs::remove_file(&temp_file).ok();
}
}
}