| /*=============================================================================# |
| # Copyright (c) 2008, 2020 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.r.core.rsource.ast; |
| |
| import org.junit.Assert; |
| import org.junit.Test; |
| |
| import org.eclipse.statet.jcommons.text.core.input.StringParserInput; |
| |
| import org.eclipse.statet.ltk.model.core.IModelManager; |
| import org.eclipse.statet.r.core.model.ArgsDefinition; |
| import org.eclipse.statet.r.core.rsource.ast.FCall.Args; |
| import org.eclipse.statet.r.core.rsource.ast.RAst.FCallArgMatch; |
| |
| |
| public class RArgumentMatchingTest { |
| |
| |
| private final RScanner scanner= new RScanner(IModelManager.MODEL_FILE); |
| |
| private final StringParserInput input= new StringParserInput(); |
| |
| |
| @Test |
| public void simple() { |
| final ArgsDefinition argsDef = new ArgsDefinition("test1", "test2"); |
| |
| assertArgs(argsDef, "1, 2", new int[] { 0, 1 }); |
| assertArgs(argsDef, "test1=1, 2", new int[] { 0, 1 }); |
| assertArgs(argsDef, "1, test1=2", new int[] { 1, 0 }); |
| assertArgs(argsDef, "test2=1, 2", new int[] { 1, 0 }); |
| assertArgs(argsDef, "test1=1, test2=2", new int[] { 0, 1 }); |
| assertArgs(argsDef, "test2=1, test1=2", new int[] { 1, 0 }); |
| } |
| |
| @Test |
| public void empty1() { |
| final ArgsDefinition argsDef = new ArgsDefinition("test1", "test2"); |
| |
| assertArgs(argsDef, ", 2", new int[] { 0, 1 }); |
| assertArgs(argsDef, "test1=, 2", new int[] { 0, 1 }); |
| assertArgs(argsDef, ", test1=2", new int[] { 1, 0 }); |
| assertArgs(argsDef, "test2=, 2", new int[] { 1, 0 }); |
| assertArgs(argsDef, "test1=, test2=2", new int[] { 0, 1 }); |
| assertArgs(argsDef, "test2=, test1=2", new int[] { 1, 0 }); |
| } |
| |
| @Test |
| public void empty2() { |
| final ArgsDefinition argsDef = new ArgsDefinition("test1", "test2"); |
| |
| assertArgs(argsDef, "1, ", new int[] { 0, 1 }); |
| assertArgs(argsDef, "test1=1, ", new int[] { 0, 1 }); |
| assertArgs(argsDef, "1, test1=", new int[] { 1, 0 }); |
| assertArgs(argsDef, "test2=1, ", new int[] { 1, 0 }); |
| assertArgs(argsDef, "test1=1, test2=", new int[] { 0, 1 }); |
| assertArgs(argsDef, "test2=1, test1=", new int[] { 1, 0 }); |
| } |
| |
| @Test |
| public void partialMatch() { |
| final ArgsDefinition argsDef = new ArgsDefinition("b", "aaa", "aa"); |
| |
| assertArgs(argsDef, "aaa=1, a=2, 3", new int[] { 1, 2, 0 }); |
| assertArgs(argsDef, "aaa=1, a=2, a=3", new int[] { 1, 2, -1 }); |
| assertArgs(argsDef, "a=1, 2", new int[] { -1, 0 }); |
| } |
| |
| |
| private void assertArgs(final ArgsDefinition argsDef, final String code, final int[] expected) { |
| final Args callArgs = this.scanner.scanFCallArgs(this.input.reset(code).init(), true); |
| |
| final FCallArgMatch readedArgs = RAst.matchArgs(callArgs, argsDef); |
| |
| Assert.assertArrayEquals(expected, readedArgs.argsNode2argsDef); |
| } |
| |
| } |