use super::DebugSessions;
#[test]
fn a_replace_clears_the_set_while_a_push_joins_it() {
let mut set: DebugSessions<&str> = DebugSessions::default();
assert!(set.is_empty(), "nothing is being debugged to start with");
set.replace_with("server", "s1");
set.push("client", "c1");
assert_eq!(set.len(), 2, "the member joined rather than replacing");
assert_eq!(set.names(), vec!["server", "client"], "in launch order");
set.replace_with("alone", "a1");
assert_eq!(set.names(), vec!["alone"]);
}
#[test]
fn focus_always_names_a_session_that_exists() {
let mut set: DebugSessions<&str> = DebugSessions::default();
assert_eq!(set.focused(), None, "an empty set focuses nothing");
assert_eq!(set.focused_name(), None);
set.replace_with("first", "f");
set.push("second", "s");
assert_eq!(
set.focused_name(),
Some("second"),
"a push focuses what it just started"
);
assert!(set.focus(0), "an existing index is focusable");
assert_eq!(set.focused(), Some(&"f"));
assert!(
!set.focus(9),
"a missing one is refused rather than dangling"
);
assert_eq!(
set.focused_name(),
Some("first"),
"and the refusal leaves the previous focus untouched"
);
set.clear();
assert!(set.is_empty() && set.focused().is_none());
set.replace_with("fresh", "x");
assert_eq!(set.focused_name(), Some("fresh"));
}
#[test]
fn removing_one_session_leaves_the_others_and_keeps_the_focus_valid() {
let mut set: DebugSessions<&str> = DebugSessions::default();
set.replace_with("server", "s");
set.push("client", "c");
set.push("worker", "w");
set.focus(2);
assert_eq!(set.remove(2).map(|s| s.name), Some(String::from("worker")));
assert_eq!(set.names(), vec!["server", "client"]);
assert_eq!(set.focused_name(), Some("client"));
set.focus(1);
assert_eq!(set.remove(0).map(|s| s.name), Some(String::from("server")));
assert_eq!(set.focused_name(), Some("client"), "still the same session");
assert!(set.remove(5).is_none(), "a missing index removes nothing");
let mut three: DebugSessions<&str> = DebugSessions::default();
three.replace_with("a", "1");
three.push("b", "2");
three.push("c", "3");
three.focus(1);
assert_eq!(three.remove(1).map(|s| s.name), Some(String::from("b")));
assert_eq!(three.names(), vec!["a", "c"]);
assert_eq!(
three.focused_name(),
Some("c"),
"the next live session, not the first"
);
assert_eq!(set.len(), 1);
set.remove(0);
assert!(set.is_empty() && set.focused().is_none());
}
#[test]
fn every_session_is_reachable_with_its_index() {
let mut set: DebugSessions<u32> = DebugSessions::default();
set.replace_with("a", 10);
set.push("b", 20);
set.push("c", 30);
let seen: Vec<(usize, u32)> = set
.iter_named_mut_indexed()
.map(|(i, s)| (i, s.session))
.collect();
assert_eq!(
seen,
vec![(0, 10), (1, 20), (2, 30)],
"all three with their positions - a poll that visited only the \
focused one would leave the rest at the gate, started and dead"
);
}
#[test]
fn every_session_is_reachable_for_a_stop() {
let mut set: DebugSessions<u32> = DebugSessions::default();
set.replace_with("a", 1);
set.push("b", 2);
set.push("c", 3);
set.focus(1);
let seen: Vec<u32> = set.iter_mut().map(|s| *s).collect();
assert_eq!(
seen,
vec![1, 2, 3],
"all three, not just the focused one - a stop that misses a \
session orphans a live process"
);
}