t / t0040-parse-options.shon commit Merge branch 'maint' (27b4070)
   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    -o <str>              get another string
  23
  24EOF
  25
  26test_expect_success 'test help' '
  27        ! test-parse-options -h > output 2> output.err &&
  28        test ! -s output &&
  29        git diff expect.err output.err
  30'
  31
  32cat > expect << EOF
  33boolean: 2
  34integer: 1729
  35string: 123
  36EOF
  37
  38test_expect_success 'short options' '
  39        test-parse-options -s123 -b -i 1729 -b > output 2> output.err &&
  40        git diff expect output &&
  41        test ! -s output.err
  42'
  43cat > expect << EOF
  44boolean: 2
  45integer: 1729
  46string: 321
  47EOF
  48
  49test_expect_success 'long options' '
  50        test-parse-options --boolean --integer 1729 --boolean --string2=321 \
  51                > output 2> output.err &&
  52        test ! -s output.err &&
  53        git diff expect output
  54'
  55
  56cat > expect << EOF
  57boolean: 1
  58integer: 13
  59string: 123
  60arg 00: a1
  61arg 01: b1
  62arg 02: --boolean
  63EOF
  64
  65test_expect_success 'intermingled arguments' '
  66        test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
  67                > output 2> output.err &&
  68        test ! -s output.err &&
  69        git diff expect output
  70'
  71
  72cat > expect << EOF
  73boolean: 0
  74integer: 2
  75string: (not set)
  76EOF
  77
  78test_expect_success 'unambiguously abbreviated option' '
  79        test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
  80        test ! -s output.err &&
  81        git diff expect output
  82'
  83
  84test_expect_success 'unambiguously abbreviated option with "="' '
  85        test-parse-options --int=2 > output 2> output.err &&
  86        test ! -s output.err &&
  87        git diff expect output
  88'
  89
  90test_expect_success 'ambiguously abbreviated option' '
  91        test-parse-options --strin 123;
  92        test $? = 129
  93'
  94
  95cat > expect << EOF
  96boolean: 0
  97integer: 0
  98string: 123
  99EOF
 100
 101test_expect_success 'non ambiguous option (after two options it abbreviates)' '
 102        test-parse-options --st 123 > output 2> output.err &&
 103        test ! -s output.err &&
 104        git diff expect output
 105'
 106
 107cat > expect.err << EOF
 108error: did you mean \`--boolean\` (with two dashes ?)
 109EOF
 110
 111test_expect_success 'detect possible typos' '
 112        ! test-parse-options -boolean > output 2> output.err &&
 113        test ! -s output &&
 114        git diff expect.err output.err
 115'
 116
 117test_done