diff options
Diffstat (limited to 'src/output/sorting.rs')
-rw-r--r-- | src/output/sorting.rs | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/src/output/sorting.rs b/src/output/sorting.rs index ef00123..f46a95c 100644 --- a/src/output/sorting.rs +++ b/src/output/sorting.rs @@ -126,10 +126,14 @@ impl Sorting { #[cfg(test)] mod tests { + use super::super::FightOutcome; use super::*; + use chrono::prelude::*; + use evtclib::Boss as B; + #[test] - fn parse_component() { + fn test_parse_component() { assert_eq!("date".parse(), Ok(Component::Date)); assert_eq!("boss".parse(), Ok(Component::Boss)); assert_eq!("outcome".parse(), Ok(Component::Outcome)); @@ -144,7 +148,7 @@ mod tests { } #[test] - fn parse_sorting() { + fn test_parse_sorting() { use Component::*; assert_eq!("date".parse(), Ok(Sorting::new(vec![Date]))); assert_eq!("date,boss".parse(), Ok(Sorting::new(vec![Date, Boss]))); @@ -154,4 +158,72 @@ mod tests { ); assert_eq!("".parse(), Ok(Sorting::default())); } + + #[test] + fn test_sorting_cmp() { + use Component::*; + + let logs: &[&LogResult] = &[ + &LogResult { + log_file: "".into(), + time: Utc.ymd(2020, 4, 3).and_hms(12, 0, 0), + boss: Some(B::Dhuum), + players: vec![], + outcome: FightOutcome::Success, + is_cm: false, + }, + &LogResult { + log_file: "".into(), + time: Utc.ymd(2020, 4, 3).and_hms(13, 0, 0), + boss: Some(B::Dhuum), + players: vec![], + outcome: FightOutcome::Success, + is_cm: false, + }, + &LogResult { + log_file: "".into(), + time: Utc.ymd(2020, 4, 3).and_hms(11, 0, 0), + boss: Some(B::Dhuum), + players: vec![], + outcome: FightOutcome::Success, + is_cm: false, + }, + &LogResult { + log_file: "".into(), + time: Utc.ymd(2020, 4, 3).and_hms(11, 0, 0), + boss: Some(B::Qadim), + players: vec![], + outcome: FightOutcome::Success, + is_cm: false, + }, + &LogResult { + log_file: "".into(), + time: Utc.ymd(2020, 4, 3).and_hms(11, 0, 0), + boss: Some(B::Dhuum), + players: vec![], + outcome: FightOutcome::Success, + is_cm: false, + }, + ]; + + let sortings: &[(&[Component], &[&LogResult])] = &[ + (&[Date], &[logs[2], logs[3], logs[4], logs[0], logs[1]]), + ( + &[Reverse(Box::new(Date))], + &[logs[1], logs[0], logs[2], logs[3], logs[4]], + ), + (&[Boss], &[logs[0], logs[1], logs[2], logs[4], logs[3]]), + ( + &[Boss, Date], + &[logs[2], logs[4], logs[0], logs[1], logs[3]], + ), + ]; + + for (sorting, expected) in sortings { + let mut data = logs.to_vec(); + let sorting = Sorting::new(sorting.to_vec()); + data.sort_by(|a, b| sorting.cmp(a, b)); + assert_eq!(&data, expected, "Sorting with {:?} failed", sorting); + } + } } |