From d7978658d4025477072f673dfaa1c26548c74186 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 10 Apr 2021 19:13:48 -0500 Subject: [PATCH 1/2] test(cli): Ensure we apply corrections --- crates/typos/src/check.rs | 1 - src/file.rs | 64 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/crates/typos/src/check.rs b/crates/typos/src/check.rs index 00d966b..cfe8372 100644 --- a/crates/typos/src/check.rs +++ b/crates/typos/src/check.rs @@ -64,7 +64,6 @@ fn process_word<'w, 's: 'w>( /// An invalid term found in the buffer. #[derive(Clone, Debug)] -#[non_exhaustive] pub struct Typo<'m> { pub byte_offset: usize, pub typo: Cow<'m, str>, diff --git a/src/file.rs b/src/file.rs index 549109c..e4a0baf 100644 --- a/src/file.rs +++ b/src/file.rs @@ -609,3 +609,67 @@ fn walk_entry( Ok(()) } + +#[cfg(test)] +mod test { + use super::*; + + fn fix_simple(line: &str, corrections: Vec<(usize, &'static str, &'static str)>) -> String { + let line = line.as_bytes().to_vec(); + let corrections: Vec<_> = corrections + .into_iter() + .map(|(byte_offset, typo, correction)| typos::Typo { + byte_offset, + typo: typo.into(), + corrections: typos::Status::Corrections(vec![correction.into()]), + }) + .collect(); + let actual = fix_buffer(line, corrections.into_iter()); + String::from_utf8(actual).unwrap() + } + + #[test] + fn test_fix_buffer_single() { + let actual = fix_simple("foo foo foo", vec![(4, "foo", "bar")]); + assert_eq!(actual, "foo bar foo"); + } + + #[test] + fn test_fix_buffer_single_grow() { + let actual = fix_simple("foo foo foo", vec![(4, "foo", "happy")]); + assert_eq!(actual, "foo happy foo"); + } + + #[test] + fn test_fix_buffer_single_shrink() { + let actual = fix_simple("foo foo foo", vec![(4, "foo", "if")]); + assert_eq!(actual, "foo if foo"); + } + + #[test] + fn test_fix_buffer_start() { + let actual = fix_simple("foo foo foo", vec![(0, "foo", "bar")]); + assert_eq!(actual, "bar foo foo"); + } + + #[test] + fn test_fix_buffer_end() { + let actual = fix_simple("foo foo foo", vec![(8, "foo", "bar")]); + assert_eq!(actual, "foo foo bar"); + } + + #[test] + fn test_fix_buffer_end_grow() { + let actual = fix_simple("foo foo foo", vec![(8, "foo", "happy")]); + assert_eq!(actual, "foo foo happy"); + } + + #[test] + fn test_fix_buffer_multiple() { + let actual = fix_simple( + "foo foo foo", + vec![(4, "foo", "happy"), (8, "foo", "world")], + ); + assert_eq!(actual, "foo happy world"); + } +} From f36bdc895f560fcabc423978a02552d37d506438 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 10 Apr 2021 20:52:34 -0500 Subject: [PATCH 2/2] test(cli): Cover extract_line --- src/file.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/file.rs b/src/file.rs index e4a0baf..6796de8 100644 --- a/src/file.rs +++ b/src/file.rs @@ -672,4 +672,39 @@ mod test { ); assert_eq!(actual, "foo happy world"); } + + #[test] + fn test_extract_line_single_line() { + let (line, offset) = extract_line(b"hello world", 6); + assert_eq!(line, b"hello world"); + assert_eq!(offset, 6); + } + + #[test] + fn test_extract_line_first() { + let (line, offset) = extract_line(b"1\n2\n3", 0); + assert_eq!(line, b"1"); + assert_eq!(offset, 0); + } + + #[test] + fn test_extract_line_middle() { + let (line, offset) = extract_line(b"1\n2\n3", 2); + assert_eq!(line, b"2"); + assert_eq!(offset, 0); + } + + #[test] + fn test_extract_line_end() { + let (line, offset) = extract_line(b"1\n2\n3", 4); + assert_eq!(line, b"3"); + assert_eq!(offset, 0); + } + + #[test] + fn test_extract_line_offset_change() { + let (line, offset) = extract_line(b"1\nhello world\n2", 8); + assert_eq!(line, b"hello world"); + assert_eq!(offset, 6); + } }