[AArch64] Fix SDNode type mismatches between *.td files and ISel (#116523)
[llvm-project.git] / lldb / unittests / Interpreter / TestOptionArgParser.cpp
blob25335e5396fbd8f14bbee24d3b37d72ddc5beeb2
1 //===-- ArgsTest.cpp ------------------------------------------------------===//
2 //
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
6 //
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) {
16 bool success = false;
17 EXPECT_TRUE(
18 OptionArgParser::ToBoolean(llvm::StringRef("true"), false, nullptr));
19 EXPECT_TRUE(
20 OptionArgParser::ToBoolean(llvm::StringRef("on"), false, nullptr));
21 EXPECT_TRUE(
22 OptionArgParser::ToBoolean(llvm::StringRef("yes"), false, nullptr));
23 EXPECT_TRUE(OptionArgParser::ToBoolean(llvm::StringRef("1"), false, nullptr));
25 EXPECT_TRUE(
26 OptionArgParser::ToBoolean(llvm::StringRef("true"), false, &success));
27 EXPECT_TRUE(success);
28 EXPECT_TRUE(
29 OptionArgParser::ToBoolean(llvm::StringRef("on"), false, &success));
30 EXPECT_TRUE(success);
31 EXPECT_TRUE(
32 OptionArgParser::ToBoolean(llvm::StringRef("yes"), false, &success));
33 EXPECT_TRUE(success);
34 EXPECT_TRUE(
35 OptionArgParser::ToBoolean(llvm::StringRef("1"), false, &success));
36 EXPECT_TRUE(success);
38 EXPECT_FALSE(
39 OptionArgParser::ToBoolean(llvm::StringRef("false"), true, nullptr));
40 EXPECT_FALSE(
41 OptionArgParser::ToBoolean(llvm::StringRef("off"), true, nullptr));
42 EXPECT_FALSE(
43 OptionArgParser::ToBoolean(llvm::StringRef("no"), true, nullptr));
44 EXPECT_FALSE(OptionArgParser::ToBoolean(llvm::StringRef("0"), true, nullptr));
46 EXPECT_FALSE(
47 OptionArgParser::ToBoolean(llvm::StringRef("false"), true, &success));
48 EXPECT_TRUE(success);
49 EXPECT_FALSE(
50 OptionArgParser::ToBoolean(llvm::StringRef("off"), true, &success));
51 EXPECT_TRUE(success);
52 EXPECT_FALSE(
53 OptionArgParser::ToBoolean(llvm::StringRef("no"), true, &success));
54 EXPECT_TRUE(success);
55 EXPECT_FALSE(
56 OptionArgParser::ToBoolean(llvm::StringRef("0"), true, &success));
57 EXPECT_TRUE(success);
59 EXPECT_FALSE(
60 OptionArgParser::ToBoolean(llvm::StringRef("10"), false, &success));
61 EXPECT_FALSE(success);
62 EXPECT_TRUE(
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);
77 else {
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,
131 nullptr));
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,
142 &success));
143 EXPECT_TRUE(success);
144 EXPECT_EQ(lldb::eScriptLanguagePython,
145 OptionArgParser::ToScriptLanguage(llvm::StringRef("python"),
146 lldb::eScriptLanguageNone,
147 &success));
148 EXPECT_TRUE(success);
149 EXPECT_EQ(lldb::eScriptLanguageNone,
150 OptionArgParser::ToScriptLanguage(llvm::StringRef("none"),
151 lldb::eScriptLanguagePython,
152 &success));
153 EXPECT_TRUE(success);
155 EXPECT_EQ(lldb::eScriptLanguagePython,
156 OptionArgParser::ToScriptLanguage(llvm::StringRef("invalid"),
157 lldb::eScriptLanguagePython,
158 &success));
159 EXPECT_FALSE(success);