Code

test-subprocess: fix segfault without arguments
[git.git] / t / t0040-parse-options.sh
index a1e4616febe6c67f67ab2b4380610abc92468ba5..e3f354a45e382dbaa9de4557aa9310fa21eb626b 100755 (executable)
@@ -10,7 +10,10 @@ test_description='our own option parser'
 cat > expect << EOF
 usage: test-parse-options <options>
 
-    -b, --boolean         get a boolean
+    --yes                 get a boolean
+    -D, --no-doubt        begins with 'no-'
+    -B, --no-fear         be brave
+    -b, --boolean         increment by one
     -4, --or4             bitwise-or boolean with ...0100
     --neg-or4             same as --no-or4
 
@@ -53,6 +56,59 @@ test_expect_success 'test help' '
 
 mv expect expect.err
 
+cat >expect.template <<EOF
+boolean: 0
+integer: 0
+timestamp: 0
+string: (not set)
+abbrev: 7
+verbose: 0
+quiet: no
+dry run: no
+file: (not set)
+EOF
+
+check() {
+       what="$1" &&
+       shift &&
+       expect="$1" &&
+       shift &&
+       sed "s/^$what .*/$what $expect/" <expect.template >expect &&
+       test-parse-options $* >output 2>output.err &&
+       test ! -s output.err &&
+       test_cmp expect output
+}
+
+check_unknown() {
+       case "$1" in
+       --*)
+               echo error: unknown option \`${1#--}\' >expect ;;
+       -*)
+               echo error: unknown switch \`${1#-}\' >expect ;;
+       esac &&
+       cat expect.err >>expect &&
+       test_must_fail test-parse-options $* >output 2>output.err &&
+       test ! -s output &&
+       test_cmp expect output.err
+}
+
+test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
+test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
+test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
+test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
+test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
+
+test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
+test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
+
+test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
+test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
+
+test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown --fear'
+test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown --no-no-fear'
+
+test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
+
 cat > expect << EOF
 boolean: 2
 integer: 1729
@@ -180,6 +236,16 @@ test_expect_success 'detect possible typos' '
        test_cmp typo.err output.err
 '
 
+cat > typo.err << EOF
+error: did you mean \`--ambiguous\` (with two dashes ?)
+EOF
+
+test_expect_success 'detect possible typos' '
+       test_must_fail test-parse-options -ambiguous > output 2> output.err &&
+       test ! -s output &&
+       test_cmp typo.err output.err
+'
+
 cat > expect <<EOF
 boolean: 0
 integer: 0
@@ -296,7 +362,7 @@ test_expect_success 'OPT_NEGBIT() works' '
        test_cmp expect output
 '
 
-test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
+test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
        test-parse-options + + + + + + > output 2> output.err &&
        test ! -s output.err &&
        test_cmp expect output