use std::env;
use std::fs;
use std::path::{Path, PathBuf};
#[cfg(feature = "monstertruck")]
use opensubdiv_petite::far::EndCapType;
#[cfg(feature = "monstertruck")]
#[allow(dead_code)]
pub fn default_end_cap_type() -> EndCapType {
#[cfg(feature = "b_spline_end_caps")]
{
EndCapType::BSplineBasis
}
#[cfg(not(feature = "b_spline_end_caps"))]
{
EndCapType::GregoryBasis
}
}
#[cfg(not(feature = "monstertruck"))]
#[allow(dead_code)]
pub fn default_end_cap_type() -> opensubdiv_petite::far::EndCapType {
opensubdiv_petite::far::EndCapType::GregoryBasis
}
pub fn should_update_expected() -> bool {
if env::var("RUST_UPDATE_EXPECTED_TEST_RESULTS").is_ok() {
return true;
}
let args: Vec<String> = env::args().collect();
args.iter().any(|arg| arg == "--update" || arg == "-u")
}
pub fn expected_results_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("expected_results")
}
pub fn test_output_dir() -> PathBuf {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("test_output");
fs::create_dir_all(&path).expect("Failed to create test output directory");
path
}
#[allow(dead_code)]
pub fn assert_file_matches(actual_path: &Path, expected_filename: &str) {
let expected_path = expected_results_dir().join(expected_filename);
if should_update_expected() {
fs::copy(actual_path, &expected_path)
.unwrap_or_else(|_| panic!("Failed to update expected file: {}", expected_filename));
println!("Updated expected file: {}", expected_filename);
} else {
assert!(
expected_path.exists(),
"Expected file does not exist: {}. Run with RUST_UPDATE_EXPECTED_TEST_RESULTS=1 or --update to create it.",
expected_path.display()
);
let actual_content = fs::read_to_string(actual_path)
.unwrap_or_else(|_| panic!("Failed to read actual file: {}", actual_path.display()));
let expected_content = fs::read_to_string(&expected_path).unwrap_or_else(|_| {
panic!("Failed to read expected file: {}", expected_path.display())
});
let normalize_step = |content: &str| -> String {
if expected_filename.ends_with(".step") {
content
.lines()
.map(|line| {
if line.starts_with("FILE_NAME(") {
let mut in_quotes = false;
let mut quote_count = 0;
let mut result = String::new();
let mut chars = line.chars();
while let Some(ch) = chars.next() {
if ch == '\'' {
in_quotes = !in_quotes;
if !in_quotes {
quote_count += 1;
}
}
result.push(ch);
if quote_count == 2 && !in_quotes {
let timestamp_end = result.len() - 1;
if let Some(timestamp_start) =
result[..timestamp_end].rfind('\'')
{
result.replace_range(
(timestamp_start + 1)..timestamp_end,
"TIMESTAMP_PLACEHOLDER",
);
}
result.push_str(&chars.collect::<String>());
break;
}
}
result
} else {
line.to_string()
}
})
.collect::<Vec<_>>()
.join("\n")
} else {
content.to_string()
}
};
let normalized_actual = normalize_step(&actual_content);
let normalized_expected = normalize_step(&expected_content);
assert_eq!(
normalized_actual,
normalized_expected,
"File content mismatch for {expected_filename}. Run with RUST_UPDATE_EXPECTED_TEST_RESULTS=1 or --update to update expected results."
);
}
}
#[allow(dead_code)] pub fn assert_content_matches(actual_content: &str, expected_filename: &str) {
let expected_path = expected_results_dir().join(expected_filename);
if should_update_expected() {
fs::write(&expected_path, actual_content)
.unwrap_or_else(|_| panic!("Failed to update expected file: {}", expected_filename));
println!("Updated expected file: {}", expected_filename);
} else {
assert!(
expected_path.exists(),
"Expected file does not exist: {}. Run with RUST_UPDATE_EXPECTED_TEST_RESULTS=1 or --update to create it.",
expected_path.display()
);
let expected_content = fs::read_to_string(&expected_path).unwrap_or_else(|_| {
panic!("Failed to read expected file: {}", expected_path.display())
});
assert_eq!(
actual_content,
expected_content,
"Content mismatch for {expected_filename}. Run with RUST_UPDATE_EXPECTED_TEST_RESULTS=1 or --update to update expected results."
);
}
}
#[allow(dead_code)]
pub fn test_output_path(filename: &str) -> PathBuf {
test_output_dir().join(filename)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_directories_exist() {
assert!(expected_results_dir().exists());
assert!(test_output_dir().exists());
}
}