Skip to content

Commit

Permalink
Fix fast lane delimiter detection
Browse files Browse the repository at this point in the history
  • Loading branch information
riquito committed Feb 13, 2024
1 parent 2c6d411 commit e707f10
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/fast_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl TryFrom<&Opt> for FastOpt {
type Error = &'static str;

fn try_from(value: &Opt) -> Result<Self, Self::Error> {
if !value.delimiter.as_bytes().len() == 1 {
if value.delimiter.as_bytes().len() != 1 {
return Err("Delimiter must be 1 byte wide for FastOpt");
}

Expand Down Expand Up @@ -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<u8>, Vec<Range<usize>>) {
let output = Vec::new();
let fields = Vec::new();
Expand Down
12 changes: 12 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e707f10

Please sign in to comment.