blob: 33a74dbeac29b427e3faa29972f79756b6ecfd08 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2008, 2021 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 static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
import org.eclipse.statet.jcommons.text.core.input.StringParserInput;
import org.eclipse.statet.ltk.model.core.ModelManager;
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.RAsts.FCallArgMatch;
public class RArgumentMatchingTest {
private final RParser rParser= new RParser(ModelManager.MODEL_FILE);
private final StringParserInput input= new StringParserInput();
public RArgumentMatchingTest() {
}
@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.rParser.scanFCallArgs(this.input.reset(code).init(), true);
final FCallArgMatch readedArgs = RAsts.matchArgs(callArgs, argsDef);
assertArrayEquals(expected, readedArgs.argsNode2argsDef);
}
}