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//===----------------------------------------------------------------------===//
8
9#include "gtest/gtest.h"
10#include "lldb/Interpreter/OptionArgParser.h"
11
12using namespace lldb_private;
13
14TEST(OptionArgParserTest, toBoolean) {
15 bool success = false;
16 EXPECT_TRUE(
17 OptionArgParser::ToBoolean(llvm::StringRef("true"), false, nullptr));
18 EXPECT_TRUE(
19 OptionArgParser::ToBoolean(llvm::StringRef("on"), false, nullptr));
20 EXPECT_TRUE(
21 OptionArgParser::ToBoolean(llvm::StringRef("yes"), false, nullptr));
22 EXPECT_TRUE(OptionArgParser::ToBoolean(llvm::StringRef("1"), false, nullptr));
23
24 EXPECT_TRUE(
25 OptionArgParser::ToBoolean(llvm::StringRef("true"), false, &success));
26 EXPECT_TRUE(success);
27 EXPECT_TRUE(
28 OptionArgParser::ToBoolean(llvm::StringRef("on"), false, &success));
29 EXPECT_TRUE(success);
30 EXPECT_TRUE(
31 OptionArgParser::ToBoolean(llvm::StringRef("yes"), false, &success));
32 EXPECT_TRUE(success);
33 EXPECT_TRUE(
34 OptionArgParser::ToBoolean(llvm::StringRef("1"), false, &success));
35 EXPECT_TRUE(success);
36
37 EXPECT_FALSE(
38 OptionArgParser::ToBoolean(llvm::StringRef("false"), true, nullptr));
39 EXPECT_FALSE(
40 OptionArgParser::ToBoolean(llvm::StringRef("off"), true, nullptr));
41 EXPECT_FALSE(
42 OptionArgParser::ToBoolean(llvm::StringRef("no"), true, nullptr));
43 EXPECT_FALSE(OptionArgParser::ToBoolean(llvm::StringRef("0"), true, nullptr));
44
45 EXPECT_FALSE(
46 OptionArgParser::ToBoolean(llvm::StringRef("false"), true, &success));
47 EXPECT_TRUE(success);
48 EXPECT_FALSE(
49 OptionArgParser::ToBoolean(llvm::StringRef("off"), true, &success));
50 EXPECT_TRUE(success);
51 EXPECT_FALSE(
52 OptionArgParser::ToBoolean(llvm::StringRef("no"), true, &success));
53 EXPECT_TRUE(success);
54 EXPECT_FALSE(
55 OptionArgParser::ToBoolean(llvm::StringRef("0"), true, &success));
56 EXPECT_TRUE(success);
57
58 EXPECT_FALSE(
59 OptionArgParser::ToBoolean(llvm::StringRef("10"), false, &success));
60 EXPECT_FALSE(success);
61 EXPECT_TRUE(
62 OptionArgParser::ToBoolean(llvm::StringRef("10"), true, &success));
63 EXPECT_FALSE(success);
64 EXPECT_TRUE(OptionArgParser::ToBoolean(llvm::StringRef(""), true, &success));
65 EXPECT_FALSE(success);
66}
67
68TEST(OptionArgParserTest, toChar) {
69 bool success = false;
70
71 EXPECT_EQ('A', OptionArgParser::ToChar("A", 'B', nullptr));
72 EXPECT_EQ('B', OptionArgParser::ToChar("B", 'A', nullptr));
73
74 EXPECT_EQ('A', OptionArgParser::ToChar("A", 'B', &success));
75 EXPECT_TRUE(success);
76 EXPECT_EQ('B', OptionArgParser::ToChar("B", 'A', &success));
77 EXPECT_TRUE(success);
78
79 EXPECT_EQ('A', OptionArgParser::ToChar("", 'A', &success));
80 EXPECT_FALSE(success);
81 EXPECT_EQ('A', OptionArgParser::ToChar("ABC", 'A', &success));
82 EXPECT_FALSE(success);
83}
84
85TEST(OptionArgParserTest, toScriptLanguage) {
86 bool success = false;
87
88 EXPECT_EQ(lldb::eScriptLanguageDefault,
89 OptionArgParser::ToScriptLanguage(llvm::StringRef("default"),
90 lldb::eScriptLanguageNone,
91 nullptr));
92 EXPECT_EQ(lldb::eScriptLanguagePython,
93 OptionArgParser::ToScriptLanguage(
94 llvm::StringRef("python"), lldb::eScriptLanguageNone, nullptr));
95 EXPECT_EQ(lldb::eScriptLanguageNone,
96 OptionArgParser::ToScriptLanguage(
97 llvm::StringRef("none"), lldb::eScriptLanguagePython, nullptr));
98
99 EXPECT_EQ(lldb::eScriptLanguageDefault,
100 OptionArgParser::ToScriptLanguage(llvm::StringRef("default"),
101 lldb::eScriptLanguageNone,
102 &success));
103 EXPECT_TRUE(success);
104 EXPECT_EQ(lldb::eScriptLanguagePython,
105 OptionArgParser::ToScriptLanguage(llvm::StringRef("python"),
106 lldb::eScriptLanguageNone,
107 &success));
108 EXPECT_TRUE(success);
109 EXPECT_EQ(lldb::eScriptLanguageNone,
110 OptionArgParser::ToScriptLanguage(llvm::StringRef("none"),
111 lldb::eScriptLanguagePython,
112 &success));
113 EXPECT_TRUE(success);
114
115 EXPECT_EQ(lldb::eScriptLanguagePython,
116 OptionArgParser::ToScriptLanguage(llvm::StringRef("invalid"),
117 lldb::eScriptLanguagePython,
118 &success));
119 EXPECT_FALSE(success);
120}
121

source code of lldb/unittests/Interpreter/TestOptionArgParser.cpp