#![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",
))]
#[path = "common/test_utils.rs"]
mod test_utils;
use std::fs;
use std::process::Command;
use tempfile::TempDir;
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_if_else_expression_no_semicolons() {
let source = r#"
pub fn get_last_selected(is_empty: bool, value: i64) -> Option<i64> {
let result = if is_empty {
None
} else {
Some(value)
};
result
}
pub fn get_last_inline(is_empty: bool, value: i64) -> Option<i64> {
if is_empty {
None
} else {
Some(value)
}
}
"#;
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let wj_path = temp_dir.path().join("test.wj");
let out_dir = temp_dir.path().join("out");
fs::write(&wj_path, source).expect("Failed to write test file");
fs::create_dir_all(&out_dir).expect("Failed to create output dir");
let output = Command::new(test_utils::wj_binary())
.args([
"build",
wj_path.to_str().unwrap(),
"-o",
out_dir.to_str().unwrap(),
"--no-cargo",
])
.output()
.expect("Failed to run compiler");
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
panic!("Compilation failed:\n{}", stderr);
}
let generated_rs = out_dir.join("test.rs");
let result = fs::read_to_string(&generated_rs).expect("Failed to read generated Rust");
assert!(
!result.contains("None;\n"),
"Should not add semicolon after None in if-else expression"
);
let rustc_out = temp_dir.path().join("rustc_out");
fs::create_dir_all(&rustc_out).expect("rustc output dir");
let rustc_output = Command::new("rustc")
.arg("--crate-type=lib")
.arg("--edition=2021")
.arg(&generated_rs)
.arg("--out-dir")
.arg(rustc_out.to_str().unwrap())
.output()
.expect("Failed to run rustc");
if !rustc_output.status.success() {
panic!(
"Generated Rust has type errors:\n{}",
String::from_utf8_lossy(&rustc_output.stderr)
);
}
}