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