diff --git a/src/algos/fzf/fzf.rs b/src/algos/fzf/fzf.rs index dc7afe4..273679f 100644 --- a/src/algos/fzf/fzf.rs +++ b/src/algos/fzf/fzf.rs @@ -588,104 +588,109 @@ pub(super) fn compute_score( score } -// #[cfg(test)] -// mod tests { -// #![allow(clippy::single_range_in_vec_init)] -// -// use super::*; -// -// #[test] -// fn equal_match_1() { -// let pattern = -// Pattern::parse("^AbC$".chars().collect::>().leak()); -// -// let mut ranges_buf = MatchedRanges::default(); -// -// assert!(exact_match( -// pattern, -// "ABC", -// todo!(), -// &Scheme::default(), -// Some(&mut ranges_buf) -// ) -// .is_none()); -// -// { -// ranges_buf = MatchedRanges::default(); -// -// assert!(exact_match( -// pattern, -// "AbC", -// todo!(), -// &Scheme::default(), -// Some(&mut ranges_buf) -// ) -// .is_some()); -// -// assert_eq!(ranges_buf.as_slice(), [0..3]); -// } -// -// { -// ranges_buf = MatchedRanges::default(); -// -// assert!(exact_match( -// pattern, -// "AbC ", -// todo!(), -// &Scheme::default(), -// Some(&mut ranges_buf) -// ) -// .is_some()); -// -// assert_eq!(ranges_buf.as_slice(), [0..3]); -// } -// -// { -// ranges_buf = MatchedRanges::default(); -// -// assert!(exact_match( -// pattern, -// " AbC ", -// todo!(), -// &Scheme::default(), -// Some(&mut ranges_buf) -// ) -// .is_some()); -// -// assert_eq!(ranges_buf.as_slice(), [1..4]); -// } -// -// { -// ranges_buf = MatchedRanges::default(); -// -// assert!(exact_match( -// pattern, -// " AbC", -// todo!(), -// &Scheme::default(), -// Some(&mut ranges_buf) -// ) -// .is_some()); -// -// assert_eq!(ranges_buf.as_slice(), [2..5]); -// } -// } -// -// #[test] -// fn exact_match_1() { -// let pattern = Pattern::parse("abc".chars().collect::>().leak()); -// -// let mut ranges_buf = MatchedRanges::default(); -// -// assert!(exact_match( -// pattern, -// "aabbcc abc", -// todo!(), -// &Scheme::default(), -// Some(&mut ranges_buf) -// ) -// .is_some()); -// -// assert_eq!(ranges_buf.as_slice(), [7..10]); -// } -// } +#[cfg(test)] +mod tests { + #![allow(clippy::single_range_in_vec_init)] + + use super::*; + + fn candidate(s: &str) -> Candidate { + assert!(s.is_ascii()); + Candidate::Ascii(s.as_bytes()) + } + + #[test] + fn equal_match_1() { + let pattern = + Pattern::parse("^AbC$".chars().collect::>().leak()); + + let mut ranges_buf = MatchedRanges::default(); + + assert!(exact_match::( + pattern, + candidate("ABC"), + utils::char_eq(false, false), + &Scheme::default(), + &mut ranges_buf + ) + .is_none()); + + { + ranges_buf = MatchedRanges::default(); + + assert!(exact_match::( + pattern, + candidate("AbC"), + utils::char_eq(false, false), + &Scheme::default(), + &mut ranges_buf + ) + .is_some()); + + assert_eq!(ranges_buf.as_slice(), [0..3]); + } + + { + ranges_buf = MatchedRanges::default(); + + assert!(exact_match::( + pattern, + candidate("AbC "), + utils::char_eq(false, false), + &Scheme::default(), + &mut ranges_buf + ) + .is_some()); + + assert_eq!(ranges_buf.as_slice(), [0..3]); + } + + { + ranges_buf = MatchedRanges::default(); + + assert!(exact_match::( + pattern, + candidate(" AbC "), + utils::char_eq(false, false), + &Scheme::default(), + &mut ranges_buf + ) + .is_some()); + + assert_eq!(ranges_buf.as_slice(), [1..4]); + } + + { + ranges_buf = MatchedRanges::default(); + + assert!(exact_match::( + pattern, + candidate(" AbC"), + utils::char_eq(false, false), + &Scheme::default(), + &mut ranges_buf + ) + .is_some()); + + assert_eq!(ranges_buf.as_slice(), [2..5]); + } + } + + #[test] + fn exact_match_1() { + let pattern = Pattern::parse("abc".chars().collect::>().leak()); + + let mut ranges_buf = MatchedRanges::default(); + + assert!(exact_match::( + pattern, + candidate("aabbcc abc"), + utils::char_eq(false, false), + &Scheme::default(), + &mut ranges_buf + ) + .is_some()); + + assert_eq!(ranges_buf.as_slice(), [7..10]); + } +} diff --git a/src/algos/fzf/fzf_v1.rs b/src/algos/fzf/fzf_v1.rs index abaa2cc..923edc6 100644 --- a/src/algos/fzf/fzf_v1.rs +++ b/src/algos/fzf/fzf_v1.rs @@ -42,6 +42,12 @@ impl FzfV1 { Self::default() } + /// TODO: docs + #[cfg(feature = "tests")] + pub fn scheme(&self) -> &Scheme { + &self.scheme + } + /// TODO: docs #[inline(always)] pub fn with_case_sensitivity( diff --git a/src/algos/fzf/fzf_v2.rs b/src/algos/fzf/fzf_v2.rs index 1fc2731..dfb50eb 100644 --- a/src/algos/fzf/fzf_v2.rs +++ b/src/algos/fzf/fzf_v2.rs @@ -43,6 +43,12 @@ impl FzfV2 { Self::default() } + /// TODO: docs + #[cfg(feature = "tests")] + pub fn scheme(&self) -> &Scheme { + &self.scheme + } + /// TODO: docs #[inline(always)] pub fn with_case_sensitivity( diff --git a/src/lib.rs b/src/lib.rs index 6364d89..0daf6c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,5 +70,5 @@ mod utils; pub use algos::*; use candidate::{Candidate, CandidateMatches}; pub use case_sensitivity::CaseSensitivity; -use matched_ranges::MatchedRanges; +pub use matched_ranges::MatchedRanges; pub use metric::Metric; diff --git a/tests/fzf_common.rs b/tests/fzf_common.rs index 653e835..a4b2ccf 100644 --- a/tests/fzf_common.rs +++ b/tests/fzf_common.rs @@ -9,9 +9,9 @@ pub fn upstream_empty() { let m = m.unwrap(); - assert_eq!(m.distance().into_score(), 0); + assert_eq!(m.distance.into_score(), 0); - assert!(m.matched_ranges().is_empty()); + assert!(m.matched_ranges.as_slice().is_empty()); } pub fn upstream_fuzzy_1() { @@ -20,13 +20,13 @@ pub fn upstream_fuzzy_1() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + bonus::CAMEL_123 - penalty::GAP_START - 3 * penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [2..4, 8..9]); + assert_eq!(m.matched_ranges.as_slice(), [2..4, 8..9]); } pub fn upstream_fuzzy_2() { @@ -35,7 +35,7 @@ pub fn upstream_fuzzy_2() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * fzf.scheme().bonus_boundary_white @@ -43,7 +43,7 @@ pub fn upstream_fuzzy_2() { - 4 * penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [0..1, 4..5, 8..9]); + assert_eq!(m.matched_ranges.as_slice(), [0..1, 4..5, 8..9]); } pub fn upstream_fuzzy_3() { @@ -52,11 +52,11 @@ pub fn upstream_fuzzy_3() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 4 * bonus::MATCH + 2 * bonus::CONSECUTIVE + bonus::CAMEL_123 ); - assert_eq!(m.matched_ranges().sorted(), [9..13]); + assert_eq!(m.matched_ranges.as_slice(), [9..13]); } pub fn upstream_fuzzy_4() { @@ -65,13 +65,13 @@ pub fn upstream_fuzzy_4() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 4 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 3) * fzf.scheme().bonus_boundary_delimiter ); - assert_eq!(m.matched_ranges().sorted(), [6..10]); + assert_eq!(m.matched_ranges.as_slice(), [6..10]); } pub fn upstream_fuzzy_5() { @@ -80,14 +80,14 @@ pub fn upstream_fuzzy_5() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 4 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * bonus::BOUNDARY + fzf.scheme().bonus_boundary_delimiter - penalty::GAP_START ); - assert_eq!(m.matched_ranges().sorted(), [8..11, 12..13]); + assert_eq!(m.matched_ranges.as_slice(), [8..11, 12..13]); } pub fn upstream_fuzzy_6() { @@ -96,13 +96,13 @@ pub fn upstream_fuzzy_6() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 5 * bonus::MATCH + 3 * bonus::CONSECUTIVE - penalty::GAP_START - penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [3..6, 8..10]); + assert_eq!(m.matched_ranges.as_slice(), [3..6, 8..10]); } pub fn upstream_fuzzy_7() { @@ -111,7 +111,7 @@ pub fn upstream_fuzzy_7() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 5 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * bonus::CAMEL_123 + bonus::CONSECUTIVE @@ -119,7 +119,7 @@ pub fn upstream_fuzzy_7() { - penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [3..6, 8..10]); + assert_eq!(m.matched_ranges.as_slice(), [3..6, 8..10]); } pub fn upstream_fuzzy_8() { @@ -128,7 +128,7 @@ pub fn upstream_fuzzy_8() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + bonus::FIRST_QUERY_CHAR_MULTIPLIER * fzf.scheme().bonus_boundary_white @@ -137,7 +137,7 @@ pub fn upstream_fuzzy_8() { - 4 * penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [0..1, 4..5, 8..9]); + assert_eq!(m.matched_ranges.as_slice(), [0..1, 4..5, 8..9]); } pub fn upstream_fuzzy_9() { @@ -146,7 +146,7 @@ pub fn upstream_fuzzy_9() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + bonus::FIRST_QUERY_CHAR_MULTIPLIER * fzf.scheme().bonus_boundary_white @@ -155,7 +155,7 @@ pub fn upstream_fuzzy_9() { - 2 * penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [0..1, 3..4, 6..7]); + assert_eq!(m.matched_ranges.as_slice(), [0..1, 3..4, 6..7]); } pub fn upstream_fuzzy_10() { @@ -164,7 +164,7 @@ pub fn upstream_fuzzy_10() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 1) * fzf.scheme().bonus_boundary_white @@ -172,7 +172,7 @@ pub fn upstream_fuzzy_10() { - 3 * penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [0..1, 4..5, 7..8]); + assert_eq!(m.matched_ranges.as_slice(), [0..1, 4..5, 7..8]); } pub fn upstream_fuzzy_11() { @@ -181,13 +181,13 @@ pub fn upstream_fuzzy_11() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 4 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 3) * fzf.scheme().bonus_boundary_white ); - assert_eq!(m.matched_ranges().sorted(), [0..4]); + assert_eq!(m.matched_ranges.as_slice(), [0..4]); } pub fn upstream_fuzzy_12() { @@ -196,14 +196,14 @@ pub fn upstream_fuzzy_12() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 5 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * bonus::CAMEL_123 + bonus::NON_WORD + bonus::BOUNDARY ); - assert_eq!(m.matched_ranges().sorted(), [1..6]); + assert_eq!(m.matched_ranges.as_slice(), [1..6]); } pub fn upstream_fuzzy_13() { @@ -212,13 +212,13 @@ pub fn upstream_fuzzy_13() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + bonus::CAMEL_123 - penalty::GAP_START - 3 * penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [2..4, 8..9]); + assert_eq!(m.matched_ranges.as_slice(), [2..4, 8..9]); } pub fn upstream_fuzzy_14() { @@ -227,7 +227,7 @@ pub fn upstream_fuzzy_14() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + bonus::FIRST_QUERY_CHAR_MULTIPLIER * fzf.scheme().bonus_boundary_white @@ -236,7 +236,7 @@ pub fn upstream_fuzzy_14() { - 4 * penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [0..1, 4..5, 8..9]); + assert_eq!(m.matched_ranges.as_slice(), [0..1, 4..5, 8..9]); } pub fn upstream_fuzzy_15() { @@ -245,7 +245,7 @@ pub fn upstream_fuzzy_15() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + bonus::FIRST_QUERY_CHAR_MULTIPLIER * fzf.scheme().bonus_boundary_white @@ -254,7 +254,7 @@ pub fn upstream_fuzzy_15() { - 2 * penalty::GAP_EXTENSION ); - assert_eq!(m.matched_ranges().sorted(), [0..1, 3..4, 6..7]); + assert_eq!(m.matched_ranges.as_slice(), [0..1, 3..4, 6..7]); } pub fn upstream_fuzzy_16() { @@ -263,14 +263,14 @@ pub fn upstream_fuzzy_16() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 4 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * fzf.scheme().bonus_boundary_white + bonus::CAMEL_123.max(fzf.scheme().bonus_boundary_white) ); - assert_eq!(m.matched_ranges().sorted(), [0..4]); + assert_eq!(m.matched_ranges.as_slice(), [0..4]); } pub fn upstream_fuzzy_17() { @@ -279,11 +279,11 @@ pub fn upstream_fuzzy_17() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 4 * bonus::MATCH + 3 * bonus::BOUNDARY ); - assert_eq!(m.matched_ranges().sorted(), [2..6]); + assert_eq!(m.matched_ranges.as_slice(), [2..6]); } pub fn upstream_fuzzy_18() { @@ -317,11 +317,11 @@ pub fn upstream_exact_3() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + bonus::CAMEL_123 + bonus::CONSECUTIVE ); - assert_eq!(m.matched_ranges().sorted(), [2..5]); + assert_eq!(m.matched_ranges.as_slice(), [2..5]); } pub fn upstream_exact_4() { @@ -330,11 +330,11 @@ pub fn upstream_exact_4() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 4 * bonus::MATCH + bonus::CAMEL_123 + 2 * bonus::CONSECUTIVE ); - assert_eq!(m.matched_ranges().sorted(), [9..13]); + assert_eq!(m.matched_ranges.as_slice(), [9..13]); } pub fn upstream_exact_5() { @@ -343,13 +343,13 @@ pub fn upstream_exact_5() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 4 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 3) * fzf.scheme().bonus_boundary_delimiter ); - assert_eq!(m.matched_ranges().sorted(), [6..10]); + assert_eq!(m.matched_ranges.as_slice(), [6..10]); } pub fn upstream_exact_6() { @@ -358,13 +358,13 @@ pub fn upstream_exact_6() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 5 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 3) * bonus::BOUNDARY + fzf.scheme().bonus_boundary_delimiter ); - assert_eq!(m.matched_ranges().sorted(), [8..13]); + assert_eq!(m.matched_ranges.as_slice(), [8..13]); } pub fn upstream_exact_7() { @@ -372,12 +372,9 @@ pub fn upstream_exact_7() { let m = m.unwrap(); - assert_eq!( - m.distance().into_score(), - 2 * bonus::MATCH + bonus::CONSECUTIVE - ); + assert_eq!(m.distance.into_score(), 2 * bonus::MATCH + bonus::CONSECUTIVE); - assert_eq!(m.matched_ranges().sorted(), [1..3]); + assert_eq!(m.matched_ranges.as_slice(), [1..3]); } pub fn upstream_prefix_1() { @@ -396,13 +393,13 @@ pub fn upstream_prefix_3() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * fzf.scheme().bonus_boundary_white ); - assert_eq!(m.matched_ranges().sorted(), [0..3]); + assert_eq!(m.matched_ranges.as_slice(), [0..3]); } pub fn upstream_prefix_4() { @@ -411,13 +408,13 @@ pub fn upstream_prefix_4() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * fzf.scheme().bonus_boundary_white ); - assert_eq!(m.matched_ranges().sorted(), [0..3]); + assert_eq!(m.matched_ranges.as_slice(), [0..3]); } pub fn upstream_prefix_5() { @@ -426,13 +423,13 @@ pub fn upstream_prefix_5() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * fzf.scheme().bonus_boundary_white ); - assert_eq!(m.matched_ranges().sorted(), [0..3]); + assert_eq!(m.matched_ranges.as_slice(), [0..3]); } pub fn upstream_prefix_6() { @@ -441,13 +438,13 @@ pub fn upstream_prefix_6() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * fzf.scheme().bonus_boundary_white ); - assert_eq!(m.matched_ranges().sorted(), [1..4]); + assert_eq!(m.matched_ranges.as_slice(), [1..4]); } pub fn upstream_prefix_7() { @@ -456,13 +453,13 @@ pub fn upstream_prefix_7() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * fzf.scheme().bonus_boundary_white ); - assert_eq!(m.matched_ranges().sorted(), [0..3]); + assert_eq!(m.matched_ranges.as_slice(), [0..3]); } pub fn upstream_prefix_8() { @@ -486,11 +483,11 @@ pub fn upstream_suffix_3() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + 2 * bonus::CONSECUTIVE ); - assert_eq!(m.matched_ranges().sorted(), [6..9]); + assert_eq!(m.matched_ranges.as_slice(), [6..9]); } pub fn upstream_suffix_4() { @@ -499,12 +496,12 @@ pub fn upstream_suffix_4() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + (bonus::FIRST_QUERY_CHAR_MULTIPLIER + 2) * bonus::CAMEL_123 ); - assert_eq!(m.matched_ranges().sorted(), [6..9]); + assert_eq!(m.matched_ranges.as_slice(), [6..9]); } pub fn upstream_suffix_5() { @@ -513,11 +510,11 @@ pub fn upstream_suffix_5() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 3 * bonus::MATCH + 2 * bonus::CONSECUTIVE ); - assert_eq!(m.matched_ranges().sorted(), [6..9]); + assert_eq!(m.matched_ranges.as_slice(), [6..9]); } pub fn upstream_suffix_6() { @@ -526,13 +523,13 @@ pub fn upstream_suffix_6() { let m = m.unwrap(); assert_eq!( - m.distance().into_score(), + m.distance.into_score(), 4 * bonus::MATCH + 2 * bonus::CONSECUTIVE + fzf.scheme().bonus_boundary_white ); - assert_eq!(m.matched_ranges().sorted(), [6..10]); + assert_eq!(m.matched_ranges.as_slice(), [6..10]); } pub use utils::*; @@ -540,8 +537,13 @@ pub use utils::*; mod utils { use core::ops::Range; + pub struct FzfMatch { + pub distance: FzfDistance, + pub matched_ranges: norm::MatchedRanges, + } + use norm::fzf::{FzfDistance, FzfParser, FzfQuery, FzfV1, FzfV2}; - use norm::{CaseSensitivity, Match, Metric}; + use norm::{CaseSensitivity, Metric}; /// TODO: docs pub trait SortedRanges { @@ -624,15 +626,23 @@ mod utils { case_sensitivity: CaseSensitivity, query: &str, candidate: &str, - ) -> (F, Option>) { + ) -> (F, Option) { let mut fzf = F::default(); fzf.with_case_sensitivity(case_sensitivity).with_matched_ranges(true); let mut parser = FzfParser::new(); - let m = fzf.distance(parser.parse(query), candidate); + let mut ranges = norm::MatchedRanges::default(); + + let Some(distance) = fzf.distance_and_ranges( + parser.parse(query), + candidate, + &mut ranges, + ) else { + return (fzf, None); + }; - (fzf, m) + (fzf, Some(FzfMatch { distance, matched_ranges: ranges })) } } diff --git a/tests/fzf_v1.rs b/tests/fzf_v1.rs index ebfde0b..0ed5311 100644 --- a/tests/fzf_v1.rs +++ b/tests/fzf_v1.rs @@ -222,13 +222,15 @@ fn fzf_v1_score_1() { let mut parser = FzfParser::new(); - let mach = fzf + let mut ranges = norm::MatchedRanges::default(); + + let _ = fzf .with_case_sensitivity(CaseSensitivity::Sensitive) .with_matched_ranges(true) - .distance(parser.parse("ZZ"), "ӥZZZ") + .distance_and_ranges(parser.parse("ZZ"), "ӥZZZ", &mut ranges) .unwrap(); - assert_eq!(mach.matched_ranges(), [2..4]); + assert_eq!(ranges.as_slice(), [2..4]); } #[test] @@ -269,17 +271,19 @@ fn fzf_v1_score_4() { let mut parser = FzfParser::new(); + let mut ranges = norm::MatchedRanges::default(); + let query = parser.parse("z\n"); let candidate = "ZZ\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\u{65e}\nZ\u{65e}"; - let mach = fzf + let _ = fzf .with_case_sensitivity(CaseSensitivity::Insensitive) .with_matched_ranges(true) - .distance(query, candidate) + .distance_and_ranges(query, candidate, &mut ranges) .unwrap(); - assert_eq!(mach.matched_ranges(), [1..2, 21..22]); + assert_eq!(ranges.as_slice(), [1..2, 21..22]); } #[test] @@ -288,12 +292,18 @@ fn fzf_v1_score_5() { let mut parser = FzfParser::new(); - let mach = fzf + let mut ranges = norm::MatchedRanges::default(); + + let _ = fzf .with_case_sensitivity(CaseSensitivity::Sensitive) .with_matched_ranges(true) .with_normalization(true) - .distance(parser.parse("e !"), " !I\\hh+\u{364}") + .distance_and_ranges( + parser.parse("e !"), + " !I\\hh+\u{364}", + &mut ranges, + ) .unwrap(); - assert_eq!(mach.matched_ranges(), [1..2, 7..9]); + assert_eq!(ranges.as_slice(), [1..2, 7..9]); } diff --git a/tests/fzf_v2.rs b/tests/fzf_v2.rs index 7074eb0..28f2a22 100644 --- a/tests/fzf_v2.rs +++ b/tests/fzf_v2.rs @@ -2,7 +2,6 @@ mod fzf_common; -use common::SortedRanges; use fzf_common as common; use norm::fzf::{bonus, FzfParser, FzfV2}; use norm::{CaseSensitivity, Metric}; @@ -223,21 +222,23 @@ fn fzf_v2_score_1() { let mut parser = FzfParser::new(); - let mach = fzf + let mut ranges = norm::MatchedRanges::default(); + + let distance = fzf .with_case_sensitivity(CaseSensitivity::Sensitive) .with_matched_ranges(true) - .distance(parser.parse("jelly"), "jellyfish") + .distance_and_ranges(parser.parse("jelly"), "jellyfish", &mut ranges) .unwrap(); assert_eq!( - mach.distance().into_score(), + distance.into_score(), bonus::MATCH * 5 + fzf.scheme().bonus_boundary_white * bonus::FIRST_QUERY_CHAR_MULTIPLIER + fzf.scheme().bonus_boundary_white * 4 ); - assert_eq!(mach.matched_ranges().sorted(), [0..5]); + assert_eq!(ranges.as_slice(), [0..5]); } #[test] @@ -246,12 +247,12 @@ fn fzf_v2_score_2() { let mut parser = FzfParser::new(); - let mach = fzf + let distance = fzf .with_case_sensitivity(CaseSensitivity::Sensitive) .with_matched_ranges(true) .distance(parser.parse("!$"), "$$2"); - assert!(mach.is_none()); + assert!(distance.is_none()); } #[test] @@ -260,13 +261,19 @@ fn fzf_v2_score_3() { let mut parser = FzfParser::new(); - let mach = fzf + let mut ranges = norm::MatchedRanges::default(); + + let _ = fzf .with_case_sensitivity(CaseSensitivity::Sensitive) .with_matched_ranges(true) - .distance(parser.parse("\0\0"), "\0#B\0\u{364}\0\0") + .distance_and_ranges( + parser.parse("\0\0"), + "\0#B\0\u{364}\0\0", + &mut ranges, + ) .unwrap(); - assert_eq!(mach.matched_ranges().sorted(), [6..8]); + assert_eq!(ranges.as_slice(), [6..8]); } #[test] @@ -275,14 +282,20 @@ fn fzf_v2_score_4() { let mut parser = FzfParser::new(); - let mach = fzf + let mut ranges = norm::MatchedRanges::default(); + + let _ = fzf .with_case_sensitivity(CaseSensitivity::Sensitive) .with_matched_ranges(true) .with_normalization(true) - .distance(parser.parse("e !"), " !I\\hh+\u{364}") + .distance_and_ranges( + parser.parse("e !"), + " !I\\hh+\u{364}", + &mut ranges, + ) .unwrap(); - assert_eq!(mach.matched_ranges(), [1..2, 7..9]); + assert_eq!(ranges.as_slice(), [1..2, 7..9]); } #[test] @@ -291,14 +304,16 @@ fn fzf_v2_score_5() { let mut parser = FzfParser::new(); - let mach = fzf + let mut ranges = norm::MatchedRanges::default(); + + let _ = fzf .with_case_sensitivity(CaseSensitivity::Insensitive) .with_matched_ranges(true) .with_normalization(true) - .distance(parser.parse("E"), "\u{364}E") + .distance_and_ranges(parser.parse("E"), "\u{364}E", &mut ranges) .unwrap(); - assert_eq!(mach.matched_ranges(), [0..2]); + assert_eq!(ranges.as_slice(), [0..2]); } #[test] @@ -307,13 +322,19 @@ fn fzf_v2_score_6() { let mut parser = FzfParser::new(); + let mut ranges = norm::MatchedRanges::default(); + let query = parser.parse("!2\t\0\0\0WWHHWHWWWWWWWZ !I"); - let mach = fzf + let distance = fzf .with_case_sensitivity(CaseSensitivity::Insensitive) .with_matched_ranges(true) .with_normalization(true) - .distance(query, "\u{6}\0\0 N\u{364}\u{e}\u{365}+"); + .distance_and_ranges( + query, + "\u{6}\0\0 N\u{364}\u{e}\u{365}+", + &mut ranges, + ); - assert!(mach.is_none()); + assert!(distance.is_none()); }