LCOV - code coverage report
Current view: top level - test - test-verbs.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 17 17 100.0 %
Date: 2019-08-22 15:41:25 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <getopt.h>
       4             : 
       5             : #include "macro.h"
       6             : #include "strv.h"
       7             : #include "verbs.h"
       8             : 
       9           3 : static int noop_dispatcher(int argc, char *argv[], void *userdata) {
      10           3 :         return 0;
      11             : }
      12             : 
      13             : #define test_dispatch_one(argv, verbs, expected) \
      14             :         optind = 0; \
      15             :         assert_se(dispatch_verb(strv_length(argv), argv, verbs, NULL) == expected);
      16             : 
      17           1 : static void test_verbs(void) {
      18             :         static const Verb verbs[] = {
      19             :                 { "help",        VERB_ANY, VERB_ANY, 0,            noop_dispatcher },
      20             :                 { "list-images", VERB_ANY, 1,        0,            noop_dispatcher },
      21             :                 { "list",        VERB_ANY, 2,        VERB_DEFAULT, noop_dispatcher },
      22             :                 { "status",      2,        VERB_ANY, 0,            noop_dispatcher },
      23             :                 { "show",        VERB_ANY, VERB_ANY, 0,            noop_dispatcher },
      24             :                 { "terminate",   2,        VERB_ANY, 0,            noop_dispatcher },
      25             :                 { "login",       2,        2,        0,            noop_dispatcher },
      26             :                 { "copy-to",     3,        4,        0,            noop_dispatcher },
      27             :                 {}
      28             :         };
      29             : 
      30             :         /* not found */
      31           1 :         test_dispatch_one(STRV_MAKE("command-not-found"), verbs, -EINVAL);
      32             : 
      33             :         /* found */
      34           1 :         test_dispatch_one(STRV_MAKE("show"), verbs, 0);
      35             : 
      36             :         /* found, too few args */
      37           1 :         test_dispatch_one(STRV_MAKE("copy-to", "foo"), verbs, -EINVAL);
      38             : 
      39             :         /* found, meets min args */
      40           1 :         test_dispatch_one(STRV_MAKE("status", "foo", "bar"), verbs, 0);
      41             : 
      42             :         /* found, too many args */
      43           1 :         test_dispatch_one(STRV_MAKE("copy-to", "foo", "bar", "baz", "quux", "qaax"), verbs, -EINVAL);
      44             : 
      45             :         /* no verb, but a default is set */
      46           1 :         test_dispatch_one(STRV_MAKE_EMPTY, verbs, 0);
      47           1 : }
      48             : 
      49           1 : static void test_verbs_no_default(void) {
      50             :         static const Verb verbs[] = {
      51             :                 { "help", VERB_ANY, VERB_ANY, 0, noop_dispatcher },
      52             :                 {},
      53             :         };
      54             : 
      55           1 :         test_dispatch_one(STRV_MAKE(NULL), verbs, -EINVAL);
      56           1 : }
      57             : 
      58           1 : int main(int argc, char *argv[]) {
      59           1 :         test_verbs();
      60           1 :         test_verbs_no_default();
      61             : 
      62           1 :         return 0;
      63             : }

Generated by: LCOV version 1.14