nu-cli/completions: added tests for dotnu completions (#5460)

This commit is contained in:
Herlon Aguiar 2022-05-10 20:18:18 +02:00 committed by GitHub
parent c0b99b7131
commit 8218f72eea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 5 deletions

View file

@ -58,7 +58,7 @@ impl Completer for DotNuCompletion {
}; };
// Check if the base_dir is a folder // Check if the base_dir is a folder
if base_dir != "./" { if base_dir != format!(".{}", SEP) {
// Add the base dir into the directories to be searched // Add the base dir into the directories to be searched
search_dirs.push(base_dir.clone()); search_dirs.push(base_dir.clone());

View file

@ -2,11 +2,51 @@ use std::path::PathBuf;
use nu_cli::NuCompleter; use nu_cli::NuCompleter;
use nu_command::create_default_context; use nu_command::create_default_context;
use nu_protocol::engine::{EngineState, Stack}; use nu_protocol::{
engine::{EngineState, Stack, StateDelta},
Value,
};
use nu_test_support::fs; use nu_test_support::fs;
use reedline::{Completer, Suggestion}; use reedline::{Completer, Suggestion};
const SEP: char = std::path::MAIN_SEPARATOR; const SEP: char = std::path::MAIN_SEPARATOR;
#[test]
fn dotnu_completions() {
// Create a new engine
let (_, dir_str, engine) = new_engine();
let mut stack = Stack::new();
// Add pwd as env var
stack.add_env_var(
"PWD".to_string(),
Value::String {
val: dir_str.clone(),
span: nu_protocol::Span {
start: 0,
end: dir_str.len(),
},
},
);
// Instatiate a new completer
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Test source completion
let completion_str = "source ".to_string();
let suggestions = completer.complete(&completion_str, completion_str.len());
assert_eq!(1, suggestions.len());
assert_eq!("test_dotnu.nu", suggestions.get(0).unwrap().value);
// Test use completion
let completion_str = "use ".to_string();
let suggestions = completer.complete(&completion_str, completion_str.len());
assert_eq!(1, suggestions.len());
assert_eq!("test_dotnu.nu", suggestions.get(0).unwrap().value);
}
#[test] #[test]
fn flag_completions() { fn flag_completions() {
// Create a new engine // Create a new engine
@ -16,7 +56,6 @@ fn flag_completions() {
// Instatiate a new completer // Instatiate a new completer
let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);
// Test completions for the 'ls' flags // Test completions for the 'ls' flags
let suggestions = completer.complete("ls -", 4); let suggestions = completer.complete("ls -", 4);
@ -61,6 +100,7 @@ fn file_completions() {
folder(dir.join("test_a")), folder(dir.join("test_a")),
folder(dir.join("test_b")), folder(dir.join("test_b")),
folder(dir.join("another")), folder(dir.join("another")),
file(dir.join("test_dotnu.nu")),
file(dir.join(".hidden_file")), file(dir.join(".hidden_file")),
folder(dir.join(".hidden_folder")), folder(dir.join(".hidden_folder")),
]; ];
@ -116,8 +156,31 @@ pub fn new_engine() -> (PathBuf, String, EngineState) {
.unwrap_or_default(); .unwrap_or_default();
dir_str.push(SEP); dir_str.push(SEP);
// Create a default engine // Create a new engine with default context
(dir.clone(), dir_str, create_default_context(dir)) let mut engine_state = create_default_context(&dir);
// New stack
let mut stack = Stack::new();
// New delta
let delta = StateDelta::new(&engine_state);
// Add pwd as env var
stack.add_env_var(
"PWD".to_string(),
Value::String {
val: dir_str.clone(),
span: nu_protocol::Span {
start: 0,
end: dir_str.len(),
},
},
);
// Merge delta
let _ = engine_state.merge_delta(delta, Some(&mut stack), &dir);
(dir.clone(), dir_str, engine_state)
} }
// match a list of suggestions with the expected values // match a list of suggestions with the expected values

View file

@ -0,0 +1,3 @@
def test[] {
Just a test
}