-
Notifications
You must be signed in to change notification settings - Fork 256
/
HoverVerificationTest.cs
506 lines (462 loc) · 15.7 KB
/
HoverVerificationTest.cs
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Boogie;
using Microsoft.Dafny.LanguageServer.IntegrationTest.Extensions;
using Microsoft.Dafny.LanguageServer.IntegrationTest.Synchronization;
using Microsoft.Dafny.LanguageServer.IntegrationTest.Util;
using Microsoft.Dafny.LanguageServer.Workspace;
using Microsoft.Dafny.LanguageServer.Workspace.Notifications;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using Xunit.Abstractions;
using Xunit;
using XunitAssertMessages;
using Assert = Xunit.Assert;
namespace Microsoft.Dafny.LanguageServer.IntegrationTest.Lookup {
[Collection("Sequential Collection")] // Let slow tests run sequentially
public class HoverVerificationTest : ClientBasedLanguageServerTest {
private const int MaxTestExecutionTimeMs = 30000;
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task HoverPutsErrorsFirst() {
var documentItem = await GetDocumentItem(@"
method {:isolate_assertions} Test(x: int, y: int)
requires x < y
{
var callIt := giveIt(x, y);
}
function giveIt(x: int, y: int): int
requires x != y // OK
requires 0 < x // Error
requires 0 < y // OK
requires x + 1 < y // Error
", "HoverPutsErrorsFirst.dfy", true);
// When hovering the postcondition, it should display the position of the failing path
await AssertVerificationHoverMatches(documentItem, (3, 21),
@"**Error:**???**Error:**???**Success:**???**Success:**???"
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task HoverGetsBasicAssertionInformation() {
var documentItem = await GetDocumentItem(@"
method Abs(x: int) returns (y: int)
// ^ Hover #4
ensures y >= 0
{ // ^ hover #1
if x < 2 { // Hover #2 on the brace
return -x;
}
assert x > 2; // Hover #3 on the '>'
return x;
}
", "testFile.dfy", true);
// When hovering the postcondition, it should display the position of the failing path
await AssertVerificationHoverMatches(documentItem, (2, 15),
@"[**Error:**](???) this postcondition could not be proved on a return path
This is assertion #??? of 4 in method `Abs`
Resource usage: ??? RU
Return path: testFile.dfy(6, 5)"
);
// When hovering the failing path, it does not display the position of the failing postcondition
// because the IDE extension already does it.
await AssertVerificationHoverMatches(documentItem, (5, 4),
@"[**Error:**](???) a postcondition could not be proved on this return path???
Could not prove: `y >= 0`
This is assertion #??? of 4 in method `Abs`
Resource usage: ??? RU"
);
await AssertVerificationHoverMatches(documentItem, (7, 11),
@"[**Error:**](???) assertion might not hold
This is assertion #??? of 4 in method `Abs`
Resource usage: ??? RU"
);
await AssertVerificationHoverMatches(documentItem, (0, 7),
@"**Verification performance metrics for method `Abs`**:
- Total resource usage: ??? RU
- Only one [assertion batch](???)"
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task HoverGetsForeignContentAsWell() {
var documentItem = await GetDocumentItem(@"
include ""foreign-verify.dfy""
predicate Q(i: int) {
P(i)
}
method DoIt() returns (x: int)
ensures Q(x)
{
return -1;
//^ hover #1
}", Path.Combine(Directory.GetCurrentDirectory(), "Lookup/TestFiles/test.dfy"), false);
// When hovering the failing path, it should extract text from the included file
await AssertVerificationHoverMatches(documentItem, (9, 4),
@"[**Error:**](???) a postcondition could not be proved on this return path???
Inside `Q(x)`
Inside `P(i)`
Could not prove: `i >= 0`
This is assertion #1 of 2 in method `DoIt`
Resource usage: ??? RU"
);
}
[Fact]
public async Task BetterMessageWhenOneAssertPerBatch() {
await SetUp(o => {
o.Set(CommonOptionBag.RelaxDefiniteAssignment, true);
// LineVerificationStatusOption.Instance.Set(o, true);
});
var documentItem = await GetDocumentItem(@"
method {:isolate_assertions} f(x: int) {
assert x >= 2; // Hover #1
assert x >= 1; // Hover #2
}
", "testfile.dfy", true);
await AssertVerificationHoverMatches(documentItem, (1, 12),
@"[**Error:**](???) assertion might not hold
This is the only assertion in [batch](???) #??? of ??? in method `f`
[Batch](???) #??? resource usage: ??? RU"
);
await AssertVerificationHoverMatches(documentItem, (2, 12),
@"<span style='color:green'>**Success:**</span> assertion always holds
This is the only assertion in [batch](???) #??? of ??? in method `f`
[Batch](???) #??? resource usage: ??? RU"
);
await AssertVerificationHoverMatches(documentItem, (0, 36),
@"**Verification performance metrics for method `f`**:
- Total resource usage: ??? RU
- Most costly [assertion batches](https://dafny-lang.github.io/dafny/DafnyRef/DafnyRef#sec-assertion-batches):
- #???/??? with 1 assertion at line ???, ??? RU
- #???/??? with 1 assertion at line ???, ??? RU "
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task BetterMessageWhenPreconditionSucceeds() {
await SetUp(o => {
o.Set(CommonOptionBag.RelaxDefiniteAssignment, true);
// LineVerificationStatusOption.Instance.Set(o, true);
});
var documentItem = await GetDocumentItem(@"
method Test(i: int)
requires {:error ""argument should be even"", ""argument is always even""} i % 2 == 0
requires i > 0
{
}
method main(k: int) {
Test(2);
Test(k);
}
", "testfile.dfy", false);
await AssertVerificationHoverMatches(documentItem, (6, 6),
@"**Success:**???argument is always even
Did prove: `i % 2 == 0`"
);
await AssertVerificationHoverMatches(documentItem, (6, 6),
@"**Success:**???the precondition always holds
Did prove: `i > 0`"
);
await AssertVerificationHoverMatches(documentItem, (7, 6),
@"**Error:**???argument should be even
Could not prove: `i % 2 == 0`"
);
await AssertVerificationHoverMatches(documentItem, (7, 6),
@"**Error:**???this is the precondition that could not be proved
Could not prove: `i > 0`"
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task BetterMessageWhenPostConditionFails() {
var documentItem = await GetDocumentItem(@"
method Test(j: int) returns (i: int)
ensures {:error ""return value should be even""} i % 2 == 0
ensures i > 0
{
i := j;
}", "testfile.dfy", true);
await AssertVerificationHoverMatches(documentItem, (3, 0),
@"**Error:**???return value should be even
Could not prove: `i % 2 == 0`"
);
await AssertVerificationHoverMatches(documentItem, (3, 0),
@"**Error:**???a postcondition could not be proved on this return path
Could not prove: `i > 0`"
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task MessagesWhenMultipleAssertionsPerBatch() {
var documentItem = await GetDocumentItem(@"
function f(x: int): int {
assert x >= 4;
assert x >= 2; // Hover #1
assert {:split_here} x >= 5; // hover #2
assert x >= 1;
x
}
", "testfile.dfy", false);
await AssertVerificationHoverMatches(documentItem, (2, 12),
@"???Success??? assertion always holds
This is assertion #2 of 2 in [batch](???) #1 of 2 in function `f`
[Batch](???) #1 resource usage: ??? RU"
);
await AssertVerificationHoverMatches(documentItem, (3, 26),
@"[**Error:**](???) assertion might not hold
This is assertion #1 of 2 in [batch](???) #2 of 2 in function `f`
[Batch](???) #2 resource usage: ??? RU"
);
await AssertVerificationHoverMatches(documentItem, (0, 36),
@"**Verification performance metrics for function `f`**:
- Total resource usage: ??? RU
- Most costly [assertion batches](https://dafny-lang.github.io/dafny/DafnyRef/DafnyRef#sec-assertion-batches):
- #???/2 with 2 assertions at line ???, ??? RU
- #???/2 with 2 assertions at line ???, ??? RU"
);
}
[Fact]
public async Task MeaningfulMessageWhenMethodWithoutAssert() {
var documentItem = await GetDocumentItem(@"
method f(x: int) {
print x;
}", "testfile.dfy", true);
await AssertVerificationHoverMatches(documentItem, (0, 7),
@"**Verification performance metrics for method `f`**:
No assertions."
);
await AssertVerificationHoverMatches(documentItem, (0, 10),
"```dafny\nx: int\n```");
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task MeaningfulMessageForFailingPreconditions() {
var documentItem = await GetDocumentItem(@"
method Test1() {
Test2(0); // Hover #1
}
method Test2(i: int)
requires i > 0 {
}", "testfile.dfy", false);
await AssertVerificationHoverMatches(documentItem, (1, 10),
@"???
Failing precondition:???"
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task MeaningfulMessageWhenMethodWithOneAssert() {
var documentItem = await GetDocumentItem(@"
method f(x: int) {
assert false;
}", "testfile1.dfy", true);
await AssertVerificationHoverMatches(documentItem, (0, 7),
@"**Verification performance metrics for method `f`**:
- Total resource usage: ??? RU
- Only one [assertion batch](???) containing 1 assertion."
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task MeaningfulMessageWhenMethodWithTwoAsserts() {
var documentItem = await GetDocumentItem(@"
method f(x: int) {
assert false;
assert false;
}", "testfile2.dfy", false);
await AssertVerificationHoverMatches(documentItem, (0, 7),
@"**Verification performance metrics for method `f`**:
- Total resource usage: ??? RU
- Only one [assertion batch](???) containing 2 assertions."
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task DoNotExtendPastExpressions2() {
var documentItem = await GetDocumentItem(@"
function Id<T>(t: T): T { t }
datatype Test = Test(i: int)
{
method Tester(other: Test) {
assert Valid(other);
assert CanAct(Id(other));
}
static predicate Valid(t: Test) {
t.i > 0
}
static predicate CanAct(t: Test) requires Valid(t) {
t.i > 1
}
}
", "testfile2.dfy", true);
await AssertVerificationHoverMatches(documentItem, (4, 20),
@"**Error:**???assertion might not hold???
Could not prove: `t.i > 0` "
);
await AssertVerificationHoverMatches(documentItem, (5, 20),
@"**Error:**???assertion might not hold???
Could not prove: `t.i > 1` "
);
await AssertVerificationHoverMatches(documentItem, (5, 20),
@"**Success:**???function precondition satisfied???
Inside `Valid(t)`
Did prove: `t.i > 0` "
);
}
[Fact/*(Timeout = MaxTestExecutionTimeMs)*/]
public async Task DoNotExtendPastExpressions3() {
var documentItem = await GetDocumentItem(@"
datatype ValidTester = Tester(next: ValidTester2) | Tester2(next: ValidTester2) | Test3(next: ValidTester2)
{
predicate Valid() {
((this.Tester? || this.Tester2?) && this.next.Valid()) || (this.Test3? && !this.next.Valid())
}
function apply(): int requires Valid() {
2
}
static method Test(c: ValidTester) {
var x := c.apply();
}
}
datatype ValidTester2 = MoreTest(i: int, next: ValidTester2) | End {
predicate Valid(defaultValue: int := 0) {
0 <= defaultValue && (this.End? || (this.MoreTest? && this.next.Valid() && i > 0))
}
}
", "testfile2.dfy", false);
await AssertVerificationHoverMatches(documentItem, (10, 16),
@"**Error:**???function precondition could not be proved???
Inside `Valid()`
Could not prove: `((this.Tester? || this.Tester2?) && this.next.Valid()) || (this.Test3? && !this.next.Valid())` "
);
}
[Fact]
public async Task DoNotExtendPastExpressions() {
var documentItem = await GetDocumentItem(@"
datatype Test = Test(i: int)
{
predicate Valid() {
i > 0
}
predicate CanAct() requires Valid() {
i > 1
}
method Tester(other: Test) {
assert other.Valid();
assert Id(other).CanAct();
}
}
function Id<T>(t: T): T { t }
", "testfile2.dfy", true);
await AssertVerificationHoverMatches(documentItem, (9, 20),
@"**Error:**???assertion might not hold???
Could not prove: `i > 0` "
);
await AssertVerificationHoverMatches(documentItem, (10, 20),
@"**Error:**???assertion might not hold???
Could not prove: `i > 1` "
);
await AssertVerificationHoverMatches(documentItem, (10, 20),
@"**Success:**???function precondition satisfied???
Inside `Valid()`
Did prove: `i > 0` "
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task DisplayNestedFailingPostconditionsAndPreconditions() {
var documentItem = await GetDocumentItem(@"
predicate P(i: int) {
i <= 0
}
predicate Q(i: int, j: int) {
i == j || -i == j
}
function Toast(i: int): int
requires P(i)
method Test(i: int) returns (j: nat)
ensures Q(i, j)
{
if i < 0 {
return -i;
} else {
return Toast(i);
}
}
", "testfile2.dfy", false);
await AssertVerificationHoverMatches(documentItem, (12, 11),
@"**Error:**???this postcondition could not be proved on a return path???
Could not prove: `i == j || -i == j`???
Return path: testfile2.dfy(18, 5)"
);
await AssertVerificationHoverMatches(documentItem, (17, 6),
@"**Error:**???a postcondition could not be proved on this return path???
Inside `Q(i, j)`???
Could not prove: `i == j || -i == j`"
);
await AssertVerificationHoverMatches(documentItem, (17, 13),
@"**Error:**???function precondition could not be proved???
Inside `P(i)`???
Could not prove: `i <= 0`"
);
}
[Fact/*(Timeout = MaxTestExecutionTimeMs)*/]
public async Task DisplayWorksOnPreviouslyFailingExample() {
var documentItem = await GetDocumentItem(@"
module ProblemModule {
datatype X =
| Cons(head: int, tail: X)
| Nil
{
predicate Valid() {
this.Cons? && tail.Valid()
}
}
}
method Test() returns (j: int)
ensures j == 1
{
return 2;
}
", "testfile2.dfy", true);
await AssertVerificationHoverMatches(documentItem, (14, 5),
@"**Error:**???a postcondition could not be proved on this return path???
Could not prove: `j == 1`"
);
}
[Fact(Timeout = MaxTestExecutionTimeMs)]
public async Task DoNotDisplayVerificationIfSyntaxError() {
var documentItem = await GetDocumentItem(@"
predicate P(i: int) {
i <= 0
}
method Test(i: int)
{
assert P(1);
}
", "testfile2.dfy", false);
await AssertVerificationHoverMatches(documentItem, (6, 11),
@"**Error:**???assertion might not hold
Inside `P(1)`
Could not prove: `i <= 0`"
);
await ApplyChangesAndWaitCompletionAsync(
ref documentItem,
new TextDocumentContentChangeEvent {
Range = ((0, 0), (0, 0)),
Text = @"/"
});
await AssertVerificationHoverMatches(documentItem, (6, 11),
"No hover information available due to program error"
);
}
[Fact(Timeout = 5 * MaxTestExecutionTimeMs)]
public async Task IndicateClickableWarningSignsOnMethodHoverWhenResourceLimitReached10MThreshold() {
var documentItem = await GetDocumentItem(@"
ghost function f(i:nat, j:nat):int {if i == 0 then 0 else f(i - 1, i * j + 1) + f(i - 1, 2 * i * j)}
lemma{:resource_limit 10000000} L()
{
assert f(10, 5) == 0;
} ", "testfileSlow.dfy", true);
await AssertVerificationHoverMatches(documentItem, (2, 22),
@"**Verification performance metrics for method `L`**:
- Total resource usage: ??? RU [⚠](???)"
);
}
public HoverVerificationTest(ITestOutputHelper output) : base(output) {
}
}
}