Skip to content

Commit ae161f1

Browse files
committed
Add meta-tests for inline expectation with related locations
1 parent 694f01a commit ae161f1

8 files changed

+75
-1
lines changed

csharp/ql/test/utils/inline-tests/InlineTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ void Problems()
1616

1717
// correct expectation comment
1818
x = "Alert"; // $ Alert[problem-query]
19+
20+
// correct expectation comments with a related location
21+
var related = "Related"; // $ RelatedLocation[problem-query-with-related-loc]
22+
x = "Alert:1"; // $ Alert[problem-query-with-related-loc]
23+
24+
// expectation comments missing the related location
25+
related = "Related";
26+
x = "Alert:1"; // $ Alert[problem-query-with-related-loc]
1927
}
2028

2129
void PathProblems()
@@ -78,5 +86,17 @@ void PathProblems()
7886
// incorrect expectation comments, using an identifier tag; the alert location coincides with the source location
7987
sink = "Sink"; // $ Sink[path-problem-query]=sink2
8088
x = "Alert:0:1"; // $ Alert[path-problem-query]=sink1
89+
90+
// correct expectation comments with a related location
91+
source = "Source"; // $ Source[path-problem-query-with-related-loc]
92+
sink = "Sink"; // $ Sink[path-problem-query-with-related-loc]
93+
var related = "Related"; // $ RelatedLocation[path-problem-query-with-related-loc]
94+
x = "Alert:3:2:1"; // $ Alert[path-problem-query-with-related-loc]
95+
96+
// expectation comments missing the related location
97+
source = "Source"; // $ Source[path-problem-query-with-related-loc]
98+
sink = "Sink"; // $ Sink[path-problem-query-with-related-loc]
99+
related = "Related";
100+
x = "Alert:3:2:1"; // $ Alert[path-problem-query-with-related-loc]
81101
}
82-
}
102+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#select
2+
| InlineTests.cs:94:13:94:25 | "Alert:3:2:1" | InlineTests.cs:91:18:91:25 | "Source" | InlineTests.cs:92:16:92:21 | "Sink" | This is a problem with $@ | InlineTests.cs:93:23:93:31 | "Related" | a related location |
3+
| InlineTests.cs:100:13:100:25 | "Alert:3:2:1" | InlineTests.cs:97:18:97:25 | "Source" | InlineTests.cs:98:16:98:21 | "Sink" | This is a problem with $@ | InlineTests.cs:99:19:99:27 | "Related" | a related location |
4+
edges
5+
testFailures
6+
| InlineTests.cs:6:26:6:35 | // ... | Missing result: Alert |
7+
| InlineTests.cs:12:34:12:43 | // ... | Missing result: Alert |
8+
| InlineTests.cs:32:32:32:42 | // ... | Missing result: Source |
9+
| InlineTests.cs:33:28:33:36 | // ... | Missing result: Sink |
10+
| InlineTests.cs:34:30:34:39 | // ... | Missing result: Alert |
11+
| InlineTests.cs:37:28:37:38 | // ... | Missing result: Source |
12+
| InlineTests.cs:38:24:38:32 | // ... | Missing result: Sink |
13+
| InlineTests.cs:39:33:39:42 | // ... | Missing result: Alert |
14+
| InlineTests.cs:93:34:93:90 | // ... | Missing result: RelatedLocation[path-problem-query-with-related-loc] |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: utils/inline-tests/queries/PathProblemQueryRelatedLocs.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: utils/inline-tests/queries/ProblemQueryRelatedLocs.ql
2+
postprocess: utils/test/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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @kind path-problem
3+
* @id path-problem-query-with-related-loc
4+
*/
5+
6+
import csharp
7+
8+
query predicate edges(StringLiteral sl1, StringLiteral sl2) { none() }
9+
10+
from StringLiteral alert, StringLiteral source, StringLiteral sink, StringLiteral related
11+
where
12+
exists(string regexp, int sourceOffset, int sinkOffset, int relatedOffset |
13+
regexp = "Alert:([0-9]+):([0-9]+):([0-9]+)"
14+
|
15+
sourceOffset = alert.getValue().regexpCapture(regexp, 1).toInt() and
16+
sinkOffset = alert.getValue().regexpCapture(regexp, 2).toInt() and
17+
relatedOffset = alert.getValue().regexpCapture(regexp, 3).toInt() and
18+
source.getLocation().getStartLine() = alert.getLocation().getStartLine() - sourceOffset and
19+
sink.getLocation().getStartLine() = alert.getLocation().getStartLine() - sinkOffset and
20+
related.getLocation().getStartLine() = alert.getLocation().getStartLine() - relatedOffset
21+
)
22+
select alert, source, sink, "This is a problem with $@", related, "a related location"

csharp/ql/test/utils/inline-tests/queries/ProblemQueryWithRelatedLocs.expected

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @kind problem
3+
* @id problem-query-with-related-locs
4+
*/
5+
6+
import csharp
7+
8+
from StringLiteral sl, StringLiteral related, int offset
9+
where
10+
sl.getValue().regexpCapture("Alert:([0-9]+)", 1).toInt() = offset and
11+
related.getLocation().getStartLine() = sl.getLocation().getStartLine() - offset
12+
select sl, "This is a problem with $@", related, "a related location"

0 commit comments

Comments
 (0)