1 //===-- ArgsTest.cpp ------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
10 #include "lldb/Interpreter/OptionArgParser.h"
11 #include "llvm/Support/Error.h"
13 using namespace lldb_private
;
15 TEST(OptionArgParserTest
, toBoolean
) {
18 OptionArgParser::ToBoolean(llvm::StringRef("true"), false, nullptr));
20 OptionArgParser::ToBoolean(llvm::StringRef("on"), false, nullptr));
22 OptionArgParser::ToBoolean(llvm::StringRef("yes"), false, nullptr));
23 EXPECT_TRUE(OptionArgParser::ToBoolean(llvm::StringRef("1"), false, nullptr));
26 OptionArgParser::ToBoolean(llvm::StringRef("true"), false, &success
));
29 OptionArgParser::ToBoolean(llvm::StringRef("on"), false, &success
));
32 OptionArgParser::ToBoolean(llvm::StringRef("yes"), false, &success
));
35 OptionArgParser::ToBoolean(llvm::StringRef("1"), false, &success
));
39 OptionArgParser::ToBoolean(llvm::StringRef("false"), true, nullptr));
41 OptionArgParser::ToBoolean(llvm::StringRef("off"), true, nullptr));
43 OptionArgParser::ToBoolean(llvm::StringRef("no"), true, nullptr));
44 EXPECT_FALSE(OptionArgParser::ToBoolean(llvm::StringRef("0"), true, nullptr));
47 OptionArgParser::ToBoolean(llvm::StringRef("false"), true, &success
));
50 OptionArgParser::ToBoolean(llvm::StringRef("off"), true, &success
));
53 OptionArgParser::ToBoolean(llvm::StringRef("no"), true, &success
));
56 OptionArgParser::ToBoolean(llvm::StringRef("0"), true, &success
));
60 OptionArgParser::ToBoolean(llvm::StringRef("10"), false, &success
));
61 EXPECT_FALSE(success
);
63 OptionArgParser::ToBoolean(llvm::StringRef("10"), true, &success
));
64 EXPECT_FALSE(success
);
65 EXPECT_TRUE(OptionArgParser::ToBoolean(llvm::StringRef(""), true, &success
));
66 EXPECT_FALSE(success
);
69 void TestToBooleanWithExpectedBool(llvm::StringRef option_arg
,
70 bool expected_parse_success
,
71 bool expected_result
) {
72 llvm::Expected
<bool> bool_or_error
=
73 OptionArgParser::ToBoolean(llvm::StringRef("test_option"), option_arg
);
74 EXPECT_EQ(expected_parse_success
, (bool)bool_or_error
);
75 if (expected_parse_success
)
76 EXPECT_EQ(expected_result
, *bool_or_error
);
78 std::string error
= llvm::toString(bool_or_error
.takeError());
79 EXPECT_NE(std::string::npos
, error
.find("test_option"));
83 TEST(OptionArgParserTest
, toBooleanWithExpectedBool
) {
84 TestToBooleanWithExpectedBool(llvm::StringRef("true"), true, true);
85 TestToBooleanWithExpectedBool(llvm::StringRef("on"), true, true);
86 TestToBooleanWithExpectedBool(llvm::StringRef("yes"), true, true);
87 TestToBooleanWithExpectedBool(llvm::StringRef("1"), true, true);
89 TestToBooleanWithExpectedBool(llvm::StringRef("True"), true, true);
90 TestToBooleanWithExpectedBool(llvm::StringRef("On"), true, true);
91 TestToBooleanWithExpectedBool(llvm::StringRef("Yes"), true, true);
93 TestToBooleanWithExpectedBool(llvm::StringRef("false"), true, false);
94 TestToBooleanWithExpectedBool(llvm::StringRef("off"), true, false);
95 TestToBooleanWithExpectedBool(llvm::StringRef("no"), true, false);
96 TestToBooleanWithExpectedBool(llvm::StringRef("0"), true, false);
98 TestToBooleanWithExpectedBool(llvm::StringRef("False"), true, false);
99 TestToBooleanWithExpectedBool(llvm::StringRef("Off"), true, false);
100 TestToBooleanWithExpectedBool(llvm::StringRef("No"), true, false);
102 TestToBooleanWithExpectedBool(llvm::StringRef("10"), false,
103 false /* doesn't matter */);
104 TestToBooleanWithExpectedBool(llvm::StringRef(""), false,
105 false /* doesn't matter */);
108 TEST(OptionArgParserTest
, toChar
) {
109 bool success
= false;
111 EXPECT_EQ('A', OptionArgParser::ToChar("A", 'B', nullptr));
112 EXPECT_EQ('B', OptionArgParser::ToChar("B", 'A', nullptr));
114 EXPECT_EQ('A', OptionArgParser::ToChar("A", 'B', &success
));
115 EXPECT_TRUE(success
);
116 EXPECT_EQ('B', OptionArgParser::ToChar("B", 'A', &success
));
117 EXPECT_TRUE(success
);
119 EXPECT_EQ('A', OptionArgParser::ToChar("", 'A', &success
));
120 EXPECT_FALSE(success
);
121 EXPECT_EQ('A', OptionArgParser::ToChar("ABC", 'A', &success
));
122 EXPECT_FALSE(success
);
125 TEST(OptionArgParserTest
, toScriptLanguage
) {
126 bool success
= false;
128 EXPECT_EQ(lldb::eScriptLanguageDefault
,
129 OptionArgParser::ToScriptLanguage(llvm::StringRef("default"),
130 lldb::eScriptLanguageNone
,
132 EXPECT_EQ(lldb::eScriptLanguagePython
,
133 OptionArgParser::ToScriptLanguage(
134 llvm::StringRef("python"), lldb::eScriptLanguageNone
, nullptr));
135 EXPECT_EQ(lldb::eScriptLanguageNone
,
136 OptionArgParser::ToScriptLanguage(
137 llvm::StringRef("none"), lldb::eScriptLanguagePython
, nullptr));
139 EXPECT_EQ(lldb::eScriptLanguageDefault
,
140 OptionArgParser::ToScriptLanguage(llvm::StringRef("default"),
141 lldb::eScriptLanguageNone
,
143 EXPECT_TRUE(success
);
144 EXPECT_EQ(lldb::eScriptLanguagePython
,
145 OptionArgParser::ToScriptLanguage(llvm::StringRef("python"),
146 lldb::eScriptLanguageNone
,
148 EXPECT_TRUE(success
);
149 EXPECT_EQ(lldb::eScriptLanguageNone
,
150 OptionArgParser::ToScriptLanguage(llvm::StringRef("none"),
151 lldb::eScriptLanguagePython
,
153 EXPECT_TRUE(success
);
155 EXPECT_EQ(lldb::eScriptLanguagePython
,
156 OptionArgParser::ToScriptLanguage(llvm::StringRef("invalid"),
157 lldb::eScriptLanguagePython
,
159 EXPECT_FALSE(success
);