def test_star_import_cache_duration(self): new = 0.01 old, api.settings.star_import_cache_validity = \ api.settings.star_import_cache_validity, new imports = api.imports imports.star_import_cache = {} # first empty... # path needs to be not-None (otherwise caching effects are not visible) api.Script('', 1,0, '').complete() time.sleep(2*new) api.Script('', 1,0, '').complete() # reset values api.settings.star_import_cache_validity = old length = len(imports.star_import_cache) imports.star_import_cache = {} self.assertEqual(length, 1)
def test_add_dynamic_mods(self): api.settings.additional_dynamic_modules = ['dynamic.py'] # Fictional module that defines a function. src1 = "def ret(a): return a" # Other fictional modules in another place in the fs. src2 = 'from .. import setup; setup.ret(1)' # .parser to load the module api.modules.Module(os.path.abspath('dynamic.py'), src2).parser script = api.Script(src1, 1, len(src1), '../setup.py') result = script.get_definition() assert len(result) == 1 assert result[0].description == 'class int'
def test_scipy_speed(self): s = 'import scipy.weave; scipy.weave.inline(' script = api.Script(s, 1, len(s), '') script.get_in_function_call()
def test_named_import(self): """ named import - jedi-vim issue #8 """ s = "import time as dt" assert len(api.Script(s, 1, 15, '/').get_definition()) == 1 assert len(api.Script(s, 1, 10, '/').get_definition()) == 1
def get_script(self, src, pos, path=None): if pos is None: lines = src.splitlines() pos = len(lines), len(lines[-1]) return api.Script(src, pos[0], pos[1], path)
def rename(new_name, *args, **kwargs): script = api.Script(*args, **kwargs) old_names = script.related_names() return []
def run_test(source, f_name, lines_to_execute): """ This is the completion test for some cases. The tests are not unit test like, they are rather integration tests. It uses comments to specify a test in the next line. The comment also says, which results are expected. The comment always begins with `#?`. The last row symbolizes the cursor. For example: >>> #? ['ab'] >>> ab = 3; a >>> #? int() >>> ab = 3; ab """ def get_defs(correct, correct_start, path): def defs(line_nr, indent): script = api.Script(source, line_nr, indent, path) return set(script.get_definition()) should_be = set() number = 0 for index in re.finditer('(?: +|$)', correct): if correct == ' ': continue # -1 for the comment, +3 because of the comment start `#? ` start = index.start() if print_debug: api.set_debug_function(None) number += 1 try: should_be |= defs(line_nr - 1, start + correct_start) except Exception: raise Exception('could not resolve %s indent %s' % (line_nr - 1, start)) if print_debug: api.set_debug_function(debug.print_to_stdout) # because the objects have different ids, `repr` it, then compare it. should_str = set(r.desc_with_module for r in should_be) if len(should_str) < number: raise Exception('Solution @%s not right, too few test results: %s' % (line_nr - 1, should_str)) return should_str fails = 0 tests = 0 correct = None test_type = None start = None for line_nr, line in enumerate(StringIO(source)): line = unicode(line) line_nr += 1 if correct: r = re.match('^(\d+)\s*(.*)$', correct) if r: index = int(r.group(1)) correct = r.group(2) start += r.regs[2][0] # second group, start index else: index = len(line) - 1 # -1 for the \n # if a list is wanted, use the completion test, otherwise the # get_definition test path = completion_test_dir + os.path.sep + f_name try: script = api.Script(source, line_nr, index, path) if test_type == '!': fails += run_goto_test(script, correct, line_nr) elif test_type == '<': fails += run_related_name_test(script, correct, line_nr) elif correct.startswith('['): fails += run_completion_test(script, correct, line_nr) else: should_str = get_defs(correct, start, path) fails += run_definition_test(script, should_str, line_nr) except Exception: print(traceback.format_exc()) print('test @%s: %s' % (line_nr - 1, line)) fails += 1 correct = None tests += 1 else: try: r = re.search(r'(?:^|(?<=\s))#([?!<])\s*([^\n]+)', line) # test_type is ? for completion and ! for goto test_type = r.group(1) correct = r.group(2) start = r.start() except AttributeError: correct = None else: # reset the test, if only one specific test is wanted if lines_to_execute and line_nr not in lines_to_execute: correct = None return tests, fails
def defs(line_nr, indent): script = api.Script(source, line_nr, indent, path) return set(script.get_definition())