#![allow(missing_docs)]
use assert_cmd::Command;
use std::fs;
use tempfile::TempDir;
fn ruchy_cmd() -> Command {
assert_cmd::cargo::cargo_bin_cmd!("ruchy")
}
#[test]
fn test_format_preserves_fun_keyword() {
let temp = TempDir::new().unwrap();
let file = temp.path().join("test.ruchy");
fs::write(&file, "fun greet(name) { println(\"Hello, {}\", name) }").unwrap();
ruchy_cmd().arg("fmt").arg(&file).assert().success();
let content = fs::read_to_string(&file).unwrap();
assert!(
content.contains("fun greet"),
"Formatter should preserve 'fun' keyword, but got: {content}"
);
assert!(
!content.contains("fn greet"),
"Formatter incorrectly converted 'fun' to 'fn': {content}"
);
}
#[test]
fn test_format_multiple_functions_preserve_fun() {
let temp = TempDir::new().unwrap();
let file = temp.path().join("test.ruchy");
fs::write(
&file,
r#"
fun add(a, b) { a + b }
fun multiply(x, y) { x * y }
fun greet(name) { println("Hello, {}", name) }
"#,
)
.unwrap();
ruchy_cmd().arg("fmt").arg(&file).assert().success();
let content = fs::read_to_string(&file).unwrap();
assert_eq!(
content.matches("fun ").count(),
3,
"Expected 3 'fun' keywords, got: {content}"
);
assert_eq!(
content.matches("fn ").count(),
0,
"Formatter incorrectly output 'fn' keyword: {content}"
);
}
#[test]
fn test_format_nested_functions_preserve_fun() {
let temp = TempDir::new().unwrap();
let file = temp.path().join("test.ruchy");
fs::write(
&file,
r"
fun outer() {
fun inner(x) { x + 1 }
inner(42)
}
",
)
.unwrap();
ruchy_cmd().arg("fmt").arg(&file).assert().success();
let content = fs::read_to_string(&file).unwrap();
assert!(
content.contains("fun outer"),
"Outer function should use 'fun': {content}"
);
assert!(
content.contains("fun inner"),
"Inner function should use 'fun': {content}"
);
assert!(
!content.contains("fn "),
"Formatter incorrectly used 'fn': {content}"
);
}
#[test]
fn test_format_typed_functions_preserve_fun() {
let temp = TempDir::new().unwrap();
let file = temp.path().join("test.ruchy");
fs::write(&file, "fun add(a: Int, b: Int) -> Int { a + b }").unwrap();
ruchy_cmd().arg("fmt").arg(&file).assert().success();
let content = fs::read_to_string(&file).unwrap();
assert!(
content.contains("fun add"),
"Typed function should use 'fun': {content}"
);
assert!(
!content.contains("fn add"),
"Formatter incorrectly used 'fn': {content}"
);
}
#[test]
fn test_format_anonymous_functions_preserve_fun() {
let temp = TempDir::new().unwrap();
let file = temp.path().join("test.ruchy");
fs::write(
&file,
r"
let double = fun(x) { x * 2 }
let result = double(21)
",
)
.unwrap();
ruchy_cmd().arg("fmt").arg(&file).assert().success();
let content = fs::read_to_string(&file).unwrap();
if content.contains("fun(") || content.contains("fun (") {
assert!(
!content.contains("fn(") && !content.contains("fn ("),
"Formatter incorrectly converted 'fun' to 'fn': {content}"
);
}
}
#[test]
fn test_format_ruchyruchy_pattern() {
let temp = TempDir::new().unwrap();
let file = temp.path().join("test.ruchy");
fs::write(
&file,
r"
struct Compiler {
source: String
}
impl Compiler {
fun new(source: String) -> Compiler {
Compiler { source }
}
fun compile(self) -> Result {
// compilation logic
Ok(())
}
}
",
)
.unwrap();
ruchy_cmd().arg("fmt").arg(&file).assert().success();
let content = fs::read_to_string(&file).unwrap();
assert!(
content.contains("fun new"),
"Method 'new' should use 'fun': {content}"
);
assert!(
content.contains("fun compile"),
"Method 'compile' should use 'fun': {content}"
);
assert_eq!(
content.matches("fn ").count(),
0,
"Formatter incorrectly used 'fn' instead of 'fun': {content}"
);
}