diff --git a/tests/test.sh b/tests/test.sh index 6ff9202807381e70ceb9fb55d7a7ea895a52db01..8f3d0f433cf56fb0d1fdafb6d520f9970ca4801f 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -14,10 +14,20 @@ text_purple="\033[0;35m" # Get sudo password now so we don't have to later. -echo -e "To fully test some functions, script may ask for your sudo password." +echo "To fully test some functions, script may ask for your sudo password." +echo "Press enter to continue or ctrl+C to cancel." +read sudo ls > /dev/null 2>&1 +# Run Arg/Kwarg handling tests. +echo "" +echo -e "Running ${text_purple}Arg/Kwarg Handling${text_reset} tests." +bats ./test_args_kwargs.sh +echo -e "Finished ${text_purple}Arg/Kwarg Handling${text_reset} Tests." +echo "" + + # Run Directory Function tests. echo "" echo -e "Running ${text_purple}Directory Function${text_reset} tests." @@ -43,9 +53,13 @@ echo -e "Finished ${text_purple}Text Function${text_reset} tests." echo "" -# Run Display Function tests. -echo "" -echo -e "Running ${text_purple}Display Function${text_reset} tests." -bats ./test_display_functions.sh -echo -e "Finished ${text_purple}Display Function${text_reset} tests." -echo "" +### + # Seems unable to run. + # Need to figure out a way to provide fake user input as piping seems to break bats. + ## +# # Run Display Function tests. +# echo "" +# echo -e "Running ${text_purple}Display Function${text_reset} tests." +# bats ./test_display_functions.sh +# echo -e "Finished ${text_purple}Display Function${text_reset} tests." +# echo "" diff --git a/tests/test_args_kwargs.sh b/tests/test_args_kwargs.sh new file mode 100644 index 0000000000000000000000000000000000000000..5c81e7e74b38d2c5c53b42e26a4e80a3c820783c --- /dev/null +++ b/tests/test_args_kwargs.sh @@ -0,0 +1,249 @@ +#!/usr/bin/env bash +### + # Tests for the Arg/Kwarg handling section of the utils script. + ## + + +@test "Arg/Kwarg Handling - Empty" { + # Import utils script. + source ../utils.sh + + handle_args_kwargs + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 0 ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] +} + + +@test "Arg/Kwarg Handling - Help Flags Set" { + # Import utils script. + source ../utils.sh + + # Test -h flag. + handle_args_kwargs -h + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 0 ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == true ]] + + # Reset flag. + help_flags=false + [[ ${help_flags} == false ]] + + # Test --help flag. + handle_args_kwargs --help + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 0 ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == true ]] +} + + +@test "Arg/Kwarg Handling - Args Only" { + # Import utils script. + source ../utils.sh + + # Test with one arg. + expected_result=("one") + handle_args_kwargs one + [[ "${#args[@]}" == 1 ]] + [[ ${args[@]} == ${expected_result[@]} ]] + [[ "${#kwargs[@]}" == 0 ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] + + # Ensure args clear on new call. + handle_args_kwargs + [[ "${#args[@]}" == 0 ]] + + # Test with two args. + expected_result=("one" "two") + handle_args_kwargs one two + [[ "${#args[@]}" == 2 ]] + [[ ${args[@]} == ${expected_result[@]} ]] + [[ "${#kwargs[@]}" == 0 ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] + + # Ensure args clear on new call. + handle_args_kwargs + [[ "${#args[@]}" == 0 ]] + + # Test with three args. + expected_result=("one" "two" "three") + handle_args_kwargs one two three + [[ "${#args[@]}" == 3 ]] + [[ ${args[@]} == ${expected_result[@]} ]] + [[ "${#kwargs[@]}" == 0 ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] +} + + +@test "Arg/Kwarg Handling - Kwargs Only - V1" { + # Import utils script. + source ../utils.sh + + declare -A expected_result=() + + # Test with one kwargs. + expected_result[a]=x + handle_args_kwargs -a x + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 1 ]] + [[ ${kwargs[@]} == ${expected_result[@]} ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] + + # Ensure kwargs clear on new call. + handle_args_kwargs + [[ "${#kwargs[@]}" == 0 ]] + + # Test with two kwargs. + expected_result[b]=y + handle_args_kwargs -a x -b y + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 2 ]] + [[ ${kwargs[@]} == ${expected_result[@]} ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] + + # Ensure kwargs clear on new call. + handle_args_kwargs + [[ "${#kwargs[@]}" == 0 ]] + + # Test with three kwargs. + expected_result[c]=z + handle_args_kwargs -a x -b y -c z + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 3 ]] + [[ ${kwargs[@]} == ${expected_result[@]} ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] +} + + +@test "Arg/Kwarg Handling - Kwargs Only - V2" { + # Import utils script. + source ../utils.sh + + declare -A expected_result=() + + # Test with one kwargs. + expected_result[a]=x + handle_args_kwargs --a x + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 1 ]] + [[ ${kwargs[@]} == ${expected_result[@]} ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] + + # Ensure kwargs clear on new call. + handle_args_kwargs + [[ "${#kwargs[@]}" == 0 ]] + + # Test with two kwargs. + expected_result[b]=y + handle_args_kwargs --a x --b y + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 2 ]] + [[ ${kwargs[@]} == ${expected_result[@]} ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] + + # Ensure kwargs clear on new call. + handle_args_kwargs + [[ "${#kwargs[@]}" == 0 ]] + + # Test with three kwargs. + expected_result[c]=z + handle_args_kwargs --a x --b y --c z + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 3 ]] + [[ ${kwargs[@]} == ${expected_result[@]} ]] + [[ "${#flags[@]}" == 0 ]] + [[ ${help_flags} == false ]] +} + + +@test "Arg/Kwarg Handling - Flags Only" { + # Import utils script. + source ../utils.sh + + possible_flags=("-a" "--b" "-c" "--d") + + # Test with single dash. + expected_result=("a" "c") + handle_args_kwargs -a -c + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 0 ]] + [[ "${#flags[@]}" == 2 ]] + [[ ${flags[@]} == ${expected_result[@]} ]] + [[ ${help_flags} == false ]] + + # Ensure kwargs clear on new call. + handle_args_kwargs + [[ "${#flags[@]}" == 0 ]] + + # Test with double dash. + expected_result=("b" "d") + handle_args_kwargs --b --d + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 0 ]] + [[ "${#flags[@]}" == 2 ]] + [[ ${flags[@]} == ${expected_result[@]} ]] + [[ ${help_flags} == false ]] + + # Ensure kwargs clear on new call. + handle_args_kwargs + [[ "${#flags[@]}" == 0 ]] + + # Test with mixed dashes. + expected_result=("a" "c" "b" "d") + handle_args_kwargs -a -c --b --d + [[ "${#args[@]}" == 0 ]] + [[ "${#kwargs[@]}" == 0 ]] + [[ "${#flags[@]}" == 4 ]] + [[ ${flags[@]} == ${expected_result[@]} ]] + [[ ${help_flags} == false ]] +} + + +@test "Arg/Kwarg Handling - Mixed Set" { + # Import utils script. + source ../utils.sh + + possible_flags=("-a" "--b") + + # Test two of each. + expected_args=("arg1" "arg2") + declare -A expected_kwargs=() + expected_kwargs["aa"]="xx" + expected_kwargs["bb"]="yy" + expected_flags=("a" "b") + handle_args_kwargs arg1 --aa xx -a arg2 --bb yy --b + [[ "${#args[@]}" == 2 ]] + [[ ${args[@]} == ${expected_args[@]} ]] + [[ "${#kwargs[@]}" == 2 ]] + [[ ${kwargs[@]} == ${expected_kwargs[@]} ]] + [[ "${#flags[@]}" == 2 ]] + [[ ${flags[@]} == ${expected_flags[@]} ]] + [[ ${help_flags} == false ]] + + # Reset flag. + help_flags=false + [[ ${help_flags} == false ]] + + # Test two of each, different ordering. + expected_args=("arg2" "arg1") + expected_flags=("b" "a") + handle_args_kwargs --b --bb yy arg2 -a --aa xx arg1 + [[ "${#args[@]}" == 2 ]] + [[ ${args[@]} == ${expected_args[@]} ]] + [[ "${#kwargs[@]}" == 2 ]] + [[ ${kwargs[@]} == ${expected_kwargs[@]} ]] + [[ "${#flags[@]}" == 2 ]] + [[ ${flags[@]} == ${expected_flags[@]} ]] + [[ ${help_flags} == false ]] +}