t / t0040-parse-options.shon commit Merge branch 'maint' (9b433e4)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Johannes Schindelin
   4#
   5
   6test_description='our own option parser'
   7
   8. ./test-lib.sh
   9
  10cat > expect.err << EOF
  11usage: test-parse-options <options>
  12
  13    -b, --boolean         get a boolean
  14    -i, --integer <n>     get a integer
  15    -j <n>                get a integer, too
  16
  17string options
  18    -s, --string <string>
  19                          get a string
  20    --string2 <str>       get another string
  21    --st <st>             get another string (pervert ordering)
  22
  23EOF
  24
  25test_expect_success 'test help' '
  26        ! test-parse-options -h > output 2> output.err &&
  27        test ! -s output &&
  28        git diff expect.err output.err
  29'
  30
  31cat > expect << EOF
  32boolean: 2
  33integer: 1729
  34string: 123
  35EOF
  36
  37test_expect_success 'short options' '
  38        test-parse-options -s123 -b -i 1729 -b > output 2> output.err &&
  39        git diff expect output &&
  40        test ! -s output.err
  41'
  42cat > expect << EOF
  43boolean: 2
  44integer: 1729
  45string: 321
  46EOF
  47
  48test_expect_success 'long options' '
  49        test-parse-options --boolean --integer 1729 --boolean --string2=321 \
  50                > output 2> output.err &&
  51        test ! -s output.err &&
  52        git diff expect output
  53'
  54
  55cat > expect << EOF
  56boolean: 1
  57integer: 13
  58string: 123
  59arg 00: a1
  60arg 01: b1
  61arg 02: --boolean
  62EOF
  63
  64test_expect_success 'intermingled arguments' '
  65        test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
  66                > output 2> output.err &&
  67        test ! -s output.err &&
  68        git diff expect output
  69'
  70
  71cat > expect << EOF
  72boolean: 0
  73integer: 2
  74string: (not set)
  75EOF
  76
  77test_expect_success 'unambiguously abbreviated option' '
  78        test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
  79        test ! -s output.err &&
  80        git diff expect output
  81'
  82
  83test_expect_success 'unambiguously abbreviated option with "="' '
  84        test-parse-options --int=2 > output 2> output.err &&
  85        test ! -s output.err &&
  86        git diff expect output
  87'
  88
  89test_expect_failure 'ambiguously abbreviated option' '
  90        test-parse-options --strin 123;
  91        test $? != 129
  92'
  93
  94cat > expect << EOF
  95boolean: 0
  96integer: 0
  97string: 123
  98EOF
  99
 100test_expect_success 'non ambiguous option (after two options it abbreviates)' '
 101        test-parse-options --st 123 > output 2> output.err &&
 102        test ! -s output.err &&
 103        git diff expect output
 104'
 105
 106test_done