#![allow(missing_docs)]
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::time::Instant;
use tempfile::NamedTempFile;
fn ruchy_cmd() -> Command {
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
}
#[test]
fn test_ruchy_run_interprets_under_2_seconds() {
let temp_file = NamedTempFile::new().unwrap();
fs::write(
&temp_file,
r#"
fun main() {
println("Hello from interpreter!")
}
"#,
)
.unwrap();
let start = Instant::now();
ruchy_cmd()
.arg("run")
.arg(temp_file.path())
.assert()
.success()
.stdout(predicate::str::contains("Hello from interpreter!"));
let duration = start.elapsed();
assert!(
duration.as_secs() < 2,
"ruchy run took {}s - it's compiling instead of interpreting!",
duration.as_secs()
);
}
#[test]
fn test_ruchy_run_no_binary_artifact() {
let temp_file = NamedTempFile::new().unwrap();
fs::write(
&temp_file,
r#"
fun main() {
println("No binary should be created")
}
"#,
)
.unwrap();
let before_files = fs::read_dir("/tmp")
.unwrap()
.filter_map(std::result::Result::ok)
.filter(|e| {
e.file_name()
.to_string_lossy()
.starts_with("ruchy_compile_")
})
.count();
ruchy_cmd()
.arg("run")
.arg(temp_file.path())
.assert()
.success();
let after_files = fs::read_dir("/tmp")
.unwrap()
.filter_map(std::result::Result::ok)
.filter(|e| {
e.file_name()
.to_string_lossy()
.starts_with("ruchy_compile_")
})
.count();
assert_eq!(
before_files, after_files,
"ruchy run created binary artifacts - it's compiling instead of interpreting!"
);
}
#[test]
fn test_ruchy_run_output_correct() {
let temp_file = NamedTempFile::new().unwrap();
fs::write(
&temp_file,
r#"
fun main() {
println("Line 1")
println("Line 2")
let x = 42
println(x)
}
"#,
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(temp_file.path())
.assert()
.success()
.stdout(predicate::str::contains("Line 1"))
.stdout(predicate::str::contains("Line 2"))
.stdout(predicate::str::contains("42"));
}
#[test]
fn test_ruchy_run_handles_errors() {
let temp_file = NamedTempFile::new().unwrap();
fs::write(
&temp_file,
r"
fun main() {
let x = 1 +
}
",
)
.unwrap();
ruchy_cmd()
.arg("run")
.arg(temp_file.path())
.assert()
.failure()
.stderr(predicate::str::contains("error").or(predicate::str::contains("Error")));
}
#[test]
fn test_ruchy_run_same_output_as_direct() {
let temp_file = NamedTempFile::new().unwrap();
fs::write(
&temp_file,
r"
fun main() {
let x = 10
let y = 20
println(x + y)
}
",
)
.unwrap();
let direct_output = ruchy_cmd()
.arg(temp_file.path())
.assert()
.success()
.get_output()
.stdout
.clone();
let run_output = ruchy_cmd()
.arg("run")
.arg(temp_file.path())
.assert()
.success()
.get_output()
.stdout
.clone();
assert_eq!(
direct_output, run_output,
"ruchy run produces different output than direct execution!"
);
}