From e2c6d5e4aa9c093fb216e8d103f4e7182361d650 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sat, 3 Dec 2022 21:50:28 -0600 Subject: test: finish impl test_process_input --- src/lib.rs | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0ef1b4c..ab3002d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,26 +107,14 @@ fn parse_nonprinting_char(mut c: u8) -> Vec { mod test { use super::*; - use std::{ - io::{BufWriter, Write}, - process::{Command, Stdio}, - }; + use std::process::{Command, Stdio}; #[test] - fn test_nonprinting_char() { - let perl_exp = r#"\ - for( my $i=0 ; $i < 256; $i++ ) { - print ( sprintf( "%c is %d %x", $i, $i ,$i ) ); - }"#; - - let perl_stdout = Command::new("perl") - .arg("-e") - .arg(perl_exp) - .output() - .unwrap() - .stdout; - - println!("{:?}", perl_stdout); + fn test_process_input() { + let input = (0..255_u8) + .map(|i| format!("{}", i as char)) + .collect::>() + .join("\n"); let cat_child = Command::new("cat") .arg("-") @@ -136,20 +124,30 @@ mod test { .spawn() .unwrap(); - BufWriter::new(cat_child.stdin.as_ref().unwrap()) - .write_all(&perl_stdout) + cat_child + .stdin + .as_ref() + .unwrap() + .write_all(input.as_bytes()) .unwrap(); - let cat_output = cat_child.wait_with_output().unwrap().stdout; + let cat_output = cat_child.wait_with_output().unwrap(); + let cat_result = String::from_utf8(cat_output.stdout).unwrap(); - let mut catr_output = Vec::new(); let opts = Opts { show_nonprinting: true, ..Default::default() }; - process_input(&*perl_stdout, &mut catr_output, &opts).unwrap(); + let mut writer = Vec::new(); + + process_input(input.as_bytes(), &mut writer, &opts).unwrap(); + + let result = String::from_utf8(writer).unwrap(); - assert_eq!(cat_output, catr_output) + cat_result + .lines() + .zip(result.lines()) + .for_each(|(c_cat, c)| assert_eq!(c_cat, c)); } } -- cgit v1.2.3-70-g09d2