#![cfg(test)]
#![allow(warnings)]
#![allow(clippy::assertions_on_constants)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::unwrap_used)]
#![allow(
clippy::expect_used,
clippy::unwrap_used,
clippy::uninlined_format_args,
clippy::print_stdout,
clippy::expect_fun_call
)]
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::NamedTempFile;
#[test]
fn test_binary_execution_all_examples() {
let simple_examples = vec!["examples/hello.ruchy"];
for example in simple_examples {
let ruchy_file = Path::new(example);
if ruchy_file.exists() {
println!("Testing: {}", ruchy_file.display());
validate_ruchy_file(ruchy_file);
}
}
}
fn validate_ruchy_file(path: &Path) {
let content = fs::read_to_string(path).expect(&format!("Failed to read {}", path.display()));
let mut parser = ruchy::Parser::new(&content);
let ast = parser
.parse()
.expect(&format!("Failed to parse {}", path.display()));
let mut transpiler = ruchy::Transpiler::new();
let rust_code = transpiler
.transpile_to_program(&ast)
.expect(&format!("Failed to transpile {}", path.display()));
let rust_code_str = rust_code.to_string();
let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
temp_file
.write_all(rust_code_str.as_bytes())
.expect("Failed to write Rust code");
temp_file.flush().expect("Failed to flush temp file");
let output_binary = temp_file.path().with_extension("exe");
let compile_result = Command::new("rustc")
.arg("--edition=2021")
.arg("--crate-name=ruchy_test")
.arg("-O") .arg("-o")
.arg(&output_binary)
.arg(temp_file.path())
.output()
.expect("Failed to execute rustc");
assert!(
compile_result.status.success(),
"Failed to compile {} to binary via LLVM:\nstderr: {}",
path.display(),
String::from_utf8_lossy(&compile_result.stderr)
);
let output_file = path.with_extension("output");
if output_file.exists() {
let run_result = Command::new(&output_binary)
.output()
.expect(&format!("Failed to run binary for {}", path.display()));
let expected_output = fs::read_to_string(&output_file).expect(&format!(
"Failed to read expected output {}",
output_file.display()
));
assert_eq!(
String::from_utf8_lossy(&run_result.stdout).trim(),
expected_output.trim(),
"Output mismatch for {}",
path.display()
);
}
if output_binary.exists() {
fs::remove_file(output_binary).ok();
}
}
#[test]
fn test_fibonacci_binary() {
}
#[test]
fn test_hello_binary() {
let hello_path = Path::new("examples/hello.ruchy");
if hello_path.exists() {
validate_ruchy_file(hello_path);
}
}
#[test]
fn test_compilation_performance() {
use std::time::Instant;
let test_files = [
"examples/hello.ruchy",
"examples/fibonacci.ruchy",
"examples/property_test_demo.ruchy",
"examples/test_blocks.ruchy",
"examples/test_strings.ruchy",
];
let ruchy_files: Vec<PathBuf> = test_files
.iter()
.map(PathBuf::from)
.filter(|p| p.exists())
.collect();
for ruchy_file in ruchy_files {
let content = fs::read_to_string(&ruchy_file)
.expect(&format!("Failed to read {}", ruchy_file.display()));
let start = Instant::now();
let mut parser = ruchy::Parser::new(&content);
let ast = parser
.parse()
.expect(&format!("Failed to parse {}", ruchy_file.display()));
let mut transpiler = ruchy::Transpiler::new();
let _rust_code = transpiler
.transpile(&ast)
.expect(&format!("Failed to transpile {}", ruchy_file.display()));
let elapsed = start.elapsed();
assert!(
elapsed.as_secs() < 5,
"Compilation of {} took too long: {:?}",
ruchy_file.display(),
elapsed
);
println!("Compiled {} in {:?}", ruchy_file.display(), elapsed);
}
}