scripts/library: introduce parseopts
[pacman-ng.git] / test / scripts / parseopts_test.sh
blobb5c07b5d8b8266f346ae26aadf1605ea8cd7c907
1 #!/bin/bash
3 declare -i testcount=0 pass=0 fail=0
5 # source the library function
6 if [[ -z $1 || ! -f $1 ]]; then
7 printf "error: path to parseopts library not provided or does not exist\n"
8 exit 1
9 fi
10 . "$1"
12 if ! type -t parseopts >/dev/null; then
13 printf 'parseopts function not found\n'
14 exit 1
17 # borrow opts from makepkg
18 OPT_SHORT="AcdefFghiLmop:rRsV"
19 OPT_LONG=('allsource' 'asroot' 'ignorearch' 'check' 'clean:' 'cleanall' 'nodeps'
20 'noextract' 'force' 'forcever:' 'geninteg' 'help' 'holdver'
21 'install' 'key:' 'log' 'nocolor' 'nobuild' 'nocheck' 'nosign' 'pkg:' 'rmdeps'
22 'repackage' 'skipinteg' 'sign' 'source' 'syncdeps' 'version' 'config:'
23 'noconfirm' 'noprogressbar')
25 parse() {
26 local result=$1 tokencount=$2; shift 2
28 (( ++testcount ))
29 parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@" 2>/dev/null
30 test_result "$result" "$tokencount" "$*" "${OPTRET[@]}"
31 unset OPTRET
34 test_result() {
35 local result=$1 tokencount=$2 input=$3; shift 3
37 if [[ $result = "$*" ]] && (( tokencount == $# )); then
38 (( ++pass ))
39 else
40 printf '[TEST %3s]: FAIL\n' "$testcount"
41 printf ' input: %s\n' "$input"
42 printf ' output: %s (%s tokens)\n' "$*" "$#"
43 printf ' expected: %s (%s tokens)\n' "$result" "$tokencount"
44 echo
45 (( ++fail ))
49 summarize() {
50 if (( !fail )); then
51 printf 'All %s tests successful\n' "$testcount"
52 exit 0
53 else
54 printf '%s of %s tests failed\n' "$fail" "$testcount"
55 exit 1
58 trap 'summarize' EXIT
60 printf 'Beginning parseopts tests\n'
62 # usage: parse <expected result> <token count> test-params...
63 # a failed parse will match only the end of options marker '--'
65 # no options
66 parse '--' 1
68 # short options
69 parse '-s -r --' 3 -s -r
71 # short options, no spaces
72 parse '-s -r --' 3 -sr
74 # short opt missing an opt arg
75 parse '--' 1 -s -p
77 # short opt with an opt arg
78 parse '-p PKGBUILD -L --' 4 -p PKGBUILD -L
80 # short opt with an opt arg, no space
81 parse '-p PKGBUILD --' 3 -pPKGBUILD
83 # valid shortopts as a long opt
84 parse '--' 1 --sir
86 # long opt wiht no optarg
87 parse '--log --' 2 --log
89 # long opt with missing optarg
90 parse '--' 1 -sr --pkg
92 # long opt with optarg
93 parse '--pkg foo --' 3 --pkg foo
95 # long opt with optarg with whitespace
96 parse '--pkg foo bar -- baz' 4 --pkg "foo bar" baz
98 # long opt with optarg with =
99 parse '--pkg foo=bar -- baz' 4 --pkg foo=bar baz
101 # long opt with explicit optarg
102 parse '--pkg bar -- foo baz' 5 foo --pkg=bar baz
104 # long opt with explicit optarg, with whitespace
105 parse '--pkg foo bar -- baz' 4 baz --pkg="foo bar"
107 # long opt with explicit optarg that doesn't take optarg
108 parse '--' 1 --force=always -s
110 # long opt with explicit optarg with =
111 parse '--pkg foo=bar --' 3 --pkg=foo=bar
113 # explicit end of options with options after
114 parse '-s -r -- foo bar baz' 6 -s -r -- foo bar baz
116 # non-option parameters mixed in with options
117 parse '-s -r -- foo baz' 5 -s foo baz -r
119 # optarg with whitespace
120 parse '-p foo bar -s --' 4 -p'foo bar' -s
122 # non-option parameter with whitespace
123 parse '-i -- foo bar' 3 -i 'foo bar'
125 # successful stem match (opt has no arg)
126 parse '--nocolor --' 2 --nocol
128 # successful stem match (opt has arg)
129 parse '--config foo --' 3 --conf foo
131 # ambiguous long opt
132 parse '--' 1 '--for'
134 # exact match on a possible stem (--force & --forcever)
135 parse '--force --' 2 --force
137 # exact match on possible stem (opt has optarg)
138 parse '--clean foo --' 3 --clean=foo