diff --git a/check_source/check_style_c.py b/check_source/check_style_c.py index 8f9faa1f122faf7dbf5b0d9fa3d0488ffed0ede6..3942f3ee28e194bf801604d58b0b57d9ae31124d 100755 --- a/check_source/check_style_c.py +++ b/check_source/check_style_c.py @@ -193,6 +193,11 @@ def extract_to_linestart(index): def extract_statement_if(index_kw): # assert(tokens[index_kw].text == "if") + # ignore preprocessor + i_linestart = tk_advance_line_start(index_kw) + if tokens[i_linestart].text.startswith("#"): + return None + # seek back i = index_kw @@ -203,11 +208,6 @@ def extract_statement_if(index_kw): # print(tokens[i_next]) - # ignore preprocessor - i_linestart = tk_advance_line_start(index_kw) - if tokens[i_linestart].text.startswith("#"): - return None - if tokens[i_next].type != Token.Punctuation or tokens[i_next].text != "(": warning("E105", "no '(' after '%s'" % tokens[index_kw].text, i_start, i_next) return None @@ -734,14 +734,14 @@ def blender_check_operator(index_start, index_end, op_text, is_cpp): elif op_text == "::~": pass else: - warning("E138", "unhandled operator 3 '%s'" % op_text, index_start, index_end) + warning("E000.1", "unhandled operator 3 '%s'" % op_text, index_start, index_end) elif len(op_text) == 4: if op_text == "*>::": pass else: - warning("E139", "unhandled operator 4 '%s'" % op_text, index_start, index_end) + warning("E000.2", "unhandled operator 4 '%s'" % op_text, index_start, index_end) else: - warning("E140", "unhandled operator (len > 4) '%s'" % op_text, index_start, index_end) + warning("E000.3", "unhandled operator (len > 4) '%s'" % op_text, index_start, index_end) if len(op_text) > 1: if op_text[0] == "*" and op_text[-1] == "*": diff --git a/tests/check_source/check_style_c_test.py b/tests/check_source/check_style_c_test.py index 57e178f1607776a922012832496f83d19d85d614..61fd7c1c099ae469959a942a23b20d2722340e3e 100755 --- a/tests/check_source/check_style_c_test.py +++ b/tests/check_source/check_style_c_test.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# python3 -m unittest check_style_c_test.SourceCodeTest.test_whitespace_kw_indent # ---- import os @@ -22,6 +23,9 @@ parser = check_style_c.create_parser() args = parser.parse_args(["."]) # ---- +# store errors we found +errors = set() + FUNC_BEGIN = """ void func(void) { @@ -33,12 +37,18 @@ FUNC_END = """ def test_code(code): warnings.clear() check_style_c.scan_source("test.c", code, args) - err_ls = [w.split(":", 3)[2].strip() for w in warnings] + err_found = [w.split(":", 3)[2].strip() for w in warnings] # print(warnings) - return err_ls + return err_found class SourceCodeTest(unittest.TestCase): + + def assertWarning(self, err_found, *errors_test): + err_set = set(err_found) + errors.update(err_set) + self.assertEqual(err_set, set(errors_test)) + def test_brace_function(self): # -------------------------------------------------------------------- # brace on not on newline (function) @@ -46,19 +56,36 @@ class SourceCodeTest(unittest.TestCase): void func(void) { \t/* pass */ }""" - err_ls = test_code(code) - self.assertEqual(1, len(err_ls)) - self.assertEqual("E101", err_ls[0]) + err_found = test_code(code) + self.assertWarning(err_found, "E101") code = """ void func(void) { \t/* pass */ }""" - err_ls = test_code(code) - self.assertEqual(0, len(err_ls)) + err_found = test_code(code) + self.assertEqual(0, len(err_found)) - def test_brace_kw(self): + def test_brace_kw_whitespace(self): + # -------------------------------------------------------------------- + # brace has whitespace after it + + code = FUNC_BEGIN + """ +\tif (1){ +\t\t/* pass */ +\t}""" + FUNC_END + err_found = test_code(code) + self.assertWarning(err_found, "E107", "E150") + + code = FUNC_BEGIN + """ +\tif (1) { +\t\t/* pass */ +\t}""" + FUNC_END + err_found = test_code(code) + self.assertEqual(0, len(err_found)) + + def test_brace_kw_newline(self): # -------------------------------------------------------------------- # brace on on newline (if, for... ) @@ -67,16 +94,15 @@ void func(void) \t{ \t\t/* pass */ \t}""" + FUNC_END - err_ls = test_code(code) - self.assertEqual(1, len(err_ls)) - self.assertEqual("E108", err_ls[0]) + err_found = test_code(code) + self.assertWarning(err_found, "E108") code = FUNC_BEGIN + """ \tif (1) { \t\t/* pass */ \t}""" + FUNC_END - err_ls = test_code(code) - self.assertEqual(0, len(err_ls)) + err_found = test_code(code) + self.assertEqual(0, len(err_found)) def test_brace_do_while(self): # -------------------------------------------------------------------- @@ -87,16 +113,15 @@ void func(void) \t{ \t\t/* pass */ \t}""" + FUNC_END - err_ls = test_code(code) - self.assertEqual(1, len(err_ls)) - self.assertEqual("E108", err_ls[0]) + err_found = test_code(code) + self.assertWarning(err_found, "E108") code = FUNC_BEGIN + """ \tif (1) { \t\t/* pass */ \t}""" + FUNC_END - err_ls = test_code(code) - self.assertEqual(0, len(err_ls)) + err_found = test_code(code) + self.assertEqual(0, len(err_found)) def test_brace_kw_multiline(self): # -------------------------------------------------------------------- @@ -107,10 +132,8 @@ void func(void) \t b) { \t\t/* pass */ \t}""" + FUNC_END - err_ls = test_code(code) - self.assertEqual(2, len(err_ls)) - self.assertEqual("E103", err_ls[0]) - self.assertEqual("E104", err_ls[1]) + err_found = test_code(code) + self.assertWarning(err_found, "E103", "E104") code = FUNC_BEGIN + """ \tif (a && @@ -119,8 +142,8 @@ void func(void) \t\t/* pass */ \t}""" + FUNC_END - err_ls = test_code(code) - self.assertEqual(0, len(err_ls)) + err_found = test_code(code) + self.assertEqual(0, len(err_found)) def test_brace_indent(self): # -------------------------------------------------------------------- @@ -129,16 +152,123 @@ void func(void) \tif (1) { \t\t/* pass */ \t\t}""" + FUNC_END - err_ls = test_code(code) - self.assertEqual(1, len(err_ls)) - self.assertEqual("E104", err_ls[0]) + err_found = test_code(code) + self.assertWarning(err_found, "E104") + + code = FUNC_BEGIN + """ +\tif (1) { +\t\t/* pass */ +\t}""" + FUNC_END + err_found = test_code(code) + self.assertEqual(0, len(err_found)) + + def test_whitespace_kw_no_parens(self): + # -------------------------------------------------------------------- + # if MACRO {} + code = FUNC_BEGIN + """ +\tif MACRO { +\t\t/* pass */ +\t\t}""" + FUNC_END + err_found = test_code(code) + self.assertWarning(err_found, "E104", "E105") + + code = FUNC_BEGIN + """ +\tif (1) { +\t\t/* pass */ +\t}""" + FUNC_END + err_found = test_code(code) + self.assertEqual(0, len(err_found)) + + def test_whitespace_kw_missing(self): + # -------------------------------------------------------------------- + code = FUNC_BEGIN + """ +\tif(1) { +\t\t/* pass */ +\t\t}""" + FUNC_END + err_found = test_code(code) + self.assertWarning(err_found, "E104", "E106") code = FUNC_BEGIN + """ \tif (1) { \t\t/* pass */ \t}""" + FUNC_END - err_ls = test_code(code) - self.assertEqual(0, len(err_ls)) + err_found = test_code(code) + self.assertEqual(0, len(err_found)) + + def test_brace_kw_newline_missing(self): + # -------------------------------------------------------------------- + code = FUNC_BEGIN + """ +\tif (1 && +\t 1) fn(); +\telse {}""" + FUNC_END + err_found = test_code(code) + self.assertWarning(err_found, "E103", "E109") + + code = FUNC_BEGIN + """ +\tif (1 && +\t 1) +\t{ +\t\tfn(); +\t} +\telse {}""" + FUNC_END + err_found = test_code(code) + self.assertEqual(0, len(err_found)) + + def test_whitespace_kw_indent(self): + # -------------------------------------------------------------------- + code = FUNC_BEGIN + """ +\tif (a && +\t\t\tb && +\t c) +\t{ +\t\t/* pass */ +\t}""" + FUNC_END + err_found = test_code(code) + self.assertWarning(err_found, "E110") + + code = FUNC_BEGIN + """ +\tif (a && +\t b && +\t c) +\t{ +\t\t/* pass */ +\t}""" + FUNC_END + err_found = test_code(code) + self.assertEqual(0, len(err_found)) + + +class SourceCodeTestComplete(unittest.TestCase): + """ Check we ran all tests. + """ + def test_complete(self): + # -------------------------------------------------------------------- + # Check we test all errors + errors_uniq = set() + with open(check_style_c.__file__, 'r', encoding='utf-8') as fh: + import re + # E100 + err_re_main = re.compile(r'.*\("(E\d+)"') + # E100.0 + err_re_subv = re.compile(r'.*\("(E\d+)\.\d+"') # --> E100 + err_re_subv_group = re.compile(r'.*\("(E\d+\.\d+)"') # --> E100.0 + for l in fh: + g = err_re_subv.match(l) + if g is None: + g = err_re_main.match(l) + is_sub = False + else: + is_sub = True + + if g: + err = g.group(1) + self.assertIn(err, errors) + + # ---- + # now check we didn't use multiple times + if is_sub: + err = err_re_subv_group.match(l).group(1) + self.assertNotIn(err, errors_uniq) + errors_uniq.add(err) if __name__ == '__main__':