From 6e3ba025105eb0c74dc76d2e27fac5589c6f4d0e Mon Sep 17 00:00:00 2001 From: markxoe Date: Mon, 3 Jun 2024 18:04:14 +0200 Subject: [PATCH] wip: test line count --- src/data/parsers/common.rs | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/data/parsers/common.rs b/src/data/parsers/common.rs index 4140f2e..3f98ddc 100644 --- a/src/data/parsers/common.rs +++ b/src/data/parsers/common.rs @@ -104,6 +104,62 @@ mod test { sync::{Arc, Mutex}, }; + mod line_count { + #[allow(unused_imports)] + use std::{env::temp_dir, io::Write}; + + #[test] + fn empty_file() { + let dir = temp_dir(); + let file_path = dir.join("test1.txt"); + + // create file + { + let _file = std::fs::File::create(&file_path).expect("Unable to create file"); + } + + let count = super::super::get_file_line_count(file_path.to_str().unwrap()); + + assert_eq!(count, 0); + } + + #[test] + fn single_line() { + let dir = temp_dir(); + let file_path = dir.join("test2.txt"); + + // create file + { + let mut file = std::fs::File::create(&file_path).expect("Unable to create file"); + writeln!(file, "test").expect("Unable to write to file"); + } + + let count = super::super::get_file_line_count(file_path.to_str().unwrap()); + + assert_eq!(count, 1); + } + + #[test] + fn multiple_lines() { + let dir = temp_dir(); + let file_path = dir.join("test3.txt"); + + // create file + { + let file = std::fs::File::create(&file_path).expect("Unable to create file"); + let mut file = std::io::BufWriter::new(file); + for _ in 0..1000 { + writeln!(file, "Test").expect("Unable to write to file"); + } + file.flush().expect("Unable to flush file"); + } + + let count = super::super::get_file_line_count(file_path.to_str().unwrap()); + + assert_eq!(count, 1000); + } + } + #[test] fn all_lines_are_read() { let dir = temp_dir();