public void testLinenoMultilineEq() { AstRoot root = parse( "\nif\n" + " (((a == \n" + " 3) && \n" + " (b == 2)) || \n" + " (c == 1)) {\n" + "}\n"); IfStatement ifStmt = (IfStatement) root.getFirstChild(); InfixExpression orTest = (InfixExpression) ifStmt.getCondition(); ParenthesizedExpression cTestParen = (ParenthesizedExpression) orTest.getRight(); InfixExpression cTest = (InfixExpression) cTestParen.getExpression(); ParenthesizedExpression andTestParen = (ParenthesizedExpression) orTest.getLeft(); InfixExpression andTest = (InfixExpression) andTestParen.getExpression(); AstNode aTest = andTest.getLeft(); AstNode bTest = andTest.getRight(); assertEquals(1, ifStmt.getLineno()); assertEquals(2, orTest.getLineno()); assertEquals(2, andTest.getLineno()); assertEquals(2, aTest.getLineno()); assertEquals(4, bTest.getLineno()); assertEquals(5, cTest.getLineno()); assertEquals(5, cTestParen.getLineno()); assertEquals(2, andTestParen.getLineno()); }
@Override public AstNode ifStatement(AstNode condition, AstNode thenPart, AstNode elsePart) { IfStatement ifs = new IfStatement(); ifs.setCondition(condition); ifs.setThenPart(thenPart); ifs.setElsePart(elsePart); return ifs; }
public void testLinenoMultilineBitTest() { AstRoot root = parse( "\nif (\n" + " ((a \n" + " | 3 \n" + " ) == \n" + " (b \n" + " & 2)) && \n" + " ((a \n" + " ^ 0xffff) \n" + " != \n" + " (c \n" + " << 1))) {\n" + "}\n"); IfStatement ifStmt = (IfStatement) root.getFirstChild(); InfixExpression andTest = (InfixExpression) ifStmt.getCondition(); ParenthesizedExpression bigLHSExpr = (ParenthesizedExpression) andTest.getLeft(); ParenthesizedExpression bigRHSExpr = (ParenthesizedExpression) andTest.getRight(); InfixExpression eqTest = (InfixExpression) bigLHSExpr.getExpression(); InfixExpression notEqTest = (InfixExpression) bigRHSExpr.getExpression(); ParenthesizedExpression test1Expr = (ParenthesizedExpression) eqTest.getLeft(); ParenthesizedExpression test2Expr = (ParenthesizedExpression) eqTest.getRight(); ParenthesizedExpression test3Expr = (ParenthesizedExpression) notEqTest.getLeft(); ParenthesizedExpression test4Expr = (ParenthesizedExpression) notEqTest.getRight(); InfixExpression bitOrTest = (InfixExpression) test1Expr.getExpression(); InfixExpression bitAndTest = (InfixExpression) test2Expr.getExpression(); InfixExpression bitXorTest = (InfixExpression) test3Expr.getExpression(); InfixExpression bitShiftTest = (InfixExpression) test4Expr.getExpression(); assertEquals(1, ifStmt.getLineno()); assertEquals(2, bigLHSExpr.getLineno()); assertEquals(7, bigRHSExpr.getLineno()); assertEquals(2, eqTest.getLineno()); assertEquals(7, notEqTest.getLineno()); assertEquals(2, test1Expr.getLineno()); assertEquals(5, test2Expr.getLineno()); assertEquals(7, test3Expr.getLineno()); assertEquals(10, test4Expr.getLineno()); assertEquals(2, bitOrTest.getLineno()); assertEquals(5, bitAndTest.getLineno()); assertEquals(7, bitXorTest.getLineno()); assertEquals(10, bitShiftTest.getLineno()); }
public void testNestedOr() { AstNode root = parse("\nif (a && \n" + " b() || \n" + " /* comment */\n" + " c) {\n" + "}\n"); IfStatement ifStmt = (IfStatement) root.getFirstChild(); InfixExpression orClause = (InfixExpression) ifStmt.getCondition(); InfixExpression andClause = (InfixExpression) orClause.getLeft(); AstNode cName = orClause.getRight(); assertEquals(1, ifStmt.getLineno()); assertEquals(1, orClause.getLineno()); assertEquals(1, andClause.getLineno()); assertEquals(4, cName.getLineno()); }
/** * @param node The node we want to have wrapped. * @return The (new) node parent (the block probably) */ private AstNode makeSureBlockExistsAround(AstNode node) { AstNode parent = node.getParent(); if (parent instanceof IfStatement) { /* the parent is an if and there are no braces, so we should make a new block */ IfStatement i = (IfStatement) parent; /* replace the if or the then, depending on what the current node is */ if (i.getThenPart().equals(node)) { i.setThenPart(createBlockWithNode(node)); } else if (i.getElsePart() != null) { if (i.getElsePart().equals(node)) i.setElsePart(createBlockWithNode(node)); } } else if (parent instanceof WhileLoop) { /* the parent is a while and there are no braces, so we should make a new block */ /* I don't think you can find this in the real world, but just to be sure */ WhileLoop w = (WhileLoop) parent; if (w.getBody().equals(node)) w.setBody(createBlockWithNode(node)); } else if (parent instanceof ForLoop) { /* the parent is a for and there are no braces, so we should make a new block */ /* I don't think you can find this in the real world, but just to be sure */ ForLoop f = (ForLoop) parent; if (f.getBody().equals(node)) f.setBody(createBlockWithNode(node)); } return node.getParent(); }
public void testLinenoIf() { AstRoot root = parse( "\nif\n" + " (a == 3)\n" + " {\n" + " b = 0;\n" + " }\n" + " else\n" + " {\n" + " c = 1;\n" + " }\n"); IfStatement ifStmt = (IfStatement) root.getFirstChild(); AstNode condClause = ifStmt.getCondition(); AstNode thenClause = ifStmt.getThenPart(); AstNode elseClause = ifStmt.getElsePart(); assertEquals(1, ifStmt.getLineno()); assertEquals(2, condClause.getLineno()); assertEquals(3, thenClause.getLineno()); assertEquals(7, elseClause.getLineno()); }