t_getopt.sh revision 1.1 1 1.1 pgoyette # $NetBSD: t_getopt.sh,v 1.1 2011/01/01 23:56:49 pgoyette 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.1 pgoyette atf_test_case getopt_long
69 1.1 pgoyette getopt_long_head()
70 1.1 pgoyette {
71 1.1 pgoyette atf_set "descr" "Checks getopt_long(3)"
72 1.1 pgoyette }
73 1.1 pgoyette getopt_long_body()
74 1.1 pgoyette {
75 1.1 pgoyette # GNU libc tests with these
76 1.1 pgoyette load="optstring: abc:
77 1.1 pgoyette longopts: 5
78 1.1 pgoyette longopt: required, required_argument, , 'r'
79 1.1 pgoyette longopt: optional, optional_argument, , 'o'
80 1.1 pgoyette longopt: none, no_argument, , 'n'
81 1.1 pgoyette longopt: color, no_argument, , 'C'
82 1.1 pgoyette longopt: colour, no_argument, , 'C'"
83 1.1 pgoyette
84 1.1 pgoyette h_getopt_long "${load}" "foo --req foobar" "-required=foobar|0"
85 1.1 pgoyette h_getopt_long "${load}" "foo --opt=bazbug" "-optional=bazbug|0"
86 1.1 pgoyette
87 1.1 pgoyette # This is problematic
88 1.1 pgoyette #
89 1.1 pgoyette # GNU libc 2.1.3 this fails with ambiguous result
90 1.1 pgoyette # h_getopt_long "${load}" "foo --col" "!?|0"
91 1.1 pgoyette #
92 1.1 pgoyette # GNU libc 2.2 >= this works
93 1.1 pgoyette h_getopt_long "${load}" "foo --col" "-color|0"
94 1.1 pgoyette
95 1.1 pgoyette h_getopt_long "${load}" "foo --colour" "-colour|0"
96 1.1 pgoyette
97 1.1 pgoyette # This is the real GNU libc test!
98 1.1 pgoyette args="foo -a -b -cfoobar --required foobar --optional=bazbug --none random --col --color --colour"
99 1.1 pgoyette # GNU libc 2.1.3 this fails with ambiguous
100 1.1 pgoyette #result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,!?,-color,-colour|1"
101 1.1 pgoyette #
102 1.1 pgoyette # GNU libc 2.2 >= this works
103 1.1 pgoyette result="a,b,c=foobar,-required=foobar,-optional=bazbug,-none,-color,-color,-colour|1"
104 1.1 pgoyette h_getopt_long "${load}" "${args}" "${result}"
105 1.1 pgoyette
106 1.1 pgoyette #
107 1.1 pgoyette # The order of long options in the long option array should not matter.
108 1.1 pgoyette # An exact match should never be treated as ambiguous.
109 1.1 pgoyette #
110 1.1 pgoyette load="optstring: fgl
111 1.1 pgoyette longopts: 3
112 1.1 pgoyette longopt: list-foobar, no_argument, lopt, 'f'
113 1.1 pgoyette longopt: list-goobar, no_argument, lopt, 'g'
114 1.1 pgoyette longopt: list, no_argument, lopt, 'l'"
115 1.1 pgoyette h_getopt_long "${load}" "foo --list" "-list|0"
116 1.1 pgoyette }
117 1.1 pgoyette
118 1.1 pgoyette
119 1.1 pgoyette atf_init_test_cases()
120 1.1 pgoyette {
121 1.1 pgoyette atf_add_test_case getopt
122 1.1 pgoyette atf_add_test_case getopt_long
123 1.1 pgoyette }
124