Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resetting more argument collections #202

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private void setCollectionValues(
"A \"null\" value was detected for an option after values for that option were already set. " +
"Clobbering previously set values for this option: %s.", getArgumentAliasDisplayString()));
}
if (!isOptional()) {
if (!argumentAnnotation.optional() && i == preprocessedValues.size() - 1) {
throw new CommandLineException(
String.format("Non \"null\" value must be provided for '%s'", getArgumentAliasDisplayString()));
}
Expand Down Expand Up @@ -704,4 +704,4 @@ public int hashCode() {
result = 31 * result + (getDefaultValueAsString() != null ? getDefaultValueAsString().hashCode() : 0);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import org.testng.annotations.Test;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class NamedArgumentDefinitionUnitTest {

Expand Down Expand Up @@ -150,4 +152,123 @@ private Field getFieldForFieldName(final Object argContainer, final String field
throw new IllegalArgumentException("Can't find field");
}

public static class ArgumentLists {
@Argument
List<String> required;
@Argument(optional = true)
List<String> optional;
@Argument
List<String> defaultedRequired = new ArrayList<>(List.of("default"));
@Argument(optional = true)
List<String> defaultedOptional = new ArrayList<>(List.of("default"));
}

@DataProvider
public Object[][] setArgumentValuesCollectionsTests() {
return new Object[][]{
{"required", Set.of(),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"optional", Set.of(),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedRequired", Set.of(),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedOptional", Set.of(),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"required", Set.of(),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"optional", Set.of(),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedRequired", Set.of(),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedOptional", Set.of(),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"required", Set.of(),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"optional", Set.of(),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"defaultedRequired", Set.of(),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"defaultedOptional", Set.of(),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"optional", Set.of(),
List.of("stuff", "more stuff", "null"), List.of()},
{"defaultedOptional", Set.of(),
List.of("stuff", "more stuff", "null"), List.of()},

{"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff"), List.of("default", "stuff", "more stuff")},
{"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff"), List.of("default", "stuff", "more stuff")},
{"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("null", "stuff", "more stuff"), List.of("stuff", "more stuff")},
{"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "null", "more stuff"), List.of("more stuff")},
{"optional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff", "null"), List.of()},
{"defaultedOptional", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff", "null"), List.of()},
};
}

@Test(dataProvider = "setArgumentValuesCollectionsTests")
public void testSetArgumentValuesCollections(
final String fieldName,
final Set<CommandLineParserOptions> parserOptions,
final List<String> newValues,
final List<String> expectedValues
) throws IllegalAccessException {
final ArgumentLists argLists = new ArgumentLists();
final Field field = getFieldForFieldName(argLists, fieldName);
final NamedArgumentDefinition argDef =
new NamedArgumentDefinition(field.getAnnotation(Argument.class), argLists, field, null);
final CommandLineArgumentParser clp =
new CommandLineArgumentParser(argLists, Collections.emptyList(), parserOptions);
argDef.setArgumentValues(clp, System.out, newValues);
Assert.assertEquals(field.get(argLists), expectedValues);
}

@DataProvider
public Object[][] setArgumentValuesCollectionsFailures() {
return new Object[][]{
{"required", Set.of(),
List.of("stuff", "more stuff", "null")},
{"defaultedRequired", Set.of(),
List.of("stuff", "more stuff", "null")},
{"required", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff", "null")},
{"defaultedRequired", Set.of(CommandLineParserOptions.APPEND_TO_COLLECTIONS),
List.of("stuff", "more stuff", "null")},
};
}

@Test(dataProvider = "setArgumentValuesCollectionsFailures", expectedExceptions = CommandLineException.class)
public void testSetArgumentValuesCollectionsFailures(
final String fieldName,
final Set<CommandLineParserOptions> parserOptions,
final List<String> newValues
) {
final ArgumentLists argLists = new ArgumentLists();
final Field field = getFieldForFieldName(argLists, fieldName);
final NamedArgumentDefinition argDef =
new NamedArgumentDefinition(field.getAnnotation(Argument.class), argLists, field, null);
final CommandLineArgumentParser clp =
new CommandLineArgumentParser(argLists, Collections.emptyList(), parserOptions);
argDef.setArgumentValues(clp, System.out, newValues);
}
}
Loading