Add tests for xlookup; add reg function
This commit is contained in:
parent
c9974d4d29
commit
b560e47dc2
@ -317,3 +317,7 @@ where
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reg(register: Register, keys: Option<&'_ str>) -> Result<Vec<String>, KakError> {
|
||||||
|
response(format!("%reg{{{}}}", register.kak_expanded()), keys)
|
||||||
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use crate::utils::get_hash;
|
use crate::utils::get_hash;
|
||||||
use evalexpr::{eval, Value};
|
use evalexpr::{eval, Value};
|
||||||
use kakplugin::{
|
use kakplugin::{
|
||||||
get_selections, open_command_fifo, set_selections, types::Register, KakError, Selection,
|
get_selections, open_command_fifo, response, set_selections, types::Register, KakError,
|
||||||
|
Selection,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
collections::{
|
collections::{
|
||||||
@ -15,11 +16,11 @@ use std::{
|
|||||||
|
|
||||||
#[derive(clap::StructOpt, Debug)]
|
#[derive(clap::StructOpt, Debug)]
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
#[clap(help = "Register with the lookup table")]
|
#[clap(help = "Register with the lookup table", default_value = "\"")]
|
||||||
register: Register,
|
register: Register,
|
||||||
}
|
}
|
||||||
pub fn xlookup(options: &Options) -> Result<String, KakError> {
|
pub fn xlookup(options: &Options) -> Result<String, KakError> {
|
||||||
let lookup_table = build_lookuptable(options.register)?;
|
let lookup_table = build_lookuptable(kakplugin::reg(options.register, None)?)?;
|
||||||
|
|
||||||
let selections = get_selections(None)?;
|
let selections = get_selections(None)?;
|
||||||
|
|
||||||
@ -50,8 +51,7 @@ pub fn xlookup(options: &Options) -> Result<String, KakError> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_lookuptable(register: Register) -> Result<BTreeMap<u64, Selection>, KakError> {
|
fn build_lookuptable(mut selections: Vec<Selection>) -> Result<BTreeMap<u64, Selection>, KakError> {
|
||||||
let mut selections = get_selections(Some(&format!("\"{register}z")))?;
|
|
||||||
let mut iter = selections.array_chunks_mut();
|
let mut iter = selections.array_chunks_mut();
|
||||||
let ret = iter.try_fold(BTreeMap::new(), |mut acc, [key, value]| {
|
let ret = iter.try_fold(BTreeMap::new(), |mut acc, [key, value]| {
|
||||||
match acc.entry(get_hash(key, false, None, false)) {
|
match acc.entry(get_hash(key, false, None, false)) {
|
||||||
@ -71,3 +71,32 @@ fn build_lookuptable(register: Register) -> Result<BTreeMap<u64, Selection>, Kak
|
|||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
macro_rules! blt {
|
||||||
|
($($x:expr),+ $(,)?) => {
|
||||||
|
build_lookuptable(vec![$($x.to_string()),+])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
macro_rules! hsh {
|
||||||
|
($expr:expr) => {
|
||||||
|
get_hash(&$expr.to_string(), false, None, false)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_build_lookuptable() {
|
||||||
|
// Must be an even number
|
||||||
|
assert!(blt!["1", "b", "c"].is_err());
|
||||||
|
// Duplicate key
|
||||||
|
assert!(blt!["1", "b", "2", "c", "2", "d"].is_err());
|
||||||
|
// Valid
|
||||||
|
assert!(blt!["1", "b", "2", "c"].is_ok());
|
||||||
|
|
||||||
|
let lt = blt!["1", "b", "2", "c"].unwrap();
|
||||||
|
assert_eq!(lt.get(&hsh!("1")), Some(&String::from("b")));
|
||||||
|
assert_eq!(lt.get(&hsh!("2")), Some(&String::from("c")));
|
||||||
|
assert_eq!(lt.get(&hsh!("3")), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user