use crate::{
run::Editor,
util::tests_helper::{run_with_splits, run_with_splits_opt, start_run},
Run, Segment, TimeSpan, Timer, TimerPhase, TimingMethod,
};
mod mark_as_modified;
mod variables;
fn run() -> Run {
let mut run = Run::new();
run.push_segment(Segment::new("A"));
run.push_segment(Segment::new("B"));
run.push_segment(Segment::new("C"));
run
}
fn timer() -> Timer {
Timer::new(run()).unwrap()
}
#[test]
fn monotically_increasing_split_times_after_resetting() {
let mut timer = timer();
let (first, second, third) = (
TimeSpan::from_seconds(5.0),
TimeSpan::from_seconds(15.0),
TimeSpan::from_seconds(10.0),
);
run_with_splits(
&mut timer,
&[
first.total_seconds(),
second.total_seconds(),
third.total_seconds(),
],
);
let run = timer.into_run(true);
assert_eq!(
run.segment(0).personal_best_split_time().game_time,
Some(first)
);
assert_eq!(
run.segment(1).personal_best_split_time().game_time,
Some(second)
);
assert_eq!(
run.segment(2).personal_best_split_time().game_time,
Some(second)
);
}
#[test]
fn deleting_best_segment_time_clears_segment_history() {
let mut timer = timer();
let (first, second, third) = (
TimeSpan::from_seconds(5.0),
TimeSpan::from_seconds(10.0),
TimeSpan::from_seconds(15.0),
);
run_with_splits(
&mut timer,
&[
first.total_seconds(),
second.total_seconds(),
third.total_seconds(),
],
);
let run = timer.into_run(true);
let run2 = run.clone();
let mut editor = Editor::new(run).unwrap();
editor.select_timing_method(TimingMethod::GameTime);
editor.select_only(1);
editor.active_segment().set_split_time(None);
assert_eq!(
editor.run().segment(1).best_segment_time().game_time,
Some(second - first)
);
editor.active_segment().set_best_segment_time(None);
assert_eq!(
editor.run().segment(1).segment_history().iter().next(),
None
);
assert_eq!(editor.run().segment(1).best_segment_time().game_time, None);
editor = Editor::new(run2).unwrap();
editor.select_timing_method(TimingMethod::GameTime);
editor.select_only(1);
editor.active_segment().set_best_segment_time(None);
assert_eq!(
editor.run().segment(1).segment_history().iter().next(),
None
);
assert_eq!(
editor.run().segment(1).best_segment_time().game_time,
Some(second - first)
);
}
#[test]
fn modifying_best_segment_time_fixes_segment_history() {
let mut timer = timer();
let (first, second, third) = (
TimeSpan::from_seconds(5.0),
TimeSpan::from_seconds(10.0),
TimeSpan::from_seconds(15.0),
);
run_with_splits(
&mut timer,
&[
first.total_seconds(),
second.total_seconds(),
third.total_seconds(),
],
);
let run = timer.into_run(true);
let run2 = run.clone();
let mut editor = Editor::new(run).unwrap();
editor.select_timing_method(TimingMethod::GameTime);
editor.select_only(1);
editor.active_segment().set_split_time(None);
assert_eq!(
editor.run().segment(1).best_segment_time().game_time,
Some(second - first)
);
let new_best = Some(TimeSpan::from_seconds(7.0));
editor.active_segment().set_best_segment_time(new_best);
assert_eq!(
editor
.run()
.segment(1)
.segment_history()
.iter()
.next()
.and_then(|(_, t)| t.game_time),
new_best
);
assert_eq!(
editor.run().segment(1).best_segment_time().game_time,
new_best
);
editor = Editor::new(run2).unwrap();
editor.select_timing_method(TimingMethod::GameTime);
editor.select_only(1);
editor.active_segment().set_best_segment_time(new_best);
assert_eq!(
editor
.run()
.segment(1)
.segment_history()
.iter()
.next()
.and_then(|(_, t)| t.game_time),
Some(second - first)
);
assert_eq!(
editor.run().segment(1).best_segment_time().game_time,
Some(second - first)
);
}
#[test]
fn import_pb_into_segment_history() {
let mut editor = Editor::new(run()).unwrap();
editor.select_timing_method(TimingMethod::GameTime);
editor.select_only(0);
let fake_first = Some(TimeSpan::from_seconds(5.0));
editor.active_segment().set_split_time(fake_first);
editor.select_only(1);
let fake_second = Some(TimeSpan::from_seconds(10.0));
editor.active_segment().set_split_time(fake_second);
editor.select_only(2);
let fake_third = Some(TimeSpan::from_seconds(15.0));
editor.active_segment().set_split_time(fake_third);
let run = editor.close();
let mut timer = Timer::new(run).unwrap();
let (real_first, real_second, real_third) = (
TimeSpan::from_seconds(4.0),
TimeSpan::from_seconds(9.0),
TimeSpan::from_seconds(13.0),
);
run_with_splits(
&mut timer,
&[
real_first.total_seconds(),
real_second.total_seconds(),
real_third.total_seconds(),
],
);
let run = timer.run();
assert_eq!(
run.segment(0)
.segment_history()
.get(-1)
.and_then(|t| t.game_time),
fake_first
);
assert_eq!(
run.segment(0)
.segment_history()
.get(1)
.and_then(|t| t.game_time),
Some(real_first)
);
assert_eq!(run.segment(1).segment_history().get(-1), None);
assert_eq!(
run.segment(1)
.segment_history()
.get(1)
.and_then(|t| t.game_time),
Some(real_second - real_first)
);
assert_eq!(
run.segment(2)
.segment_history()
.get(-1)
.and_then(|t| t.game_time),
catch! { fake_third? - fake_second? }
);
assert_eq!(
run.segment(2)
.segment_history()
.get(1)
.and_then(|t| t.game_time),
Some(real_third - real_second)
);
}
#[test]
fn import_pb_into_segment_history_and_remove_null_values() {
let mut editor = Editor::new(run()).unwrap();
editor.select_timing_method(TimingMethod::GameTime);
editor.select_only(0);
let fake_first = Some(TimeSpan::from_seconds(5.0));
editor.active_segment().set_split_time(fake_first);
editor.select_only(2);
let fake_third = Some(TimeSpan::from_seconds(15.0));
editor.active_segment().set_split_time(fake_third);
let run = editor.close();
let mut timer = Timer::new(run).unwrap();
let (real_first, real_third) = (TimeSpan::from_seconds(4.0), TimeSpan::from_seconds(14.0));
run_with_splits_opt(
&mut timer,
&[
Some(real_first.total_seconds()),
None,
Some(real_third.total_seconds()),
],
);
let run = timer.run();
assert_eq!(
run.segment(0)
.segment_history()
.get(-1)
.and_then(|t| t.game_time),
fake_first
);
assert_eq!(
run.segment(0)
.segment_history()
.get(1)
.and_then(|t| t.game_time),
Some(real_first)
);
assert_eq!(run.segment(1).segment_history().get(-1), None);
assert_eq!(
run.segment(1).segment_history().get(1).map(|t| t.game_time),
Some(None)
);
assert_eq!(run.segment(2).segment_history().get(-1), None);
assert_eq!(
run.segment(2)
.segment_history()
.get(1)
.and_then(|t| t.game_time),
Some(real_third - real_first)
);
}
#[test]
fn import_best_segment_with_game_time_usage() {
let mut timer = timer();
let first = TimeSpan::from_seconds(5.0);
run_with_splits(&mut timer, &[first.total_seconds()]);
let run = timer.into_run(true);
let mut editor = Editor::new(run).unwrap();
editor.select_timing_method(TimingMethod::GameTime);
editor.select_only(0);
let best = Some(TimeSpan::from_seconds(4.0));
editor.active_segment().set_best_segment_time(best);
editor.insert_segment_above();
let history = editor.run().segment(0).segment_history();
assert_eq!(history.get(0).map(|t| t.game_time), Some(None));
assert_eq!(history.get(1).map(|t| t.game_time), Some(None));
let history = editor.run().segment(1).segment_history();
assert_eq!(history.get(0).and_then(|t| t.game_time), best);
assert_eq!(history.get(1).and_then(|t| t.game_time), Some(first));
}
#[test]
fn clears_run_id_when_pbing() {
let mut timer = timer();
let (first, second, third) = (
TimeSpan::from_seconds(5.0),
TimeSpan::from_seconds(10.0),
TimeSpan::from_seconds(15.0),
);
run_with_splits(
&mut timer,
&[
first.total_seconds(),
second.total_seconds(),
third.total_seconds(),
],
);
let mut run = timer.into_run(true);
assert_eq!(run.metadata().run_id(), "");
run.metadata_mut().set_run_id("34567");
assert_eq!(run.metadata().run_id(), "34567");
let mut timer = Timer::new(run).unwrap();
let (first, second, third) = (
TimeSpan::from_seconds(6.0),
TimeSpan::from_seconds(11.0),
TimeSpan::from_seconds(16.0),
);
run_with_splits(
&mut timer,
&[
first.total_seconds(),
second.total_seconds(),
third.total_seconds(),
],
);
assert_eq!(timer.run().metadata().run_id(), "34567");
let (first, second, third) = (
TimeSpan::from_seconds(4.0),
TimeSpan::from_seconds(9.0),
TimeSpan::from_seconds(14.0),
);
run_with_splits(
&mut timer,
&[
first.total_seconds(),
second.total_seconds(),
third.total_seconds(),
],
);
timer.reset(true);
assert_eq!(timer.run().metadata().run_id(), "");
}
#[test]
fn reset_and_set_attempt_as_pb() {
let mut timer = timer();
assert_eq!(timer.current_phase(), TimerPhase::NotRunning);
timer.reset_and_set_attempt_as_pb();
for segment in timer.run().segments() {
assert_eq!(segment.personal_best_split_time().game_time, None);
}
start_run(&mut timer);
assert_eq!(timer.current_phase(), TimerPhase::Running);
timer.reset_and_set_attempt_as_pb();
assert_eq!(timer.current_phase(), TimerPhase::NotRunning);
for segment in timer.run().segments() {
assert_eq!(segment.personal_best_split_time().game_time, None);
}
start_run(&mut timer);
timer.pause();
assert_eq!(timer.current_phase(), TimerPhase::Paused);
timer.reset_and_set_attempt_as_pb();
assert_eq!(timer.current_phase(), TimerPhase::NotRunning);
for segment in timer.run().segments() {
assert_eq!(segment.personal_best_split_time().game_time, None);
}
start_run(&mut timer);
let first = TimeSpan::from_seconds(1.0);
timer.set_game_time(first);
timer.split();
let second = TimeSpan::from_seconds(2.0);
timer.set_game_time(second);
timer.split();
assert_eq!(timer.current_phase(), TimerPhase::Running);
timer.reset_and_set_attempt_as_pb();
assert_eq!(
timer.run().segment(0).personal_best_split_time().game_time,
Some(first)
);
assert_eq!(
timer.run().segment(1).personal_best_split_time().game_time,
Some(second)
);
assert_eq!(
timer.run().segment(2).personal_best_split_time().game_time,
None
);
start_run(&mut timer);
let first = TimeSpan::from_seconds(4.0);
timer.set_game_time(first);
timer.split();
let second = TimeSpan::from_seconds(5.0);
timer.set_game_time(second);
timer.split();
let third = TimeSpan::from_seconds(6.0);
timer.set_game_time(third);
timer.split();
assert_eq!(timer.current_phase(), TimerPhase::Ended);
timer.reset_and_set_attempt_as_pb();
assert_eq!(
timer.run().segment(0).personal_best_split_time().game_time,
Some(first)
);
assert_eq!(
timer.run().segment(1).personal_best_split_time().game_time,
Some(second)
);
assert_eq!(
timer.run().segment(2).personal_best_split_time().game_time,
Some(third)
);
let old_third = third;
start_run(&mut timer);
let first = TimeSpan::from_seconds(14.0);
timer.set_game_time(first);
timer.split();
let second = TimeSpan::from_seconds(15.0);
timer.set_game_time(second);
timer.split();
let third = TimeSpan::from_seconds(16.0);
timer.set_game_time(third);
timer.split();
assert_eq!(timer.current_phase(), TimerPhase::Ended);
timer.reset_and_set_attempt_as_pb();
assert_eq!(
timer.run().segment(0).personal_best_split_time().game_time,
Some(first)
);
assert_eq!(
timer.run().segment(1).personal_best_split_time().game_time,
Some(second)
);
assert_eq!(
timer.run().segment(2).personal_best_split_time().game_time,
Some(third)
);
let mut attempt_history = timer.run().attempt_history().iter().rev();
let attempt = attempt_history.next().unwrap();
assert_eq!(attempt.time().game_time, Some(third));
assert!(attempt.ended().unwrap().time >= attempt.started().unwrap().time);
let attempt = attempt_history.next().unwrap();
assert_eq!(attempt.time().game_time, Some(old_third));
assert!(attempt.ended().unwrap().time >= attempt.started().unwrap().time);
let attempt = attempt_history.next().unwrap();
assert_eq!(attempt.time().game_time, None);
assert!(attempt.ended().unwrap().time >= attempt.started().unwrap().time);
}