nu-cli/completions: add custom completion test (#5543)

This commit is contained in:
Herlon Aguiar
2022-05-14 22:09:41 +02:00
committed by GitHub
parent 16bd7b6d0d
commit c047fd4778
10 changed files with 80 additions and 46 deletions

View File

@ -5,13 +5,12 @@ use nu_engine::eval_block;
use nu_parser::parse;
use nu_protocol::{
engine::{EngineState, Stack, StateDelta, StateWorkingSet},
PipelineData, Span, Value,
PipelineData, ShellError, Span, Value,
};
use nu_test_support::fs;
use reedline::Suggestion;
const SEP: char = std::path::MAIN_SEPARATOR;
#[allow(dead_code)]
// creates a new engine with the current path into the completions fixtures folder
pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
// Target folder inside assets
@ -48,12 +47,41 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
let merge_result = engine_state.merge_delta(delta, Some(&mut stack), &dir);
assert!(merge_result.is_ok());
// Add record value as example
let record = "let actor = { name: 'Tom Hardy', age: 44 }";
(dir, dir_str, engine_state, stack)
}
// match a list of suggestions with the expected values
pub fn match_suggestions(expected: Vec<String>, suggestions: Vec<Suggestion>) {
expected.iter().zip(suggestions).for_each(|it| {
assert_eq!(it.0, &it.1.value);
});
}
// append the separator to the converted path
pub fn folder(path: PathBuf) -> String {
let mut converted_path = file(path);
converted_path.push(SEP);
converted_path
}
// convert a given path to string
pub fn file(path: PathBuf) -> String {
path.into_os_string().into_string().unwrap_or_default()
}
// merge_input executes the given input into the engine
// and merges the state
pub fn merge_input(
input: &[u8],
engine_state: &mut EngineState,
stack: &mut Stack,
dir: PathBuf,
) -> Result<(), ShellError> {
let (block, delta) = {
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, record.as_bytes(), false, &[]);
let (block, err) = parse(&mut working_set, None, input, false, &[]);
assert!(err.is_none());
@ -61,7 +89,7 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
};
assert!(eval_block(
&engine_state,
&mut stack,
stack,
&block,
PipelineData::Value(
Value::Nothing {
@ -75,31 +103,5 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
.is_ok());
// Merge delta
let merge_result = engine_state.merge_delta(delta, Some(&mut stack), &dir);
assert!(merge_result.is_ok());
(dir.clone(), dir_str, engine_state, stack)
}
#[allow(dead_code)]
// match a list of suggestions with the expected values
pub fn match_suggestions(expected: Vec<String>, suggestions: Vec<Suggestion>) {
expected.iter().zip(suggestions).for_each(|it| {
assert_eq!(it.0, &it.1.value);
});
}
#[allow(dead_code)]
// append the separator to the converted path
pub fn folder(path: PathBuf) -> String {
let mut converted_path = file(path);
converted_path.push(SEP);
converted_path
}
#[allow(dead_code)]
// convert a given path to string
pub fn file(path: PathBuf) -> String {
path.into_os_string().into_string().unwrap_or_default()
engine_state.merge_delta(delta, Some(stack), &dir)
}