#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "codegen_tests",
))]
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_str_param_to_string_arg_auto_conversion() {
let wj_binary = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("release")
.join("wj");
let test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("test_str_to_string");
fs::create_dir_all(&test_dir).unwrap();
let test_content = r#"
fn greet(name: string) -> string {
format!("Hello, {}", name)
}
fn store_name(name: string) {
println!("{}", name);
}
fn main() {
let name = "Alice";
store_name(greet(name));
}
"#;
let test_file = test_dir.join("str_to_string.wj");
fs::write(&test_file, test_content).unwrap();
let output = Command::new(&wj_binary)
.current_dir(&test_dir)
.arg("build")
.arg("--no-cargo")
.arg(&test_file)
.output()
.expect("Failed to execute wj build");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
println!("STDOUT:\n{}", stdout);
println!("STDERR:\n{}", stderr);
let rust_file = test_dir.join("build").join("str_to_string.rs");
let rust_code = fs::read_to_string(&rust_file).unwrap();
println!("Generated Rust:\n{}", rust_code);
assert!(
rust_code.contains("store_name(greet(") || rust_code.contains("store_name(&greet("),
"Expected store_name to receive greet result.\nGenerated code:\n{}",
rust_code
);
let compile_output = Command::new("rustc")
.current_dir(test_dir.join("build"))
.arg("--crate-type")
.arg("bin")
.arg("str_to_string.rs")
.output()
.expect("Failed to run rustc");
let compile_stderr = String::from_utf8_lossy(&compile_output.stderr);
assert!(
compile_output.status.success(),
"Expected generated code to compile.\nRustc errors:\n{}",
compile_stderr
);
let _ = fs::remove_dir_all(&test_dir);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_struct_field_str_to_string() {
let wj_binary = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("release")
.join("wj");
let test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("test_field_str_to_string");
fs::create_dir_all(&test_dir).unwrap();
let test_content = r#"
struct User {
name: string,
}
fn create_user(name: string) -> User {
User { name: name }
}
fn main() {
let user = create_user("Bob");
}
"#;
let test_file = test_dir.join("field_str_to_string.wj");
fs::write(&test_file, test_content).unwrap();
let output = Command::new(&wj_binary)
.current_dir(&test_dir)
.arg("build")
.arg("--no-cargo")
.arg(&test_file)
.output()
.expect("Failed to execute wj build");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
println!("STDOUT:\n{}", stdout);
println!("STDERR:\n{}", stderr);
let rust_file = test_dir.join("build").join("field_str_to_string.rs");
let rust_code = fs::read_to_string(&rust_file).unwrap();
println!("Generated Rust:\n{}", rust_code);
assert!(
rust_code.contains("name: name.to_string()")
|| rust_code.contains("User { name }")
|| rust_code.contains("User{name}")
|| rust_code.contains("name: name"),
"Expected valid String field assignment.\nGenerated code:\n{}",
rust_code
);
let compile_output = Command::new("rustc")
.current_dir(test_dir.join("build"))
.arg("--crate-type")
.arg("bin")
.arg("field_str_to_string.rs")
.output()
.expect("Failed to run rustc");
let compile_stderr = String::from_utf8_lossy(&compile_output.stderr);
assert!(
compile_output.status.success(),
"Expected generated code to compile.\nRustc errors:\n{}",
compile_stderr
);
let _ = fs::remove_dir_all(&test_dir);
}