def test_do_statement_count(self): code = """ do a-- ; while ( a ); """ node = self.statement(code) self.assertEqual(branches(node), 1)
def test_if_statement_count(self): code = """ if (itr == '\r') { int status = 1; } """ node = self.statement(code) self.assertEqual(branches(node), 1)
def test_for_statement_count(self): code = """ for (int i = 0; i < amounts.length; i++) { result += amounts[i]; } """ node = self.statement(code) self.assertEqual(branches(node), 1)
def test_while_statement_count(self): code = """ while (i < 5) { System.out.println(i); i++; } """ node = self.statement(code) self.assertEqual(branches(node), 1)
def test_switch_statement_count(self): code = """ switch ( a ) { case 1: return ; } """ node = self.statement(code) self.assertEqual(branches(node.cases[0]), 1)
def test_catch_statements(self): code = """ try { Throwable t = new Exception(); throw t; } catch (RuntimeException e) { System.err.println("catch RuntimeException"); } catch (Exception e) { System.err.println("catch Exception"); } catch (Throwable e) { System.err.println("catch Throwable"); } System.err.println("next statement"); """ node = self.statement(code) self.assertEqual(branches(node), 3)
for path, node in ast: receivedNames += compound(node) if (receivedNames != expectedNames): raise Exception('\nTest failed. Expected ' + str(expectedNames) + ', received ' + str(receivedNames)) except Exception as e: sys.exit(str(e) + ': ' + java) for java in glob(os.path.join(fileDir, 'java/cc/*.java')): with open(java, encoding='utf-8') as f: try: ast = javalang.parse.parse(f.read()) receivedCC = 1 expectedCC = cc(ast) for path, node in ast: receivedCC += branches(node) if (receivedCC != expectedCC): raise Exception('\nTest failed. Expected ' + str(expectedCC) + ', received ' + str(receivedCC)) print('.', end='', flush=True), except Exception as e: sys.exit(str(e) + ': ' + java) print('\nOK')
def test_ternary_operator(self): code = 'value == "uppercase" ? "JOHN" : "john";' node = self.expression(code) self.assertEqual(branches(node), 1)
def test_logic_operator_not_count(self): code = 'if ( a > b ) {}' ifNode = self.statement(code) self.assertEqual(branches(ifNode.children[1]), 0)
def test_branch_not_count(self): code = "nextKey = new BlockKey(serialNo, System.currentTimeMillis() + 3.0);" node = self.expression(code) self.assertEqual(branches(node), 0)