Skip to content

Commit ff9811b

Browse files
committed
C#: Add tests for the inline test post-processor
1 parent cc94c42 commit ff9811b

File tree

9 files changed

+148
-0
lines changed

9 files changed

+148
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
class C
2+
{
3+
void Problems()
4+
{
5+
// correct expectation comment, but only for `problem-query`
6+
var x = "Alert"; // $ Alert
7+
8+
// irrelevant expectation comment, will be ignored
9+
x = "Not an alert"; // $ IrrelevantTag
10+
11+
// incorrect expectation comment
12+
x = "Also not an alert"; // $ Alert
13+
14+
// missing expectation comment, but only for `problem-query`
15+
x = "Alert";
16+
17+
// correct expectation comment
18+
x = "Alert"; // $ Alert[problem-query]
19+
}
20+
21+
void PathProblems()
22+
{
23+
// correct expectation comments, but only for `path-problem-query`
24+
var source = "Source"; // $ Source
25+
var sink = "Sink"; // $ Sink
26+
var x = "Alert:2:1"; // $ Alert
27+
28+
// incorrect expectation comments
29+
source = "Source"; // $ Source
30+
sink = "Sink"; // $ Sink
31+
x = "Not an alert:2:1"; // $ Alert
32+
33+
// missing expectation comments, but only for `path-problem-query`
34+
source = "Source";
35+
sink = "Sink";
36+
x = "Alert:2:1";
37+
38+
// correct expectation comments
39+
source = "Source"; // $ Source[path-problem-query]
40+
sink = "Sink"; // $ Sink[path-problem-query]
41+
x = "Alert:2:1"; // $ Alert[path-problem-query]
42+
43+
// correct expectation comments; the alert location coincides with the sink location
44+
source = "Source"; // $ Source[path-problem-query]
45+
x = "Alert:1:0"; // $ Alert[path-problem-query]
46+
47+
// correct expectation comments; the alert location coincides with the source location
48+
sink = "Sink"; // $ Sink[path-problem-query]
49+
x = "Alert:0:1"; // $ Alert[path-problem-query]
50+
51+
// correct expectation comments, using an identifier tag
52+
source = "Source"; // $ Source[path-problem-query]=source1
53+
sink = "Sink"; // $ Sink[path-problem-query]=source1
54+
x = "Alert:2:1"; // $ Alert[path-problem-query]=source1
55+
56+
// incorrect expectation comment, using wrong identifier tag at the sink
57+
source = "Source"; // $ Source[path-problem-query]=source2
58+
sink = "Sink"; // $ Sink[path-problem-query]=source1
59+
x = "Alert:2:1"; // $ Alert[path-problem-query]=source2
60+
61+
// incorrect expectation comment, using wrong identifier tag at the alert
62+
source = "Source"; // $ Source[path-problem-query]=source3
63+
sink = "Sink"; // $ Sink[path-problem-query]=source3
64+
x = "Alert:2:1"; // $ Alert[path-problem-query]=source2
65+
66+
// correct expectation comments, using an identifier tag; the alert location coincides with the sink location
67+
source = "Source"; // $ Source[path-problem-query]=source4
68+
x = "Alert:1:0"; // $ Alert[path-problem-query]=source4
69+
70+
// incorrect expectation comments, using an identifier tag; the alert location coincides with the sink location
71+
source = "Source"; // $ Source[path-problem-query]=source5
72+
x = "Alert:1:0"; // $ Alert[path-problem-query]=source4
73+
74+
// correct expectation comments, using an identifier tag; the alert location coincides with the source location
75+
sink = "Sink"; // $ Sink[path-problem-query]=sink1
76+
x = "Alert:0:1"; // $ Alert[path-problem-query]=sink1
77+
78+
// incorrect expectation comments, using an identifier tag; the alert location coincides with the source location
79+
sink = "Sink"; // $ Sink[path-problem-query]=sink2
80+
x = "Alert:0:1"; // $ Alert[path-problem-query]=sink1
81+
}
82+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#select
2+
| InlineTests.cs:26:17:26:27 | "Alert:2:1" | InlineTests.cs:24:22:24:29 | "Source" | InlineTests.cs:25:20:25:25 | "Sink" | This is a problem |
3+
| InlineTests.cs:36:13:36:23 | "Alert:2:1" | InlineTests.cs:34:18:34:25 | "Source" | InlineTests.cs:35:16:35:21 | "Sink" | This is a problem |
4+
| InlineTests.cs:41:13:41:23 | "Alert:2:1" | InlineTests.cs:39:18:39:25 | "Source" | InlineTests.cs:40:16:40:21 | "Sink" | This is a problem |
5+
| InlineTests.cs:45:13:45:23 | "Alert:1:0" | InlineTests.cs:44:18:44:25 | "Source" | InlineTests.cs:45:13:45:23 | "Alert:1:0" | This is a problem |
6+
| InlineTests.cs:49:13:49:23 | "Alert:0:1" | InlineTests.cs:49:13:49:23 | "Alert:0:1" | InlineTests.cs:48:16:48:21 | "Sink" | This is a problem |
7+
| InlineTests.cs:54:13:54:23 | "Alert:2:1" | InlineTests.cs:52:18:52:25 | "Source" | InlineTests.cs:53:16:53:21 | "Sink" | This is a problem |
8+
| InlineTests.cs:59:13:59:23 | "Alert:2:1" | InlineTests.cs:57:18:57:25 | "Source" | InlineTests.cs:58:16:58:21 | "Sink" | This is a problem |
9+
| InlineTests.cs:64:13:64:23 | "Alert:2:1" | InlineTests.cs:62:18:62:25 | "Source" | InlineTests.cs:63:16:63:21 | "Sink" | This is a problem |
10+
| InlineTests.cs:68:13:68:23 | "Alert:1:0" | InlineTests.cs:67:18:67:25 | "Source" | InlineTests.cs:68:13:68:23 | "Alert:1:0" | This is a problem |
11+
| InlineTests.cs:72:13:72:23 | "Alert:1:0" | InlineTests.cs:71:18:71:25 | "Source" | InlineTests.cs:72:13:72:23 | "Alert:1:0" | This is a problem |
12+
| InlineTests.cs:76:13:76:23 | "Alert:0:1" | InlineTests.cs:76:13:76:23 | "Alert:0:1" | InlineTests.cs:75:16:75:21 | "Sink" | This is a problem |
13+
| InlineTests.cs:80:13:80:23 | "Alert:0:1" | InlineTests.cs:80:13:80:23 | "Alert:0:1" | InlineTests.cs:79:16:79:21 | "Sink" | This is a problem |
14+
edges
15+
testFailures
16+
| InlineTests.cs:6:26:6:35 | // ... | Missing result: Alert |
17+
| InlineTests.cs:12:34:12:43 | // ... | Missing result: Alert |
18+
| InlineTests.cs:29:28:29:38 | // ... | Missing result: Source |
19+
| InlineTests.cs:30:24:30:32 | // ... | Missing result: Sink |
20+
| InlineTests.cs:31:33:31:42 | // ... | Missing result: Alert |
21+
| InlineTests.cs:34:18:34:25 | "Source" | Unexpected result: Source |
22+
| InlineTests.cs:35:16:35:21 | "Sink" | Unexpected result: Sink |
23+
| InlineTests.cs:36:13:36:23 | InlineTests.cs:34:18:34:25 | Unexpected result: Alert |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: TestUtilities/inline-tests/queries/PathProblemQuery.ql
2+
postprocess: TestUtilities/InlineExpectationsTestQuery.ql
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#select
2+
| InlineTests.cs:6:17:6:23 | "Alert" | This is a problem |
3+
| InlineTests.cs:15:13:15:19 | "Alert" | This is a problem |
4+
| InlineTests.cs:18:13:18:19 | "Alert" | This is a problem |
5+
testFailures
6+
| InlineTests.cs:12:34:12:43 | // ... | Missing result: Alert |
7+
| InlineTests.cs:15:13:15:19 | This is a problem | Unexpected result: Alert |
8+
| InlineTests.cs:26:30:26:39 | // ... | Missing result: Alert |
9+
| InlineTests.cs:31:33:31:42 | // ... | Missing result: Alert |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: TestUtilities/inline-tests/queries/ProblemQuery.ql
2+
postprocess: TestUtilities/InlineExpectationsTestQuery.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
edges
2+
#select
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @kind path-problem
3+
* @id path-problem-query
4+
*/
5+
6+
import csharp
7+
8+
query predicate edges(StringLiteral sl1, StringLiteral sl2) { none() }
9+
10+
from StringLiteral alert, StringLiteral source, StringLiteral sink
11+
where
12+
exists(string regexp, int sourceOffset, int sinkOffset | regexp = "Alert:([0-9]+):([0-9]+)" |
13+
sourceOffset = alert.getValue().regexpCapture(regexp, 1).toInt() and
14+
sinkOffset = alert.getValue().regexpCapture(regexp, 2).toInt() and
15+
source.getLocation().getStartLine() = alert.getLocation().getStartLine() - sourceOffset and
16+
sink.getLocation().getStartLine() = alert.getLocation().getStartLine() - sinkOffset
17+
)
18+
select alert, source, sink, "This is a problem"

csharp/ql/test/TestUtilities/inline-tests/queries/ProblemQuery.expected

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @kind problem
3+
* @id problem-query
4+
*/
5+
6+
import csharp
7+
8+
from StringLiteral sl
9+
where sl.getValue() = "Alert"
10+
select sl, "This is a problem"

0 commit comments

Comments
 (0)