LCOV - code coverage report
Current view: top level - test/cctest - test-flags.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 130 130 100.0 %
Date: 2017-10-20 Functions: 20 20 100.0 %

          Line data    Source code
       1             : // Copyright 2006-2008 the V8 project authors. All rights reserved.
       2             : // Redistribution and use in source and binary forms, with or without
       3             : // modification, are permitted provided that the following conditions are
       4             : // met:
       5             : //
       6             : //     * Redistributions of source code must retain the above copyright
       7             : //       notice, this list of conditions and the following disclaimer.
       8             : //     * Redistributions in binary form must reproduce the above
       9             : //       copyright notice, this list of conditions and the following
      10             : //       disclaimer in the documentation and/or other materials provided
      11             : //       with the distribution.
      12             : //     * Neither the name of Google Inc. nor the names of its
      13             : //       contributors may be used to endorse or promote products derived
      14             : //       from this software without specific prior written permission.
      15             : //
      16             : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      17             : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      18             : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      19             : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      20             : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      21             : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      22             : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      23             : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      24             : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      25             : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      26             : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      27             : 
      28             : #include <stdlib.h>
      29             : 
      30             : #include "src/flags.h"
      31             : #include "src/v8.h"
      32             : #include "test/cctest/cctest.h"
      33             : 
      34             : namespace v8 {
      35             : namespace internal {
      36             : 
      37             : // This test must be executed first!
      38       23820 : TEST(Default) {
      39         102 :   CHECK(FLAG_testing_bool_flag);
      40         102 :   CHECK_EQ(13, FLAG_testing_int_flag);
      41         102 :   CHECK_EQ(2.5, FLAG_testing_float_flag);
      42         102 :   CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "Hello, world!"));
      43         102 : }
      44             : 
      45             : 
      46             : static void SetFlagsToDefault() {
      47          96 :   FlagList::ResetAllFlags();
      48          96 :   TestDefault();
      49             : }
      50             : 
      51             : 
      52       23724 : TEST(Flags1) {
      53           6 :   FlagList::PrintHelp();
      54           6 : }
      55             : 
      56             : 
      57       23724 : TEST(Flags2) {
      58             :   SetFlagsToDefault();
      59           6 :   int argc = 8;
      60             :   const char* argv[] = { "Test2", "-notesting-bool-flag",
      61             :                          "--notesting-maybe-bool-flag", "notaflag",
      62             :                          "--testing_int_flag=77", "-testing_float_flag=.25",
      63           6 :                          "--testing_string_flag", "no way!" };
      64           6 :   CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
      65             :                                                 const_cast<char **>(argv),
      66             :                                                 false));
      67           6 :   CHECK_EQ(8, argc);
      68           6 :   CHECK(!FLAG_testing_bool_flag);
      69           6 :   CHECK(FLAG_testing_maybe_bool_flag.has_value);
      70           6 :   CHECK(!FLAG_testing_maybe_bool_flag.value);
      71           6 :   CHECK_EQ(77, FLAG_testing_int_flag);
      72           6 :   CHECK_EQ(.25, FLAG_testing_float_flag);
      73           6 :   CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no way!"));
      74           6 : }
      75             : 
      76             : 
      77       23724 : TEST(Flags2b) {
      78             :   SetFlagsToDefault();
      79             :   const char* str =
      80             :       " -notesting-bool-flag notaflag   --testing_int_flag=77 "
      81             :       "-notesting-maybe-bool-flag   "
      82             :       "-testing_float_flag=.25  "
      83             :       "--testing_string_flag   no_way!  ";
      84           6 :   CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
      85           6 :   CHECK(!FLAG_testing_bool_flag);
      86           6 :   CHECK(FLAG_testing_maybe_bool_flag.has_value);
      87           6 :   CHECK(!FLAG_testing_maybe_bool_flag.value);
      88           6 :   CHECK_EQ(77, FLAG_testing_int_flag);
      89           6 :   CHECK_EQ(.25, FLAG_testing_float_flag);
      90           6 :   CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no_way!"));
      91           6 : }
      92             : 
      93             : 
      94       23724 : TEST(Flags3) {
      95             :   SetFlagsToDefault();
      96           6 :   int argc = 9;
      97             :   const char* argv[] =
      98             :       { "Test3", "--testing_bool_flag", "--testing-maybe-bool-flag", "notaflag",
      99             :         "--testing_int_flag", "-666",
     100           6 :         "--testing_float_flag", "-12E10", "-testing-string-flag=foo-bar" };
     101           6 :   CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
     102             :                                                 const_cast<char **>(argv),
     103             :                                                 true));
     104           6 :   CHECK_EQ(2, argc);
     105           6 :   CHECK(FLAG_testing_bool_flag);
     106           6 :   CHECK(FLAG_testing_maybe_bool_flag.has_value);
     107           6 :   CHECK(FLAG_testing_maybe_bool_flag.value);
     108           6 :   CHECK_EQ(-666, FLAG_testing_int_flag);
     109           6 :   CHECK_EQ(-12E10, FLAG_testing_float_flag);
     110           6 :   CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar"));
     111           6 : }
     112             : 
     113             : 
     114       23724 : TEST(Flags3b) {
     115             :   SetFlagsToDefault();
     116             :   const char* str =
     117             :       "--testing_bool_flag --testing-maybe-bool-flag notaflag "
     118             :       "--testing_int_flag -666 "
     119             :       "--testing_float_flag -12E10 "
     120             :       "-testing-string-flag=foo-bar";
     121           6 :   CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
     122           6 :   CHECK(FLAG_testing_bool_flag);
     123           6 :   CHECK(FLAG_testing_maybe_bool_flag.has_value);
     124           6 :   CHECK(FLAG_testing_maybe_bool_flag.value);
     125           6 :   CHECK_EQ(-666, FLAG_testing_int_flag);
     126           6 :   CHECK_EQ(-12E10, FLAG_testing_float_flag);
     127           6 :   CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar"));
     128           6 : }
     129             : 
     130             : 
     131       23724 : TEST(Flags4) {
     132             :   SetFlagsToDefault();
     133           6 :   int argc = 3;
     134           6 :   const char* argv[] = { "Test4", "--testing_bool_flag", "--foo" };
     135           6 :   CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
     136             :                                                 const_cast<char **>(argv),
     137             :                                                 true));
     138           6 :   CHECK_EQ(2, argc);
     139           6 :   CHECK(!FLAG_testing_maybe_bool_flag.has_value);
     140           6 : }
     141             : 
     142             : 
     143       23724 : TEST(Flags4b) {
     144             :   SetFlagsToDefault();
     145             :   const char* str = "--testing_bool_flag --foo";
     146           6 :   CHECK_EQ(2, FlagList::SetFlagsFromString(str, StrLength(str)));
     147           6 :   CHECK(!FLAG_testing_maybe_bool_flag.has_value);
     148           6 : }
     149             : 
     150             : 
     151       23724 : TEST(Flags5) {
     152             :   SetFlagsToDefault();
     153           6 :   int argc = 2;
     154           6 :   const char* argv[] = { "Test5", "--testing_int_flag=\"foobar\"" };
     155           6 :   CHECK_EQ(1, FlagList::SetFlagsFromCommandLine(&argc,
     156             :                                                 const_cast<char **>(argv),
     157             :                                                 true));
     158           6 :   CHECK_EQ(2, argc);
     159           6 : }
     160             : 
     161             : 
     162       23724 : TEST(Flags5b) {
     163             :   SetFlagsToDefault();
     164             :   const char* str = "                     --testing_int_flag=\"foobar\"";
     165           6 :   CHECK_EQ(1, FlagList::SetFlagsFromString(str, StrLength(str)));
     166           6 : }
     167             : 
     168             : 
     169       23724 : TEST(Flags6) {
     170             :   SetFlagsToDefault();
     171           6 :   int argc = 4;
     172             :   const char* argv[] = { "Test5", "--testing-int-flag", "0",
     173           6 :                          "--testing_float_flag" };
     174           6 :   CHECK_EQ(3, FlagList::SetFlagsFromCommandLine(&argc,
     175             :                                                 const_cast<char **>(argv),
     176             :                                                 true));
     177           6 :   CHECK_EQ(2, argc);
     178           6 : }
     179             : 
     180             : 
     181       23724 : TEST(Flags6b) {
     182             :   SetFlagsToDefault();
     183             :   const char* str = "       --testing-int-flag 0      --testing_float_flag    ";
     184           6 :   CHECK_EQ(3, FlagList::SetFlagsFromString(str, StrLength(str)));
     185           6 : }
     186             : 
     187             : 
     188       23724 : TEST(FlagsJSArguments1) {
     189             :   SetFlagsToDefault();
     190           6 :   int argc = 6;
     191             :   const char* argv[] = {"TestJSArgs1",
     192             :                         "--testing-int-flag", "42",
     193           6 :                         "--", "testing-float-flag", "7"};
     194           6 :   CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc,
     195             :                                                 const_cast<char **>(argv),
     196             :                                                 true));
     197           6 :   CHECK_EQ(42, FLAG_testing_int_flag);
     198           6 :   CHECK_EQ(2.5, FLAG_testing_float_flag);
     199           6 :   CHECK_EQ(2, FLAG_js_arguments.argc);
     200           6 :   CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
     201           6 :   CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
     202           6 :   CHECK_EQ(1, argc);
     203           6 : }
     204             : 
     205             : 
     206       23724 : TEST(FlagsJSArguments1b) {
     207             :   SetFlagsToDefault();
     208             :   const char* str = "--testing-int-flag 42 -- testing-float-flag 7";
     209           6 :   CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
     210           6 :   CHECK_EQ(42, FLAG_testing_int_flag);
     211           6 :   CHECK_EQ(2.5, FLAG_testing_float_flag);
     212           6 :   CHECK_EQ(2, FLAG_js_arguments.argc);
     213           6 :   CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
     214           6 :   CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
     215           6 : }
     216             : 
     217             : 
     218       23724 : TEST(FlagsJSArguments2) {
     219             :   SetFlagsToDefault();
     220             :   const char* str = "--testing-int-flag 42 --js-arguments testing-float-flag 7";
     221           6 :   CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
     222           6 :   CHECK_EQ(42, FLAG_testing_int_flag);
     223           6 :   CHECK_EQ(2.5, FLAG_testing_float_flag);
     224           6 :   CHECK_EQ(2, FLAG_js_arguments.argc);
     225           6 :   CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
     226           6 :   CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
     227           6 : }
     228             : 
     229             : 
     230       23724 : TEST(FlagsJSArguments3) {
     231             :   SetFlagsToDefault();
     232             :   const char* str = "--testing-int-flag 42 --js-arguments=testing-float-flag 7";
     233           6 :   CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
     234           6 :   CHECK_EQ(42, FLAG_testing_int_flag);
     235           6 :   CHECK_EQ(2.5, FLAG_testing_float_flag);
     236           6 :   CHECK_EQ(2, FLAG_js_arguments.argc);
     237           6 :   CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
     238           6 :   CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
     239           6 : }
     240             : 
     241             : 
     242       23724 : TEST(FlagsJSArguments4) {
     243             :   SetFlagsToDefault();
     244             :   const char* str = "--testing-int-flag 42 --";
     245           6 :   CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
     246           6 :   CHECK_EQ(42, FLAG_testing_int_flag);
     247           6 :   CHECK_EQ(0, FLAG_js_arguments.argc);
     248           6 : }
     249             : 
     250             : 
     251       23724 : TEST(FlagsRemoveIncomplete) {
     252             :   // Test that processed command line arguments are removed, even
     253             :   // if the list of arguments ends unexpectedly.
     254             :   SetFlagsToDefault();
     255           6 :   int argc = 3;
     256           6 :   const char* argv[] = {"", "--opt", "--expose-natives-as"};
     257           6 :   CHECK_EQ(2, FlagList::SetFlagsFromCommandLine(&argc,
     258             :                                                 const_cast<char **>(argv),
     259             :                                                 true));
     260           6 :   CHECK(argv[1]);
     261           6 :   CHECK_EQ(2, argc);
     262           6 : }
     263             : 
     264             : }  // namespace internal
     265       71154 : }  // namespace v8

Generated by: LCOV version 1.10