#![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::fs;
use std::process::Command;
use tempfile::TempDir;
fn compile_project(sources: &[(&str, &str)]) -> (String, String) {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let project_dir = temp_dir.path();
for (filename, content) in sources {
let file_path = project_dir.join(filename);
if let Some(parent) = file_path.parent() {
fs::create_dir_all(parent).ok();
}
fs::write(&file_path, content).expect("Failed to write source file");
}
let wj_binary = env!("CARGO_BIN_EXE_wj");
let output_dir = project_dir.join("out");
let _output = Command::new(wj_binary)
.current_dir(project_dir)
.arg("build")
.arg(project_dir.join("main.wj"))
.arg("--output")
.arg(&output_dir)
.arg("--no-cargo")
.output()
.expect("Failed to run wj compiler");
let lib_rs = fs::read_to_string(output_dir.join("lib.rs")).unwrap_or_else(|_| String::new());
let main_rs = fs::read_to_string(output_dir.join("main.rs")).unwrap_or_else(|_| String::new());
(lib_rs, main_rs)
}
#[test]
fn test_main_not_in_lib_rs() {
let sources = &[
(
"main.wj",
r#"
use helper
fn main() {
let result = helper::process()
println!("Result: {}", result)
}
"#,
),
(
"helper.wj",
r#"
pub fn process() -> int {
42
}
"#,
),
];
let (lib_rs, main_rs) = compile_project(sources);
println!("Generated lib.rs:\n{}", lib_rs);
println!("\nGenerated main.rs:\n{}", main_rs);
assert!(
!lib_rs.contains("pub mod main"),
"lib.rs should not include 'pub mod main' for binary projects.\n\
main() belongs in main.rs, not lib.rs.\n\
Generated lib.rs:\n{}",
lib_rs
);
assert!(
lib_rs.contains("pub mod helper") || lib_rs.is_empty(),
"lib.rs should contain other modules or be empty.\n\
Generated lib.rs:\n{}",
lib_rs
);
assert!(
main_rs.contains("fn main()"),
"main.rs should contain fn main().\n\
Generated main.rs:\n{}",
main_rs
);
}
#[test]
fn test_library_project_no_main() {
let sources = &[(
"helper.wj",
r#"
pub fn library_function() -> int {
100
}
"#,
)];
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let project_dir = temp_dir.path();
for (filename, content) in sources {
fs::write(project_dir.join(filename), content).expect("Failed to write source");
}
let wj_binary = env!("CARGO_BIN_EXE_wj");
let output_dir = project_dir.join("out");
let _output = Command::new(wj_binary)
.current_dir(project_dir)
.arg("build")
.arg(project_dir.join("helper.wj"))
.arg("--output")
.arg(&output_dir)
.arg("--no-cargo")
.output()
.expect("Failed to run wj compiler");
let helper_rs =
fs::read_to_string(output_dir.join("helper.rs")).unwrap_or_else(|_| String::new());
println!("Generated helper.rs:\n{}", helper_rs);
assert!(
helper_rs.contains("pub fn library_function"),
"Generated Rust should contain library function"
);
assert!(
!helper_rs.contains("pub mod main"),
"Library file should not reference main module"
);
assert!(
!helper_rs.contains("fn main()"),
"Library file should not contain main() function"
);
}
#[test]
fn test_main_with_submodules() {
let sources = &[
(
"main.wj",
r#"
use game::player
use game::enemy
fn main() {
player::spawn()
enemy::spawn()
}
"#,
),
(
"game/player.wj",
r#"
pub fn spawn() {
println!("Player spawned")
}
"#,
),
(
"game/enemy.wj",
r#"
pub fn spawn() {
println!("Enemy spawned")
}
"#,
),
(
"game/mod.wj",
r#"
pub mod player
pub mod enemy
"#,
),
];
let (lib_rs, main_rs) = compile_project(sources);
println!("Generated lib.rs (nested modules):\n{}", lib_rs);
println!("\nGenerated main.rs (nested modules):\n{}", main_rs);
assert!(
!lib_rs.contains("pub mod main"),
"lib.rs should not include 'pub mod main' even with nested modules"
);
assert!(
lib_rs.contains("pub mod game") || lib_rs.is_empty(),
"lib.rs should contain game module"
);
assert!(
main_rs.contains("fn main()"),
"main.rs should contain fn main()"
);
}