1//===-- TestRegexCommand.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 "Commands/CommandObjectRegexCommand.h"
10#include "llvm/Testing/Support/Error.h"
11#include "gtest/gtest.h"
12
13using namespace lldb_private;
14using namespace lldb;
15
16namespace {
17class TestRegexCommand : public CommandObjectRegexCommand {
18public:
19 using CommandObjectRegexCommand::SubstituteVariables;
20
21 static std::string
22 Substitute(llvm::StringRef input,
23 const llvm::SmallVectorImpl<llvm::StringRef> &replacements) {
24 llvm::Expected<std::string> str = SubstituteVariables(input, replacements);
25 if (!str)
26 return llvm::toString(E: str.takeError());
27 return *str;
28 }
29};
30} // namespace
31
32TEST(RegexCommandTest, SubstituteVariablesSuccess) {
33 const llvm::SmallVector<llvm::StringRef, 4> substitutions = {"all", "foo",
34 "bar", "baz"};
35
36 EXPECT_EQ(TestRegexCommand::Substitute("%0", substitutions), "all");
37 EXPECT_EQ(TestRegexCommand::Substitute("%1", substitutions), "foo");
38 EXPECT_EQ(TestRegexCommand::Substitute("%2", substitutions), "bar");
39 EXPECT_EQ(TestRegexCommand::Substitute("%3", substitutions), "baz");
40 EXPECT_EQ(TestRegexCommand::Substitute("%1%2%3", substitutions), "foobarbaz");
41 EXPECT_EQ(TestRegexCommand::Substitute("#%1#%2#%3#", substitutions),
42 "#foo#bar#baz#");
43}
44
45TEST(RegexCommandTest, SubstituteVariablesFailed) {
46 const llvm::SmallVector<llvm::StringRef, 4> substitutions = {"all", "foo",
47 "bar", "baz"};
48
49 ASSERT_THAT_EXPECTED(
50 TestRegexCommand::SubstituteVariables("%1%2%3%4", substitutions),
51 llvm::Failed());
52 ASSERT_THAT_EXPECTED(
53 TestRegexCommand::SubstituteVariables("%5", substitutions),
54 llvm::Failed());
55 ASSERT_THAT_EXPECTED(
56 TestRegexCommand::SubstituteVariables("%11", substitutions),
57 llvm::Failed());
58}
59
60TEST(RegexCommandTest, SubstituteVariablesNoRecursion) {
61 const llvm::SmallVector<llvm::StringRef, 4> substitutions = {"all", "%2",
62 "%3", "%4"};
63 EXPECT_EQ(TestRegexCommand::Substitute("%0", substitutions), "all");
64 EXPECT_EQ(TestRegexCommand::Substitute("%1", substitutions), "%2");
65 EXPECT_EQ(TestRegexCommand::Substitute("%2", substitutions), "%3");
66 EXPECT_EQ(TestRegexCommand::Substitute("%3", substitutions), "%4");
67 EXPECT_EQ(TestRegexCommand::Substitute("%1%2%3", substitutions), "%2%3%4");
68}
69

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