diff --git a/src/algos/fzf/mod.rs b/src/algos/fzf/mod.rs index 9a0bc01..5d5a74f 100644 --- a/src/algos/fzf/mod.rs +++ b/src/algos/fzf/mod.rs @@ -15,7 +15,8 @@ pub use distance::FzfDistance; pub use parser::FzfParser; pub use query::FzfQuery; pub use scheme::FzfScheme; -use scheme::Scheme; +#[doc(hidden)] +pub use scheme::Scheme; use scoring::*; #[cfg(feature = "fzf-v1")] pub use v1::FzfV1; diff --git a/src/algos/fzf/v1.rs b/src/algos/fzf/v1.rs index a43d8b3..b16ed17 100644 --- a/src/algos/fzf/v1.rs +++ b/src/algos/fzf/v1.rs @@ -24,6 +24,12 @@ impl FzfV1 { Self::default() } + /// TODO: docs + #[cfg(feature = "tests")] + pub fn scheme(&self) -> &Scheme { + &self.scheme + } + /// TODO: docs #[inline] pub fn with_case_sensitivity( diff --git a/tests/fzf_common.rs b/tests/fzf_common.rs index f1204b2..4e17a06 100644 --- a/tests/fzf_common.rs +++ b/tests/fzf_common.rs @@ -5,22 +5,43 @@ use norm::CaseSensitivity; use CaseSensitivity::*; pub fn empty_query() { - assert!(fzf::(Insensitive, "", "foo").is_none()); + let (_, m) = fzf::(Insensitive, "", "foo"); + assert!(m.is_none()); } pub fn upstream_1() { - let m = fzf::(Insensitive, "oBZ", "fooBarbaz").unwrap(); + let (_, m) = fzf::(Insensitive, "oBZ", "fooBarbaz1"); + + let m = m.unwrap(); assert_eq!( m.distance().into_score(), - bonus::MATCH * 3 + bonus::CAMEL_123 + 3 * bonus::MATCH + bonus::CAMEL_123 - penalty::GAP_START - - penalty::GAP_EXTENSION * 3 + - 3 * penalty::GAP_EXTENSION ); assert_eq!(m.matched_ranges().sorted(), [2..4, 8..9]); } +pub fn upstream_2() { + let (fzf, m) = fzf::(Insensitive, "fbb", "foo bar baz"); + + let m = m.unwrap(); + + assert_eq!( + m.distance().into_score(), + 3 * bonus::MATCH + + bonus::FIRST_QUERY_CHAR_MULTIPLIER + * fzf.scheme().bonus_boundary_white + + 2 * fzf.scheme().bonus_boundary_white + - 2 * penalty::GAP_START * 2 + - 4 * penalty::GAP_EXTENSION + ); + + assert_eq!(m.matched_ranges().sorted(), [0..1, 4..5, 8..9]); +} + pub use utils::*; mod utils { @@ -41,6 +62,7 @@ mod utils { sorted } } + pub trait Fzf: Default + for<'a> Metric = FzfQuery<'a>, Distance = FzfDistance> @@ -51,6 +73,8 @@ mod utils { ) -> Self; fn with_matched_ranges(self, matched_ranges: bool) -> Self; + + fn scheme(&self) -> &norm::fzf::Scheme; } impl Fzf for FzfV1 { @@ -64,6 +88,18 @@ mod utils { fn with_matched_ranges(self, matched_ranges: bool) -> Self { self.with_matched_ranges(matched_ranges) } + + fn scheme(&self) -> &norm::fzf::Scheme { + #[cfg(feature = "tests")] + { + self.scheme() + } + + #[cfg(not(feature = "tests"))] + { + unreachable!() + } + } } impl Fzf for FzfV2 { @@ -77,19 +113,33 @@ mod utils { fn with_matched_ranges(self, matched_ranges: bool) -> Self { self.with_matched_ranges(matched_ranges) } + + fn scheme(&self) -> &norm::fzf::Scheme { + #[cfg(feature = "tests")] + { + self.scheme() + } + + #[cfg(not(feature = "tests"))] + { + unreachable!() + } + } } pub(super) fn fzf( case_sensitivity: CaseSensitivity, query: &str, candidate: &str, - ) -> Option> { + ) -> (F, Option>) { let mut fzf = F::default() .with_case_sensitivity(case_sensitivity) .with_matched_ranges(true); let mut parser = FzfParser::new(); - fzf.distance(parser.parse(query), candidate) + let m = fzf.distance(parser.parse(query), candidate); + + (fzf, m) } } diff --git a/tests/fzf_v1.rs b/tests/fzf_v1.rs index 097d7c5..00cae66 100644 --- a/tests/fzf_v1.rs +++ b/tests/fzf_v1.rs @@ -14,3 +14,8 @@ fn fzf_v1_empty_query() { fn fzf_v1_upstream_1() { common::upstream_1::(); } + +#[test] +fn fzf_v1_upstream_2() { + common::upstream_2::(); +} diff --git a/tests/fzf_v2.rs b/tests/fzf_v2.rs index c4f7712..1a59560 100644 --- a/tests/fzf_v2.rs +++ b/tests/fzf_v2.rs @@ -17,6 +17,11 @@ fn fzf_v2_upstream_1() { common::upstream_1::(); } +#[test] +fn fzf_v1_upstream_2() { + common::upstream_2::(); +} + #[test] fn fzf_v2_score_1() { let mut fzf = FzfV2::new()