DraftStoreTests.cpp
9.87 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//===-- DraftStoreTests.cpp -------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Annotations.h"
#include "DraftStore.h"
#include "SourceCode.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace {
struct IncrementalTestStep {
llvm::StringRef Src;
llvm::StringRef Contents;
};
int rangeLength(llvm::StringRef Code, const Range &Rng) {
llvm::Expected<size_t> Start = positionToOffset(Code, Rng.start);
llvm::Expected<size_t> End = positionToOffset(Code, Rng.end);
assert(Start);
assert(End);
return *End - *Start;
}
/// Send the changes one by one to updateDraft, verify the intermediate results.
void stepByStep(llvm::ArrayRef<IncrementalTestStep> Steps) {
DraftStore DS;
Annotations InitialSrc(Steps.front().Src);
constexpr llvm::StringLiteral Path("/hello.cpp");
// Set the initial content.
EXPECT_EQ(0, DS.addDraft(Path, llvm::None, InitialSrc.code()));
for (size_t i = 1; i < Steps.size(); i++) {
Annotations SrcBefore(Steps[i - 1].Src);
Annotations SrcAfter(Steps[i].Src);
llvm::StringRef Contents = Steps[i - 1].Contents;
TextDocumentContentChangeEvent Event{
SrcBefore.range(),
rangeLength(SrcBefore.code(), SrcBefore.range()),
Contents.str(),
};
llvm::Expected<DraftStore::Draft> Result =
DS.updateDraft(Path, llvm::None, {Event});
ASSERT_TRUE(!!Result);
EXPECT_EQ(Result->Contents, SrcAfter.code());
EXPECT_EQ(DS.getDraft(Path)->Contents, SrcAfter.code());
EXPECT_EQ(Result->Version, static_cast<int64_t>(i));
}
}
/// Send all the changes at once to updateDraft, check only the final result.
void allAtOnce(llvm::ArrayRef<IncrementalTestStep> Steps) {
DraftStore DS;
Annotations InitialSrc(Steps.front().Src);
Annotations FinalSrc(Steps.back().Src);
constexpr llvm::StringLiteral Path("/hello.cpp");
std::vector<TextDocumentContentChangeEvent> Changes;
for (size_t i = 0; i < Steps.size() - 1; i++) {
Annotations Src(Steps[i].Src);
llvm::StringRef Contents = Steps[i].Contents;
Changes.push_back({
Src.range(),
rangeLength(Src.code(), Src.range()),
Contents.str(),
});
}
// Set the initial content.
EXPECT_EQ(0, DS.addDraft(Path, llvm::None, InitialSrc.code()));
llvm::Expected<DraftStore::Draft> Result =
DS.updateDraft(Path, llvm::None, Changes);
ASSERT_TRUE(!!Result) << llvm::toString(Result.takeError());
EXPECT_EQ(Result->Contents, FinalSrc.code());
EXPECT_EQ(DS.getDraft(Path)->Contents, FinalSrc.code());
EXPECT_EQ(Result->Version, 1);
}
TEST(DraftStoreIncrementalUpdateTest, Simple) {
// clang-format off
IncrementalTestStep Steps[] =
{
// Replace a range
{
R"cpp(static int
hello[[World]]()
{})cpp",
"Universe"
},
// Delete a range
{
R"cpp(static int
hello[[Universe]]()
{})cpp",
""
},
// Add a range
{
R"cpp(static int
hello[[]]()
{})cpp",
"Monde"
},
{
R"cpp(static int
helloMonde()
{})cpp",
""
}
};
// clang-format on
stepByStep(Steps);
allAtOnce(Steps);
}
TEST(DraftStoreIncrementalUpdateTest, MultiLine) {
// clang-format off
IncrementalTestStep Steps[] =
{
// Replace a range
{
R"cpp(static [[int
helloWorld]]()
{})cpp",
R"cpp(char
welcome)cpp"
},
// Delete a range
{
R"cpp(static char[[
welcome]]()
{})cpp",
""
},
// Add a range
{
R"cpp(static char[[]]()
{})cpp",
R"cpp(
cookies)cpp"
},
// Replace the whole file
{
R"cpp([[static char
cookies()
{}]])cpp",
R"cpp(#include <stdio.h>
)cpp"
},
// Delete the whole file
{
R"cpp([[#include <stdio.h>
]])cpp",
"",
},
// Add something to an empty file
{
"[[]]",
R"cpp(int main() {
)cpp",
},
{
R"cpp(int main() {
)cpp",
""
}
};
// clang-format on
stepByStep(Steps);
allAtOnce(Steps);
}
TEST(DraftStoreIncrementalUpdateTest, WrongRangeLength) {
DraftStore DS;
Path File = "foo.cpp";
DS.addDraft(File, llvm::None, "int main() {}\n");
TextDocumentContentChangeEvent Change;
Change.range.emplace();
Change.range->start.line = 0;
Change.range->start.character = 0;
Change.range->end.line = 0;
Change.range->end.character = 2;
Change.rangeLength = 10;
Expected<DraftStore::Draft> Result =
DS.updateDraft(File, llvm::None, {Change});
EXPECT_TRUE(!Result);
EXPECT_EQ(
toString(Result.takeError()),
"Change's rangeLength (10) doesn't match the computed range length (2).");
}
TEST(DraftStoreIncrementalUpdateTest, EndBeforeStart) {
DraftStore DS;
Path File = "foo.cpp";
DS.addDraft(File, llvm::None, "int main() {}\n");
TextDocumentContentChangeEvent Change;
Change.range.emplace();
Change.range->start.line = 0;
Change.range->start.character = 5;
Change.range->end.line = 0;
Change.range->end.character = 3;
auto Result = DS.updateDraft(File, llvm::None, {Change});
EXPECT_TRUE(!Result);
EXPECT_EQ(toString(Result.takeError()),
"Range's end position (0:3) is before start position (0:5)");
}
TEST(DraftStoreIncrementalUpdateTest, StartCharOutOfRange) {
DraftStore DS;
Path File = "foo.cpp";
DS.addDraft(File, llvm::None, "int main() {}\n");
TextDocumentContentChangeEvent Change;
Change.range.emplace();
Change.range->start.line = 0;
Change.range->start.character = 100;
Change.range->end.line = 0;
Change.range->end.character = 100;
Change.text = "foo";
auto Result = DS.updateDraft(File, llvm::None, {Change});
EXPECT_TRUE(!Result);
EXPECT_EQ(toString(Result.takeError()),
"utf-16 offset 100 is invalid for line 0");
}
TEST(DraftStoreIncrementalUpdateTest, EndCharOutOfRange) {
DraftStore DS;
Path File = "foo.cpp";
DS.addDraft(File, llvm::None, "int main() {}\n");
TextDocumentContentChangeEvent Change;
Change.range.emplace();
Change.range->start.line = 0;
Change.range->start.character = 0;
Change.range->end.line = 0;
Change.range->end.character = 100;
Change.text = "foo";
auto Result = DS.updateDraft(File, llvm::None, {Change});
EXPECT_TRUE(!Result);
EXPECT_EQ(toString(Result.takeError()),
"utf-16 offset 100 is invalid for line 0");
}
TEST(DraftStoreIncrementalUpdateTest, StartLineOutOfRange) {
DraftStore DS;
Path File = "foo.cpp";
DS.addDraft(File, llvm::None, "int main() {}\n");
TextDocumentContentChangeEvent Change;
Change.range.emplace();
Change.range->start.line = 100;
Change.range->start.character = 0;
Change.range->end.line = 100;
Change.range->end.character = 0;
Change.text = "foo";
auto Result = DS.updateDraft(File, llvm::None, {Change});
EXPECT_TRUE(!Result);
EXPECT_EQ(toString(Result.takeError()), "Line value is out of range (100)");
}
TEST(DraftStoreIncrementalUpdateTest, EndLineOutOfRange) {
DraftStore DS;
Path File = "foo.cpp";
DS.addDraft(File, llvm::None, "int main() {}\n");
TextDocumentContentChangeEvent Change;
Change.range.emplace();
Change.range->start.line = 0;
Change.range->start.character = 0;
Change.range->end.line = 100;
Change.range->end.character = 0;
Change.text = "foo";
auto Result = DS.updateDraft(File, llvm::None, {Change});
EXPECT_TRUE(!Result);
EXPECT_EQ(toString(Result.takeError()), "Line value is out of range (100)");
}
/// Check that if a valid change is followed by an invalid change, the original
/// version of the document (prior to all changes) is kept.
TEST(DraftStoreIncrementalUpdateTest, InvalidRangeInASequence) {
DraftStore DS;
Path File = "foo.cpp";
StringRef OriginalContents = "int main() {}\n";
EXPECT_EQ(0, DS.addDraft(File, llvm::None, OriginalContents));
// The valid change
TextDocumentContentChangeEvent Change1;
Change1.range.emplace();
Change1.range->start.line = 0;
Change1.range->start.character = 0;
Change1.range->end.line = 0;
Change1.range->end.character = 0;
Change1.text = "Hello ";
// The invalid change
TextDocumentContentChangeEvent Change2;
Change2.range.emplace();
Change2.range->start.line = 0;
Change2.range->start.character = 5;
Change2.range->end.line = 0;
Change2.range->end.character = 100;
Change2.text = "something";
auto Result = DS.updateDraft(File, llvm::None, {Change1, Change2});
EXPECT_TRUE(!Result);
EXPECT_EQ(toString(Result.takeError()),
"utf-16 offset 100 is invalid for line 0");
Optional<DraftStore::Draft> Contents = DS.getDraft(File);
EXPECT_TRUE(Contents);
EXPECT_EQ(Contents->Contents, OriginalContents);
EXPECT_EQ(Contents->Version, 0);
}
TEST(DraftStore, Version) {
DraftStore DS;
Path File = "foo.cpp";
EXPECT_EQ(25, DS.addDraft(File, 25, ""));
EXPECT_EQ(25, DS.getDraft(File)->Version);
EXPECT_EQ(26, DS.addDraft(File, llvm::None, ""));
EXPECT_EQ(26, DS.getDraft(File)->Version);
// We allow versions to go backwards.
EXPECT_EQ(7, DS.addDraft(File, 7, ""));
EXPECT_EQ(7, DS.getDraft(File)->Version);
// Valid (no-op) change modifies version.
auto Result = DS.updateDraft(File, 10, {});
EXPECT_TRUE(!!Result);
EXPECT_EQ(10, Result->Version);
EXPECT_EQ(10, DS.getDraft(File)->Version);
Result = DS.updateDraft(File, llvm::None, {});
EXPECT_TRUE(!!Result);
EXPECT_EQ(11, Result->Version);
EXPECT_EQ(11, DS.getDraft(File)->Version);
TextDocumentContentChangeEvent InvalidChange;
InvalidChange.range.emplace();
InvalidChange.rangeLength = 99;
Result = DS.updateDraft(File, 15, {InvalidChange});
EXPECT_FALSE(!!Result);
consumeError(Result.takeError());
EXPECT_EQ(11, DS.getDraft(File)->Version);
}
} // namespace
} // namespace clangd
} // namespace clang