#![allow(deprecated)]
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/09-pattern-matching")
.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();
let code = std::fs::read_to_string(example).unwrap();
ruchy_cmd().arg("-e").arg(&code).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();
let temp_file = std::env::temp_dir().join(format!(
"wasm_validation_test_{}_{}.ruchy",
example.file_stem().unwrap().to_string_lossy(),
std::process::id()
));
std::fs::write(&temp_file, "let x = 42\nprintln(x)").unwrap();
ruchy_cmd().arg("wasm").arg(&temp_file).assert().success();
std::fs::remove_file(&temp_file).ok();
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();
ruchy_cmd().arg("notebook").arg(example).assert().success();
}
#[test]
fn test_langcomp_009_01_integer_literal_pattern() {
let temp_file = std::env::temp_dir().join("langcomp_009_01_int_literal.ruchy");
std::fs::write(
&temp_file,
r#"
let number = 42
let result = match number {
0 => "zero",
42 => "the answer",
_ => "something else"
}
println(result)
"#,
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("the answer"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_009_01_string_literal_pattern() {
let temp_file = std::env::temp_dir().join("langcomp_009_01_str_literal.ruchy");
std::fs::write(
&temp_file,
r#"
let status = "success"
let message = match status {
"success" => "Operation completed",
"error" => "Operation failed",
_ => "Unknown status"
}
println(message)
"#,
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("Operation completed"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_009_01_literal_patterns_example_file() {
let example = example_path("01_literal_patterns.ruchy");
validate_with_15_tools(&example);
ruchy_cmd()
.arg("run")
.arg(&example)
.assert()
.success()
.stdout(predicate::str::contains("the answer"))
.stdout(predicate::str::contains("Operation completed"));
}
#[test]
fn test_langcomp_009_02_variable_binding_pattern() {
let temp_file = std::env::temp_dir().join("langcomp_009_02_var_bind.ruchy");
std::fs::write(
&temp_file,
r#"
let value = 100
let category = match value {
0 => "zero".to_string(),
x if x < 10 => "single digit".to_string(),
x => f"large number: {x}"
}
println(category)
"#,
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("large number: 100"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_009_02_wildcard_pattern() {
let temp_file = std::env::temp_dir().join("langcomp_009_02_wildcard.ruchy");
std::fs::write(
&temp_file,
r#"
let status_code = 404
let response = match status_code {
200 => "OK",
404 => "Not Found",
500 => "Server Error",
_ => "Unknown"
}
println(response)
"#,
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("Not Found"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_009_02_variable_patterns_example_file() {
let example = example_path("02_variable_patterns.ruchy");
validate_with_15_tools(&example);
ruchy_cmd()
.arg("run")
.arg(&example)
.assert()
.success()
.stdout(predicate::str::contains("large number"))
.stdout(predicate::str::contains("Not Found"));
}
#[test]
fn test_langcomp_009_03_tuple_literal_pattern() {
let temp_file = std::env::temp_dir().join("langcomp_009_03_tuple_literal.ruchy");
std::fs::write(
&temp_file,
r#"
let point = (0, 0)
let location = match point {
(0, 0) => "origin",
(x, 0) => "on x-axis",
(0, y) => "on y-axis",
(x, y) => "in quadrant"
}
println(location)
"#,
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("origin"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_009_03_tuple_variable_pattern() {
let temp_file = std::env::temp_dir().join("langcomp_009_03_tuple_var.ruchy");
std::fs::write(
&temp_file,
r#"
let pair = (42, "answer")
match pair {
(num, text) => {
println(num)
println(text)
}
}
"#,
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("42"))
.stdout(predicate::str::contains("answer"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_009_03_tuple_patterns_example_file() {
let example = example_path("03_tuple_patterns.ruchy");
validate_with_15_tools(&example);
ruchy_cmd()
.arg("run")
.arg(&example)
.assert()
.success()
.stdout(predicate::str::contains("in quadrant"));
}
#[test]
fn test_langcomp_009_04_let_destructuring() {
let temp_file = std::env::temp_dir().join("langcomp_009_04_let_destruct.ruchy");
std::fs::write(
&temp_file,
r"
let coordinates = (100, 200)
let (x, y) = coordinates
println(x)
println(y)
",
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("100"))
.stdout(predicate::str::contains("200"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_009_04_nested_destructuring() {
let temp_file = std::env::temp_dir().join("langcomp_009_04_nested.ruchy");
std::fs::write(
&temp_file,
r"
let nested = ((1, 2), (3, 4))
let ((a, b), (c, d)) = nested
println(a)
println(d)
",
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(&temp_file)
.assert()
.success()
.stdout(predicate::str::contains("1"))
.stdout(predicate::str::contains("4"));
std::fs::remove_file(&temp_file).ok();
}
#[test]
fn test_langcomp_009_04_destructuring_example_file() {
let example = example_path("04_destructuring.ruchy");
validate_with_15_tools(&example);
ruchy_cmd()
.arg("run")
.arg(&example)
.assert()
.success()
.stdout(predicate::str::contains("100"))
.stdout(predicate::str::contains("200"));
}