diff options
author | Daniel <kingdread@gmx.de> | 2020-05-14 15:55:41 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2020-05-14 15:55:41 +0200 |
commit | 61889e2d289a6d0d83d55afc9b1c98e6dd112249 (patch) | |
tree | f1773453c23a1dfc21823f36fd8246eebe8ec680 | |
parent | a5e326bdd9b8c751653047bc28e34ded3e340431 (diff) | |
download | raidgrep-61889e2d289a6d0d83d55afc9b1c98e6dd112249.tar.gz raidgrep-61889e2d289a6d0d83d55afc9b1c98e6dd112249.tar.bz2 raidgrep-61889e2d289a6d0d83d55afc9b1c98e6dd112249.zip |
add a small test for sorting with Sorting
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/output/sorting.rs | 76 |
2 files changed, 75 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index cb914d3..43a6f59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -107,7 +107,7 @@ pub struct Opt { } /// A log that matches the search criteria. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct LogResult { /// The path to the log file. log_file: PathBuf, 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); + } + } } |