use ruchy::{Parser, Transpiler};
#[test]
fn test_str_parameter_transpilation() {
let mut parser = Parser::new("fun greet(name: str) { println(name) }");
let ast = parser.parse().expect("Should parse");
let mut transpiler = Transpiler::new();
let result = transpiler.transpile_to_program(&ast);
assert!(result.is_ok(), "Should transpile successfully");
let code = result.unwrap().to_string();
println!("Generated code: {code}");
assert!(
code.contains("& str") || code.contains("&str"),
"Function parameter should be &str"
);
assert!(
!code.contains("name : str"),
"Should not use unsized str type"
);
}
#[test]
fn test_string_literal_no_unnecessary_to_string() {
let mut parser = Parser::new("fun main() { greet(\"Hello\") }");
let ast = parser.parse().expect("Should parse");
let mut transpiler = Transpiler::new();
let result = transpiler.transpile_to_program(&ast);
assert!(result.is_ok(), "Should transpile successfully");
let code = result.unwrap().to_string();
assert!(
!code.contains("\"Hello\" . to_string ()"),
"Should not add .to_string() to string literals"
);
}
#[test]
fn test_println_format_correctness() {
let mut parser = Parser::new("fun test() { println(\"Hello, {}!\", \"World\") }");
let ast = parser.parse().expect("Should parse");
let mut transpiler = Transpiler::new();
let result = transpiler.transpile_to_program(&ast);
assert!(result.is_ok(), "Should transpile successfully");
let code = result.unwrap().to_string();
assert!(!code.contains("\"{} {:?}\""), "Should not use debug format");
assert!(code.contains("println !"), "Should generate println! macro");
}
#[test]
fn test_no_unnecessary_hashmap_import() {
let mut parser = Parser::new("fun main() { println(\"Hello\") }");
let ast = parser.parse().expect("Should parse");
let mut transpiler = Transpiler::new();
let result = transpiler.transpile_to_program(&ast);
assert!(result.is_ok(), "Should transpile successfully");
let code = result.unwrap().to_string();
assert!(
!code.contains("use std :: collections :: HashMap"),
"Should not import HashMap unnecessarily"
);
}
#[test]
fn test_no_extra_braces() {
let mut parser = Parser::new("fun test() { 42 }");
let ast = parser.parse().expect("Should parse");
let mut transpiler = Transpiler::new();
let result = transpiler.transpile_to_program(&ast);
assert!(result.is_ok(), "Should transpile successfully");
let code = result.unwrap().to_string();
assert!(!code.contains("{ {"), "Should not generate nested braces");
assert!(!code.contains("} }"), "Should not generate nested braces");
}
#[test]
fn test_complete_string_example_compiles() {
let code = r#"
fun greet(name: str) {
println("Hello, {}!", name)
}
fun main() {
greet("World")
}
"#;
let mut parser = Parser::new(code);
let ast = parser.parse().expect("Should parse");
let mut transpiler = Transpiler::new();
let result = transpiler.transpile_to_program(&ast);
assert!(result.is_ok(), "Should transpile without errors");
let generated = result.unwrap().to_string();
assert!(
generated.contains("&str") || generated.contains("& str"),
"Should use &str for parameters"
);
assert!(
!generated.contains("name : str)"),
"Should not use unsized str"
);
assert!(
!generated.contains("\"World\" . to_string"),
"Should not add .to_string() unnecessarily"
);
}