Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 12d7f56

Browse files
authored
More gen.py updates (#1166)
1 parent 74aa212 commit 12d7f56

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/gen.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import defaultdict
66
import string
77
import sys
8-
import os.path
8+
import os
99

1010

1111
def main():
@@ -14,9 +14,7 @@ def main():
1414
default_input = os.path.join(
1515
script_dir, "UnitTests", "TestData", "gen", "completion"
1616
)
17-
default_output = os.path.join(
18-
script_dir, "LanguageServer", "Test", "GenTests.cs"
19-
)
17+
default_output = os.path.join(script_dir, "LanguageServer", "Test", "GenTests.cs")
2018

2119
parser = argparse.ArgumentParser(
2220
description="Generate completion and hover tests",
@@ -45,6 +43,13 @@ def main():
4543
default=default_input,
4644
help="location of completions directory",
4745
)
46+
parser.add_argument(
47+
"--table",
48+
nargs="?",
49+
type=argparse.FileType("w"),
50+
default=os.devnull,
51+
help="file to write test names to",
52+
)
4853
args = parser.parse_args()
4954

5055
if args.only:
@@ -77,12 +82,12 @@ def main():
7782
for name in to_generate:
7883
filename = os.path.join(args.input, name + ".py")
7984
ignored_lines = line_skip[name]
80-
create_tests(name, filename, ignored_lines)
85+
create_tests(name, filename, ignored_lines, args.table)
8186

8287
print(POSTAMBLE)
8388

8489

85-
def create_tests(name, filename, ignored_lines):
90+
def create_tests(name, filename, ignored_lines, table_f):
8691
camel_name = snake_to_camel(name)
8792

8893
with open(filename) as fp:
@@ -115,13 +120,19 @@ def create_tests(name, filename, ignored_lines):
115120
pass
116121

117122
filt = next_line[:col].lstrip()
118-
filt = select_filter(filt, ". {[(")
123+
filt = rightmost_token(filt, ". {[(\t@")
119124

120125
args = line.strip()
121126
func_name = "Line_{0:0{pad}}".format(i + 1, pad=width)
122127
func_name = camel_name + "_" + func_name
123128

124-
tmpl = COMPLETION_TEST if args.startswith("[") else HOVER_TEST
129+
is_completion = args.startswith("[")
130+
131+
func_name += "_Completion" if is_completion else "_Hover"
132+
tmpl = COMPLETION_TEST if is_completion else HOVER_TEST
133+
134+
print(func_name, file=table_f)
135+
125136
tests.append(
126137
tmpl.format(
127138
name=func_name,
@@ -175,6 +186,7 @@ def create_tests(name, filename, ignored_lines):
175186
"precedence",
176187
"recursion",
177188
"stdlib",
189+
"stubs",
178190
"sys_path",
179191
"types",
180192
]
@@ -260,14 +272,15 @@ def create_tests(name, filename, ignored_lines):
260272
}
261273
262274
protected async Task DoCompletionTest(string module, int lineNum, int col, string args, string filter) {
263-
var tests = string.IsNullOrWhiteSpace(args) ? new List<string>() : ParseStringList(args);
275+
filter = filter.ToLowerInvariant();
276+
var tests = string.IsNullOrWhiteSpace(args) ? new List<string>() : ParseStringList(args).Select(s => s.ToLowerInvariant()).ToList();
264277
265278
var analysis = await GetGenAnalysisAsync(module);
266279
267280
var res = _cs.GetCompletions(analysis, new SourceLocation(lineNum + 1, col + 1));
268-
res.Should().NotBeNull();
269-
270-
var items = res.Completions?.Select(item => item.insertText).Where(t => t.Contains(filter)).ToList() ?? new List<string>();
281+
var items = res?.Completions?.Select(item => item.insertText.ToLowerInvariant())
282+
.Where(t => t.ToLowerInvariant().Contains(filter))
283+
.ToList() ?? new List<string>();
271284
272285
if (tests.Count == 0) {
273286
items.Should().BeEmpty();
@@ -284,11 +297,11 @@ def create_tests(name, filename, ignored_lines):
284297
var analysis = await GetGenAnalysisAsync(module);
285298
286299
var res = _hs.GetHover(analysis, new SourceLocation(lineNum + 1, col + 1));
287-
res.Should().NotBeNull();
288300
289301
if (tests.Count == 0) {
290-
res.contents.value.Should().BeEmpty();
302+
res?.contents.value.Should().BeEmpty();
291303
} else {
304+
res.Should().NotBeNull();
292305
res.contents.value.Should().ContainAll(tests);
293306
}
294307
}
@@ -335,28 +348,24 @@ def create_tests(name, filename, ignored_lines):
335348
}"""
336349

337350
COMPLETION_TEST = """
338-
[TestMethod, Priority(0)] public async Task {name}_Completion() => await DoCompletionTest({module}, {line}, {col}, {args}, {filter});"""
351+
[TestMethod, Priority(0)] public async Task {name}() => await DoCompletionTest({module}, {line}, {col}, {args}, {filter});"""
339352

340353

341354
HOVER_TEST = """
342-
[TestMethod, Priority(0)] public async Task {name}_Hover() => await DoHoverTest({module}, {line}, {col}, {args});"""
355+
[TestMethod, Priority(0)] public async Task {name}() => await DoHoverTest({module}, {line}, {col}, {args});"""
343356

344357

345358
def snake_to_camel(s):
346359
return string.capwords(s, "_").replace("_", "")
347360

348361

349-
def select_filter(s, cs):
350-
found = False
362+
def rightmost_token(s, cs):
351363
for c in cs:
352364
i = s.rfind(c)
353365
if i != -1:
354-
found = True
355366
s = s[i + 1 :]
356367

357-
if found:
358-
return s
359-
return ""
368+
return s
360369

361370

362371
def csharp_str(s):
@@ -369,4 +378,3 @@ def csharp_str(s):
369378

370379
if __name__ == "__main__":
371380
main()
372-

0 commit comments

Comments
 (0)