1
0
Fork 0
mirror of https://github.com/jugeeya/UltimateTrainingModpack.git synced 2024-11-28 04:44:06 +00:00
UltimateTrainingModpack/training_mod_tui/tests/test_stateful_slider.rs
asimon-1 65f87df1e0
Weighted Probability Menu Selections + TUI Backend Redesign (#655)
* Add replacement training_mod_tui crate

* Begin work on converting over to byteflags

* Allow StatefulTable.iter_mut()

* Additional merge work

* Fixing more compile errors from the merge

* Replace training_mod_tui with training_mod_tui_2, update byteflags dependency for random selection

* Fix button_config, hero buffs, caps issues. Move button logic into App

* Restore some functions that I cut too agressively

* Move to_vec into byteflags crate

* Fix src/training/ui

* Set `_` match arms on byteflags to `unreachable!()`

* Fix final few compiler errors

* Run formatter

* Reconsider whether unreachable parts are actually unreachable

* Adjust logging and remove dead code

* Fix some menu bugs

* Adjust icon sizes

* Separate checkmark from is_single_option

* Allow fast increment / decrement of slider handles with up/down

* Allow resetting current menu

* Change button behavior: `R/Z` now resets either the whole menu or the selected submenu depending on app.page, and `Y` now clears the current toggle so you don't have to press A six times. Only show keyhelp for `Y` if its relevant.

* Remove unneeded command line interface

* Take care of some minor nomenclature TODO's

* Increase stack size for menu::load_from_file() to avoid crashes

* Implement confirmation page before restoring from defaults

* Run rustfmt

* Fix warnings
2023-12-02 09:02:43 -08:00

288 lines
7.6 KiB
Rust

use training_mod_tui_2::{SliderState, StatefulSlider};
fn initialize_slider(state: SliderState) -> StatefulSlider {
StatefulSlider {
state: state,
lower: 0,
upper: 150,
min: 0,
max: 150,
incr_amount_slow: 1,
incr_amount_fast: 10,
}
}
#[test]
fn stateful_slider_new() {
let s = initialize_slider(SliderState::LowerHover);
assert_eq!(s, StatefulSlider::new());
}
#[test]
fn stateful_slider_increment_selected_slow() {
let mut s = initialize_slider(SliderState::LowerHover);
s.upper = 50;
// Check LowerHover: no increment
s.increment_selected_slow();
assert_eq!(s.lower, 0);
// Check LowerSelected: lower increments
s.state = SliderState::LowerSelected;
s.increment_selected_slow();
assert_eq!(s.lower, 1);
// Check LowerSelected: lower can't go above upper
s.lower = s.upper;
s.increment_selected_slow();
assert_eq!(s.lower, s.upper);
// Check UpperHover: no increment
s.lower = 5;
s.upper = 50;
s.state = SliderState::UpperHover;
s.increment_selected_slow();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, 50);
// Check UpperSelected: upper increments
s.state = SliderState::UpperSelected;
s.increment_selected_slow();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, 51);
// Check UpperSelected: upper can't go above max
s.upper = s.max;
s.increment_selected_slow();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, s.max);
}
#[test]
fn stateful_slider_increment_selected_fast() {
let mut s = initialize_slider(SliderState::LowerHover);
s.upper = 50;
// Check LowerHover: no increment
s.increment_selected_fast();
assert_eq!(s.lower, 0);
// Check LowerSelected: lower increments
s.state = SliderState::LowerSelected;
s.increment_selected_fast();
assert_eq!(s.lower, 10);
// Check LowerSelected: lower can't go above upper
s.lower = s.upper;
s.increment_selected_fast();
assert_eq!(s.lower, s.upper);
// Check UpperHover: no increment
s.lower = 5;
s.upper = 50;
s.state = SliderState::UpperHover;
s.increment_selected_fast();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, 50);
// Check UpperSelected: upper increments
s.state = SliderState::UpperSelected;
s.increment_selected_fast();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, 60);
// Check UpperSelected: upper can't go above max
s.upper = s.max;
s.increment_selected_fast();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, s.max);
}
#[test]
fn stateful_slider_decrement_selected_slow() {
let mut s = initialize_slider(SliderState::LowerHover);
s.min = 4;
s.lower = 5;
s.upper = 50;
// Check LowerHover: no decrement
s.decrement_selected_slow();
assert_eq!(s.lower, 5);
// Check LowerSelected: lower decrements
s.state = SliderState::LowerSelected;
s.decrement_selected_slow();
assert_eq!(s.lower, 4);
// Check LowerSelected: lower can't go below min
s.lower = s.min;
s.decrement_selected_slow();
assert_eq!(s.lower, s.min);
// Check LowerSelected: lower can't go below 0
s.min = 0;
s.lower = 0;
s.decrement_selected_slow();
assert_eq!(s.lower, 0);
// Check UpperHover: no decrement
s.lower = 5;
s.upper = 50;
s.state = SliderState::UpperHover;
s.decrement_selected_slow();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, 50);
// Check UpperSelected: upper decrements
s.state = SliderState::UpperSelected;
s.decrement_selected_slow();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, 49);
// Check UpperSelected: upper can't go below lower
s.upper = s.lower;
s.decrement_selected_slow();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, s.lower);
}
#[test]
fn stateful_slider_decrement_selected_fast() {
let mut s = initialize_slider(SliderState::LowerHover);
s.min = 20;
s.lower = 35;
s.upper = 50;
// Check LowerHover: no decrement
s.decrement_selected_fast();
assert_eq!(s.lower, 35);
// Check LowerSelected: lower decrements
s.state = SliderState::LowerSelected;
s.decrement_selected_fast();
assert_eq!(s.lower, 25);
// Check LowerSelected: lower can't go below min
s.lower = s.min;
s.decrement_selected_fast();
assert_eq!(s.lower, s.min);
// Check LowerSelected: lower can't go below 0
s.min = 0;
s.lower = 0;
s.decrement_selected_fast();
assert_eq!(s.lower, 0);
// Check UpperHover: no decrement
s.lower = 5;
s.upper = 50;
s.state = SliderState::UpperHover;
s.decrement_selected_fast();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, 50);
// Check UpperSelected: upper decrements
s.state = SliderState::UpperSelected;
s.decrement_selected_fast();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, 40);
// Check UpperSelected: upper can't go below lower
s.upper = s.lower;
s.decrement_selected_fast();
assert_eq!(s.lower, 5);
assert_eq!(s.upper, s.lower);
}
#[test]
fn stateful_slider_select_deselect() {
let mut s = initialize_slider(SliderState::LowerHover);
assert_eq!(s.state, SliderState::LowerHover);
s.select_deselect();
assert_eq!(s.state, SliderState::LowerSelected);
s.select_deselect();
assert_eq!(s.state, SliderState::LowerHover);
s.state = SliderState::UpperHover;
s.select_deselect();
assert_eq!(s.state, SliderState::UpperSelected);
s.select_deselect();
assert_eq!(s.state, SliderState::UpperHover);
s.state = SliderState::None;
s.select_deselect();
assert_eq!(s.state, SliderState::None);
}
#[test]
fn stateful_slider_deselect() {
let mut s = initialize_slider(SliderState::LowerHover);
s.deselect();
assert_eq!(s.state, SliderState::LowerHover);
s.state = SliderState::LowerSelected;
s.deselect();
assert_eq!(s.state, SliderState::LowerHover);
s.state = SliderState::UpperHover;
s.deselect();
assert_eq!(s.state, SliderState::UpperHover);
s.state = SliderState::UpperSelected;
s.deselect();
assert_eq!(s.state, SliderState::UpperHover);
s.state = SliderState::None;
s.deselect();
assert_eq!(s.state, SliderState::None);
}
#[test]
fn stateful_slider_switch_hover() {
let mut s = initialize_slider(SliderState::LowerHover);
s.switch_hover();
assert_eq!(s.state, SliderState::UpperHover);
s.state = SliderState::LowerSelected;
s.switch_hover();
assert_eq!(s.state, SliderState::LowerSelected);
s.state = SliderState::UpperHover;
s.switch_hover();
assert_eq!(s.state, SliderState::LowerHover);
s.state = SliderState::UpperSelected;
s.switch_hover();
assert_eq!(s.state, SliderState::UpperSelected);
s.state = SliderState::None;
s.switch_hover();
assert_eq!(s.state, SliderState::None);
}
#[test]
fn stateful_slider_is_handle_selected() {
let mut s = initialize_slider(SliderState::LowerHover);
assert_eq!(s.is_handle_selected(), false);
s.state = SliderState::LowerSelected;
assert_eq!(s.is_handle_selected(), true);
s.state = SliderState::UpperHover;
assert_eq!(s.is_handle_selected(), false);
s.state = SliderState::UpperSelected;
assert_eq!(s.is_handle_selected(), true);
s.state = SliderState::None;
assert_eq!(s.is_handle_selected(), false);
}
#[test]
fn stateful_slider_serialize() {
let mut s = initialize_slider(SliderState::LowerHover);
let s_json = serde_json::to_string(&s).unwrap();
assert_eq!(&s_json, "[0,150]");
s.lower = 25;
s.upper = 75;
let s_json = serde_json::to_string(&s).unwrap();
assert_eq!(&s_json, "[25,75]");
}