1
0
forked from VimPlug/jedi

tests for get_definitions

This commit is contained in:
David Halter
2012-05-13 16:14:37 +02:00
parent ed6b47c12c
commit 92df46bd58
3 changed files with 65 additions and 21 deletions

View File

@@ -162,7 +162,7 @@ def gen():
yield 1 yield 1
yield "" yield ""
gen_exe = gen() gen_exe = gen()
def nexti(iterator, default=list): def nexti(iterator, default=list):
if hasattr("next"): if hasattr("next"):

View File

@@ -241,3 +241,5 @@ gen_exe = gen()
next(gen_exe).upper next(gen_exe).upper
#? ['real'] #? ['real']
next(gen_exe).real next(gen_exe).real
#? float() str()
next(gen_exe)

View File

@@ -11,6 +11,59 @@ import functions
#functions.set_debug_function(functions.debug.print_to_stdout) #functions.set_debug_function(functions.debug.print_to_stdout)
def run_completion_test(correct, source, line_nr, line):
"""
Runs tests for completions.
Return if the test was a fail or not, with 1 for fail and 0 for success.
"""
# lines start with 1 and column is just the last (makes no
# difference for testing)
try:
completions = functions.complete(source, line_nr, 999,
completion_test_dir)
except Exception:
print('test @%s: %s' % (line_nr-1, line))
print(traceback.format_exc())
return 1
else:
# TODO remove sorted? completions should be sorted?
# TODO remove set! duplicates should not be normal
comp_str = str(sorted(set([str(c) for c in completions])))
if comp_str != correct:
print('Solution @%s not right, received %s, wanted %s'\
% (line_nr - 1, comp_str, correct))
return 1
return 0
def run_definition_test(correct, source, line_nr, line):
"""
Runs tests for definitions.
Return if the test was a fail or not, with 1 for fail and 0 for success.
"""
def defs(line_nr, indent):
return set(functions.get_definitions(source, line_nr, indent,
completion_test_dir))
try:
result = defs(line_nr, 999)
except Exception:
print('test @%s: %s' % (line_nr-1, line))
print(traceback.format_exc())
return 1
else:
should_be = set()
for index in re.finditer('(?: +|$)', correct):
# -1 for the comment, +3 because of the comment start `#? `
should_be |= defs(line_nr-1, index.start() + 3)
# because the objects have different ids, `repr` it, then compare it.
should_str = sorted(str(r) for r in should_be)
is_str = sorted(str(r) for r in result)
if is_str != should_str:
print('Solution @%s not right, received %s, wanted %s' \
% (line_nr - 1, is_str, should_str))
return 1
return 0
def completion_test(source): def completion_test(source):
""" """
@@ -30,24 +83,13 @@ def completion_test(source):
for line_nr, line in enumerate(StringIO.StringIO(source)): for line_nr, line in enumerate(StringIO.StringIO(source)):
line_nr += 1 line_nr += 1
if correct: if correct:
# lines start with 1 and column is just the last (makes no # if a list is wanted, use the completion test, otherwise the
# difference for testing) # get_definition test
try: if correct.startswith('['):
completions = functions.complete(source, line_nr, 999, fails += run_completion_test(correct, source, line_nr, line)
completion_test_dir)
except Exception:
print 'test @%s: %s' % (line_nr-1, line)
print traceback.format_exc()
fails += 1
else: else:
# TODO remove sorted? completions should be sorted? fails += run_definition_test(correct, source, line_nr, line)
# TODO remove set! duplicates should not be normal
comp_str = str(sorted(set([str(c) for c in completions])))
if comp_str != correct:
print 'Solution @%s not right, received %s, wanted %s'\
% (line_nr - 1, comp_str, correct)
#print [(c.name, c.name.parent) for c in completions]
fails += 1
correct = None correct = None
tests += 1 tests += 1
else: else:
@@ -75,9 +117,9 @@ for f_name in os.listdir(completion_test_dir):
f = open(path) f = open(path)
num_tests, fails = completion_test(f.read()) num_tests, fails = completion_test(f.read())
s = 'run %s tests with %s fails (%s)' % (num_tests, fails, f_name) s = 'run %s tests with %s fails (%s)' % (num_tests, fails, f_name)
print s print(s)
summary.append(s) summary.append(s)
print '\nSummary:' print('\nSummary:')
for s in summary: for s in summary:
print s print(s)