|
| 1 | +/* |
| 2 | + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. |
| 3 | + * Copyright (C) 2011, 2013-2023 The JavaParser Team. |
| 4 | + * |
| 5 | + * This file is part of JavaParser. |
| 6 | + * |
| 7 | + * JavaParser can be used either under the terms of |
| 8 | + * a) the GNU Lesser General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * b) the terms of the Apache License |
| 12 | + * |
| 13 | + * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | + * LICENCE.APACHE. Please refer to those files for details. |
| 15 | + * |
| 16 | + * JavaParser is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.github.javaparser.ast.nodeTypes; |
| 23 | + |
| 24 | +import com.github.javaparser.ast.expr.Expression; |
| 25 | +import com.github.javaparser.ast.expr.MethodCallExpr; |
| 26 | +import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import static com.github.javaparser.ast.expr.Expression.EXCLUDE_ENCLOSED_EXPR; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | + |
| 32 | +class NodeWithArgumentsTest extends AbstractLexicalPreservingTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + void testGetArgumentPosition() { |
| 36 | + considerCode("" + |
| 37 | + "class Foo {\n" + |
| 38 | + " Map<Integer,String> map = new HashMap<>();\n" + |
| 39 | + " public String bar(int i) {\n" + |
| 40 | + " return map.put(((i)),((\"baz\")));\n" + |
| 41 | + " } \n" + |
| 42 | + "}"); |
| 43 | + MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get(); |
| 44 | + Expression arg0 = mce.getArgument(0); |
| 45 | + Expression arg1 = mce.getArgument(1); |
| 46 | + Expression innerExpr0 = arg0.asEnclosedExpr().getInner() |
| 47 | + .asEnclosedExpr().getInner(); |
| 48 | + Expression innerExpr1 = arg1.asEnclosedExpr().getInner() |
| 49 | + .asEnclosedExpr().getInner(); |
| 50 | + |
| 51 | + assertEquals(0, mce.getArgumentPosition(arg0)); // with no conversion |
| 52 | + assertEquals(0, mce.getArgumentPosition(innerExpr0, EXCLUDE_ENCLOSED_EXPR)); // with a conversion skipping EnclosedExprs |
| 53 | + assertEquals(1, mce.getArgumentPosition(arg1)); // with no conversion |
| 54 | + assertEquals(1, mce.getArgumentPosition(innerExpr1, EXCLUDE_ENCLOSED_EXPR)); // with a conversion skipping EnclosedExprs |
| 55 | + } |
| 56 | +} |
0 commit comments