use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; #[fixture] fn matrix() -> csvbinmatrix::prelude::CSVBinaryMatrix { use csvbinmatrix::prelude::CSVBinaryMatrix; CSVBinaryMatrix::try_from(&[[0, 0, 0], [0, 0, 1], [0, 1, 1], [1, 1, 1]]).unwrap() } // TODO use simpler filename for without_consuming and with_consuming // Be aware that these functions run in // #[rstest] fn without_consuming(matrix: csvbinmatrix::prelude::CSVBinaryMatrix) { // Without consuming the matrix match matrix.to_file("mymatrix_without_consuming.csvbm") { Ok(_) => println!("[INFO] File created"), Err(e) => println!("[ERROR] creating file fails: {e}"), } std::fs::remove_file("mymatrix_without_consuming.csvbm").unwrap_or(()); // You can still use `matrix` assert_eq!(matrix.number_of_ones(), 6); } #[rstest] fn with_consuming(matrix: csvbinmatrix::prelude::CSVBinaryMatrix) { use csvbinmatrix::prelude::CSVBinaryMatrix; let expected_matrix_from_file = matrix.clone(); match matrix.into_file("mymatrix_with_consuming.csvbm") { Ok(_) => println!("[INFO] File created"), Err(e) => println!("[ERROR] creating file fails: {e}"), } // `matrix` is consumed so you cannot use it anymore. let matrix_from_file = match CSVBinaryMatrix::try_from_file("mymatrix_with_consuming.csvbm") { Ok(m) => m, Err(e) => panic!("[ERROR] reading file fails: {e}"), }; assert_eq!(matrix_from_file, expected_matrix_from_file); std::fs::remove_file("mymatrix_with_consuming.csvbm").unwrap_or(()); }