From e707f1034dcb30d366e2de3e82248696cdc23ade Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Mon, 12 Feb 2024 23:22:44 -0800 Subject: [PATCH] Fix fast lane delimiter detection --- src/fast_lane.rs | 16 +++++++++++++++- tests/cli.rs | 12 ++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/fast_lane.rs b/src/fast_lane.rs index fb53939..db6b5c6 100644 --- a/src/fast_lane.rs +++ b/src/fast_lane.rs @@ -162,7 +162,7 @@ impl TryFrom<&Opt> for FastOpt { type Error = &'static str; fn try_from(value: &Opt) -> Result { - if !value.delimiter.as_bytes().len() == 1 { + if value.delimiter.as_bytes().len() != 1 { return Err("Delimiter must be 1 byte wide for FastOpt"); } @@ -307,6 +307,20 @@ mod tests { assert_eq!(output, b"foo\n".as_slice()); } + #[test] + fn fail_to_convert_opt_with_long_delimiter_to_fastopt() { + let opt = Opt { + delimiter: "foo".to_owned(), + ..Default::default() + }; + + assert!(FastOpt::try_from(&opt).is_err()); + assert_eq!( + FastOpt::try_from(&opt).unwrap_err(), + "Delimiter must be 1 byte wide for FastOpt" + ); + } + fn make_cut_str_buffers() -> (Vec, Vec>) { let output = Vec::new(); let fields = Vec::new(); diff --git a/tests/cli.rs b/tests/cli.rs index e77b672..5c58fd1 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -42,6 +42,18 @@ fn it_cut_consecutive_delimiters() { assert.success().stdout("foobar\n"); } +#[test] +fn it_cut_using_multibyte_delimiters() { + let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + + let assert = cmd + .args(["-d", "...", "-f", "2"]) + .write_stdin("foo...bar") + .assert(); + + assert.success().stdout("bar\n"); +} + #[test] fn it_works_on_multiple_lines() { let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();