proc setup {} {
    save_env
#    assert_bash_exec {cd $SRCDIR/fixtures/picocompletion-demo}
}

proc teardown {} {
    assert_env_unmodified {/OLDPWD=/d}
}

proc run_completion_test {cmd test candidates} {
    send_log "\nTESTING: $test\n";
    verbose "Sending ${cmd}\t..."
    send "${cmd}\t"

    expect "${cmd}\r\n"
    expect {
        -re $candidates { send_log "\n"; pass "$test" }
        -re /@ { send_log "\n"; unresolved "$test at prompt" }
        default { send_log "\n"; unresolved "$test" }
    }
    sync_after_int
}

proc run_completion_test_glob {cmd test candidates} {
    send_log "\nTESTING: $test\n";
    verbose "Sending ${cmd}\t..."
    send "${cmd}\t"

    expect "${cmd}\r\n"
    expect {
        -gl $candidates { send_log "\n"; pass "$test" }
        -re /@ { send_log "\n"; unresolved "$test at prompt" }
        default { send_log "\n"; unresolved "$test" }
    }
    sync_after_int
}

setup


    # Try completion without args
set cmd "picocompletion-demo "
set test "Tab should show sub1 and sub2 for '${cmd}'"
set candidates "sub1\\s*sub2\r\n/@${cmd}sub"
run_completion_test $cmd $test $candidates

    # Try completion for sub1 without options
set cmd "picocompletion-demo sub1 "
set test "Tab should not show completions for '${cmd}'"
verbose "Sending ${cmd}\t..."
send "${cmd}\t"

set timeout 1
expect "${cmd}\r\n"
expect {
    -re "${cmd}\t\[^\\s]*" { fail "$test: we got $expect_out(0,string)" }
    default { pass "$test" }
}
# reset to default timeout
set timeout 10
sync_after_int

    # Try completion for sub1
set cmd "picocompletion-demo sub1 -"
set test "Tab should show sub1 options for '${cmd}'"
set candidates "--candidates\\s*--num\\s*--str\r\n/@${cmd}-"
run_completion_test $cmd $test $candidates

    # Try completion for sub1 --candidates
set cmd "picocompletion-demo sub1 --candidates "
set test "Tab should show enum values for '${cmd}'"
set candidates "aaa\\s*bbb\\s*ccc\r\n/@${cmd}"
run_completion_test $cmd $test $candidates

    # Try completion for sub2
set cmd "picocompletion-demo sub2 "
set test "Tab should show positionals and subcommands for '${cmd}'"
set candidates "Aaa\\s*Bbb\\s*Ccc\\s*subsub1\\s*subsub2\\s*subsub3\r\n/@${cmd}"
run_completion_test $cmd $test $candidates

    # Try completion for sub2 -
set cmd "picocompletion-demo sub2 -"
set test "Tab should show options for '${cmd}'"
set candidates "--directory\\s*--num2\\s*-d\r\n/@${cmd}|-d\\s*--directory\\s*--num2\r\n/@${cmd}"
run_completion_test $cmd $test $candidates

    # Try completion for sub2 Aaa
set cmd "picocompletion-demo sub2 Aaa "
set test "Tab should show subcommands for '${cmd}'"
set candidates "subsub1\\s*subsub2\\s*subsub3\r\n/@${cmd}subsub"
run_completion_test $cmd $test $candidates

    # Try completion for sub2 Aaa s
set cmd "picocompletion-demo sub2 Aaa s"
set test "Tab should show subcommands for '${cmd}'"
set candidates "subsub1\\s*subsub2\\s*subsub3\r\n/@${cmd}ubsub"
run_completion_test $cmd $test $candidates

    # Try completion for sub2 Aaa subsub1 -
set cmd "picocompletion-demo sub2 Aaa subsub1 -"
set test "Tab should show options for '${cmd}'"
set candidates "--host\\s*-h\r\n/@${cmd}|-h\\s*--host\r\n/@${cmd}"
run_completion_test $cmd $test $candidates

    # Try completion for sub2 Aaa subsub2
set cmd "picocompletion-demo sub2 Aaa subsub2 "
set test "Tab should show enum values for positional0'${cmd}'"
set candidates "aaa\\s*bbb\\s*ccc\r\n/@${cmd}"
run_completion_test $cmd $test $candidates

#exp_internal 1 # tell Expect to print diagnostics on its internal operations
    # Try completion for sub2 Aaa subsub2 -
set cmd "picocompletion-demo sub2 Aaa subsub2 -"
set test "Tab should show options for '${cmd}'"
set candidates "--timeUnit\\s*--timeout\\s*-t\\s*-u\r\n/@${cmd}|-t\\s*--timeout\\s*--timeUnit\\s*-u\r\n/@${cmd}"
run_completion_test $cmd $test $candidates
#exp_internal 0

#exp_internal 1 # tell Expect to print diagnostics on its internal operations
    # Try completion for sub2 Aaa subsub2 --timeUnit
set cmd "picocompletion-demo sub2 Aaa subsub2 --timeUnit "
set test "Tab should show time unit enum values for '${cmd}'"
set candidates "DAYS\\s*HOURS\\s*MICROSECONDS\\s*MILLISECONDS\\s*MINUTES\\s*NANOSECONDS\\s*SECONDS\r\n/@${cmd}"
run_completion_test $cmd $test $candidates

    # Try completion for sub2 Aaa subsub3
set cmd "picocompletion-demo sub2 Aaa subsub3 "
set test "Tab should show enum values for positional0'${cmd}'"
set candidates "aaa\\s*bbb\\s*ccc\r\n/@${cmd}"
run_completion_test $cmd $test $candidates

#exp_internal 1 # tell Expect to print diagnostics on its internal operations
    # Try completion for sub2 Aaa subsub3 aaa
set cmd "picocompletion-demo sub2 Aaa subsub3 aaa "
set test "Tab should show directories for positional1'${cmd}'"
set candidates "README.adoc\\s*completion/\\s*config/\\s*lib/.*run\\s*runCompletion.*\r\n/@${cmd}|completion/\\s*config/\\s*lib/.*README.adoc.*run\\s*runCompletion.*\r\n/@${cmd}"
run_completion_test $cmd $test $candidates

    # Try completion for sub2 Aaa subsub3 aaa xxx
set cmd "picocompletion-demo sub2 Aaa subsub3 aaa xxx "
set test "Tab should show directories for positional2'${cmd}'"
set candidates "README.adoc\\s*completion/\\s*config/\\s*lib/.*run\\s*runCompletion.*\r\n/@${cmd}|completion/\\s*config/\\s*lib/.*README.adoc.*run\\s*runCompletion.*\r\n/@${cmd}"
run_completion_test $cmd $test $candidates

#exp_internal 1 # tell Expect to print diagnostics on its internal operations
    # Try completion for sub2 Aaa subsub3 aaa xxx yyy
# The environment variable __MY_HOSTS__ is initialized in src/test/dejagnu.tests/config/bashrc
assert_bash_exec {printf "%s" "$__MY_HOSTS__"} {} /@ myhosts
verbose myhosts=$myhosts

set cmd "picocompletion-demo sub2 Aaa subsub3 aaa xxx yyy "
set test "Tab should show hosts for positional3'${cmd}'"
set candidates "${myhosts}\r\n/@${cmd}"
run_completion_test_glob $cmd $test $candidates


teardown
