1 | /* |
---|
2 | * This file Copyright (C) 2013-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: tr-getopt-test.c 14241 2014-01-21 03:10:30Z jordan $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include "transmission.h" |
---|
11 | #include "tr-getopt.h" |
---|
12 | |
---|
13 | #include "libtransmission-test.h" |
---|
14 | |
---|
15 | static const struct tr_option options[] = |
---|
16 | { |
---|
17 | { 'p', "private", "Allow this torrent to only be used with the specified tracker(s)", "p", 0, NULL }, |
---|
18 | { 'o', "outfile", "Save the generated .torrent to this filename", "o", 1, "<file>" }, |
---|
19 | { 's', "piecesize", "Set how many KiB each piece should be, overriding the preferred default", "s", 1, "<size in KiB>" }, |
---|
20 | { 'c', "comment", "Add a comment", "c", 1, "<comment>" }, |
---|
21 | { 't', "tracker", "Add a tracker's announce URL", "t", 1, "<url>" }, |
---|
22 | { 'q', "pooka", "Pooka", "pk", 0, NULL }, |
---|
23 | { 'V', "version", "Show version number and exit", "V", 0, NULL }, |
---|
24 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
25 | }; |
---|
26 | |
---|
27 | static int |
---|
28 | run_test (int argc, |
---|
29 | const char ** argv, |
---|
30 | int expected_n, |
---|
31 | int * expected_c, |
---|
32 | const char ** expected_optarg) |
---|
33 | { |
---|
34 | int c; |
---|
35 | int n; |
---|
36 | const char * optarg; |
---|
37 | |
---|
38 | n = 0; |
---|
39 | tr_optind = 1; |
---|
40 | while ((c = tr_getopt ("summary", argc, argv, options, &optarg))) |
---|
41 | { |
---|
42 | check (n < expected_n); |
---|
43 | check_int_eq (expected_c[n], c); |
---|
44 | check_streq (optarg, expected_optarg[n]); |
---|
45 | ++n; |
---|
46 | } |
---|
47 | |
---|
48 | check_int_eq (expected_n, n); |
---|
49 | return 0; |
---|
50 | } |
---|
51 | |
---|
52 | /*** |
---|
53 | **** |
---|
54 | ***/ |
---|
55 | |
---|
56 | static int |
---|
57 | test_no_options (void) |
---|
58 | { |
---|
59 | int argc = 1; |
---|
60 | const char * argv[] = { "/some/path/tr-getopt-test" }; |
---|
61 | int expected_n = 0; |
---|
62 | int expected_c[] = { 0 }; |
---|
63 | const char * expected_optarg[] = { NULL }; |
---|
64 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
65 | } |
---|
66 | |
---|
67 | static int |
---|
68 | test_short_noarg (void) |
---|
69 | { |
---|
70 | int argc = 2; |
---|
71 | const char * argv[] = { "/some/path/tr-getopt-test", "-p" }; |
---|
72 | int expected_n = 1; |
---|
73 | int expected_c[] = { 'p' }; |
---|
74 | const char * expected_optarg[] = { NULL }; |
---|
75 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
76 | } |
---|
77 | |
---|
78 | static int |
---|
79 | test_long_noarg (void) |
---|
80 | { |
---|
81 | int argc = 2; |
---|
82 | const char * argv[] = { "/some/path/tr-getopt-test", "--private" }; |
---|
83 | int expected_n = 1; |
---|
84 | int expected_c[] = { 'p' }; |
---|
85 | const char * expected_optarg[] = { NULL }; |
---|
86 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
87 | } |
---|
88 | |
---|
89 | static int |
---|
90 | test_short_with_arg (void) |
---|
91 | { |
---|
92 | int argc = 3; |
---|
93 | const char * argv[] = { "/some/path/tr-getopt-test", "-o", "/tmp/outfile" }; |
---|
94 | int expected_n = 1; |
---|
95 | int expected_c[] = { 'o' }; |
---|
96 | const char * expected_optarg[] = { "/tmp/outfile" }; |
---|
97 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
98 | } |
---|
99 | |
---|
100 | static int |
---|
101 | test_long_with_arg (void) |
---|
102 | { |
---|
103 | int argc = 3; |
---|
104 | const char * argv[] = { "/some/path/tr-getopt-test", "--outfile", "/tmp/outfile" }; |
---|
105 | int expected_n = 1; |
---|
106 | int expected_c[] = { 'o' }; |
---|
107 | const char * expected_optarg[] = { "/tmp/outfile" }; |
---|
108 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
109 | } |
---|
110 | |
---|
111 | static int |
---|
112 | test_short_with_arg_after_eq (void) |
---|
113 | { |
---|
114 | int argc = 2; |
---|
115 | const char * argv[] = { "/some/path/tr-getopt-test", "-o=/tmp/outfile" }; |
---|
116 | int expected_n = 1; |
---|
117 | int expected_c[] = { 'o' }; |
---|
118 | const char * expected_optarg[] = { "/tmp/outfile" }; |
---|
119 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
120 | } |
---|
121 | |
---|
122 | static int |
---|
123 | test_long_with_arg_after_eq (void) |
---|
124 | { |
---|
125 | int argc = 2; |
---|
126 | const char * argv[] = { "/some/path/tr-getopt-test", "--outfile=/tmp/outfile" }; |
---|
127 | int expected_n = 1; |
---|
128 | int expected_c[] = { 'o' }; |
---|
129 | const char * expected_optarg[] = { "/tmp/outfile" }; |
---|
130 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
131 | } |
---|
132 | |
---|
133 | static int |
---|
134 | test_unknown_option (void) |
---|
135 | { |
---|
136 | int argc = 2; |
---|
137 | const char * argv[] = { "/some/path/tr-getopt-test", "-z" }; |
---|
138 | int expected_n = 1; |
---|
139 | int expected_c[] = { TR_OPT_UNK }; |
---|
140 | const char * expected_optarg[] = { "-z" }; |
---|
141 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
142 | } |
---|
143 | |
---|
144 | static int |
---|
145 | test_missing_arg (void) |
---|
146 | { |
---|
147 | int argc = 2; |
---|
148 | const char * argv[] = { "/some/path/tr-getopt-test", "-o" }; |
---|
149 | int expected_n = 1; |
---|
150 | int expected_c[] = { TR_OPT_ERR }; |
---|
151 | const char * expected_optarg[] = { NULL }; |
---|
152 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
153 | } |
---|
154 | |
---|
155 | static int |
---|
156 | test_lots_of_options (void) |
---|
157 | { |
---|
158 | int argc = 6; |
---|
159 | const char * argv[] = { "/some/path/tr-getopt-test", "--piecesize=4", "-c", "hello world", "-p", "--tracker=foo" }; |
---|
160 | int expected_n = 4; |
---|
161 | int expected_c[] = { 's', 'c', 'p', 't' }; |
---|
162 | const char * expected_optarg[] = { "4", "hello world", NULL, "foo" }; |
---|
163 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
164 | } |
---|
165 | |
---|
166 | static int |
---|
167 | test_match_longer_key (void) |
---|
168 | { |
---|
169 | /* confirm that this resolves to 'q' and not 'p' */ |
---|
170 | int argc = 2; |
---|
171 | const char * argv[] = { "/some/path/tr-getopt-test", "-pk" }; |
---|
172 | int expected_n = 1; |
---|
173 | int expected_c[] = { 'q' }; |
---|
174 | const char * expected_optarg[] = { NULL }; |
---|
175 | return run_test (argc, argv, expected_n, expected_c, expected_optarg); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | /*** |
---|
180 | **** |
---|
181 | ***/ |
---|
182 | |
---|
183 | int |
---|
184 | main (void) |
---|
185 | { |
---|
186 | const testFunc tests[] = { test_no_options, |
---|
187 | test_short_noarg, |
---|
188 | test_long_noarg, |
---|
189 | test_short_with_arg, |
---|
190 | test_long_with_arg, |
---|
191 | test_short_with_arg_after_eq, |
---|
192 | test_long_with_arg_after_eq, |
---|
193 | test_unknown_option, |
---|
194 | test_missing_arg, |
---|
195 | test_match_longer_key, |
---|
196 | test_lots_of_options }; |
---|
197 | |
---|
198 | return runTests (tests, NUM_TESTS (tests)); |
---|
199 | } |
---|
200 | |
---|