From bee8dc5c73d853141da33326f93279c359c2290a Mon Sep 17 00:00:00 2001 From: Albert Allagulov Date: Wed, 17 Jun 2020 22:33:50 +0300 Subject: [PATCH] check if try statement has catches --- computation/calc.py | 13 ++++++++----- computation/calc_unit_test.py | 8 ++++---- computation/java/names/FirstTest.java | 15 --------------- computation/java/names/Test.java | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 24 deletions(-) delete mode 100644 computation/java/names/FirstTest.java create mode 100644 computation/java/names/Test.java diff --git a/computation/calc.py b/computation/calc.py index acb03d9..17cda89 100644 --- a/computation/calc.py +++ b/computation/calc.py @@ -45,9 +45,11 @@ def branches(node): ))): count = 1 elif(isinstance(node, tree.SwitchStatementCase)): - count = len(node.case) + if node.case is not None: + count = len(node.case) elif(isinstance(node, tree.TryStatement)): - count = len(node.catches) + if node.catches is not None: + count = len(node.catches) return count """Check the name for compound inside the methods (i.e. for local variables) @@ -57,9 +59,10 @@ def branches(node): """ def compound(node): flag = False - if (isinstance(node, tree.LocalVariableDeclaration)): - name = node.declarators[0].name - flag = len(re.findall(r'(?:[a-z][A-Z]+)|(?:[_])', name)) != 0 + if (isinstance(node, tree.VariableDeclarator)): + found = re.findall(r'(?:[a-z][A-Z]+)|(?:[_])', node.name) + if found is not None: + flag = len(found) != 0 return flag diff --git a/computation/calc_unit_test.py b/computation/calc_unit_test.py index a3b06b1..f06cd99 100644 --- a/computation/calc_unit_test.py +++ b/computation/calc_unit_test.py @@ -107,14 +107,14 @@ def test_catch_statements(self): def test_compound_with_camel_and_snake_cases(self): for s in ['camelCase', 'CamelCase', 'camelCASE', 'snake_case', 'snake_CASE', 'SNAKE_CASE', 'SNAKE_case']: code = "int %s = 0;" % (s) - node = self.parser(code).parse_local_variable_declaration_statement() - self.assertEqual(compound(node), 1) + node = self.parser(code).parse_method_or_field_declaraction() + self.assertEqual(compound(node.declarators[0]), 1) def test_compound_without_camel_and_snake_cases(self): for s in ['camelcase, CAMELCASE, Camelcase']: code = "int %s = 0;" % (s) - node = self.parser(code).parse_local_variable_declaration_statement() - self.assertEqual(compound(node), 0) + node = self.parser(code).parse_method_or_field_declaraction() + self.assertEqual(compound(node.declarators[0]), 0) def expression(self, code): return self.parser(code).parse_expression() diff --git a/computation/java/names/FirstTest.java b/computation/java/names/FirstTest.java deleted file mode 100644 index e534e47..0000000 --- a/computation/java/names/FirstTest.java +++ /dev/null @@ -1,15 +0,0 @@ -/** - * 3 - * */ -public class FirstTest { - public int thisVariableNotTaken = 0; - public static final boolean this_variable_not_taken_too; - - public static void main(String args[]) { - final Test1 compoundNameOne = new Test1(); - boolean COUMPOUND_NAME_TWO = false; - String anotherOne; - int a0$ = 0; - int nottaken = 0; - } -} diff --git a/computation/java/names/Test.java b/computation/java/names/Test.java new file mode 100644 index 0000000..1c9bae9 --- /dev/null +++ b/computation/java/names/Test.java @@ -0,0 +1,19 @@ +/** + * 5 + * */ +public class Test { + public int thisVariableCounted = 0; // 1 + public static final boolean this_variable_not_counted_too; // 2 + private double nottaken; + + public static void main(String args[]) { + final Test1 compoundNameOne = new Test1(); // 3 + boolean COUMPOUND_NAME_TWO = false; // 4 + int nottaken = 0; + } + + void foo() { + String anotherOneCounted; // 5 + int a0$nottakentoo = 0; + } +}