| 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 |
|
| 13 |
using namespace lldb_private; |
| 14 |
using namespace lldb; |
| 15 |
|
| 16 |
namespace { |
| 17 |
class TestRegexCommand : public CommandObjectRegexCommand { |
| 18 |
public: |
| 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(str.takeError()); |
| 27 |
return *str; |
| 28 |
} |
| 29 |
}; |
| 30 |
} // namespace |
| 31 |
|
| 32 |
TEST(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 |
|
| 45 |
TEST(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 |
|
| 60 |
TEST(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 |
} |