Repository: nushell/nushell
File: crates/nu-cli/tests/completions/mod.rs (Rust)

Code before:
    #[cfg(windows)]
    let expected: Vec<_> = vec!["Path", "PWD", "TEST"];
    #[cfg(not(windows))]
    let expected: Vec<_> = vec!["PATH", "PWD", "TEST"];

    // Match results
    match_suggestions(&expected, &suggestions);

    // Test completions for $env
    let suggestions = completer.complete("$env.T", 6);

    assert_eq!(1, suggestions.len());

    let expected: Vec<_> = vec!["TEST"];

    // Match results
    match_suggestions(&expected, &suggestions);

    let suggestions = completer.complete("$", 1);
    let expected: Vec<_> = vec!["$actor", "$env", "$in", "$nu"];

    match_suggestions(&expected, &suggestions);
}

#[test]
fn local_variable_completion() {
    let (_, _, engine, stack) = new_engine();
    let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));

    let completion_str =
        "def test [foo?: string, --fooo: bool, ...foooo] { let fooooo = true; $foo";
    let suggestions = completer.complete(completion_str, completion_str.len());

    // https://github.com/nushell/nushell/issues/15291
    let expected: Vec<_> = vec!["$foo", "$fooo", "$foooo", "$fooooo"];
    match_suggestions(&expected, &suggestions);

    let completion_str = "if true { let foo = true; $foo";
    let suggestions = completer.complete(completion_str, completion_str.len());
    let expected: Vec<_> = vec!["$foo"];
    match_suggestions(&expected, &suggestions);

    let completion_str = "if true {let fooo = 1} else {let foo = true; $foo";
    let suggestions = completer.complete(completion_str, completion_str.len());
    let expected: Vec<_> = vec!["$foo"];
    match_suggestions(&expected, &suggestions);

    let completion_str = "for foo in [1] { let fooo = true; $foo";
    let suggestions = completer.complete(completion_str, completion_str.len());
    let expected: Vec<_> = vec!["$foo", "$fooo"];
    match_suggestions(&expected, &suggestions);

Review comment (suggestion):
All of these tests try to complete `$foo` with a variable named `foo` in scope. I'd like to see a test where you try to complete `$f` or something (with no variable named `f` in scope)

Code after:
    #[cfg(windows)]
    let expected: Vec<_> = vec!["Path", "PWD", "TEST"];
    #[cfg(not(windows))]
    let expected: Vec<_> = vec!["PATH", "PWD", "TEST"];

    // Match results
    match_suggestions(&expected, &suggestions);

    // Test completions for $env
    let suggestions = completer.complete("$env.T", 6);

    assert_eq!(1, suggestions.len());

    let expected: Vec<_> = vec!["TEST"];

    // Match results
    match_suggestions(&expected, &suggestions);

    let suggestions = completer.complete("$", 1);
    let expected: Vec<_> = vec!["$actor", "$env", "$in", "$nu"];

    match_suggestions(&expected, &suggestions);
}

#[test]
fn local_variable_completion() {
    let (_, _, engine, stack) = new_engine();
    let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));

    let completion_str = "def test [foo?: string, --foo1: bool, ...foo2] { let foo3 = true; $foo";
    let suggestions = completer.complete(completion_str, completion_str.len());

    // https://github.com/nushell/nushell/issues/15291
    let expected: Vec<_> = vec!["$foo", "$foo1", "$foo2", "$foo3"];
    match_suggestions(&expected, &suggestions);

    let completion_str = "if true { let foo = true; $foo";
    let suggestions = completer.complete(completion_str, completion_str.len());
    let expected: Vec<_> = vec!["$foo"];
    match_suggestions(&expected, &suggestions);

    let completion_str = "if true {let foo1 = 1} else {let foo = true; $foo";
    let suggestions = completer.complete(completion_str, completion_str.len());
    let expected: Vec<_> = vec!["$foo"];
    match_suggestions(&expected, &suggestions);

    let completion_str = "for foo in [1] { let foo1 = true; $foo";
    let suggestions = completer.complete(completion_str, completion_str.len());
    let expected: Vec<_> = vec!["$foo", "$foo1"];
    match_suggestions(&expected, &suggestions);