Home | History | Annotate | Line # | Download | only in stdlib
t_getopt.sh revision 1.3
      1  1.3  gutterid # $NetBSD: t_getopt.sh,v 1.3 2023/05/10 23:44:15 gutteridge Exp $
      2  1.1  pgoyette #
      3  1.1  pgoyette # Copyright (c) 2008 The NetBSD Foundation, Inc.
      4  1.1  pgoyette # All rights reserved.
      5  1.1  pgoyette #
      6  1.1  pgoyette # Redistribution and use in source and binary forms, with or without
      7  1.1  pgoyette # modification, are permitted provided that the following conditions
      8  1.1  pgoyette # are met:
      9  1.1  pgoyette # 1. Redistributions of source code must retain the above copyright
     10  1.1  pgoyette #    notice, this list of conditions and the following disclaimer.
     11  1.1  pgoyette # 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pgoyette #    notice, this list of conditions and the following disclaimer in the
     13  1.1  pgoyette #    documentation and/or other materials provided with the distribution.
     14  1.1  pgoyette #
     15  1.1  pgoyette # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  1.1  pgoyette # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  1.1  pgoyette # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  1.1  pgoyette # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  1.1  pgoyette # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  1.1  pgoyette # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  1.1  pgoyette # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  1.1  pgoyette # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  1.1  pgoyette # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  1.1  pgoyette # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  pgoyette # POSSIBILITY OF SUCH DAMAGE.
     26  1.1  pgoyette #
     27  1.1  pgoyette 
     28  1.1  pgoyette h_getopt()
     29  1.1  pgoyette {
     30  1.1  pgoyette 	atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt" <<EOF
     31  1.1  pgoyette load:	$1
     32  1.1  pgoyette args:	$2
     33  1.1  pgoyette result:	$3
     34  1.1  pgoyette EOF
     35  1.1  pgoyette 	cat stderr
     36  1.1  pgoyette 	rm -f stderr
     37  1.1  pgoyette }
     38  1.1  pgoyette 
     39  1.1  pgoyette h_getopt_long()
     40  1.1  pgoyette {
     41  1.1  pgoyette 	atf_check -e save:stderr -x "$(atf_get_srcdir)/h_getopt_long" <<EOF
     42  1.1  pgoyette $1
     43  1.1  pgoyette args:	$2
     44  1.1  pgoyette result:	$3
     45  1.1  pgoyette EOF
     46  1.1  pgoyette 	cat stderr
     47  1.1  pgoyette 	rm -f stderr
     48  1.1  pgoyette }
     49  1.1  pgoyette 
     50  1.1  pgoyette atf_test_case getopt
     51  1.1  pgoyette getopt_head()
     52  1.1  pgoyette {
     53  1.1  pgoyette 	atf_set "descr" "Checks getopt(3)"
     54  1.1  pgoyette }
     55  1.1  pgoyette getopt_body()
     56  1.1  pgoyette {
     57  1.1  pgoyette 	load="c:d"
     58  1.1  pgoyette 
     59  1.1  pgoyette 	h_getopt "${load}" "foo -c 1 -d foo" "c=1,d|1"
     60  1.1  pgoyette 	h_getopt "${load}" "foo -d foo bar" "d|2"
     61  1.1  pgoyette 	h_getopt "${load}" "foo -c 2 foo bar" "c=2|2"
     62  1.1  pgoyette 	h_getopt "${load}" "foo -e 1 foo bar" "!?|3"
     63  1.1  pgoyette 	h_getopt "${load}" "foo -d -- -c 1" "d|2"
     64  1.1  pgoyette 	h_getopt "${load}" "foo -c- 1" "c=-|1"
     65  1.1  pgoyette 	h_getopt "${load}" "foo -d - 1" "d|2"
     66  1.1  pgoyette }
     67  1.1  pgoyette 
     68  1.2  christos atf_test_case getopt_optval
     69  1.3  gutterid getopt_optval_head()
     70  1.2  christos {
     71  1.2  christos 	atf_set "descr" "Checks getopt(3) with optional value"
     72  1.2  christos }
     73  1.2  christos getopt_optval_body()
     74  1.2  christos {
     75  1.2  christos 	h_getopt "o::" "foo -o" "o=(null)|0"
     76  1.2  christos 	h_getopt "o::" "foo -o1 2" "o=1|1"
     77  1.2  christos 	h_getopt "o::" "foo -o 1 2" "o=(null)|2"
     78  1.2  christos }
     79  1.2  christos 
     80  1.1  pgoyette atf_test_case getopt_long
     81  1.1  pgoyette getopt_long_head()
     82  1.1  pgoyette {
     83  1.1  pgoyette 	atf_set "descr" "Checks getopt_long(3)"
     84  1.1  pgoyette }
     85  1.1  pgoyette getopt_long_body()
     86  1.1  pgoyette {
     87  1.1  pgoyette 	# GNU libc tests with these
     88  1.1  pgoyette 	load="optstring:	abc:
     89  1.1  pgoyette longopts:	5
     90  1.1  pgoyette longopt:	required, required_argument, , 'r'
     91  1.1  pgoyette longopt:	optional, optional_argument, , 'o'
     92  1.1  pgoyette longopt:	none, no_argument, , 'n'
     93  1.1  pgoyette longopt:	color, no_argument, , 'C'
     94  1.1  pgoyette longopt:	colour, no_argument, , 'C'"
     95  1.1  pgoyette 
     96  1.1  pgoyette 	h_getopt_long "${load}" "foo --req foobar" "-required=foobar|0"
     97  1.1  pgoyette 	h_getopt_long "${load}" "foo --opt=bazbug" "-optional=bazbug|0"
     98  1.1  pgoyette 	
     99  1.1  pgoyette 	# This is problematic
    100  1.1  pgoyette 	#
    101  1.1  pgoyette 	# GNU libc 2.1.3 this fails with ambiguous result
    102  1.1  pgoyette 	# h_getopt_long "${load}" "foo --col" "!?|0"
    103  1.1  pgoyette 	#
    104  1.1  pgoyette 	# GNU libc 2.2 >= this works
    105  1.1  pgoyette 	h_getopt_long "${load}" "foo --col" "-color|0"
    106  1.1  pgoyette 
    107  1.1  pgoyette 	h_getopt_long "${load}" "foo --colour" "-colour|0"
    108  1.1  pgoyette 
    109  1.1  pgoyette 	# This is the real GNU libc test!
    110  1.1  pgoyette 	args="foo -a -b -cfoobar --required foobar --optional=bazbug --none random --col --color --colour"
    111  1.1  pgoyette 	# GNU libc 2.1.3 this fails with ambiguous
    112  1.1  pgoyette 	#result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,!?,-color,-colour|1"
    113  1.1  pgoyette 	#
    114  1.1  pgoyette 	# GNU libc 2.2 >= this works
    115  1.1  pgoyette 	result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,-color,-color,-colour|1"
    116  1.1  pgoyette 	h_getopt_long "${load}" "${args}" "${result}"
    117  1.1  pgoyette 
    118  1.1  pgoyette 	#
    119  1.1  pgoyette 	# The order of long options in the long option array should not matter.
    120  1.1  pgoyette 	# An exact match should never be treated as ambiguous.
    121  1.1  pgoyette 	#
    122  1.1  pgoyette 	load="optstring:	fgl
    123  1.1  pgoyette longopts:	3
    124  1.1  pgoyette longopt:	list-foobar, no_argument, lopt, 'f'
    125  1.1  pgoyette longopt:	list-goobar, no_argument, lopt, 'g'
    126  1.1  pgoyette longopt:	list, no_argument, lopt, 'l'"
    127  1.1  pgoyette 	h_getopt_long "${load}" "foo --list" "-list|0"
    128  1.1  pgoyette }
    129  1.1  pgoyette 
    130  1.1  pgoyette 
    131  1.1  pgoyette atf_init_test_cases()
    132  1.1  pgoyette {
    133  1.1  pgoyette 	atf_add_test_case getopt
    134  1.2  christos 	atf_add_test_case getopt_optval
    135  1.1  pgoyette 	atf_add_test_case getopt_long
    136  1.1  pgoyette }
    137