mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
Added tests to ButtonAxisSettings (#3272)
# Objective `ButtonAxisSettings` wasn't tested properly. ## Solution I added tests and fixed issue with `filter` method Co-authored-by: CrazyRoka <rokarostuk@gmail.com>
This commit is contained in:
parent
c05a9cf8f5
commit
bf96f266d7
1 changed files with 70 additions and 6 deletions
|
@ -186,17 +186,20 @@ impl Default for ButtonAxisSettings {
|
|||
|
||||
impl ButtonAxisSettings {
|
||||
fn filter(&self, new_value: f32, old_value: Option<f32>) -> Option<f32> {
|
||||
let new_value = if new_value <= self.low {
|
||||
0.0
|
||||
} else if new_value >= self.high {
|
||||
1.0
|
||||
} else {
|
||||
new_value
|
||||
};
|
||||
|
||||
if let Some(old_value) = old_value {
|
||||
if (new_value - old_value).abs() <= self.threshold {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if new_value <= self.low {
|
||||
return Some(0.0);
|
||||
}
|
||||
if new_value >= self.high {
|
||||
return Some(1.0);
|
||||
}
|
||||
|
||||
Some(new_value)
|
||||
}
|
||||
}
|
||||
|
@ -306,3 +309,64 @@ const ALL_AXIS_TYPES: [GamepadAxisType; 8] = [
|
|||
GamepadAxisType::DPadX,
|
||||
GamepadAxisType::DPadY,
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ButtonAxisSettings;
|
||||
|
||||
fn test_button_axis_settings_filter(
|
||||
settings: ButtonAxisSettings,
|
||||
new_value: f32,
|
||||
old_value: Option<f32>,
|
||||
expected: Option<f32>,
|
||||
) {
|
||||
let actual = settings.filter(new_value, old_value);
|
||||
assert_eq!(
|
||||
expected, actual,
|
||||
"Testing filtering for {:?} with new_value = {:?}, old_value = {:?}",
|
||||
settings, new_value, old_value
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_button_axis_settings_default_filter() {
|
||||
let cases = [
|
||||
(1.0, None, Some(1.0)),
|
||||
(0.99, None, Some(1.0)),
|
||||
(0.96, None, Some(1.0)),
|
||||
(0.95, None, Some(1.0)),
|
||||
(0.9499, None, Some(0.9499)),
|
||||
(0.84, None, Some(0.84)),
|
||||
(0.43, None, Some(0.43)),
|
||||
(0.05001, None, Some(0.05001)),
|
||||
(0.05, None, Some(0.0)),
|
||||
(0.04, None, Some(0.0)),
|
||||
(0.01, None, Some(0.0)),
|
||||
(0.0, None, Some(0.0)),
|
||||
];
|
||||
|
||||
for (new_value, old_value, expected) in cases {
|
||||
let settings = ButtonAxisSettings::default();
|
||||
test_button_axis_settings_filter(settings, new_value, old_value, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_button_axis_settings_default_filter_with_old_value() {
|
||||
let cases = [
|
||||
(0.43, Some(0.44001), Some(0.43)),
|
||||
(0.43, Some(0.44), None),
|
||||
(0.43, Some(0.43), None),
|
||||
(0.43, Some(0.41999), Some(0.43)),
|
||||
(0.43, Some(0.17), Some(0.43)),
|
||||
(0.43, Some(0.84), Some(0.43)),
|
||||
(0.05, Some(0.055), Some(0.0)),
|
||||
(0.95, Some(0.945), Some(1.0)),
|
||||
];
|
||||
|
||||
for (new_value, old_value, expected) in cases {
|
||||
let settings = ButtonAxisSettings::default();
|
||||
test_button_axis_settings_filter(settings, new_value, old_value, expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue