1 | //===-- RegularExpressionTest.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 "lldb/Utility/RegularExpression.h" |
10 | #include "llvm/ADT/SmallVector.h" |
11 | #include "gtest/gtest.h" |
12 | |
13 | using namespace lldb_private; |
14 | using namespace llvm; |
15 | |
16 | TEST(RegularExpression, Valid) { |
17 | RegularExpression r1("^[0-9]+$" ); |
18 | cantFail(Err: r1.GetError()); |
19 | EXPECT_TRUE(r1.IsValid()); |
20 | EXPECT_EQ("^[0-9]+$" , r1.GetText()); |
21 | EXPECT_TRUE(r1.Execute("916" )); |
22 | } |
23 | |
24 | TEST(RegularExpression, CopyAssignment) { |
25 | RegularExpression r1("^[0-9]+$" ); |
26 | RegularExpression r2 = r1; |
27 | cantFail(Err: r2.GetError()); |
28 | EXPECT_TRUE(r2.IsValid()); |
29 | EXPECT_EQ("^[0-9]+$" , r2.GetText()); |
30 | EXPECT_TRUE(r2.Execute("916" )); |
31 | } |
32 | |
33 | TEST(RegularExpression, Empty) { |
34 | RegularExpression r1("" ); |
35 | Error err = r1.GetError(); |
36 | EXPECT_TRUE(static_cast<bool>(err)); |
37 | consumeError(Err: std::move(err)); |
38 | EXPECT_FALSE(r1.IsValid()); |
39 | EXPECT_EQ("" , r1.GetText()); |
40 | EXPECT_FALSE(r1.Execute("916" )); |
41 | } |
42 | |
43 | TEST(RegularExpression, Invalid) { |
44 | RegularExpression r1("a[b-" ); |
45 | Error err = r1.GetError(); |
46 | EXPECT_TRUE(static_cast<bool>(err)); |
47 | consumeError(Err: std::move(err)); |
48 | EXPECT_FALSE(r1.IsValid()); |
49 | EXPECT_EQ("a[b-" , r1.GetText()); |
50 | EXPECT_FALSE(r1.Execute("ab" )); |
51 | } |
52 | |
53 | TEST(RegularExpression, Match) { |
54 | RegularExpression r1("[0-9]+([a-f])?:([0-9]+)" ); |
55 | cantFail(Err: r1.GetError()); |
56 | EXPECT_TRUE(r1.IsValid()); |
57 | EXPECT_EQ("[0-9]+([a-f])?:([0-9]+)" , r1.GetText()); |
58 | |
59 | SmallVector<StringRef, 3> matches; |
60 | EXPECT_TRUE(r1.Execute("9a:513b" , &matches)); |
61 | EXPECT_EQ(3u, matches.size()); |
62 | EXPECT_EQ("9a:513" , matches[0].str()); |
63 | EXPECT_EQ("a" , matches[1].str()); |
64 | EXPECT_EQ("513" , matches[2].str()); |
65 | } |
66 | |