blob: e423fdc7e0decd2e4861ed135b91e44904b189f4 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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 Murdochb8a8cc12014-11-26 15:28:44 +000030#include "src/v8.h"
31#include "test/cctest/cctest.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032
33using namespace v8::internal;
34
35// This test must be executed first!
36TEST(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
44static void SetFlagsToDefault() {
45 FlagList::ResetAllFlags();
46 TestDefault();
47}
48
49
50TEST(Flags1) {
51 FlagList::PrintHelp();
52}
53
54
55TEST(Flags2) {
56 SetFlagsToDefault();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 int argc = 8;
58 const char* argv[] = { "Test2", "-notesting-bool-flag",
59 "--notesting-maybe-bool-flag", "notaflag",
Steve Blocka7e24c12009-10-30 11:49:00 +000060 "--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 Murdochb8a8cc12014-11-26 15:28:44 +000065 CHECK_EQ(8, argc);
Steve Blocka7e24c12009-10-30 11:49:00 +000066 CHECK(!FLAG_testing_bool_flag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 CHECK(FLAG_testing_maybe_bool_flag.has_value);
68 CHECK(!FLAG_testing_maybe_bool_flag.value);
Steve Blocka7e24c12009-10-30 11:49:00 +000069 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
75TEST(Flags2b) {
76 SetFlagsToDefault();
77 const char* str =
78 " -notesting-bool-flag notaflag --testing_int_flag=77 "
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 "-notesting-maybe-bool-flag "
Steve Blocka7e24c12009-10-30 11:49:00 +000080 "-testing_float_flag=.25 "
81 "--testing_string_flag no_way! ";
Steve Blockd0582a62009-12-15 09:54:21 +000082 CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
Steve Blocka7e24c12009-10-30 11:49:00 +000083 CHECK(!FLAG_testing_bool_flag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084 CHECK(FLAG_testing_maybe_bool_flag.has_value);
85 CHECK(!FLAG_testing_maybe_bool_flag.value);
Steve Blocka7e24c12009-10-30 11:49:00 +000086 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
92TEST(Flags3) {
93 SetFlagsToDefault();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 int argc = 9;
Steve Blocka7e24c12009-10-30 11:49:00 +000095 const char* argv[] =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 { "Test3", "--testing_bool_flag", "--testing-maybe-bool-flag", "notaflag",
Steve Blocka7e24c12009-10-30 11:49:00 +000097 "--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 Murdochb8a8cc12014-11-26 15:28:44 +0000104 CHECK(FLAG_testing_maybe_bool_flag.has_value);
105 CHECK(FLAG_testing_maybe_bool_flag.value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 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
112TEST(Flags3b) {
113 SetFlagsToDefault();
114 const char* str =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 "--testing_bool_flag --testing-maybe-bool-flag notaflag "
116 "--testing_int_flag -666 "
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 "--testing_float_flag -12E10 "
118 "-testing-string-flag=foo-bar";
Steve Blockd0582a62009-12-15 09:54:21 +0000119 CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 CHECK(FLAG_testing_bool_flag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 CHECK(FLAG_testing_maybe_bool_flag.has_value);
122 CHECK(FLAG_testing_maybe_bool_flag.value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000123 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
129TEST(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 Murdochb8a8cc12014-11-26 15:28:44 +0000137 CHECK(!FLAG_testing_maybe_bool_flag.has_value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000138}
139
140
141TEST(Flags4b) {
142 SetFlagsToDefault();
143 const char* str = "--testing_bool_flag --foo";
Steve Blockd0582a62009-12-15 09:54:21 +0000144 CHECK_EQ(2, FlagList::SetFlagsFromString(str, StrLength(str)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 CHECK(!FLAG_testing_maybe_bool_flag.has_value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000146}
147
148
149TEST(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
160TEST(Flags5b) {
161 SetFlagsToDefault();
162 const char* str = " --testing_int_flag=\"foobar\"";
Steve Blockd0582a62009-12-15 09:54:21 +0000163 CHECK_EQ(1, FlagList::SetFlagsFromString(str, StrLength(str)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000164}
165
166
167TEST(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 Murdochb8a8cc12014-11-26 15:28:44 +0000175 CHECK_EQ(2, argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000176}
177
178
179TEST(Flags6b) {
180 SetFlagsToDefault();
181 const char* str = " --testing-int-flag 0 --testing_float_flag ";
Steve Blockd0582a62009-12-15 09:54:21 +0000182 CHECK_EQ(3, FlagList::SetFlagsFromString(str, StrLength(str)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000183}
184
185
186TEST(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 Murdochb8a8cc12014-11-26 15:28:44 +0000197 CHECK_EQ(2, FLAG_js_arguments.argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 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
204TEST(FlagsJSArguments1b) {
205 SetFlagsToDefault();
206 const char* str = "--testing-int-flag 42 -- testing-float-flag 7";
Steve Blockd0582a62009-12-15 09:54:21 +0000207 CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 CHECK_EQ(42, FLAG_testing_int_flag);
209 CHECK_EQ(2.5, FLAG_testing_float_flag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 CHECK_EQ(2, FLAG_js_arguments.argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000211 CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
212 CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
213}
214
215
216TEST(FlagsJSArguments2) {
217 SetFlagsToDefault();
218 const char* str = "--testing-int-flag 42 --js-arguments testing-float-flag 7";
Steve Blockd0582a62009-12-15 09:54:21 +0000219 CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 CHECK_EQ(42, FLAG_testing_int_flag);
221 CHECK_EQ(2.5, FLAG_testing_float_flag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000222 CHECK_EQ(2, FLAG_js_arguments.argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
224 CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
225}
226
227
228TEST(FlagsJSArguments3) {
229 SetFlagsToDefault();
230 const char* str = "--testing-int-flag 42 --js-arguments=testing-float-flag 7";
Steve Blockd0582a62009-12-15 09:54:21 +0000231 CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000232 CHECK_EQ(42, FLAG_testing_int_flag);
233 CHECK_EQ(2.5, FLAG_testing_float_flag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234 CHECK_EQ(2, FLAG_js_arguments.argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 CHECK_EQ(0, strcmp(FLAG_js_arguments[0], "testing-float-flag"));
236 CHECK_EQ(0, strcmp(FLAG_js_arguments[1], "7"));
237}
238
239
240TEST(FlagsJSArguments4) {
241 SetFlagsToDefault();
242 const char* str = "--testing-int-flag 42 --";
Steve Blockd0582a62009-12-15 09:54:21 +0000243 CHECK_EQ(0, FlagList::SetFlagsFromString(str, StrLength(str)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000244 CHECK_EQ(42, FLAG_testing_int_flag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 CHECK_EQ(0, FLAG_js_arguments.argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000246}
247
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248
249TEST(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 Murdoch4a90d5f2016-03-22 12:00:34 +0000258 CHECK(argv[1]);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 CHECK_EQ(argc, 2);
260}