示例#1
0
 def test_update_fetch_hover_failure(self, script_path, load_yaml):
     """Test update_fetch_hover() against the failure test cases."""
     analyser = TestUtils.prepare_analyser(script_path)
     for failure in load_yaml.get('negative_cases', []):
         location = failure['location']
         hover = analyser.update_fetch_hover(script_path,
                                             (location[0], location[1]))
         assert hover is None
示例#2
0
 def test_update_fetch_hover_success(self, script_path, load_yaml):
     """Test update_fetch_hover() against the successful test cases."""
     analyser = TestUtils.prepare_analyser(script_path)
     for lens in load_yaml.get('code_lenses', []):
         location, mro = lens['location'], lens['mro']
         hover = analyser.update_fetch_hover(script_path,
                                             (location[0], location[1]))
         assert hover and hover['contents'] == mro
示例#3
0
	def test_get_code_lens(self, script_path, load_yaml):
		# prepare the calculator
		calculator = TestUtils.prepare_calculator(script_path)
		# update and then check the code lenses are correct
		calculator.update_all()
		lenses = calculator.get_code_lens(script_path)
		TestUtils.compare_code_lenses(
			lenses,
			load_yaml.get('code_lenses', []),
		)
示例#4
0
 def test_get_class_def_ast_from_lines(self, script_path, load_yaml):
     calculator, script = self.prepare_calculator_and_script(script_path)
     # check against every custom defined class
     for expected in load_yaml.get('code_lenses', []):
         parsed_class = self.construct_parsed_custom_class(
             calculator, script, expected)
         # check that there is one and only one class definition from the
         # code lines
         codes = '\n'.join(parsed_class._lines)
         mod = ast.parse(codes)
         assert len([n for n in mod.body
                     if isinstance(n, ast.ClassDef)]) == 1
示例#5
0
	def test_get_code_lens_and_range(self, script_path, load_yaml):
		# prepare the calculator
		calculator = TestUtils.prepare_calculator(script_path)
		# update the script
		calculator.update_all()
		# fetch the code lenses and ranges and separate them
		fetched = calculator.get_code_lens_and_range(script_path)
		lenses = [t[0] for t in fetched]
		ranges = [t[1] for t in fetched]
		# check the code lenses are correct
		TestUtils.compare_code_lenses(
			lenses,
			load_yaml.get('code_lenses', []),
		)
		# check the ranges are correct
		locations = [
			(
				lens['location'][0], lens['location'][1]
			) for lens in load_yaml.get('code_lenses', [])
		]
		TestUtils.assert_locations_in_ranges(ranges, locations)
示例#6
0
 def test_mro_parsed_list(self, script_path, load_yaml):
     calculator, script = self.prepare_calculator_and_script(script_path)
     # check against every custom defined class
     for expected in load_yaml.get('code_lenses', []):
         parsed_class = self.construct_parsed_custom_class(
             calculator, script, expected)
         if expected['mro'] == [ParsedClass.CONFLICT_MRO_MSG]:
             with pytest.raises(TypeError):
                 parsed_mro_list = parsed_class.mro_parsed_list
         else:
             # check the parsed mro list against the expected result
             parsed_mro_list = parsed_class.mro_parsed_list
             assert [p.jedi_name.name
                     for p in parsed_mro_list] == expected['mro']
示例#7
0
 def test_get_code_lines(self, script_path, load_yaml):
     calculator, script = self.prepare_calculator_and_script(script_path)
     # get the original lines of the script
     script_lines = calculator.content_cache[script_path]
     # check against every custom defined class
     for expected in load_yaml.get('code_lenses', []):
         parsed_class = self.construct_parsed_custom_class(
             calculator, script, expected)
         parsed_lines = parsed_class._get_code_lines()
         # the beginning and the end may have extract codes, so using
         # `in` instead of `==`
         assert '\n'.join(parsed_lines) in '\n'.join(
             script_lines[expected['location'][0]:expected['location'][0] +
                          len(parsed_lines)])
示例#8
0
	def test_is_original_class(self, script_path, load_yaml):
		# prepare the calculator
		calculator = TestUtils.prepare_calculator(script_path)
		# update the script
		calculator.update_all()
		# fetch the correspondent Jedi Script and its Jedi Context
		script = calculator.jedi_scripts_by_path[script_path]
		context = script.get_context()
		# every ParsedClass should be based on an original Jedi Name
		for name in calculator.parsed_names_by_path[script_path]:
			assert calculator._is_original_class(name.jedi_name, context)
		# the number of original name should equal to the number of code lenses
		n_original = 0
		for name in script.get_names():
			if calculator._is_original_class(name, context):
				n_original += 1
		assert n_original == len(load_yaml.get('code_lenses', []))
示例#9
0
 def test_update_fetch_code_lens(self, script_path, load_yaml):
     """Test update_fetch_code_lens() against the successful test cases."""
     analyser = TestUtils.prepare_analyser(script_path)
     expected_lenses = load_yaml.get('code_lenses', [])
     # test code lens result with the original file content
     lenses = analyser.update_fetch_code_lens(script_path)
     TestUtils.compare_code_lenses(lenses, expected_lenses)
     # return if no need to test adding new content
     if 'dummy_content' not in load_yaml:
         return
     # add new test content into the file
     lines = analyser.calculator.content_cache[script_path]
     n_last_line = len(lines) - 1
     n_last_char = len(lines[-1])
     analyser.update_script_content(script_path, (n_last_line, n_last_char),
                                    (n_last_line, n_last_char),
                                    '\n'.join(load_yaml['dummy_content']))
     # test code lens result with the new test content
     lenses = analyser.update_fetch_code_lens(script_path)
     expected_lenses.append(load_yaml['dummy_code_lens'])
     TestUtils.compare_code_lenses(lenses, expected_lenses)
示例#10
0
	def assert_script_updated(
		calculator: MROCalculator,
		script_path: str,
		load_yaml: dict,
	):
		# the script should now be parsed into a Jedi Script and the content
		# should match
		assert script_path in calculator.jedi_scripts_by_path
		script = calculator.jedi_scripts_by_path[script_path]
		assert script._code.strip() == open(script_path).read().strip()
		# the class definitions in the script should also be parsed and cached
		# in the two dictionaries for lookup by script path or class full name
		assert script_path in calculator.parsed_names_by_path
		names = calculator.parsed_names_by_path[script_path]
		TestUtils.compare_code_lenses(
			[name.code_lens for name in names],
			load_yaml.get('code_lenses', []),
		)
		for name in names:
			assert name.full_name in calculator.parsed_name_by_full_name
			assert name == calculator.parsed_name_by_full_name[name.full_name]