Skip to content

Commit

Permalink
tests: add upstream fzf test
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 27, 2023
1 parent 063f71e commit 9c3efaf
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/algos/fzf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/algos/fzf/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
62 changes: 56 additions & 6 deletions tests/fzf_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,43 @@ use norm::CaseSensitivity;
use CaseSensitivity::*;

pub fn empty_query<F: Fzf>() {
assert!(fzf::<F>(Insensitive, "", "foo").is_none());
let (_, m) = fzf::<F>(Insensitive, "", "foo");
assert!(m.is_none());
}

pub fn upstream_1<F: Fzf>() {
let m = fzf::<F>(Insensitive, "oBZ", "fooBarbaz").unwrap();
let (_, m) = fzf::<F>(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<F: Fzf>() {
let (fzf, m) = fzf::<F>(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 {
Expand All @@ -41,6 +62,7 @@ mod utils {
sorted
}
}

pub trait Fzf:
Default
+ for<'a> Metric<Query<'a> = FzfQuery<'a>, Distance = FzfDistance>
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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<F: Fzf>(
case_sensitivity: CaseSensitivity,
query: &str,
candidate: &str,
) -> Option<Match<FzfDistance>> {
) -> (F, Option<Match<FzfDistance>>) {
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)
}
}
5 changes: 5 additions & 0 deletions tests/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ fn fzf_v1_empty_query() {
fn fzf_v1_upstream_1() {
common::upstream_1::<FzfV1>();
}

#[test]
fn fzf_v1_upstream_2() {
common::upstream_2::<FzfV1>();
}
5 changes: 5 additions & 0 deletions tests/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ fn fzf_v2_upstream_1() {
common::upstream_1::<FzfV2>();
}

#[test]
fn fzf_v1_upstream_2() {
common::upstream_2::<FzfV2>();
}

#[test]
fn fzf_v2_score_1() {
let mut fzf = FzfV2::new()
Expand Down

0 comments on commit 9c3efaf

Please sign in to comment.