Skip to content

Commit

Permalink
First unit tests for positional args (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvesse committed Apr 29, 2019
1 parent dce3292 commit a9eecb0
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
@Target({ FIELD })
@Documented
public @interface PositionalArgument {

/**
* Helper constants for more explicitly specifying the positional index of
* the argument
*/
public static final int FIRST = 0, SECOND = 1, THIRD = 2, FOURTH = 3, FIFTH = 4, SIXTH = 5, SEVENTH = 6, EIGTH = 7,
NINTH = 8, TENTH = 9;

/**
* Name of the argument
*
Expand All @@ -49,29 +57,31 @@
String description() default "";

/**
* The positional index (one-based) for the argument
* The positional index (zero-based) for the argument
* <p>
* So {@code 1} represents the first argument, {@code 3} the third argument
* and so forth
* So {@code 0} represents the first argument, {@code 3} the fourth argument
* and so forth. Helper constants are provided on the annotation class e.g.
* {@link #SECOND} for making uses of this annotation more clear
* </p>
*
* @return
*/
int position();

/**
* If true this parameter can override parameters of the same index (set via
* the {@link PositionalArgument#position()} property) declared by parent classes assuming
* the argument definitions are compatible.
* the {@link PositionalArgument#position()} property) declared by parent
* classes assuming the argument definitions are compatible.
* <p>
* See
* {@link PositionalArgumentMetadata#override(PositionalArgumentMetadata, PositionalArgumentMetadata)}
* for legal overrides
* </p>
* <p>
* Note that where the child argument definition is an exact duplicate of the
* parent then overriding is implicitly permitted
* Note that where the child argument definition is an exact duplicate of
* the parent then overriding is implicitly permitted
* </p>
*
* @return True if an override, false otherwise
*/
boolean override() default false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,11 @@ private static List<PositionalArgumentMetadata> overridePositionalArgumentSet(Li
}
}

List<PositionalArgumentMetadata> posArgs = new ArrayList<>(maxIndex);
for (int i = 0; i < maxIndex; i++) {
List<PositionalArgumentMetadata> posArgs = new ArrayList<>(maxIndex + 1);
for (int i = 0; i <= maxIndex; i++) {
while (posArgs.size() <= i) {
posArgs.add(null);
}
posArgs.set(i, argsIndex.get(i));
if (posArgs.get(i) == null) {
throw new IllegalStateException(String.format(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.rvesse.airline;

import com.github.rvesse.airline.args.ArgsPositional;
import com.github.rvesse.airline.args.ArgsPositionalGap;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.parser.command.SingleCommandParser;

import static com.github.rvesse.airline.TestingUtil.singleCommandParser;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestPositionalArgs {

@Test
public void positional_args_01() {
SingleCommand<ArgsPositional> parser = singleCommandParser(ArgsPositional.class);
assertFalse(parser.getCommandMetadata().getPositionalArguments().isEmpty());
assertEquals(parser.getCommandMetadata().getPositionalArguments().size(), 2);

PositionalArgumentMetadata posArg = parser.getCommandMetadata().getPositionalArguments().get(0);
assertEquals(posArg.getZeroBasedPosition(), 0);
assertEquals(posArg.getOneBasedPosition(), 1);
assertEquals(posArg.getJavaType(), String.class);

posArg = parser.getCommandMetadata().getPositionalArguments().get(1);
assertEquals(posArg.getZeroBasedPosition(), 1);
assertEquals(posArg.getOneBasedPosition(), 2);
assertEquals(posArg.getJavaType(), Integer.class);

}

@Test(expectedExceptions = IllegalStateException.class)
public void positional_args_gap_01() {
singleCommandParser(ArgsPositionalGap.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (C) 2010-16 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.rvesse.airline.args;

import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.PositionalArgument;
import com.github.rvesse.airline.annotations.restrictions.Required;

import java.util.ArrayList;
import java.util.List;

@Command(name = "ArgsPositional", description = "ArgsPositional description")
public class ArgsPositional
{
@PositionalArgument(position = PositionalArgument.FIRST, title = "File")
@Required
public String file;

@PositionalArgument(position = PositionalArgument.SECOND, title = "Mode")
public Integer mode;

@Arguments
public List<String> parameters = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (C) 2010-16 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.rvesse.airline.args;

import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.PositionalArgument;
import com.github.rvesse.airline.annotations.restrictions.Required;

import java.util.ArrayList;
import java.util.List;

@Command(name = "ArgsPositional", description = "ArgsPositional description")
public class ArgsPositionalGap
{
@PositionalArgument(position = PositionalArgument.FIRST, title = "File")
@Required
public String file;

@PositionalArgument(position = PositionalArgument.THIRD, title = "Mode")
public Integer mode;

@Arguments
public List<String> parameters = new ArrayList<>();
}

0 comments on commit a9eecb0

Please sign in to comment.