diff --git a/crates/nu-cli/tests/custom_completions.rs b/crates/nu-cli/tests/custom_completions.rs new file mode 100644 index 000000000..4edffcafa --- /dev/null +++ b/crates/nu-cli/tests/custom_completions.rs @@ -0,0 +1,29 @@ +pub mod support; + +use nu_cli::NuCompleter; +use reedline::Completer; +use support::{match_suggestions, new_engine}; + +#[test] +fn variables_completions() { + // Create a new engine + let (dir, _, mut engine, mut stack) = new_engine(); + + // Add record value as example + let record = r#"def animals [] { ["cat", "dog", "eel" ] } + def my-command [animal: string@animals] { print $animal }"#; + assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); + + // Instatiate a new completer + let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); + + // Test completions for $nu + let suggestions = completer.complete("my-command ".into(), 11); + + assert_eq!(3, suggestions.len()); + + let expected: Vec = vec!["cat".into(), "dog".into(), "eel".into()]; + + // Match results + match_suggestions(expected, suggestions); +} diff --git a/crates/nu-cli/tests/dotnu_completions.rs b/crates/nu-cli/tests/dotnu_completions.rs index cf884d51d..676e43fb7 100644 --- a/crates/nu-cli/tests/dotnu_completions.rs +++ b/crates/nu-cli/tests/dotnu_completions.rs @@ -1,4 +1,4 @@ -mod support; +pub mod support; use nu_cli::NuCompleter; use reedline::Completer; @@ -17,12 +17,12 @@ fn dotnu_completions() { let suggestions = completer.complete(&completion_str, completion_str.len()); assert_eq!(1, suggestions.len()); - assert_eq!("test_dotnu.nu", suggestions.get(0).unwrap().value); + assert_eq!("custom_completion.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); + assert_eq!("custom_completion.nu", suggestions.get(0).unwrap().value); } diff --git a/crates/nu-cli/tests/file_completions.rs b/crates/nu-cli/tests/file_completions.rs index 85e6a474c..aa29fae50 100644 --- a/crates/nu-cli/tests/file_completions.rs +++ b/crates/nu-cli/tests/file_completions.rs @@ -1,4 +1,4 @@ -mod support; +pub mod support; use nu_cli::NuCompleter; use reedline::Completer; @@ -22,7 +22,7 @@ fn file_completions() { folder(dir.join("test_a")), folder(dir.join("test_b")), folder(dir.join("another")), - file(dir.join("test_dotnu.nu")), + file(dir.join("custom_completion.nu")), file(dir.join(".hidden_file")), folder(dir.join(".hidden_folder")), ]; diff --git a/crates/nu-cli/tests/flag_completions.rs b/crates/nu-cli/tests/flag_completions.rs index 861020b20..6828feeb9 100644 --- a/crates/nu-cli/tests/flag_completions.rs +++ b/crates/nu-cli/tests/flag_completions.rs @@ -1,4 +1,4 @@ -mod support; +pub mod support; use nu_cli::NuCompleter; use reedline::Completer; diff --git a/crates/nu-cli/tests/folder_completions.rs b/crates/nu-cli/tests/folder_completions.rs index 239033500..edfd572e1 100644 --- a/crates/nu-cli/tests/folder_completions.rs +++ b/crates/nu-cli/tests/folder_completions.rs @@ -1,4 +1,4 @@ -mod support; +pub mod support; use nu_cli::NuCompleter; use reedline::Completer; diff --git a/crates/nu-cli/tests/support/completions_helpers.rs b/crates/nu-cli/tests/support/completions_helpers.rs index e428f4c3f..68cca5419 100644 --- a/crates/nu-cli/tests/support/completions_helpers.rs +++ b/crates/nu-cli/tests/support/completions_helpers.rs @@ -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, suggestions: Vec) { + 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, suggestions: Vec) { - 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) } diff --git a/crates/nu-cli/tests/support/mod.rs b/crates/nu-cli/tests/support/mod.rs index 5bc454614..8a2fce324 100644 --- a/crates/nu-cli/tests/support/mod.rs +++ b/crates/nu-cli/tests/support/mod.rs @@ -1,3 +1,3 @@ pub mod completions_helpers; -pub use completions_helpers::{file, folder, match_suggestions, new_engine}; +pub use completions_helpers::{file, folder, match_suggestions, merge_input, new_engine}; diff --git a/crates/nu-cli/tests/variables_completions.rs b/crates/nu-cli/tests/variables_completions.rs index df8946166..1b5cdc8fb 100644 --- a/crates/nu-cli/tests/variables_completions.rs +++ b/crates/nu-cli/tests/variables_completions.rs @@ -1,4 +1,4 @@ -mod support; +pub mod support; use nu_cli::NuCompleter; use reedline::Completer; @@ -7,7 +7,11 @@ use support::{match_suggestions, new_engine}; #[test] fn variables_completions() { // Create a new engine - let (_, _, engine, stack) = new_engine(); + let (dir, _, mut engine, mut stack) = new_engine(); + + // Add record value as example + let record = "let actor = { name: 'Tom Hardy', age: 44 }"; + assert!(support::merge_input(record.as_bytes(), &mut engine, &mut stack, dir).is_ok()); // Instatiate a new completer let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack); diff --git a/tests/fixtures/completions/custom_completion.nu b/tests/fixtures/completions/custom_completion.nu new file mode 100644 index 000000000..7fd74a315 --- /dev/null +++ b/tests/fixtures/completions/custom_completion.nu @@ -0,0 +1,2 @@ +def animals [] { ["cat", "dog", "eel" ] } +def my-command [animal: string@animals] { print $animal } diff --git a/tests/fixtures/completions/test_dotnu.nu b/tests/fixtures/completions/test_dotnu.nu deleted file mode 100644 index 00f113ee9..000000000 --- a/tests/fixtures/completions/test_dotnu.nu +++ /dev/null @@ -1,3 +0,0 @@ -def test[] { - Just a test -}