LCOV - code coverage report
Current view: top level - test/core/support - cmdline_test.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 202 202 100.0 %
Date: 2015-10-10 Functions: 17 17 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include <grpc/support/cmdline.h>
      35             : 
      36             : #include <string.h>
      37             : 
      38             : #include <grpc/support/alloc.h>
      39             : #include <grpc/support/log.h>
      40             : #include <grpc/support/useful.h>
      41             : #include "test/core/util/test_config.h"
      42             : 
      43             : #define LOG_TEST() gpr_log(GPR_INFO, "%s", __FILE__)
      44             : 
      45           1 : static void test_simple_int(void) {
      46           1 :   int x = 1;
      47             :   gpr_cmdline *cl;
      48           1 :   char *args[] = {(char *)__FILE__, "-foo", "3"};
      49             : 
      50           1 :   LOG_TEST();
      51             : 
      52           1 :   cl = gpr_cmdline_create(NULL);
      53           1 :   gpr_cmdline_add_int(cl, "foo", NULL, &x);
      54           1 :   GPR_ASSERT(x == 1);
      55           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
      56           1 :   GPR_ASSERT(x == 3);
      57           1 :   gpr_cmdline_destroy(cl);
      58           1 : }
      59             : 
      60           1 : static void test_eq_int(void) {
      61           1 :   int x = 1;
      62             :   gpr_cmdline *cl;
      63           1 :   char *args[] = {(char *)__FILE__, "-foo=3"};
      64             : 
      65           1 :   LOG_TEST();
      66             : 
      67           1 :   cl = gpr_cmdline_create(NULL);
      68           1 :   gpr_cmdline_add_int(cl, "foo", NULL, &x);
      69           1 :   GPR_ASSERT(x == 1);
      70           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
      71           1 :   GPR_ASSERT(x == 3);
      72           1 :   gpr_cmdline_destroy(cl);
      73           1 : }
      74             : 
      75           1 : static void test_2dash_int(void) {
      76           1 :   int x = 1;
      77             :   gpr_cmdline *cl;
      78           1 :   char *args[] = {(char *)__FILE__, "--foo", "3"};
      79             : 
      80           1 :   LOG_TEST();
      81             : 
      82           1 :   cl = gpr_cmdline_create(NULL);
      83           1 :   gpr_cmdline_add_int(cl, "foo", NULL, &x);
      84           1 :   GPR_ASSERT(x == 1);
      85           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
      86           1 :   GPR_ASSERT(x == 3);
      87           1 :   gpr_cmdline_destroy(cl);
      88           1 : }
      89             : 
      90           1 : static void test_2dash_eq_int(void) {
      91           1 :   int x = 1;
      92             :   gpr_cmdline *cl;
      93           1 :   char *args[] = {(char *)__FILE__, "--foo=3"};
      94             : 
      95           1 :   LOG_TEST();
      96             : 
      97           1 :   cl = gpr_cmdline_create(NULL);
      98           1 :   gpr_cmdline_add_int(cl, "foo", NULL, &x);
      99           1 :   GPR_ASSERT(x == 1);
     100           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     101           1 :   GPR_ASSERT(x == 3);
     102           1 :   gpr_cmdline_destroy(cl);
     103           1 : }
     104             : 
     105           1 : static void test_simple_string(void) {
     106           1 :   char *x = NULL;
     107             :   gpr_cmdline *cl;
     108           1 :   char *args[] = {(char *)__FILE__, "-foo", "3"};
     109             : 
     110           1 :   LOG_TEST();
     111             : 
     112           1 :   cl = gpr_cmdline_create(NULL);
     113           1 :   gpr_cmdline_add_string(cl, "foo", NULL, &x);
     114           1 :   GPR_ASSERT(x == NULL);
     115           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     116           1 :   GPR_ASSERT(0 == strcmp(x, "3"));
     117           1 :   gpr_cmdline_destroy(cl);
     118           1 : }
     119             : 
     120           1 : static void test_eq_string(void) {
     121           1 :   char *x = NULL;
     122             :   gpr_cmdline *cl;
     123           1 :   char *args[] = {(char *)__FILE__, "-foo=3"};
     124             : 
     125           1 :   LOG_TEST();
     126             : 
     127           1 :   cl = gpr_cmdline_create(NULL);
     128           1 :   gpr_cmdline_add_string(cl, "foo", NULL, &x);
     129           1 :   GPR_ASSERT(x == NULL);
     130           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     131           1 :   GPR_ASSERT(0 == strcmp(x, "3"));
     132           1 :   gpr_cmdline_destroy(cl);
     133           1 : }
     134             : 
     135           1 : static void test_2dash_string(void) {
     136           1 :   char *x = NULL;
     137             :   gpr_cmdline *cl;
     138           1 :   char *args[] = {(char *)__FILE__, "--foo", "3"};
     139             : 
     140           1 :   LOG_TEST();
     141             : 
     142           1 :   cl = gpr_cmdline_create(NULL);
     143           1 :   gpr_cmdline_add_string(cl, "foo", NULL, &x);
     144           1 :   GPR_ASSERT(x == NULL);
     145           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     146           1 :   GPR_ASSERT(0 == strcmp(x, "3"));
     147           1 :   gpr_cmdline_destroy(cl);
     148           1 : }
     149             : 
     150           1 : static void test_2dash_eq_string(void) {
     151           1 :   char *x = NULL;
     152             :   gpr_cmdline *cl;
     153           1 :   char *args[] = {(char *)__FILE__, "--foo=3"};
     154             : 
     155           1 :   LOG_TEST();
     156             : 
     157           1 :   cl = gpr_cmdline_create(NULL);
     158           1 :   gpr_cmdline_add_string(cl, "foo", NULL, &x);
     159           1 :   GPR_ASSERT(x == NULL);
     160           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     161           1 :   GPR_ASSERT(0 == strcmp(x, "3"));
     162           1 :   gpr_cmdline_destroy(cl);
     163           1 : }
     164             : 
     165           1 : static void test_flag_on(void) {
     166           1 :   int x = 2;
     167             :   gpr_cmdline *cl;
     168           1 :   char *args[] = {(char *)__FILE__, "--foo"};
     169             : 
     170           1 :   LOG_TEST();
     171             : 
     172           1 :   cl = gpr_cmdline_create(NULL);
     173           1 :   gpr_cmdline_add_flag(cl, "foo", NULL, &x);
     174           1 :   GPR_ASSERT(x == 2);
     175           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     176           1 :   GPR_ASSERT(x == 1);
     177           1 :   gpr_cmdline_destroy(cl);
     178           1 : }
     179             : 
     180           1 : static void test_flag_no(void) {
     181           1 :   int x = 2;
     182             :   gpr_cmdline *cl;
     183           1 :   char *args[] = {(char *)__FILE__, "--no-foo"};
     184             : 
     185           1 :   LOG_TEST();
     186             : 
     187           1 :   cl = gpr_cmdline_create(NULL);
     188           1 :   gpr_cmdline_add_flag(cl, "foo", NULL, &x);
     189           1 :   GPR_ASSERT(x == 2);
     190           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     191           1 :   GPR_ASSERT(x == 0);
     192           1 :   gpr_cmdline_destroy(cl);
     193           1 : }
     194             : 
     195           1 : static void test_flag_val_1(void) {
     196           1 :   int x = 2;
     197             :   gpr_cmdline *cl;
     198           1 :   char *args[] = {(char *)__FILE__, "--foo=1"};
     199             : 
     200           1 :   LOG_TEST();
     201             : 
     202           1 :   cl = gpr_cmdline_create(NULL);
     203           1 :   gpr_cmdline_add_flag(cl, "foo", NULL, &x);
     204           1 :   GPR_ASSERT(x == 2);
     205           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     206           1 :   GPR_ASSERT(x == 1);
     207           1 :   gpr_cmdline_destroy(cl);
     208           1 : }
     209             : 
     210           1 : static void test_flag_val_0(void) {
     211           1 :   int x = 2;
     212             :   gpr_cmdline *cl;
     213           1 :   char *args[] = {(char *)__FILE__, "--foo=0"};
     214             : 
     215           1 :   LOG_TEST();
     216             : 
     217           1 :   cl = gpr_cmdline_create(NULL);
     218           1 :   gpr_cmdline_add_flag(cl, "foo", NULL, &x);
     219           1 :   GPR_ASSERT(x == 2);
     220           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     221           1 :   GPR_ASSERT(x == 0);
     222           1 :   gpr_cmdline_destroy(cl);
     223           1 : }
     224             : 
     225           1 : static void test_flag_val_true(void) {
     226           1 :   int x = 2;
     227             :   gpr_cmdline *cl;
     228           1 :   char *args[] = {(char *)__FILE__, "--foo=true"};
     229             : 
     230           1 :   LOG_TEST();
     231             : 
     232           1 :   cl = gpr_cmdline_create(NULL);
     233           1 :   gpr_cmdline_add_flag(cl, "foo", NULL, &x);
     234           1 :   GPR_ASSERT(x == 2);
     235           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     236           1 :   GPR_ASSERT(x == 1);
     237           1 :   gpr_cmdline_destroy(cl);
     238           1 : }
     239             : 
     240           1 : static void test_flag_val_false(void) {
     241           1 :   int x = 2;
     242             :   gpr_cmdline *cl;
     243           1 :   char *args[] = {(char *)__FILE__, "--foo=false"};
     244             : 
     245           1 :   LOG_TEST();
     246             : 
     247           1 :   cl = gpr_cmdline_create(NULL);
     248           1 :   gpr_cmdline_add_flag(cl, "foo", NULL, &x);
     249           1 :   GPR_ASSERT(x == 2);
     250           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     251           1 :   GPR_ASSERT(x == 0);
     252           1 :   gpr_cmdline_destroy(cl);
     253           1 : }
     254             : 
     255           1 : static void test_many(void) {
     256           1 :   char *str = NULL;
     257           1 :   int x = 0;
     258           1 :   int flag = 2;
     259             :   gpr_cmdline *cl;
     260             : 
     261           1 :   char *args[] = {(char *)__FILE__, "--str", "hello", "-x=4", "-no-flag"};
     262             : 
     263           1 :   LOG_TEST();
     264             : 
     265           1 :   cl = gpr_cmdline_create(NULL);
     266           1 :   gpr_cmdline_add_string(cl, "str", NULL, &str);
     267           1 :   gpr_cmdline_add_int(cl, "x", NULL, &x);
     268           1 :   gpr_cmdline_add_flag(cl, "flag", NULL, &flag);
     269           1 :   gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
     270           1 :   GPR_ASSERT(x == 4);
     271           1 :   GPR_ASSERT(0 == strcmp(str, "hello"));
     272           1 :   GPR_ASSERT(flag == 0);
     273           1 :   gpr_cmdline_destroy(cl);
     274           1 : }
     275             : 
     276           1 : static void test_usage(void) {
     277             :   gpr_cmdline *cl;
     278             :   char *usage;
     279             : 
     280           1 :   char *str = NULL;
     281           1 :   int x = 0;
     282           1 :   int flag = 2;
     283             : 
     284           1 :   cl = gpr_cmdline_create(NULL);
     285           1 :   gpr_cmdline_add_string(cl, "str", NULL, &str);
     286           1 :   gpr_cmdline_add_int(cl, "x", NULL, &x);
     287           1 :   gpr_cmdline_add_flag(cl, "flag", NULL, &flag);
     288             : 
     289           1 :   usage = gpr_cmdline_usage_string(cl, "test");
     290           1 :   GPR_ASSERT(
     291             :       0 == strcmp(usage,
     292             :                   "Usage: test [--str=string] [--x=int] [--flag|--no-flag]\n"));
     293           1 :   gpr_free(usage);
     294             : 
     295           1 :   gpr_cmdline_destroy(cl);
     296           1 : }
     297             : 
     298           1 : int main(int argc, char **argv) {
     299           1 :   grpc_test_init(argc, argv);
     300           1 :   test_simple_int();
     301           1 :   test_eq_int();
     302           1 :   test_2dash_int();
     303           1 :   test_2dash_eq_int();
     304           1 :   test_simple_string();
     305           1 :   test_eq_string();
     306           1 :   test_2dash_string();
     307           1 :   test_2dash_eq_string();
     308           1 :   test_flag_on();
     309           1 :   test_flag_no();
     310           1 :   test_flag_val_1();
     311           1 :   test_flag_val_0();
     312           1 :   test_flag_val_true();
     313           1 :   test_flag_val_false();
     314           1 :   test_many();
     315           1 :   test_usage();
     316           1 :   return 0;
     317             : }

Generated by: LCOV version 1.10