From 2da9c33f7c2d13892aba218f3ff9992f5af07492 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Sat, 21 Oct 2023 14:34:08 -0700 Subject: [PATCH] Improve tests to normalize (sort) test case output / expected --- tests/cli.rs | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/tests/cli.rs b/tests/cli.rs index 5aefef5..701e4c8 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,8 +1,47 @@ use assert_cmd::Command; use glob::glob; -use predicates::prelude::*; // Used for writing assertions +use predicates::prelude::*; +use predicates::reflection::PredicateReflection; +// Used for writing assertions use rstest::*; -use std::{error::Error, fs::File, io::Read, path::Path}; +use std::fmt::Display; +use std::{error::Error, fs::File, io::Read, path::Path, str}; + +struct SortedEquals { + expect: Vec, +} + +fn sort_buf(input: &[u8]) -> Vec { + let mut lines = input + .split(|x| *x == b'\n') + .map(|x| Vec::::from(x)) + .collect::>>(); + lines.sort(); + lines.join(&b'\n') +} + +impl SortedEquals { + fn new(expect: &[u8]) -> SortedEquals { + let sorted = sort_buf(expect); + SortedEquals { expect: sorted } + } +} + +impl Display for SortedEquals { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(str::from_utf8(self.expect.as_slice()).unwrap()) + } +} + +impl Predicate<[u8]> for SortedEquals { + fn eval(&self, variable: &[u8]) -> bool { + // sort self into temporary, then compare with variable + let sorted = sort_buf(variable); + sorted == self.expect + } +} + +impl PredicateReflection for SortedEquals {} // Really should normalize the data (lex sort) before comparison #[rstest] @@ -28,7 +67,7 @@ fn dfz_test(#[case] path: &str, #[case] args: &str) -> Result<(), Box assert .success() - .stdout(predicate::eq(expect_data)) + .stdout(SortedEquals::new(&expect_data)) .stderr(predicate::str::is_empty()); Ok(()) @@ -79,7 +118,7 @@ fn multi_input_test(#[case] path: &str, #[case] args: &str) -> Result<(), Box