1 //===-- TestRegexCommand.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 "Commands/CommandObjectRegexCommand.h"
10 #include "llvm/Testing/Support/Error.h"
11 #include "gtest/gtest.h"
13 using namespace lldb_private
;
17 class TestRegexCommand
: public CommandObjectRegexCommand
{
19 using CommandObjectRegexCommand::SubstituteVariables
;
22 Substitute(llvm::StringRef input
,
23 const llvm::SmallVectorImpl
<llvm::StringRef
> &replacements
) {
24 llvm::Expected
<std::string
> str
= SubstituteVariables(input
, replacements
);
26 return llvm::toString(str
.takeError());
32 TEST(RegexCommandTest
, SubstituteVariablesSuccess
) {
33 const llvm::SmallVector
<llvm::StringRef
, 4> substitutions
= {"all", "foo",
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
),
45 TEST(RegexCommandTest
, SubstituteVariablesFailed
) {
46 const llvm::SmallVector
<llvm::StringRef
, 4> substitutions
= {"all", "foo",
50 TestRegexCommand::SubstituteVariables("%1%2%3%4", substitutions
),
53 TestRegexCommand::SubstituteVariables("%5", substitutions
),
56 TestRegexCommand::SubstituteVariables("%11", substitutions
),
60 TEST(RegexCommandTest
, SubstituteVariablesNoRecursion
) {
61 const llvm::SmallVector
<llvm::StringRef
, 4> substitutions
= {"all", "%2",
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");