forked from google/effcee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor_test.cc
179 lines (151 loc) · 5.18 KB
/
cursor_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2017 The Effcee Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gmock/gmock.h"
#include "cursor.h"
namespace {
using effcee::Cursor;
using effcee::LineMessage;
using effcee::StringPiece;
using ::testing::Eq;
using ::testing::HasSubstr;
// text method
// remaining and Advance methods
TEST(Cursor, AdvanceReturnsTheCursorItself) {
Cursor c("foo");
EXPECT_THAT(&c.Advance(1), Eq(&c));
}
TEST(Cursor, RemainingBeginsEqualToText) {
const char* original = "The Smiths";
Cursor c(original);
EXPECT_THAT(c.remaining().begin(), Eq(original));
}
TEST(Cursor, RemainingDiminishesByPreviousAdvanceCalls) {
const char* original = "The Smiths are a great 80s band";
Cursor c(original);
c.Advance(4);
EXPECT_THAT(c.remaining(), Eq("Smiths are a great 80s band"));
EXPECT_THAT(c.remaining().begin(), Eq(original + 4));
c.Advance(11);
EXPECT_THAT(c.remaining(), Eq("a great 80s band"));
EXPECT_THAT(c.remaining().begin(), Eq(original + 15));
c.Advance(c.remaining().size());
EXPECT_THAT(c.remaining(), Eq(""));
EXPECT_THAT(c.remaining().begin(), Eq(original + 31));
}
// Exhausted method
TEST(Cursor, ExhaustedImmediatelyWhenStartingWithEmptyString) {
Cursor c("");
EXPECT_TRUE(c.Exhausted());
}
TEST(Cursor, ExhaustedWhenRemainingIsEmpty) {
Cursor c("boo");
EXPECT_FALSE(c.Exhausted());
c.Advance(2);
EXPECT_FALSE(c.Exhausted());
c.Advance(1);
EXPECT_TRUE(c.Exhausted());
}
// RestOfLine method
TEST(Cursor, RestOfLineOnEmptyReturnsEmpty) {
const char* original = "";
Cursor c(original);
EXPECT_THAT(c.RestOfLine(), Eq(""));
EXPECT_THAT(c.RestOfLine().begin(), Eq(original));
}
TEST(Cursor, RestOfLineWithoutNewline) {
Cursor c("The end");
EXPECT_THAT(c.RestOfLine(), Eq("The end"));
}
TEST(Cursor, RestOfLineGetsLineUpToAndIncludingNewline) {
Cursor c("The end\nOf an era");
EXPECT_THAT(c.RestOfLine(), Eq("The end\n"));
}
TEST(Cursor, RestOfLineGetsOnlyFromRemainingText) {
Cursor c("The end\nOf an era");
c.Advance(4);
EXPECT_THAT(c.remaining(), Eq("end\nOf an era"));
EXPECT_THAT(c.RestOfLine(), Eq("end\n"));
}
// AdvanceLine and line_num methods
TEST(Cursor, AdvanceLineReturnsTheCursorItself) {
Cursor c("foo\nbar");
EXPECT_THAT(&c.AdvanceLine(), Eq(&c));
}
TEST(Cursor, AdvanceLineWalksThroughTextByLineAndCountsLines) {
const char* original = "The end\nOf an era\nIs here";
Cursor c(original);
EXPECT_THAT(c.line_num(), Eq(1));
c.AdvanceLine();
EXPECT_THAT(c.line_num(), Eq(2));
EXPECT_THAT(c.remaining(), Eq("Of an era\nIs here"));
EXPECT_THAT(c.remaining().begin(), Eq(original + 8));
c.AdvanceLine();
EXPECT_THAT(c.line_num(), Eq(3));
EXPECT_THAT(c.remaining(), Eq("Is here"));
EXPECT_THAT(c.remaining().begin(), Eq(original + 18));
c.AdvanceLine();
EXPECT_THAT(c.line_num(), Eq(4));
EXPECT_THAT(c.remaining(), Eq(""));
EXPECT_THAT(c.remaining().begin(), Eq(original + 25));
}
TEST(Cursor, AdvanceLineIsNoopAfterEndIsReached) {
Cursor c("One\nTwo");
c.AdvanceLine();
EXPECT_THAT(c.line_num(), Eq(2));
EXPECT_THAT(c.remaining(), Eq("Two"));
c.AdvanceLine();
EXPECT_THAT(c.line_num(), Eq(3));
EXPECT_THAT(c.remaining(), Eq(""));
c.AdvanceLine();
EXPECT_THAT(c.line_num(), Eq(3));
EXPECT_THAT(c.remaining(), Eq(""));
}
// LineMessage free function.
TEST(LineMessage, SubtextIsFirst) {
StringPiece text("Foo\nBar");
StringPiece subtext(text.begin(), 3);
EXPECT_THAT(LineMessage(text, subtext, "loves quiche"),
Eq(":1:1: loves quiche\nFoo\n^\n"));
}
TEST(LineMessage, SubtextDoesNotEndInNewline) {
StringPiece text("Foo\nBar");
StringPiece subtext(text.begin()+4, 3);
EXPECT_THAT(LineMessage(text, subtext, "loves quiche"),
Eq(":2:1: loves quiche\nBar\n^\n"));
}
TEST(LineMessage, SubtextPartwayThroughItsLine) {
StringPiece text("Food Life\nBar");
StringPiece subtext(text.begin() + 5, 3); // "Lif"
EXPECT_THAT(LineMessage(text, subtext, "loves quiche"),
Eq(":1:6: loves quiche\nFood Life\n ^\n"));
}
TEST(LineMessage, SubtextOnSubsequentLine) {
StringPiece text("Food Life\nBar Fight\n");
StringPiece subtext(text.begin() + 14, 5); // "Fight"
EXPECT_THAT(LineMessage(text, subtext, "loves quiche"),
Eq(":2:5: loves quiche\nBar Fight\n ^\n"));
}
TEST(LineMessage, SubtextIsEmptyAndInMiddle) {
StringPiece text("Food");
StringPiece subtext(text.begin() + 2, 0);
EXPECT_THAT(LineMessage(text, subtext, "loves quiche"),
Eq(":1:3: loves quiche\nFood\n ^\n"));
}
TEST(LineMessage, SubtextIsEmptyAndAtVeryEnd) {
StringPiece text("Food");
StringPiece subtext(text.begin() + 4, 0);
EXPECT_THAT(LineMessage(text, subtext, "loves quiche"),
Eq(":1:5: loves quiche\nFood\n ^\n"));
}
} // namespace