-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: share code in fzf tests between v1 and v2
- Loading branch information
Showing
4 changed files
with
109 additions
and
62 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#![allow(clippy::single_range_in_vec_init)] | ||
|
||
use norm::fzf::{bonus, penalty}; | ||
use norm::CaseSensitivity; | ||
use CaseSensitivity::*; | ||
|
||
pub fn empty_query<F: Fzf>() { | ||
assert!(fzf::<F>(Insensitive, "", "foo").is_none()); | ||
} | ||
|
||
pub fn upstream_1<F: Fzf>() { | ||
let m = fzf::<F>(Insensitive, "oBZ", "fooBarbaz").unwrap(); | ||
|
||
assert_eq!( | ||
m.distance().into_score(), | ||
bonus::MATCH * 3 + bonus::CAMEL_123 | ||
- penalty::GAP_START | ||
- penalty::GAP_EXTENSION * 3 | ||
); | ||
|
||
assert_eq!(m.matched_ranges().sorted(), [2..4, 8..9]); | ||
} | ||
|
||
pub use utils::*; | ||
|
||
mod utils { | ||
use core::ops::Range; | ||
|
||
use norm::fzf::{FzfDistance, FzfParser, FzfQuery, FzfV1, FzfV2}; | ||
use norm::{CaseSensitivity, Match, Metric}; | ||
|
||
/// TODO: docs | ||
pub trait SortedRanges { | ||
fn sorted(&self) -> Vec<Range<usize>>; | ||
} | ||
|
||
impl SortedRanges for &[Range<usize>] { | ||
fn sorted(&self) -> Vec<Range<usize>> { | ||
let mut sorted = self.to_vec(); | ||
sorted.sort_by_key(|r| r.start); | ||
sorted | ||
} | ||
} | ||
pub trait Fzf: | ||
Default | ||
+ for<'a> Metric<Query<'a> = FzfQuery<'a>, Distance = FzfDistance> | ||
{ | ||
fn with_case_sensitivity( | ||
self, | ||
case_sensitivity: CaseSensitivity, | ||
) -> Self; | ||
|
||
fn with_matched_ranges(self, matched_ranges: bool) -> Self; | ||
} | ||
|
||
impl Fzf for FzfV1 { | ||
fn with_case_sensitivity( | ||
self, | ||
case_sensitivity: CaseSensitivity, | ||
) -> Self { | ||
self.with_case_sensitivity(case_sensitivity) | ||
} | ||
|
||
fn with_matched_ranges(self, matched_ranges: bool) -> Self { | ||
self.with_matched_ranges(matched_ranges) | ||
} | ||
} | ||
|
||
impl Fzf for FzfV2 { | ||
fn with_case_sensitivity( | ||
self, | ||
case_sensitivity: CaseSensitivity, | ||
) -> Self { | ||
self.with_case_sensitivity(case_sensitivity) | ||
} | ||
|
||
fn with_matched_ranges(self, matched_ranges: bool) -> Self { | ||
self.with_matched_ranges(matched_ranges) | ||
} | ||
} | ||
|
||
pub(super) fn fzf<F: Fzf>( | ||
case_sensitivity: CaseSensitivity, | ||
query: &str, | ||
candidate: &str, | ||
) -> 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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,16 @@ | ||
#![allow(clippy::single_range_in_vec_init)] | ||
|
||
mod common; | ||
mod fzf_common; | ||
|
||
use common::SortedRanges; | ||
use norm::fzf::{bonus, penalty, FzfParser, FzfV1}; | ||
use norm::{CaseSensitivity, Metric}; | ||
use fzf_common as common; | ||
use norm::fzf::FzfV1; | ||
|
||
#[test] | ||
fn fzf_v1_empty_query() { | ||
let mut fzf = FzfV1::new(); | ||
let mut parser = FzfParser::new(); | ||
assert!(fzf.distance(parser.parse(""), "foo").is_none()); | ||
common::empty_query::<FzfV1>(); | ||
} | ||
|
||
#[test] | ||
fn fzf_v1_upstream_1() { | ||
let mut fzf = FzfV1::new() | ||
.with_case_sensitivity(CaseSensitivity::Insensitive) | ||
.with_matched_ranges(true); | ||
|
||
let mut parser = FzfParser::new(); | ||
|
||
let mach = fzf.distance(parser.parse("oBZ"), "fooBarbaz").unwrap(); | ||
|
||
assert_eq!( | ||
mach.distance().into_score(), | ||
bonus::MATCH * 3 + bonus::CAMEL_123 | ||
- penalty::GAP_START | ||
- penalty::GAP_EXTENSION * 3 | ||
); | ||
|
||
assert_eq!(mach.matched_ranges().sorted(), [2..4, 8..9]); | ||
common::upstream_1::<FzfV1>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters