|
| 1 | +@echo off |
| 2 | +:: SPDX-FileContributor: Sebastian Thomschke |
| 3 | +:: SPDX-License-Identifier: CC0-1.0 |
| 4 | +:: SPDX-ArtifactOfProjectHomePage: https://github.com/sebthom/batch-functions |
| 5 | + |
| 6 | +setlocal |
| 7 | + |
| 8 | +pushd %~dp0..\modules\ |
| 9 | + |
| 10 | +set failures=0 |
| 11 | + |
| 12 | +:::::::::::::::::::::::::::::::: |
| 13 | +:: test has_arg |
| 14 | +:::::::::::::::::::::::::::::::: |
| 15 | +call :assert_exec_ok has_arg foo foo |
| 16 | +call :assert_exec_ok has_arg foo foo bar |
| 17 | +call :assert_exec_ok has_arg foo bar foo |
| 18 | +call :assert_exec_ok has_arg foo "" foo |
| 19 | +call :assert_exec_ok has_arg foo "" "" "" "" "" "" foo |
| 20 | +call :assert_exec_ok has_arg foo "foo" |
| 21 | +call :assert_exec_ok has_arg "foo" foo |
| 22 | +call :assert_exec_ok has_arg * foo * |
| 23 | +call :assert_exec_ok has_arg * foo "*" |
| 24 | +call :assert_exec_nok has_arg foo |
| 25 | +call :assert_exec_nok has_arg foo bar |
| 26 | +call :assert_exec_nok has_arg * |
| 27 | + |
| 28 | + |
| 29 | +:::::::::::::::::::::::::::::::: |
| 30 | +:: test get_1st_positional_arg |
| 31 | +:::::::::::::::::::::::::::::::: |
| 32 | +call :exec get_1st_positional_arg OUTPUT foo bar |
| 33 | +call :assert_OUTPUT_equals foo |
| 34 | + |
| 35 | +call :exec get_1st_positional_arg OUTPUT * foo bar |
| 36 | +call :assert_OUTPUT_equals * |
| 37 | + |
| 38 | +call :exec get_1st_positional_arg OUTPUT "foo foo" "bar bar" |
| 39 | +call :assert_OUTPUT_equals "foo foo" |
| 40 | + |
| 41 | +call :exec get_1st_positional_arg OUTPUT /a "foo foo" -b "bar bar" |
| 42 | +call :assert_OUTPUT_equals "foo foo" |
| 43 | + |
| 44 | +call :exec get_1st_positional_arg OUTPUT |
| 45 | +call :assert_OUTPUT_equals "" |
| 46 | + |
| 47 | +call :exec get_1st_positional_arg OUTPUT "" foo bar |
| 48 | +call :assert_OUTPUT_equals "" |
| 49 | + |
| 50 | + |
| 51 | +:::::::::::::::::::::::::::::::: |
| 52 | +:: test get_2nd_positional_arg |
| 53 | +:::::::::::::::::::::::::::::::: |
| 54 | +call :exec get_2nd_positional_arg OUTPUT foo bar |
| 55 | +call :assert_OUTPUT_equals bar |
| 56 | + |
| 57 | +call :exec get_2nd_positional_arg OUTPUT foo * bar |
| 58 | +call :assert_OUTPUT_equals * |
| 59 | + |
| 60 | +call :exec get_2nd_positional_arg OUTPUT /a "foo foo" -b "bar bar" |
| 61 | +call :assert_OUTPUT_equals "bar bar" |
| 62 | + |
| 63 | +call :exec get_2nd_positional_arg OUTPUT |
| 64 | +call :assert_OUTPUT_equals "" |
| 65 | + |
| 66 | +call :exec get_2nd_positional_arg OUTPUT foo "" bar |
| 67 | +call :assert_OUTPUT_equals "" |
| 68 | + |
| 69 | + |
| 70 | +:::::::::::::::::::::::::::::::: |
| 71 | +:: test get_nth_positional_arg |
| 72 | +:::::::::::::::::::::::::::::::: |
| 73 | +call :exec get_nth_positional_arg 5 OUTPUT arg1 arg2 arg3 arg4 arg5 arg6 |
| 74 | +call :assert_OUTPUT_equals arg5 |
| 75 | + |
| 76 | +call :exec get_nth_positional_arg 5 OUTPUT arg1 arg2 arg3 arg4 * arg6 |
| 77 | +call :assert_OUTPUT_equals * |
| 78 | + |
| 79 | +call :exec get_nth_positional_arg 5 OUTPUT /a /b /c -d "" "" "" "arg4 arg4" /e /f -g "arg5 arg5" "arg6 arg6" |
| 80 | +call :assert_OUTPUT_equals "arg5 arg5" |
| 81 | + |
| 82 | +call :exec get_nth_positional_arg 5 OUTPUT |
| 83 | +call :assert_OUTPUT_equals "" |
| 84 | + |
| 85 | +call :exec get_nth_positional_arg 5 OUTPUT arg1 arg2 arg3 arg4 "" arg6 |
| 86 | +call :assert_OUTPUT_equals "" |
| 87 | + |
| 88 | +popd |
| 89 | + |
| 90 | +exit /b %failures% |
| 91 | + |
| 92 | + |
| 93 | +:::::::::::::::::::::::::::::::: |
| 94 | +:: utilities |
| 95 | +:::::::::::::::::::::::::::::::: |
| 96 | +:exec |
| 97 | + echo Testing: args.cmd %* |
| 98 | + call args.cmd %* |
| 99 | +goto :eof |
| 100 | + |
| 101 | + |
| 102 | +:assert_exec_ok |
| 103 | + echo Testing: args.cmd %* |
| 104 | + call args.cmd %* || ( |
| 105 | + echo -^> FAILED |
| 106 | + set /a failures+=1 |
| 107 | + exit /b 1 |
| 108 | + ) |
| 109 | + echo -^> OK |
| 110 | +goto :eof |
| 111 | + |
| 112 | + |
| 113 | +:assert_exec_nok |
| 114 | + echo Testing: args.cmd %* |
| 115 | + call args.cmd %* && ( |
| 116 | + set /a failures+=1 |
| 117 | + echo -^> FAILED |
| 118 | + exit /b 1 |
| 119 | + ) |
| 120 | + echo -^> OK |
| 121 | +goto :eof |
| 122 | + |
| 123 | +:assert_OUTPUT_equals |
| 124 | + if not "%OUTPUT%" == "%~1" ( |
| 125 | + echo -^> FAILED (OUTPUT is not "%~1" but "%OUTPUT%"^) |
| 126 | + set /a failures+=1 |
| 127 | + exit /b 1 |
| 128 | + ) |
| 129 | + echo -^> OK (OUTPUT == "%OUTPUT%"^) |
| 130 | +goto :eof |
| 131 | + |
| 132 | + |
| 133 | +:assert_OUTPUT_not_equals |
| 134 | + if "%OUTPUT%" == "%~1" ( |
| 135 | + echo -^> FAILED (OUTPUT == "%~1" but should not be^) |
| 136 | + set /a failures+=1 |
| 137 | + exit /b 1 |
| 138 | + ) |
| 139 | + echo -^> OK (OUTPUT != "%OUTPUT%"^) |
| 140 | +goto :eof |
0 commit comments