]> granicus.if.org Git - transmission/commitdiff
add unit tests for tr-getopt
authorJordan Lee <jordan@transmissionbt.com>
Sat, 13 Apr 2013 18:27:36 +0000 (18:27 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Sat, 13 Apr 2013 18:27:36 +0000 (18:27 +0000)
libtransmission/Makefile.am
libtransmission/tr-getopt-test.c [new file with mode: 0644]

index f5658c435dcda05b35a711feb1b15acc20bf6f0f..0bc5c389df463da632a1d198abf9e72a7a815d52 100644 (file)
@@ -140,6 +140,7 @@ TESTS = \
   rename-test \
   rpc-test \
   test-peer-id \
+  tr-getopt-test \
   utils-test \
   variant-test
 
@@ -211,6 +212,10 @@ test_peer_id_SOURCES = test-peer-id.c $(TEST_SOURCES)
 test_peer_id_LDADD = ${apps_ldadd}
 test_peer_id_LDFLAGS = ${apps_ldflags}
 
+tr_getopt_test_SOURCES = tr-getopt-test.c $(TEST_SOURCES)
+tr_getopt_test_LDADD = ${apps_ldadd}
+tr_getopt_test_LDFLAGS = ${apps_ldflags}
+
 utils_test_SOURCES = utils-test.c $(TEST_SOURCES)
 utils_test_LDADD = ${apps_ldadd}
 utils_test_LDFLAGS = ${apps_ldflags}
diff --git a/libtransmission/tr-getopt-test.c b/libtransmission/tr-getopt-test.c
new file mode 100644 (file)
index 0000000..6344293
--- /dev/null
@@ -0,0 +1,191 @@
+#include "transmission.h"
+#include "tr-getopt.h"
+
+#include "libtransmission-test.h"
+
+static const struct tr_option options[] =
+{
+ { 'p', "private", "Allow this torrent to only be used with the specified tracker(s)", "p", 0, NULL },
+ { 'o', "outfile", "Save the generated .torrent to this filename", "o", 1, "<file>" },
+ { 's', "piecesize", "Set how many KiB each piece should be, overriding the preferred default", "s", 1, "<size in KiB>" },
+ { 'c', "comment", "Add a comment", "c", 1, "<comment>" },
+ { 't', "tracker", "Add a tracker's announce URL", "t", 1, "<url>" },
+ { 'q', "pooka", "Pooka", "pk", 0, NULL },
+ { 'V', "version", "Show version number and exit", "V", 0, NULL },
+ { 0, NULL, NULL, NULL, 0, NULL }
+};
+
+static int
+run_test (int           argc,
+          const char ** argv,
+          int           expected_n,
+          int         * expected_c,
+          const char ** expected_optarg)
+{
+  int c;
+  int n;
+  const char * optarg;
+
+  n = 0;
+  tr_optind = 1;
+  while ((c = tr_getopt ("summary", argc, argv, options, &optarg)))
+    {
+      check (n < expected_n);
+      check_int_eq (expected_c[n], c);
+      check_streq (optarg, expected_optarg[n]);
+      ++n;
+    }
+
+  check_int_eq (expected_n, n);
+  return 0;
+}
+
+/***
+****
+***/
+
+static int
+test_no_options (void)
+{
+  int argc = 1;
+  const char * argv[] = { "/some/path/tr-getopt-test" };
+  int expected_n = 0;
+  int expected_c[] = { };
+  const char * expected_optarg[] = { };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_short_noarg (void)
+{
+  int argc = 2;
+  const char * argv[] = { "/some/path/tr-getopt-test", "-p" };
+  int expected_n = 1;
+  int expected_c[] = { 'p' };
+  const char * expected_optarg[] = { NULL };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_long_noarg (void)
+{
+  int argc = 2;
+  const char * argv[] = { "/some/path/tr-getopt-test", "--private" };
+  int expected_n = 1;
+  int expected_c[] = { 'p' };
+  const char * expected_optarg[] = { NULL };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_short_with_arg (void)
+{
+  int argc = 3;
+  const char * argv[] = { "/some/path/tr-getopt-test", "-o", "/tmp/outfile" };
+  int expected_n = 1;
+  int expected_c[] = { 'o' };
+  const char * expected_optarg[] = { "/tmp/outfile" };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_long_with_arg (void)
+{
+  int argc = 3;
+  const char * argv[] = { "/some/path/tr-getopt-test", "--outfile", "/tmp/outfile" };
+  int expected_n = 1;
+  int expected_c[] = { 'o' };
+  const char * expected_optarg[] = { "/tmp/outfile" };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_short_with_arg_after_eq (void)
+{
+  int argc = 2;
+  const char * argv[] = { "/some/path/tr-getopt-test", "-o=/tmp/outfile" };
+  int expected_n = 1;
+  int expected_c[] = { 'o' };
+  const char * expected_optarg[] = { "/tmp/outfile" };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_long_with_arg_after_eq (void)
+{
+  int argc = 2;
+  const char * argv[] = { "/some/path/tr-getopt-test", "--outfile=/tmp/outfile" };
+  int expected_n = 1;
+  int expected_c[] = { 'o' };
+  const char * expected_optarg[] = { "/tmp/outfile" };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_unknown_option (void)
+{
+  int argc = 2;
+  const char * argv[] = { "/some/path/tr-getopt-test", "-z" };
+  int expected_n = 1;
+  int expected_c[] = { TR_OPT_UNK };
+  const char * expected_optarg[] = { "-z" };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_missing_arg (void)
+{
+  int argc = 2;
+  const char * argv[] = { "/some/path/tr-getopt-test", "-o" };
+  int expected_n = 1;
+  int expected_c[] = { TR_OPT_ERR };
+  const char * expected_optarg[] = { NULL };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_lots_of_options (void)
+{
+  int argc = 6;
+  const char * argv[] = { "/some/path/tr-getopt-test", "--piecesize=4", "-c", "hello world", "-p", "--tracker=foo" };
+  int expected_n = 4;
+  int expected_c[] = { 's', 'c', 'p', 't' };
+  const char * expected_optarg[] = { "4", "hello world", NULL, "foo" };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+static int
+test_match_longer_key (void)
+{
+  /* confirm that this resolves to 'q' and not 'p' */
+  int argc = 2;
+  const char * argv[] = { "/some/path/tr-getopt-test", "-pk" };
+  int expected_n = 1;
+  int expected_c[] = { 'q' };
+  const char * expected_optarg[] = { NULL };
+  return run_test (argc, argv, expected_n, expected_c, expected_optarg);
+}
+
+
+/***
+****
+***/
+
+int
+main (void)
+{
+  const testFunc tests[] = { test_no_options,
+                             test_short_noarg,
+                             test_long_noarg,
+                             test_short_with_arg,
+                             test_long_with_arg,
+                             test_short_with_arg_after_eq,
+                             test_long_with_arg_after_eq,
+                             test_unknown_option,
+                             test_missing_arg,
+                             test_match_longer_key,
+                             test_lots_of_options };
+
+  return runTests (tests, NUM_TESTS (tests));
+}
+