Skip to content

Commit c9976cf

Browse files
authored
Merge pull request github#14307 from yoff/python/inline-regex-location-tests
Python: switch regex location tests to inline expectations
2 parents 831e50c + 417907b commit c9976cf

File tree

3 files changed

+49
-47
lines changed

3 files changed

+49
-47
lines changed
Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,2 @@
1-
| locations.py | 14 | 2 |
2-
| locations.py | 19 | 3 |
3-
| locations.py | 24 | 3 |
4-
| locations.py | 29 | 4 |
5-
| locations.py | 34 | 4 |
6-
| locations.py | 39 | 5 |
7-
| locations.py | 44 | 5 |
8-
| locations.py | 49 | 6 |
9-
| locations.py | 54 | 2 |
10-
| locations.py | 54 | 23 |
11-
| locations.py | 59 | 2 |
12-
| locations.py | 59 | 23 |
13-
| locations.py | 65 | 2 |
14-
| locations.py | 65 | 23 |
15-
| locations.py | 72 | 6 |
16-
| locations.py | 72 | 27 |
17-
| locations.py | 80 | 3 |
18-
| locations.py | 85 | 5 |
19-
| locations.py | 90 | 2 |
20-
| locations.py | 90 | 23 |
1+
testFailures
2+
failures
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1+
import python
12
import semmle.python.regexp.RegexTreeView::RegexTreeView
3+
import TestUtilities.InlineExpectationsTest
4+
private import semmle.python.dataflow.new.internal.PrintNode
25

3-
from RegExpTerm t, string file, int line, int column
4-
where
5-
t.toString() = "[this]" and
6-
t.hasLocationInfo(file, line, column, _, _)
7-
select file, line, column
6+
module RegexLocationTest implements TestSig {
7+
string getARelevantTag() { result = "location" }
8+
9+
predicate hasActualResult(Location location, string element, string tag, string value) {
10+
exists(location.getFile().getRelativePath()) and
11+
exists(Call compile, RegExpTerm t, int line, int column |
12+
// All the tested regexes are inside a call to `compile`
13+
compile.getAnArg() = t.getRegex() and
14+
t.toString() = "[this]" and
15+
t.hasLocationInfo(_, line, column, _, _)
16+
|
17+
// put the annotation on the start line of the call to `compile`
18+
location = compile.getFunc().getLocation() and
19+
element = t.toString() and
20+
// show the (relative) line and column for the fragment
21+
value = (line - location.getStartLine()).toString() + ":" + column.toString() and
22+
tag = "location"
23+
)
24+
}
25+
}
26+
27+
import MakeTest<RegexLocationTest>

python/ql/test/library-tests/regexparser/locations.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,86 @@
77
#
88
# To make the location information easier to understand, we generally put each
99
# regexp on its own line, even though this is not idiomatic Python.
10-
# Comments indicate cases we currently do not handle correctly.
10+
# Comments indicate the found locations relative to the call to `compile`.
1111

1212
# plain string
13-
re.compile(
13+
re.compile( # $location=1:2
1414
'[this] is a test'
1515
)
1616

1717
# raw string
18-
re.compile(
18+
re.compile( # $ location=1:3
1919
r'[this] is a test'
2020
)
2121

2222
# byte string
23-
re.compile(
23+
re.compile( # $ location=1:3
2424
b'[this] is a test'
2525
)
2626

2727
# byte raw string
28-
re.compile(
28+
re.compile( # $ location=1:4
2929
br'[this] is a test'
3030
)
3131

3232
# multiline string
33-
re.compile(
33+
re.compile( # $ location=1:4
3434
'''[this] is a test'''
3535
)
3636

3737
# multiline raw string
38-
re.compile(
38+
re.compile( # $ location=1:5
3939
r'''[this] is a test'''
4040
)
4141

4242
# multiline byte string
43-
re.compile(
43+
re.compile( # $ location=1:5
4444
b'''[this] is a test'''
4545
)
4646

4747
# multiline byte raw string
48-
re.compile(
48+
re.compile( # $ location=1:6
4949
br'''[this] is a test'''
5050
)
5151

52-
# plain string with multiple parts (second [this] gets wrong column: 23 instead of 26)
53-
re.compile(
52+
# plain string with multiple parts
53+
re.compile( # $ location=1:2 SPURIOUS:location=1:23 MISSING:location=1:26
5454
'[this] is a test' ' and [this] is another test'
5555
)
5656

57-
# plain string with multiple parts across lines (second [this] gets wrong location: 59:23 instead of 60:7)
58-
re.compile(
57+
# plain string with multiple parts across lines
58+
re.compile( # $ location=1:2 SPURIOUS:location=1:23 MISSING:location=2:7
5959
'[this] is a test'
6060
' and [this] is another test'
6161
)
6262

63-
# plain string with multiple parts across lines and comments (second [this] gets wrong location: 65:23 instead of 67:7)
64-
re.compile(
63+
# plain string with multiple parts across lines and comments
64+
re.compile( # $ location=1:2 SPURIOUS:location=1:23 MISSING:location=3:7
6565
'[this] is a test'
6666
# comment
6767
' and [this] is another test'
6868
)
6969

70-
# actual multiline string (both [this]s get wrong location: 72:6 and 72:27 instead of 73:1 and 74:5)
71-
re.compile(
70+
# actual multiline string
71+
re.compile( # $ SPURIOUS:location=1:6 location=1:27 MISSING:location=2:1 location=3:5
7272
r'''
7373
[this] is a test
7474
and [this] is another test
7575
'''
7676
)
7777

78-
# plain string with escape sequences ([this] gets wrong location: 80:3 instead of 80:4)
79-
re.compile(
78+
# plain string with escape sequences
79+
re.compile( # $ SPURIOUS:location=1:3 MISSING:location=1:4
8080
'\t[this] is a test'
8181
)
8282

8383
# raw string with escape sequences
84-
re.compile(
84+
re.compile( # $ location=1:5
8585
r'\A[this] is a test'
8686
)
8787

88-
# plain string with escaped newline (second [this] gets wrong location: 90:23 instead of 91:6)
89-
re.compile(
88+
# plain string with escaped newline
89+
re.compile( # $ location=1:2 SPURIOUS:location=1:23 MISSING:location=2:6
9090
'[this] is a test\
9191
and [this] is another test'
9292
)

0 commit comments

Comments
 (0)