Skip to content

Commit

Permalink
tests: share code in fzf tests between v1 and v2
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 27, 2023
1 parent d886c21 commit 063f71e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 62 deletions.
14 changes: 0 additions & 14 deletions tests/common.rs

This file was deleted.

95 changes: 95 additions & 0 deletions tests/fzf_common.rs
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)
}
}
28 changes: 5 additions & 23 deletions tests/fzf_v1.rs
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>();
}
34 changes: 9 additions & 25 deletions tests/fzf_v2.rs
Original file line number Diff line number Diff line change
@@ -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::<FzfV2>();
}

#[test]
fn fzf_v2_upstream_1() {
common::upstream_1::<FzfV2>();
}

#[test]
Expand All @@ -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]);
}

0 comments on commit 063f71e

Please sign in to comment.