From 063f71ef04d1518784cdacb9ca51d69b1657fcdc Mon Sep 17 00:00:00 2001 From: Riccardo Mazzarini Date: Fri, 27 Oct 2023 23:34:37 +0200 Subject: [PATCH] tests: share code in fzf tests between v1 and v2 --- tests/common.rs | 14 ------- tests/fzf_common.rs | 95 +++++++++++++++++++++++++++++++++++++++++++++ tests/fzf_v1.rs | 28 +++---------- tests/fzf_v2.rs | 34 +++++----------- 4 files changed, 109 insertions(+), 62 deletions(-) delete mode 100644 tests/common.rs create mode 100644 tests/fzf_common.rs diff --git a/tests/common.rs b/tests/common.rs deleted file mode 100644 index af27ae7..0000000 --- a/tests/common.rs +++ /dev/null @@ -1,14 +0,0 @@ -use core::ops::Range; - -/// TODO: docs -pub trait SortedRanges { - fn sorted(&self) -> Vec>; -} - -impl SortedRanges for &[Range] { - fn sorted(&self) -> Vec> { - let mut sorted = self.to_vec(); - sorted.sort_by_key(|r| r.start); - sorted - } -} diff --git a/tests/fzf_common.rs b/tests/fzf_common.rs new file mode 100644 index 0000000..f1204b2 --- /dev/null +++ b/tests/fzf_common.rs @@ -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() { + assert!(fzf::(Insensitive, "", "foo").is_none()); +} + +pub fn upstream_1() { + let m = fzf::(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>; + } + + impl SortedRanges for &[Range] { + fn sorted(&self) -> Vec> { + let mut sorted = self.to_vec(); + sorted.sort_by_key(|r| r.start); + sorted + } + } + pub trait Fzf: + Default + + for<'a> Metric = 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( + case_sensitivity: CaseSensitivity, + query: &str, + candidate: &str, + ) -> 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) + } +} diff --git a/tests/fzf_v1.rs b/tests/fzf_v1.rs index 45be0e5..097d7c5 100644 --- a/tests/fzf_v1.rs +++ b/tests/fzf_v1.rs @@ -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::(); } #[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::(); } diff --git a/tests/fzf_v2.rs b/tests/fzf_v2.rs index ba9c8f8..c4f7712 100644 --- a/tests/fzf_v2.rs +++ b/tests/fzf_v2.rs @@ -1,16 +1,20 @@ #![allow(clippy::single_range_in_vec_init)] -mod common; +mod fzf_common; use common::SortedRanges; -use norm::fzf::{bonus, penalty, FzfParser, FzfV2}; +use fzf_common as common; +use norm::fzf::{bonus, FzfParser, FzfV2}; use norm::{CaseSensitivity, Metric}; #[test] fn fzf_v2_empty_query() { - let mut fzf = FzfV2::new(); - let mut parser = FzfParser::new(); - assert!(fzf.distance(parser.parse(""), "foo").is_none()); + common::empty_query::(); +} + +#[test] +fn fzf_v2_upstream_1() { + common::upstream_1::(); } #[test] @@ -33,23 +37,3 @@ fn fzf_v2_score_1() { assert_eq!(mach.matched_ranges().sorted(), [0..5]); } - -#[test] -fn fzf_v2_upstream_1() { - let mut fzf = FzfV2::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]); -}