#[cfg(feature = "leptos-integration")]
use super::*;
#[cfg(feature = "leptos-integration")]
mod tests {
use super::*;
fn motion_value_fixture() -> MotionValue<f64> {
MotionValue::new(42.0)
}
fn motion_transform_fixture() -> MotionTransform {
MotionTransform::identity()
}
fn motion_values_fixture() -> MotionValues {
let mut values = MotionValues::new();
values.add("opacity", AnimationValue::Number(0.5));
values.add("x", AnimationValue::Pixels(100.0));
values.add("scale", AnimationValue::Number(1.0));
values
}
#[test]
fn test_motion_value_creation_should_initialize_correctly() {
let motion_value = MotionValue::new(100.0);
assert_eq!(motion_value.get(), 100.0);
assert_eq!(motion_value.get_velocity(), 0.0);
}
#[test]
fn test_motion_value_set_should_update_value() {
let motion_value = MotionValue::new(0.0);
motion_value.set(50.0);
assert_eq!(motion_value.get(), 50.0);
}
#[test]
fn test_motion_value_creation_with_different_values() {
let test_cases = vec![(0.0, 0.0), (42.0, 42.0), (-10.0, -10.0), (999.999, 999.999)];
for (initial_value, expected_value) in test_cases {
let motion_value = MotionValue::new(initial_value);
assert_eq!(motion_value.get(), expected_value);
}
}
#[test]
fn test_motion_number_increment() {
let test_cases = vec![
(0.0, 5.0, 5.0),
(10.0, 3.0, 13.0),
(-5.0, 2.0, -3.0),
(100.0, 0.0, 100.0),
];
for (initial, increment, expected) in test_cases {
let motion_num = MotionValue::new(initial);
motion_num.increment(increment);
assert_eq!(motion_num.get(), expected);
}
}
#[test]
fn test_motion_value_properties() {
let test_values = vec![0.0, 42.0, -10.0, 999.999, f64::MAX, f64::MIN];
for initial in &test_values {
for new_value in &test_values {
for increment in &test_values {
let motion_value = MotionValue::new(*initial);
assert_eq!(motion_value.get(), *initial);
motion_value.set(*new_value);
assert_eq!(motion_value.get(), *new_value);
let before_increment = motion_value.get();
motion_value.increment(*increment);
let after_increment = motion_value.get();
assert_eq!(after_increment, before_increment + increment);
assert!(motion_value.get_velocity().is_finite());
}
}
}
}
#[test]
fn test_motion_value_velocity_properties() {
let test_values = vec![0.0, 42.0, -10.0, 999.999, f64::MAX, f64::MIN];
for velocity in &test_values {
for value in &test_values {
let motion_value = MotionValue::new(0.0);
motion_value.set_velocity(*velocity);
assert_eq!(motion_value.get_velocity(), *velocity);
motion_value.set_with_velocity(*value, *velocity);
assert_eq!(motion_value.get(), *value);
assert_eq!(motion_value.get_velocity(), *velocity);
}
}
}
#[test]
fn test_motion_value_subscription() {
let motion_value = motion_value_fixture();
let received_values = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let received_values_clone = received_values.clone();
motion_value.subscribe(move |value| {
received_values_clone.lock().unwrap().push(*value);
});
motion_value.set(100.0);
motion_value.set(200.0);
let values = received_values.lock().unwrap();
assert_eq!(values.len(), 2);
assert_eq!(values[0], 100.0);
assert_eq!(values[1], 200.0);
}
#[test]
fn test_motion_transform_identity_should_be_identity() {
let motion_transform = MotionTransform::identity();
let transform = motion_transform.get();
assert_eq!(transform.x, None);
assert_eq!(transform.y, None);
assert_eq!(transform.rotate_z, None);
assert_eq!(transform.scale, None);
}
#[test]
fn test_motion_transform_set_translate_should_update_position() {
let motion_transform = MotionTransform::identity();
motion_transform.set_translate(10.0, 20.0);
let transform = motion_transform.get();
assert_eq!(transform.x, Some(10.0));
assert_eq!(transform.y, Some(20.0));
}
#[test]
fn test_motion_transform_set_rotation_should_update_rotation() {
let motion_transform = MotionTransform::identity();
motion_transform.set_rotation(45.0);
let transform = motion_transform.get();
assert_eq!(transform.rotate_z, Some(45.0));
}
#[test]
fn test_motion_transform_set_scale_should_update_scale() {
let motion_transform = MotionTransform::identity();
motion_transform.set_scale(2.0);
let transform = motion_transform.get();
assert_eq!(transform.scale, Some(2.0));
}
#[test]
fn test_motion_values_creation_should_be_empty() {
let values = MotionValues::new();
assert_eq!(values.get_all().len(), 0);
}
#[test]
fn test_motion_values_add_should_store_value() {
let mut values = MotionValues::new();
values.add("opacity", AnimationValue::Number(0.5));
assert!(values.get("opacity").is_some());
if let Some(opacity) = values.get("opacity") {
assert_eq!(opacity.get(), AnimationValue::Number(0.5));
}
}
#[test]
fn test_motion_values_set_should_update_existing_value() {
let mut values = MotionValues::new();
values.add("opacity", AnimationValue::Number(0.5));
values.set("opacity", AnimationValue::Number(1.0));
if let Some(opacity) = values.get("opacity") {
assert_eq!(opacity.get(), AnimationValue::Number(1.0));
}
}
#[test]
fn test_motion_values_set_all_should_update_multiple_values() {
let mut values = MotionValues::new();
values.add("opacity", AnimationValue::Number(0.0));
values.add("x", AnimationValue::Pixels(0.0));
let mut target = HashMap::new();
target.insert("opacity".to_string(), AnimationValue::Number(1.0));
target.insert("x".to_string(), AnimationValue::Pixels(100.0));
values.set_all(&target);
if let Some(opacity) = values.get("opacity") {
assert_eq!(opacity.get(), AnimationValue::Number(1.0));
}
if let Some(x) = values.get("x") {
assert_eq!(x.get(), AnimationValue::Pixels(100.0));
}
}
#[test]
fn test_motion_values_complex_operations() {
let values = motion_values_fixture();
assert_eq!(values.get_all().len(), 3);
assert!(values.get("opacity").is_some());
assert!(values.get("x").is_some());
assert!(values.get("scale").is_some());
assert!(values.get("nonexistent").is_none());
}
#[test]
fn test_motion_values_properties() {
let test_keys = vec!["opacity", "x", "y", "scale", "rotation"];
let test_values = vec![0.0, 0.5, 1.0, 100.0, 360.0];
for (i, key) in test_keys.iter().enumerate() {
if i < test_values.len() {
let mut motion_values = MotionValues::new();
motion_values.add(*key, AnimationValue::Number(test_values[i]));
assert!(motion_values.get(*key).is_some());
if let Some(motion_value) = motion_values.get(*key) {
assert_eq!(motion_value.get(), AnimationValue::Number(test_values[i]));
}
let all_values = motion_values.get_all();
assert_eq!(all_values.len(), 1);
}
}
}
#[test]
fn test_motion_value_edge_cases() {
let motion_value = MotionValue::new(f64::MAX);
assert_eq!(motion_value.get(), f64::MAX);
let motion_value = MotionValue::new(f64::MIN);
assert_eq!(motion_value.get(), f64::MIN);
let motion_value = MotionValue::new(f64::NAN);
assert!(motion_value.get().is_nan());
let motion_value = MotionValue::new(f64::INFINITY);
assert_eq!(motion_value.get(), f64::INFINITY);
}
#[test]
fn test_motion_value_velocity_edge_cases() {
let motion_value = MotionValue::new(0.0);
motion_value.set_velocity(f64::MAX);
assert_eq!(motion_value.get_velocity(), f64::MAX);
motion_value.set_velocity(f64::MIN);
assert_eq!(motion_value.get_velocity(), f64::MIN);
motion_value.set_velocity(f64::NAN);
assert!(motion_value.get_velocity().is_nan());
}
#[cfg(feature = "bench")]
mod benches {
use super::*;
#[test]
fn bench_motion_value_creation() {
for _ in 0..1000 {
let _motion_value = MotionValue::new(42.0);
}
}
#[test]
fn bench_motion_value_get_set() {
let motion_value = MotionValue::new(0.0);
for i in 0..1000 {
motion_value.set(i as f64);
let _value = motion_value.get();
}
}
#[test]
fn bench_motion_values_collection_operations() {
let mut values = MotionValues::new();
for i in 0..100 {
values.add(format!("key_{}", i), AnimationValue::Number(i as f64));
}
for i in 0..100 {
values.set(
&format!("key_{}", i),
AnimationValue::Number((i * 2) as f64),
);
}
let _all_values = values.get_all();
}
}
#[test]
fn test_motion_value_integration_with_animation_system() {
let motion_value = MotionValue::new(0.0);
motion_value.set_with_velocity(100.0, 50.0);
assert_eq!(motion_value.get(), 100.0);
assert_eq!(motion_value.get_velocity(), 50.0);
motion_value.set_with_velocity(150.0, 25.0);
assert_eq!(motion_value.get(), 150.0);
assert_eq!(motion_value.get_velocity(), 25.0);
}
#[test]
fn test_motion_values_nonexistent_key_handling() {
let values = MotionValues::new();
values.set("nonexistent", AnimationValue::Number(42.0));
assert!(values.get("nonexistent").is_none());
}
#[test]
fn test_motion_value_thread_safety() {
use std::sync::Arc;
use std::thread;
let motion_value = Arc::new(MotionValue::new(0.0));
let mut handles = vec![];
for i in 0..10 {
let motion_value = Arc::clone(&motion_value);
let handle = thread::spawn(move || {
for j in 0..100 {
motion_value.set((i * 100 + j) as f64);
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
let final_value = motion_value.get();
assert!(final_value >= 0.0 && final_value < 1000.0);
}
}