#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "analyzer_tests",
))]
use tempfile::TempDir;
use windjammer::{build_project_ext, CompilationTarget};
#[test]
fn test_float_inference_single_file_with_f32_param() {
let temp = TempDir::new().unwrap();
let src = temp.path().join("src");
let build = temp.path().join("build");
std::fs::create_dir_all(&src).unwrap();
std::fs::write(
src.join("mira.wj"),
r#"
pub struct Companion {
pub health: f32
}
impl Companion {
pub fn update(self, dt: f32) {
// dt is f32, so 1000.0 should be inferred as f32
let frame_time_ms = dt * 1000.0
self.health = self.health + dt
}
}
"#,
)
.unwrap();
build_project_ext(
&src.join("mira.wj"),
&build,
CompilationTarget::Rust,
false,
false, &[],
)
.expect("Single file build should succeed");
let rust_code = std::fs::read_to_string(build.join("mira.rs")).unwrap();
assert!(
!rust_code.contains("1000.0_f64"),
"1000.0 should be inferred as f32 (used with f32 parameter dt)"
);
}
#[test]
fn test_float_inference_multi_file_with_f32_param() {
let temp = TempDir::new().unwrap();
let src = temp.path().join("src");
let build = temp.path().join("build");
std::fs::create_dir_all(src.join("companions")).unwrap();
std::fs::write(
src.join("companions/companion.wj"),
r#"
pub struct Companion {
pub health: f32,
pub name: string
}
impl Companion {
pub fn new(name: string) -> Companion {
Companion {
health: 100.0,
name: name
}
}
}
"#,
)
.unwrap();
std::fs::write(
src.join("companions/mira.wj"),
r#"
use crate::companions::companion::Companion
pub struct Mira {
pub companion: Companion
}
impl Mira {
pub fn new() -> Mira {
let companion = Companion::new("Mira")
Mira { companion: companion }
}
pub fn update(self, dt: f32) {
// dt is f32, so 1000.0 should be inferred as f32
// BUG: In multi-file builds, this literal is incorrectly inferred as f64
let frame_time_ms = dt * 1000.0
self.companion.health = self.companion.health + dt
}
}
"#,
)
.unwrap();
std::fs::write(
src.join("companions/mod.wj"),
r#"
pub mod companion
pub mod mira
"#,
)
.unwrap();
std::fs::write(
src.join("mod.wj"),
r#"
pub mod companions
"#,
)
.unwrap();
build_project_ext(
&src.join("mod.wj"),
&build,
CompilationTarget::Rust,
false,
true, &[],
)
.expect("Multi-file library build should succeed");
let rust_code = std::fs::read_to_string(build.join("companions/mira.rs")).unwrap();
assert!(
!rust_code.contains("1000.0_f64"),
"1000.0 should be inferred as f32 (used with f32 parameter dt). Found:\n{}",
rust_code
.lines()
.find(|l| l.contains("1000.0"))
.unwrap_or("NOT FOUND")
);
}
#[test]
fn test_float_inference_respects_parameter_types_across_files() {
let temp = TempDir::new().unwrap();
let src = temp.path().join("src");
let build = temp.path().join("build");
std::fs::create_dir_all(&src).unwrap();
std::fs::write(
src.join("a.wj"),
r#"
pub fn helper(x: f32) -> f32 {
x * 2.0
}
"#,
)
.unwrap();
std::fs::write(
src.join("b.wj"),
r#"
pub fn main_func(dt: f32) -> f32 {
dt * 1000.0
}
"#,
)
.unwrap();
std::fs::write(
src.join("mod.wj"),
r#"
pub mod a
pub mod b
"#,
)
.unwrap();
build_project_ext(
&src.join("mod.wj"),
&build,
CompilationTarget::Rust,
false,
true,
&[],
)
.expect("Build should succeed");
let b_code = std::fs::read_to_string(build.join("b.rs")).unwrap();
assert!(
!b_code.contains("1000.0_f64"),
"1000.0 in b.wj should be f32 (dt parameter is f32)"
);
}