1 //===-- StringLexerTest.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 "lldb/Utility/StringLexer.h"
10 #include "gtest/gtest.h"
12 using namespace lldb_private
;
14 TEST(StringLexerTest
, GetUnlexed
) {
16 EXPECT_EQ("foo", l
.GetUnlexed());
18 EXPECT_EQ("oo", l
.GetUnlexed());
21 EXPECT_EQ("", l
.GetUnlexed());
24 TEST(StringLexerTest
, HasAtLeast
) {
26 EXPECT_FALSE(l
.HasAtLeast(5));
27 EXPECT_FALSE(l
.HasAtLeast(4));
28 EXPECT_TRUE(l
.HasAtLeast(3));
29 EXPECT_TRUE(l
.HasAtLeast(2));
30 EXPECT_TRUE(l
.HasAtLeast(1));
33 EXPECT_FALSE(l
.HasAtLeast(5));
34 EXPECT_FALSE(l
.HasAtLeast(4));
35 EXPECT_FALSE(l
.HasAtLeast(3));
36 EXPECT_TRUE(l
.HasAtLeast(2));
37 EXPECT_TRUE(l
.HasAtLeast(1));
41 EXPECT_FALSE(l
.HasAtLeast(5));
42 EXPECT_FALSE(l
.HasAtLeast(4));
43 EXPECT_FALSE(l
.HasAtLeast(3));
44 EXPECT_FALSE(l
.HasAtLeast(2));
45 EXPECT_FALSE(l
.HasAtLeast(1));
48 TEST(StringLexerTest
, AdvanceIf
) {
49 StringLexer
l("foobar");
51 EXPECT_FALSE(l
.AdvanceIf("oo"));
52 // Skip the "fo" part.
53 EXPECT_TRUE(l
.AdvanceIf("fo"));
54 EXPECT_FALSE(l
.AdvanceIf("obarz"));
55 // Skip the remaining string.
56 EXPECT_TRUE(l
.AdvanceIf("obar"));
58 EXPECT_FALSE(l
.AdvanceIf("obarz"));
59 EXPECT_FALSE(l
.AdvanceIf("foo"));
60 EXPECT_FALSE(l
.AdvanceIf("o"));
61 EXPECT_FALSE(l
.AdvanceIf(" "));
64 TEST(StringLexerTest
, PutBack
) {
69 EXPECT_EQ("foo", l
.GetUnlexed());
75 EXPECT_EQ("oo", l
.GetUnlexed());
78 EXPECT_EQ("foo", l
.GetUnlexed());
81 TEST(StringLexerTest
, Peek
) {
84 EXPECT_EQ('f', l
.Peek());
86 EXPECT_EQ('o', l
.Peek());
88 EXPECT_EQ('o', l
.Peek());
91 TEST(StringLexerTest
, Next
) {
93 EXPECT_EQ('f', l
.Next());
94 EXPECT_EQ('o', l
.Next());
95 EXPECT_EQ('o', l
.Next());
98 TEST(StringLexerTest
, NextIf
) {
101 EXPECT_FALSE(l
.NextIf('\0'));
102 EXPECT_FALSE(l
.NextIf(' '));
103 EXPECT_FALSE(l
.NextIf('o'));
105 EXPECT_TRUE(l
.NextIf('f'));
107 EXPECT_FALSE(l
.NextIf('\0'));
108 EXPECT_FALSE(l
.NextIf(' '));
109 EXPECT_FALSE(l
.NextIf('f'));
111 EXPECT_TRUE(l
.NextIf('o'));
113 EXPECT_FALSE(l
.NextIf('\0'));
114 EXPECT_FALSE(l
.NextIf(' '));
115 EXPECT_FALSE(l
.NextIf('f'));
117 EXPECT_TRUE(l
.NextIf('o'));
120 TEST(StringLexerTest
, NextIfList
) {
121 StringLexer
l("foo");
123 EXPECT_FALSE(l
.NextIf({'\0', ' ', 'o'}).first
);
125 auto r
= l
.NextIf({'f'});
126 EXPECT_TRUE(r
.first
);
127 EXPECT_EQ('f', r
.second
);
129 EXPECT_FALSE(l
.NextIf({'\0', ' ', 'f'}).first
);
131 r
= l
.NextIf({'f', 'o'});
132 EXPECT_TRUE(r
.first
);
133 EXPECT_EQ('o', r
.second
);
135 EXPECT_FALSE(l
.NextIf({'\0', ' ', 'f'}).first
);
137 r
= l
.NextIf({'*', 'f', 'o', 'o'});
138 EXPECT_TRUE(r
.first
);
139 EXPECT_EQ('o', r
.second
);