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