198 lines
5.1 KiB
Rust
198 lines
5.1 KiB
Rust
//! Tests for the #[derive(UnityComponent)] macro
|
|
|
|
use unity_parser::{ComponentContext, FileID, UnityComponent};
|
|
|
|
/// Test component matching the PlaySFX script from VR_Horror_YouCantRun
|
|
#[derive(Debug, Clone, UnityComponent)]
|
|
#[unity_class("PlaySFX")]
|
|
struct PlaySFX {
|
|
#[unity_field("volume")]
|
|
volume: f64,
|
|
|
|
#[unity_field("startTime")]
|
|
start_time: f64,
|
|
|
|
#[unity_field("endTime")]
|
|
end_time: f64,
|
|
|
|
#[unity_field("isLoop")]
|
|
is_loop: bool,
|
|
}
|
|
|
|
/// Test component with different field types
|
|
#[derive(Debug, Clone, UnityComponent)]
|
|
#[unity_class("TestComponent")]
|
|
struct TestComponent {
|
|
#[unity_field("floatValue")]
|
|
float_value: f32,
|
|
|
|
#[unity_field("intValue")]
|
|
int_value: i32,
|
|
|
|
#[unity_field("stringValue")]
|
|
string_value: String,
|
|
|
|
#[unity_field("boolValue")]
|
|
bool_value: bool,
|
|
}
|
|
|
|
#[test]
|
|
fn test_play_sfx_parsing() {
|
|
let yaml_str = r#"
|
|
volume: 0.75
|
|
startTime: 1.5
|
|
endTime: 3.0
|
|
isLoop: 1
|
|
"#;
|
|
|
|
let yaml: serde_yaml::Value = serde_yaml::from_str(yaml_str).unwrap();
|
|
let mapping = yaml.as_mapping().unwrap();
|
|
|
|
let ctx = ComponentContext {
|
|
type_id: 114,
|
|
file_id: FileID::from_i64(12345),
|
|
class_name: "PlaySFX",
|
|
entity: None,
|
|
linking_ctx: None,
|
|
yaml: mapping,
|
|
};
|
|
|
|
let result = PlaySFX::parse(mapping, &ctx);
|
|
assert!(result.is_some(), "Failed to parse PlaySFX component");
|
|
|
|
let component = result.unwrap();
|
|
assert_eq!(component.volume, 0.75);
|
|
assert_eq!(component.start_time, 1.5);
|
|
assert_eq!(component.end_time, 3.0);
|
|
assert_eq!(component.is_loop, true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_play_sfx_default_values() {
|
|
// Test with missing fields (should use Default::default())
|
|
let yaml_str = r#"
|
|
volume: 0.5
|
|
"#;
|
|
|
|
let yaml: serde_yaml::Value = serde_yaml::from_str(yaml_str).unwrap();
|
|
let mapping = yaml.as_mapping().unwrap();
|
|
|
|
let ctx = ComponentContext {
|
|
type_id: 114,
|
|
file_id: FileID::from_i64(12345),
|
|
class_name: "PlaySFX",
|
|
entity: None,
|
|
linking_ctx: None,
|
|
yaml: mapping,
|
|
};
|
|
|
|
let result = PlaySFX::parse(mapping, &ctx);
|
|
assert!(result.is_some(), "Failed to parse PlaySFX component with defaults");
|
|
|
|
let component = result.unwrap();
|
|
assert_eq!(component.volume, 0.5);
|
|
assert_eq!(component.start_time, 0.0); // Default for f64
|
|
assert_eq!(component.end_time, 0.0); // Default for f64
|
|
assert_eq!(component.is_loop, false); // Default for bool
|
|
}
|
|
|
|
#[test]
|
|
fn test_test_component_parsing() {
|
|
let yaml_str = r#"
|
|
floatValue: 3.14
|
|
intValue: 42
|
|
stringValue: "Hello, Unity!"
|
|
boolValue: 1
|
|
"#;
|
|
|
|
let yaml: serde_yaml::Value = serde_yaml::from_str(yaml_str).unwrap();
|
|
let mapping = yaml.as_mapping().unwrap();
|
|
|
|
let ctx = ComponentContext {
|
|
type_id: 114,
|
|
file_id: FileID::from_i64(67890),
|
|
class_name: "TestComponent",
|
|
entity: None,
|
|
linking_ctx: None,
|
|
yaml: mapping,
|
|
};
|
|
|
|
let result = TestComponent::parse(mapping, &ctx);
|
|
assert!(result.is_some(), "Failed to parse TestComponent");
|
|
|
|
let component = result.unwrap();
|
|
assert!((component.float_value - 3.14_f32).abs() < 0.001);
|
|
assert_eq!(component.int_value, 42);
|
|
assert_eq!(component.string_value, "Hello, Unity!");
|
|
assert_eq!(component.bool_value, true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_component_registration() {
|
|
// Verify that components are registered in the inventory
|
|
let mut found_play_sfx = false;
|
|
let mut found_test_component = false;
|
|
|
|
for reg in inventory::iter::<unity_parser::ComponentRegistration> {
|
|
if reg.class_name == "PlaySFX" {
|
|
found_play_sfx = true;
|
|
assert_eq!(reg.type_id, 114);
|
|
}
|
|
if reg.class_name == "TestComponent" {
|
|
found_test_component = true;
|
|
assert_eq!(reg.type_id, 114);
|
|
}
|
|
}
|
|
|
|
assert!(
|
|
found_play_sfx,
|
|
"PlaySFX component was not registered in inventory"
|
|
);
|
|
assert!(
|
|
found_test_component,
|
|
"TestComponent was not registered in inventory"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_component_registration_parser() {
|
|
// Test that the registered parser function works
|
|
let yaml_str = r#"
|
|
volume: 0.8
|
|
startTime: 2.0
|
|
endTime: 4.0
|
|
isLoop: 0
|
|
"#;
|
|
|
|
let yaml: serde_yaml::Value = serde_yaml::from_str(yaml_str).unwrap();
|
|
let mapping = yaml.as_mapping().unwrap();
|
|
|
|
let ctx = ComponentContext {
|
|
type_id: 114,
|
|
file_id: FileID::from_i64(11111),
|
|
class_name: "PlaySFX",
|
|
entity: None,
|
|
linking_ctx: None,
|
|
yaml: mapping,
|
|
};
|
|
|
|
// Find the PlaySFX registration and call its parser
|
|
for reg in inventory::iter::<unity_parser::ComponentRegistration> {
|
|
if reg.class_name == "PlaySFX" {
|
|
let result = (reg.parser)(mapping, &ctx);
|
|
assert!(result.is_some(), "Registered parser failed to parse");
|
|
|
|
// Downcast to verify it's the right type
|
|
let boxed = result.unwrap();
|
|
assert!(
|
|
boxed.downcast_ref::<PlaySFX>().is_some(),
|
|
"Parsed component is not PlaySFX type"
|
|
);
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
panic!("PlaySFX registration not found");
|
|
}
|