1//===-- StringLexerTest.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/StringLexer.h"
10#include "gtest/gtest.h"
11
12using namespace lldb_private;
13
14TEST(StringLexerTest, GetUnlexed) {
15 StringLexer l("foo");
16 EXPECT_EQ("foo", l.GetUnlexed());
17 l.Next();
18 EXPECT_EQ("oo", l.GetUnlexed());
19 l.Next();
20 l.Next();
21 EXPECT_EQ("", l.GetUnlexed());
22}
23
24TEST(StringLexerTest, HasAtLeast) {
25 StringLexer l("foo");
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));
31
32 l.Next();
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));
38
39 l.Next();
40 l.Next();
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));
46}
47
48TEST(StringLexerTest, AdvanceIf) {
49 StringLexer l("foobar");
50
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"));
57
58 EXPECT_FALSE(l.AdvanceIf("obarz"));
59 EXPECT_FALSE(l.AdvanceIf("foo"));
60 EXPECT_FALSE(l.AdvanceIf("o"));
61 EXPECT_FALSE(l.AdvanceIf(" "));
62}
63
64TEST(StringLexerTest, PutBack) {
65 StringLexer l("foo");
66
67 l.Next();
68 l.PutBack(s: 1);
69 EXPECT_EQ("foo", l.GetUnlexed());
70
71 l.Next();
72 l.Next();
73 l.Next();
74 l.PutBack(s: 2);
75 EXPECT_EQ("oo", l.GetUnlexed());
76
77 l.PutBack(s: 1);
78 EXPECT_EQ("foo", l.GetUnlexed());
79}
80
81TEST(StringLexerTest, Peek) {
82 StringLexer l("foo");
83
84 EXPECT_EQ('f', l.Peek());
85 l.Next();
86 EXPECT_EQ('o', l.Peek());
87 l.Next();
88 EXPECT_EQ('o', l.Peek());
89}
90
91TEST(StringLexerTest, Next) {
92 StringLexer l("foo");
93 EXPECT_EQ('f', l.Next());
94 EXPECT_EQ('o', l.Next());
95 EXPECT_EQ('o', l.Next());
96}
97
98TEST(StringLexerTest, NextIf) {
99 StringLexer l("foo");
100
101 EXPECT_FALSE(l.NextIf('\0'));
102 EXPECT_FALSE(l.NextIf(' '));
103 EXPECT_FALSE(l.NextIf('o'));
104
105 EXPECT_TRUE(l.NextIf('f'));
106
107 EXPECT_FALSE(l.NextIf('\0'));
108 EXPECT_FALSE(l.NextIf(' '));
109 EXPECT_FALSE(l.NextIf('f'));
110
111 EXPECT_TRUE(l.NextIf('o'));
112
113 EXPECT_FALSE(l.NextIf('\0'));
114 EXPECT_FALSE(l.NextIf(' '));
115 EXPECT_FALSE(l.NextIf('f'));
116
117 EXPECT_TRUE(l.NextIf('o'));
118}
119
120TEST(StringLexerTest, NextIfList) {
121 StringLexer l("foo");
122
123 EXPECT_FALSE(l.NextIf({'\0', ' ', 'o'}).first);
124
125 auto r = l.NextIf(cs: {'f'});
126 EXPECT_TRUE(r.first);
127 EXPECT_EQ('f', r.second);
128
129 EXPECT_FALSE(l.NextIf({'\0', ' ', 'f'}).first);
130
131 r = l.NextIf(cs: {'f', 'o'});
132 EXPECT_TRUE(r.first);
133 EXPECT_EQ('o', r.second);
134
135 EXPECT_FALSE(l.NextIf({'\0', ' ', 'f'}).first);
136
137 r = l.NextIf(cs: {'*', 'f', 'o', 'o'});
138 EXPECT_TRUE(r.first);
139 EXPECT_EQ('o', r.second);
140}
141

source code of lldb/unittests/Utility/StringLexerTest.cpp