def test_contained_in_3(self): start = SourcePosition('a.py', line=1, column=5) end = SourcePosition('a.py', line=5, column=1) smaller = SourceRange(start, end) start = SourcePosition('a.py', line=2, column=5) end = SourcePosition('a.py', line=5, column=1) bigger = SourceRange(start, end) self.assertFalse(contained_in(smaller, bigger))
def test_string_conversion(self): uut = SourcePosition('filename', 1) self.assertRegex( repr(uut), "<SourcePosition object\\(file='.*filename', line=1, " 'column=None\\) at 0x[0-9a-fA-F]+>') uut = SourcePosition('None', None) self.assertRegex( repr(uut), "<SourcePosition object\\(file='.*None', line=None, column=None\\) " 'at 0x[0-9a-fA-F]+>')
def test_initialization(self): with self.assertRaises(TypeError): SourcePosition(None, 0) with self.assertRaises(ValueError): SourcePosition('file', None, 1) # However these should work: SourcePosition('file', None, None) SourcePosition('file', 4, None) SourcePosition('file', 4, 5)
def test_string_conversion(self): uut = SourcePosition("filename", 1) self.assertRegex( repr(uut), "<SourcePosition object\\(file='filename', line=1, column=None\\) " "at 0x[0-9a-fA-F]+>") uut = SourcePosition("None", None) self.assertRegex( repr(uut), "<SourcePosition object\\(file='None', line=None, column=None\\) " "at 0x[0-9a-fA-F]+>")
def from_values(cls, file, start_line=None, start_column=None, end_line=None, end_column=None): start = SourcePosition(file, start_line, start_column) if not end_line: end = None else: end = SourcePosition(file, end_line, end_column) return cls(start, end)
def from_values(cls, file, start_line=None, start_column=None, end_line=None, end_column=None): start = SourcePosition(file, start_line, start_column) if end_line or (end_column and end_column > start_column): end = SourcePosition(file, end_line if end_line else start_line, end_column) else: end = None return cls(start, end)
def from_absolute_position(cls, file: str, position_start: AbsolutePosition, position_end: (AbsolutePosition, None)=None): """ Creates a SourceRange from a start and end positions. :param file: Name of the file. :param position_start: Start of range given by AbsolutePosition. :param position_end: End of range given by AbsolutePosition or None. """ start = SourcePosition(file, position_start.line, position_start.column) end = None if position_end: end = SourcePosition(file, position_end.line, position_end.column) return cls(start, end)
def test_line_number(self): self.uut = Setting('key', '22\n', origin=SourcePosition('filename', 3)) self.assertEqual(self.uut.line_number, 3) with self.assertRaisesRegex( TypeError, "Instantiated with str 'origin' " 'which does not have line numbers. ' 'Use SourcePosition for line numbers.'): self.uut = Setting('key', '22\n', origin='filename') self.uut.line_number
def test_glob(self): self.uut = Setting('key', '.', origin=os.path.join('test (1)', 'somefile')) self.assertEqual(glob(self.uut), glob_escape(os.path.abspath('test (1)'))) self.uut = Setting('key', '.', origin=SourcePosition( os.path.join('test (1)', 'somefile'))) self.assertEqual(glob(self.uut), glob_escape(os.path.abspath('test (1)')))
def test_renamed_file(self): src_range = SourceRange(SourcePosition('test_file')) self.assertEqual(src_range.renamed_file({}), abspath('test_file')) self.assertEqual( src_range.renamed_file({abspath('test_file'): Diff([])}), abspath('test_file')) self.assertEqual( src_range.renamed_file( {abspath('test_file'): Diff([], rename='another_file')}), 'another_file')
def test_from_absolute_position(self): text = ('a\n', 'b\n') start = AbsolutePosition(text, 0) end = AbsolutePosition(text, 2) uut = SourceRange.from_absolute_position('F', start, end) compare = SourceRange.from_values('F', 1, 1, 2, 1) self.assertEqual(uut, compare) uut = SourceRange.from_absolute_position('F', start, None) compare = SourceRange(SourcePosition('F', 1, 1), None) self.assertEqual(uut, compare)
def FullTokenListWithPos(self): if not self.tokenListWithPos: linenum = 1 for line in self.file_contents: tokens = tokenize.tokenize( BytesIO(line.encode('utf-8')).readline) for toknum, tokval, startpos, _, _ in tokens: if Tokenizer.ShouldAdd(toknum): self.tokenListWithPos[SourcePosition( self.file, linenum, startpos[1])] = (toknum, tokval) linenum += 1 return self.tokenListWithPos
def test_path_list(self): abspath = os.path.abspath('.') # Need to escape backslashes since we use list conversion self.uut = Setting('key', '., ' + abspath.replace('\\', '\\\\'), origin=os.path.join('test', 'somefile')) self.assertEqual(path_list(self.uut), [os.path.abspath(os.path.join('test', '.')), abspath]) self.uut = Setting('key', '., ' + abspath.replace('\\', '\\\\'), origin=SourcePosition( os.path.join('test', 'somefile'))) self.assertEqual(path_list(self.uut), [os.path.abspath(os.path.join('test', '.')), abspath])
def test_path(self): self.uut = Setting( 'key', ' 22\n', '.' + os.path.sep, strip_whitespaces=True) self.assertEqual(path(self.uut), os.path.abspath(os.path.join('.', '22'))) abspath = PathArg(os.path.abspath('.')) self.uut = Setting('key', abspath) self.assertEqual(path(self.uut), abspath) self.uut = Setting('key', ' 22', '') self.assertRaises(ValueError, path, self.uut) self.assertEqual(path(self.uut, origin='test' + os.path.sep), os.path.abspath(os.path.join('test', '22'))) self.uut = Setting('key', '22\n', SourcePosition('.' + os.path.sep), strip_whitespaces=True) self.assertEqual(path(self.uut), os.path.abspath(os.path.join('.', '22')))
def test_json(self): with prepare_file([''], None) as (_, filename): uut = SourcePosition(filename, 1) self.assertEqual(uut.__json__(use_relpath=True) ['file'], relpath(filename))
def setUp(self): self.result_fileA_noline = SourcePosition("A") self.result_fileA_line2 = SourcePosition("A", 2) self.result_fileB_noline = SourcePosition("B") self.result_fileB_line2 = SourcePosition("B", 2) self.result_fileB_line4 = SourcePosition("B", 4)
def __parse_lines(self, lines, origin): current_section_name = 'default' current_section = self.get_section(current_section_name) current_keys = [] no_section = True line_number = 0 for line in lines: (section_name, keys, value, append, comment) = self.line_parser._parse(line) line_number += 1 if comment != '': self.__add_comment(current_section, comment, origin) if section_name != '': no_section = False current_section_name = section_name current_section = self.get_section(current_section_name, True) current_keys = [] continue if comment == '' and keys == [] and value == '': self.__add_comment(current_section, '', origin) continue if keys != []: current_keys = keys for section_override, key in current_keys: if no_section: logging.warning('A setting does not have a section.' 'This is a deprecated feature please ' 'put this setting in a section defined' ' with `[<your-section-name]` in a ' 'configuration file.') if key == '': continue if key in current_section.contents and keys != []: logging.warning( f'{key} setting has already been defined in' f' section {current_section.name}. The ' 'previous setting will be overridden.') if section_override == '': current_section.add_or_create_setting( Setting( key, value, SourcePosition(str(origin), line=line_number), to_append=append, # Start ignoring PEP8Bear, PycodestyleBear* # they fail to resolve this remove_empty_iter_elements=self. __remove_empty_iter_elements), # Stop ignoring allow_appending=(keys == [])) else: self.get_section( section_override, True ).add_or_create_setting( Setting( key, value, SourcePosition(str(origin), line=line_number), to_append=append, # Start ignoring PEP8Bear, PycodestyleBear* # they fail to resolve this remove_empty_iter_elements=self. __remove_empty_iter_elements), # Stop ignoring allow_appending=(keys == []))
def test_expand(self): empty_position = SourcePosition('filename') file = ['abc\n', 'def\n', 'ghi\n'] empty_range = SourceRange(empty_position, empty_position) full_range = SourceRange.from_values('filename', 1, 1, 3, 4) self.assertEqual(empty_range.expand(file), full_range)
def setUp(self): self.result_fileA_noline = SourcePosition('A') self.result_fileA_line2 = SourcePosition('A', 2) self.result_fileB_noline = SourcePosition('B') self.result_fileB_line2 = SourcePosition('B', 2) self.result_fileB_line4 = SourcePosition('B', 4)
def test_expand(self): empty_position = SourcePosition("filename") file = ["abc\n", "def\n", "ghi\n"] empty_range = SourceRange(empty_position, empty_position) full_range = SourceRange.from_values("filename", 1, 1, 3, 4) self.assertEqual(empty_range.expand(file), full_range)