163 lines
5.7 KiB
Rust
163 lines
5.7 KiB
Rust
use cursebreaker_parser::UnityFile;
|
|
use std::path::Path;
|
|
|
|
#[test]
|
|
fn test_parse_cardgrabber_prefab() {
|
|
let path = Path::new("data/tests/unity-sampleproject/PiratePanic/Assets/PiratePanic/Prefabs/Menu/Battle/Hand/CardGrabber.prefab");
|
|
|
|
// Skip if the file doesn't exist (CI/CD might not have submodules)
|
|
if !path.exists() {
|
|
eprintln!("Skipping test: file not found at {:?}", path);
|
|
return;
|
|
}
|
|
|
|
let file = UnityFile::from_path(path).expect("Failed to parse CardGrabber.prefab");
|
|
|
|
// Verify we parsed multiple documents
|
|
assert!(file.documents.len() > 0, "Should have at least one document");
|
|
|
|
// Find the GameObject
|
|
let game_objects = file.get_documents_by_class("GameObject");
|
|
assert!(!game_objects.is_empty(), "Should have at least one GameObject");
|
|
|
|
let game_object = game_objects[0];
|
|
assert_eq!(game_object.type_id, 1, "GameObject should have type ID 1");
|
|
|
|
// Verify the name property exists
|
|
if let Some(go_props) = game_object.get("GameObject") {
|
|
if let Some(props) = go_props.as_mapping() {
|
|
let has_name = props.keys().any(|k| k.as_str() == Some("m_Name"));
|
|
assert!(has_name, "GameObject should have m_Name property");
|
|
}
|
|
}
|
|
|
|
// Find RectTransform
|
|
let transforms = file.get_documents_by_class("RectTransform");
|
|
assert!(!transforms.is_empty(), "Should have at least one RectTransform");
|
|
|
|
let transform = transforms[0];
|
|
assert_eq!(transform.type_id, 224, "RectTransform should have type ID 224");
|
|
}
|
|
|
|
#[test]
|
|
fn test_parse_scene_file() {
|
|
let path = Path::new("data/tests/unity-sampleproject/PiratePanic/Assets/PiratePanic/Scenes/Scene01MainMenu.unity");
|
|
|
|
// Skip if the file doesn't exist
|
|
if !path.exists() {
|
|
eprintln!("Skipping test: file not found at {:?}", path);
|
|
return;
|
|
}
|
|
|
|
let file = UnityFile::from_path(path).expect("Failed to parse Scene01MainMenu.unity");
|
|
|
|
// Scenes typically have many documents
|
|
assert!(file.documents.len() > 10, "Scene should have many documents");
|
|
|
|
// Should have GameObjects
|
|
let game_objects = file.get_documents_by_class("GameObject");
|
|
assert!(!game_objects.is_empty(), "Scene should have GameObjects");
|
|
|
|
println!("Parsed {} documents from scene", file.documents.len());
|
|
println!("Found {} GameObjects", game_objects.len());
|
|
}
|
|
|
|
#[test]
|
|
fn test_parse_multiple_prefabs() {
|
|
let prefab_paths = [
|
|
"data/tests/unity-sampleproject/PiratePanic/Assets/PiratePanic/Prefabs/Menu/Battle/Hand/CostPanel.prefab",
|
|
"data/tests/unity-sampleproject/PiratePanic/Assets/PiratePanic/Prefabs/Menu/Battle/Hand/GoldPanel.prefab",
|
|
"data/tests/unity-sampleproject/PiratePanic/Assets/PiratePanic/Prefabs/Menu/Battle/Map/Node.prefab",
|
|
];
|
|
|
|
let mut total_documents = 0;
|
|
|
|
for path_str in &prefab_paths {
|
|
let path = Path::new(path_str);
|
|
|
|
if !path.exists() {
|
|
eprintln!("Skipping test: file not found at {:?}", path);
|
|
continue;
|
|
}
|
|
|
|
match UnityFile::from_path(path) {
|
|
Ok(file) => {
|
|
assert!(file.documents.len() > 0, "File {:?} should have documents", path);
|
|
total_documents += file.documents.len();
|
|
println!("Parsed {:?}: {} documents", path.file_name().unwrap(), file.documents.len());
|
|
}
|
|
Err(e) => {
|
|
panic!("Failed to parse {:?}: {}", path, e);
|
|
}
|
|
}
|
|
}
|
|
|
|
if total_documents > 0 {
|
|
assert!(total_documents > 3, "Should have parsed multiple documents across files");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_file_id_lookup() {
|
|
let path = Path::new("data/tests/unity-sampleproject/PiratePanic/Assets/PiratePanic/Prefabs/Menu/Battle/Hand/CardGrabber.prefab");
|
|
|
|
if !path.exists() {
|
|
eprintln!("Skipping test: file not found at {:?}", path);
|
|
return;
|
|
}
|
|
|
|
let file = UnityFile::from_path(path).expect("Failed to parse file");
|
|
|
|
// Get the first document's file ID
|
|
if let Some(first_doc) = file.documents.first() {
|
|
let file_id = first_doc.file_id;
|
|
|
|
// Look it up
|
|
let found = file.get_document(file_id);
|
|
assert!(found.is_some(), "Should be able to find document by file ID");
|
|
assert_eq!(found.unwrap().file_id, file_id, "Found document should have correct file ID");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_get_documents_by_type() {
|
|
let path = Path::new("data/tests/unity-sampleproject/PiratePanic/Assets/PiratePanic/Prefabs/Menu/Battle/Hand/CardGrabber.prefab");
|
|
|
|
if !path.exists() {
|
|
eprintln!("Skipping test: file not found at {:?}", path);
|
|
return;
|
|
}
|
|
|
|
let file = UnityFile::from_path(path).expect("Failed to parse file");
|
|
|
|
// Get all GameObjects (type ID 1)
|
|
let game_objects = file.get_documents_by_type(1);
|
|
assert!(!game_objects.is_empty(), "Should find GameObjects by type ID");
|
|
|
|
// Verify they're actually GameObjects
|
|
for go in game_objects {
|
|
assert_eq!(go.type_id, 1, "All returned documents should have type ID 1");
|
|
assert!(go.is_game_object(), "Document should be identified as GameObject");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_error_handling_invalid_file() {
|
|
let result = UnityFile::from_path("nonexistent_file.unity");
|
|
assert!(result.is_err(), "Should return error for nonexistent file");
|
|
}
|
|
|
|
#[test]
|
|
fn test_error_handling_invalid_format() {
|
|
// Create a temporary file with invalid content
|
|
let temp_dir = std::env::temp_dir();
|
|
let temp_file = temp_dir.join("invalid_unity_file.unity");
|
|
std::fs::write(&temp_file, "This is not a Unity file").expect("Failed to write temp file");
|
|
|
|
let result = UnityFile::from_path(&temp_file);
|
|
assert!(result.is_err(), "Should return error for invalid Unity file format");
|
|
|
|
// Clean up
|
|
let _ = std::fs::remove_file(&temp_file);
|
|
}
|