Add tests for xlookup; add reg function

This commit is contained in:
Austen Adler 2022-10-02 10:42:59 -04:00
parent c9974d4d29
commit b560e47dc2
2 changed files with 38 additions and 5 deletions

View File

@ -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)
}

View File

@ -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);
}
}