1272343Sngie# $NetBSD: t_getopt.sh,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ 2272343Sngie# 3272343Sngie# Copyright (c) 2008 The NetBSD Foundation, Inc. 4272343Sngie# All rights reserved. 5272343Sngie# 6272343Sngie# Redistribution and use in source and binary forms, with or without 7272343Sngie# modification, are permitted provided that the following conditions 8272343Sngie# are met: 9272343Sngie# 1. Redistributions of source code must retain the above copyright 10272343Sngie# notice, this list of conditions and the following disclaimer. 11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright 12272343Sngie# notice, this list of conditions and the following disclaimer in the 13272343Sngie# documentation and/or other materials provided with the distribution. 14272343Sngie# 15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18272343Sngie# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25272343Sngie# POSSIBILITY OF SUCH DAMAGE. 26272343Sngie# 27272343Sngie 28272343Sngieh_getopt() 29272343Sngie{ 30272343Sngie atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt" <<EOF 31272343Sngieload: $1 32272343Sngieargs: $2 33272343Sngieresult: $3 34272343SngieEOF 35272343Sngie cat stderr 36272343Sngie rm -f stderr 37272343Sngie} 38272343Sngie 39272343Sngieh_getopt_long() 40272343Sngie{ 41272343Sngie atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt_long" <<EOF 42272343Sngie$1 43272343Sngieargs: $2 44272343Sngieresult: $3 45272343SngieEOF 46272343Sngie cat stderr 47272343Sngie rm -f stderr 48272343Sngie} 49272343Sngie 50272343Sngieatf_test_case getopt 51272343Sngiegetopt_head() 52272343Sngie{ 53272343Sngie atf_set "descr" "Checks getopt(3)" 54272343Sngie} 55272343Sngiegetopt_body() 56272343Sngie{ 57272343Sngie load="c:d" 58272343Sngie 59272343Sngie h_getopt "${load}" "foo -c 1 -d foo" "c=1,d|1" 60272343Sngie h_getopt "${load}" "foo -d foo bar" "d|2" 61272343Sngie h_getopt "${load}" "foo -c 2 foo bar" "c=2|2" 62272343Sngie h_getopt "${load}" "foo -e 1 foo bar" "!?|3" 63272343Sngie h_getopt "${load}" "foo -d -- -c 1" "d|2" 64272343Sngie h_getopt "${load}" "foo -c- 1" "c=-|1" 65272343Sngie h_getopt "${load}" "foo -d - 1" "d|2" 66272343Sngie} 67272343Sngie 68272343Sngieatf_test_case getopt_long 69272343Sngiegetopt_long_head() 70272343Sngie{ 71272343Sngie atf_set "descr" "Checks getopt_long(3)" 72272343Sngie} 73272343Sngiegetopt_long_body() 74272343Sngie{ 75272343Sngie # GNU libc tests with these 76272343Sngie load="optstring: abc: 77272343Sngielongopts: 5 78272343Sngielongopt: required, required_argument, , 'r' 79272343Sngielongopt: optional, optional_argument, , 'o' 80272343Sngielongopt: none, no_argument, , 'n' 81272343Sngielongopt: color, no_argument, , 'C' 82272343Sngielongopt: colour, no_argument, , 'C'" 83272343Sngie 84272343Sngie h_getopt_long "${load}" "foo --req foobar" "-required=foobar|0" 85272343Sngie h_getopt_long "${load}" "foo --opt=bazbug" "-optional=bazbug|0" 86272343Sngie 87272343Sngie # This is problematic 88272343Sngie # 89272343Sngie # GNU libc 2.1.3 this fails with ambiguous result 90272343Sngie # h_getopt_long "${load}" "foo --col" "!?|0" 91272343Sngie # 92272343Sngie # GNU libc 2.2 >= this works 93272343Sngie h_getopt_long "${load}" "foo --col" "-color|0" 94272343Sngie 95272343Sngie h_getopt_long "${load}" "foo --colour" "-colour|0" 96272343Sngie 97272343Sngie # This is the real GNU libc test! 98272343Sngie args="foo -a -b -cfoobar --required foobar --optional=bazbug --none random --col --color --colour" 99272343Sngie # GNU libc 2.1.3 this fails with ambiguous 100272343Sngie #result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,!?,-color,-colour|1" 101272343Sngie # 102272343Sngie # GNU libc 2.2 >= this works 103272343Sngie result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,-color,-color,-colour|1" 104272343Sngie h_getopt_long "${load}" "${args}" "${result}" 105272343Sngie 106272343Sngie # 107272343Sngie # The order of long options in the long option array should not matter. 108272343Sngie # An exact match should never be treated as ambiguous. 109272343Sngie # 110272343Sngie load="optstring: fgl 111272343Sngielongopts: 3 112272343Sngielongopt: list-foobar, no_argument, lopt, 'f' 113272343Sngielongopt: list-goobar, no_argument, lopt, 'g' 114272343Sngielongopt: list, no_argument, lopt, 'l'" 115272343Sngie h_getopt_long "${load}" "foo --list" "-list|0" 116272343Sngie} 117272343Sngie 118272343Sngie 119272343Sngieatf_init_test_cases() 120272343Sngie{ 121272343Sngie atf_add_test_case getopt 122272343Sngie atf_add_test_case getopt_long 123272343Sngie} 124