def __reindent_property_name(self, args: list, start_index: int) -> Tuple[list, int]: for i in range(start_index + 1, len(args)): if self.__is_argument(args[i]): if not args[i].startswith(Tokens.reindent(1)): args[i] = Tokens.reindent(1) + args[i] return args, i return args, 0
def test_is_command_keyword(self): self.assertTrue(KeywordVerifier.is_command_keyword('COMMAND')) self.assertTrue( KeywordVerifier.is_command_keyword(Tokens.reindent(1) + 'COMMAND')) self.assertTrue( KeywordVerifier.is_command_keyword(Tokens.reindent(1) + 'ARGS')) self.assertFalse(KeywordVerifier.is_command_keyword('CMD')) self.assertFalse( KeywordVerifier.is_command_keyword(Tokens.reindent(2) + 'COMMAND'))
def test_matching_line_comment_tokens(self): self.assertTrue(Tokens.is_line_comment('# comment')) self.assertTrue(Tokens.is_line_comment('#comment')) self.assertTrue(Tokens.is_line_comment(Tokens.reindent(1) + '#comment')) self.assertTrue(Tokens.is_line_comment(Tokens.remove_spaces() + Tokens.reindent(1) + '#comment')) self.assertTrue(Tokens.is_line_comment('\t # something')) self.assertTrue(Tokens.is_line_comment('\t #')) self.assertFalse(Tokens.is_line_comment('_# not a comment')) self.assertFalse(Tokens.is_line_comment('comment'))
def test_double_keywords(self): self.assertTrue( self.verify.is_double_keyword( Tokens.reindent(1) + 'RUNTIME', 'DESTINATION')) self.assertTrue( self.verify.is_double_keyword('ARCHIVE', Tokens.reindent(1) + 'DESTINATION')) self.assertTrue(self.verify.is_double_keyword('LIBRARY', 'DESTINATION')) self.assertFalse(self.verify.is_double_keyword('OUTPUT', 'DESTINATION')) self.assertFalse(self.verify.is_double_keyword('LIBRARY', 'OUTPUT'))
def test_whether_token_is_first_class_keyword(self): self.assertTrue(self.verify.is_first_class_keyword('PROPERTY')) self.assertTrue(self.verify.is_first_class_keyword('PROPERTIES')) self.assertTrue( self.verify.is_first_class_keyword( Tokens.reindent(1) + 'PROPERTY')) self.assertFalse(self.verify.is_first_class_keyword('PROPERTY2')) self.assertFalse(self.verify.is_first_class_keyword('proPERTY'))
def test_unquoted_arguments_with_uppercase_letters_only_are_keywords(self): self.settings['unquoted_uppercase_as_keyword'] = True self.verify = KeywordVerifier(self.settings) self.assertTrue(self.verify.is_keyword('OTHER')) self.assertTrue(self.verify.is_keyword(Tokens.reindent(1) + 'OTHER')) self.assertTrue(self.verify.is_keyword('WITH_SEPARATION')) self.assertFalse(self.verify.is_keyword('"$OTHER"')) self.assertFalse(self.verify.is_keyword('SOMeARG')) self.assertFalse(self.verify.is_keyword('a_ARGUMENT')) self.assertFalse(self.verify.is_keyword('NOT_')) self.assertFalse(self.verify.is_keyword('_SOME'))
def test_ignore_reindent_tokens(self): self.assertEqual(0, self.calculator.calculate(Tokens.reindent(3))) self.assertEqual(14, self.calculator.calculate(f'\tsome{Tokens.reindent(99)}\tso'))
def __call__(self, data: list) -> str: formatted = ''.join(data) if '\n' in data[1]: formatted = Tokens.reindent(99) + formatted return formatted
def __reindent_all_previous_comments(args: list, start: int) -> None: for i in reversed(range(start)): if Tokens.is_line_comment(args[i]): args[i] = Tokens.reindent(1) + args[i] else: break
def is_property(self, data: str) -> bool: data = data.replace(Tokens.reindent(1), '') return data in KeywordVerifier.__PROPERTIES["properties_full_names"] or \ self.__is_property_regex_starting(data) or \ self.__is_property_ending_with(data)
def is_command_keyword(data: str) -> bool: data = data.replace(Tokens.reindent(1), '') return data == 'COMMAND' or data == 'ARGS'
def is_double_keyword(first: str, second: str) -> bool: first = first.replace(Tokens.reindent(1), '') second = second.replace(Tokens.reindent(1), '') return any(keyword.startswith(first) and keyword.endswith(second) \ for keyword in KeywordVerifier.__PROPERTIES['double-keywords'])
def test_should_accept_keyword_when_on_the_list(self): self.assertTrue(self.verify.is_keyword('some')) self.assertTrue(self.verify.is_keyword(Tokens.reindent(1) + 'some')) self.assertFalse(self.verify.is_keyword('some2')) self.assertFalse(self.verify.is_keyword('${some}'))
def __format(self, data): if self.__state['has_first_class_keyword']: return Tokens.reindent(3) + data if self.__state['keyword_argument']: return Tokens.reindent(2) + data return Tokens.reindent(1) + data
def __format_data(self, data: str) -> str: if self.__keyword_argument_already_found and self.__is_reindent_needed( data): return Tokens.reindent(1) + data return data
def test_reindent_token_generation_and_matching(self): self.assertRegex(Tokens.reindent(1), Tokens.get_reindent_regex()) self.assertRegex(Tokens.reindent(9), Tokens.get_reindent_regex()) self.assertRegex(Tokens.reindent(), Tokens.get_reindent_regex())
def test_cmake_properties_with_reindent_token(self): self.assertTrue( self.verify.is_property(Tokens.reindent(1) + 'LINK_DIRECTORIES'))
def __cleanup_reindent_all(data: str) -> str: return re.sub('[ \t]*' + Tokens.reindent(), '', data)
def is_first_class_keyword(data: str) -> bool: data = data.replace(Tokens.reindent(1), '') return data in KeywordVerifier.__FIRST_CLASS_KEYWORDS
def is_keyword(self, data: str) -> bool: data = data.replace(Tokens.reindent(1), '') return self.__is_one_of_defined_keywords(data) or \ self.__should_be_handled_as_keyword(data) or \ self.is_first_class_keyword(data) or \ self.__is_keyword_in_cmake(data)