Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | #include "src/v8.h" |
| 31 | #include "test/cctest/cctest.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace v8::internal; |
| 34 | |
| 35 | // This test must be executed first! |
| 36 | TEST(Default) { |
| 37 | CHECK(FLAG_testing_bool_flag); |
| 38 | CHECK_EQ(13, FLAG_testing_int_flag); |
| 39 | CHECK_EQ(2.5, FLAG_testing_float_flag); |
| 40 | CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "Hello, world!")); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | static void SetFlagsToDefault() { |
| 45 | FlagList::ResetAllFlags(); |
| 46 | TestDefault(); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | TEST(Flags1) { |
| 51 | FlagList::PrintHelp(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | TEST(Flags2) { |
| 56 | SetFlagsToDefault(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | int argc = 8; |
| 58 | const char* argv[] = { "Test2", "-notesting-bool-flag", |
| 59 | "--notesting-maybe-bool-flag", "notaflag", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | "--testing_int_flag=77", "-testing_float_flag=.25", |
| 61 | "--testing_string_flag", "no way!" }; |
| 62 | CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc, |
| 63 | const_cast<char **>(argv), |
| 64 | false)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 65 | CHECK_EQ(8, argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | CHECK(!FLAG_testing_bool_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 67 | CHECK(FLAG_testing_maybe_bool_flag.has_value); |
| 68 | CHECK(!FLAG_testing_maybe_bool_flag.value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | CHECK_EQ(77, FLAG_testing_int_flag); |
| 70 | CHECK_EQ(.25, FLAG_testing_float_flag); |
| 71 | CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no way!")); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | TEST(Flags2b) { |
| 76 | SetFlagsToDefault(); |
| 77 | const char* str = |
| 78 | " -notesting-bool-flag notaflag --testing_int_flag=77 " |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | "-notesting-maybe-bool-flag " |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | "-testing_float_flag=.25 " |
| 81 | "--testing_string_flag no_way! "; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 82 | CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | CHECK(!FLAG_testing_bool_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 84 | CHECK(FLAG_testing_maybe_bool_flag.has_value); |
| 85 | CHECK(!FLAG_testing_maybe_bool_flag.value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | CHECK_EQ(77, FLAG_testing_int_flag); |
| 87 | CHECK_EQ(.25, FLAG_testing_float_flag); |
| 88 | CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "no_way!")); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | TEST(Flags3) { |
| 93 | SetFlagsToDefault(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 94 | int argc = 9; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | const char* argv[] = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 96 | { "Test3", "--testing_bool_flag", "--testing-maybe-bool-flag", "notaflag", |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | "--testing_int_flag", "-666", |
| 98 | "--testing_float_flag", "-12E10", "-testing-string-flag=foo-bar" }; |
| 99 | CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc, |
| 100 | const_cast<char **>(argv), |
| 101 | true)); |
| 102 | CHECK_EQ(2, argc); |
| 103 | CHECK(FLAG_testing_bool_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 104 | CHECK(FLAG_testing_maybe_bool_flag.has_value); |
| 105 | CHECK(FLAG_testing_maybe_bool_flag.value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | CHECK_EQ(-666, FLAG_testing_int_flag); |
| 107 | CHECK_EQ(-12E10, FLAG_testing_float_flag); |
| 108 | CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar")); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | TEST(Flags3b) { |
| 113 | SetFlagsToDefault(); |
| 114 | const char* str = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 115 | "--testing_bool_flag --testing-maybe-bool-flag notaflag " |
| 116 | "--testing_int_flag -666 " |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | "--testing_float_flag -12E10 " |
| 118 | "-testing-string-flag=foo-bar"; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 119 | CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 120 | CHECK(FLAG_testing_bool_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | CHECK(FLAG_testing_maybe_bool_flag.has_value); |
| 122 | CHECK(FLAG_testing_maybe_bool_flag.value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 123 | CHECK_EQ(-666, FLAG_testing_int_flag); |
| 124 | CHECK_EQ(-12E10, FLAG_testing_float_flag); |
| 125 | CHECK_EQ(0, strcmp(FLAG_testing_string_flag, "foo-bar")); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | TEST(Flags4) { |
| 130 | SetFlagsToDefault(); |
| 131 | int argc = 3; |
| 132 | const char* argv[] = { "Test4", "--testing_bool_flag", "--foo" }; |
| 133 | CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc, |
| 134 | const_cast<char **>(argv), |
| 135 | true)); |
| 136 | CHECK_EQ(2, argc); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | CHECK(!FLAG_testing_maybe_bool_flag.has_value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | TEST(Flags4b) { |
| 142 | SetFlagsToDefault(); |
| 143 | const char* str = "--testing_bool_flag --foo"; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 144 | CHECK_EQ(2, FlagList::SetFlagsFromString(str, StrLength(str))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | CHECK(!FLAG_testing_maybe_bool_flag.has_value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | TEST(Flags5) { |
| 150 | SetFlagsToDefault(); |
| 151 | int argc = 2; |
| 152 | const char* argv[] = { "Test5", "--testing_int_flag=\"foobar\"" }; |
| 153 | CHECK_EQ(1, FlagList::SetFlagsFromCommandLine(&argc, |
| 154 | const_cast<char **>(argv), |
| 155 | true)); |
| 156 | CHECK_EQ(2, argc); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | TEST(Flags5b) { |
| 161 | SetFlagsToDefault(); |
| 162 | const char* str = " --testing_int_flag=\"foobar\""; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 163 | CHECK_EQ(1, FlagList::SetFlagsFromString(str, StrLength(str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
| 167 | TEST(Flags6) { |
| 168 | SetFlagsToDefault(); |
| 169 | int argc = 4; |
| 170 | const char* argv[] = { "Test5", "--testing-int-flag", "0", |
| 171 | "--testing_float_flag" }; |
| 172 | CHECK_EQ(3, FlagList::SetFlagsFromCommandLine(&argc, |
| 173 | const_cast<char **>(argv), |
| 174 | true)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | CHECK_EQ(2, argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
| 179 | TEST(Flags6b) { |
| 180 | SetFlagsToDefault(); |
| 181 | const char* str = " --testing-int-flag 0 --testing_float_flag "; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 182 | CHECK_EQ(3, FlagList::SetFlagsFromString(str, StrLength(str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | |
| 186 | TEST(FlagsJSArguments1) { |
| 187 | SetFlagsToDefault(); |
| 188 | int argc = 6; |
| 189 | const char* argv[] = {"TestJSArgs1", |
| 190 | "--testing-int-flag", "42", |
| 191 | "--", "testing-float-flag", "7"}; |
| 192 | CHECK_EQ(0, FlagList::SetFlagsFromCommandLine(&argc, |
| 193 | const_cast<char **>(argv), |
| 194 | true)); |
| 195 | CHECK_EQ(42, FLAG_testing_int_flag); |
| 196 | CHECK_EQ(2.5, FLAG_testing_float_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 197 | CHECK_EQ(2, FLAG_js_arguments.argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 198 | CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag")); |
| 199 | CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7")); |
| 200 | CHECK_EQ(1, argc); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | TEST(FlagsJSArguments1b) { |
| 205 | SetFlagsToDefault(); |
| 206 | const char* str = "--testing-int-flag 42 -- testing-float-flag 7"; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 207 | CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 208 | CHECK_EQ(42, FLAG_testing_int_flag); |
| 209 | CHECK_EQ(2.5, FLAG_testing_float_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | CHECK_EQ(2, FLAG_js_arguments.argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag")); |
| 212 | CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7")); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | TEST(FlagsJSArguments2) { |
| 217 | SetFlagsToDefault(); |
| 218 | const char* str = "--testing-int-flag 42 --js-arguments testing-float-flag 7"; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 219 | CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 220 | CHECK_EQ(42, FLAG_testing_int_flag); |
| 221 | CHECK_EQ(2.5, FLAG_testing_float_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 222 | CHECK_EQ(2, FLAG_js_arguments.argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 223 | CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag")); |
| 224 | CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7")); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | TEST(FlagsJSArguments3) { |
| 229 | SetFlagsToDefault(); |
| 230 | const char* str = "--testing-int-flag 42 --js-arguments=testing-float-flag 7"; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 231 | CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 232 | CHECK_EQ(42, FLAG_testing_int_flag); |
| 233 | CHECK_EQ(2.5, FLAG_testing_float_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 234 | CHECK_EQ(2, FLAG_js_arguments.argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 235 | CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag")); |
| 236 | CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7")); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | TEST(FlagsJSArguments4) { |
| 241 | SetFlagsToDefault(); |
| 242 | const char* str = "--testing-int-flag 42 --"; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 243 | CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | CHECK_EQ(42, FLAG_testing_int_flag); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 245 | CHECK_EQ(0, FLAG_js_arguments.argc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 248 | |
| 249 | TEST(FlagsRemoveIncomplete) { |
| 250 | // Test that processed command line arguments are removed, even |
| 251 | // if the list of arguments ends unexpectedly. |
| 252 | SetFlagsToDefault(); |
| 253 | int argc = 3; |
| 254 | const char* argv[] = { "", "--crankshaft", "--expose-debug-as" }; |
| 255 | CHECK_EQ(2, FlagList::SetFlagsFromCommandLine(&argc, |
| 256 | const_cast<char **>(argv), |
| 257 | true)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 258 | CHECK(argv[1]); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 259 | CHECK_EQ(argc, 2); |
| 260 | } |