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

Kedo issue1383 #1502

Merged
merged 5 commits into from
Dec 8, 2021
Merged
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
103 changes: 103 additions & 0 deletions src/test/java/picocli/MultipleArgumentsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package picocli;

import org.junit.Test;
import picocli.CommandLine.Command;
import java.util.Locale;
import static org.junit.Assert.assertEquals;

/**
* Multiple Arguments Testing based on https://picocli.info/#_multiple_values
* for clarification based on analysis from issue 1383
* https:/remkop/picocli/issues/1383
* <p>
* These tests demonstrate the correct picocli implementation for
* multiple argument with split, flags, and type conversion
* @author @madfoal, @lind6
*/
public class MultipleArgumentsTest {

/** default */static final String FLAG_STRING = "-t"; // String literal for the argument flag
/** default */static final String GOOD_STRING = "good test"; // String literal for the good test
/** default */static final String BAD_STRING = "Bad Test"; // String literal for the bad test
/** default */static final String errorMessage = "Invalid value for option '-t' (<args>): "
+"Type Conversion Failure\n" +
"Argument: \"Bad Test\" must be lower case.\n";
@Command(name = "Issue-1383")
/**
* Tests issue 1383 https:/remkop/picocli/issues/1383
* Based on documentation for https://picocli.info/#_repeated_options
* <p>
* Allows for multiple java class arguments within an array to be used with
* a converter class
* @author @madfoal, @lind6
*/
static class Issue1383{
@CommandLine.Option(names = FLAG_STRING,split=",", converter = TestType.TestConverter.class)
TestType[] args;

static class TestType {
String str; // only data field for this test type

private TestType(final String txt) {
str = txt;
}

/**
* Test Conversion for issue 1383. Overloads convert.
* @author @madfoal, @lind6
*/
public static class TestConverter implements
CommandLine.ITypeConverter<Issue1383.TestType> {

@Override
public Issue1383.TestType convert(final String arg) throws Exception {
if (!arg.toLowerCase(Locale.ROOT).equals(arg)) {
throw new CommandLine.TypeConversionException(
"Type Conversion Failure\nArgument: \""
+ arg + "\" must be lower case.\n");
}
return new Issue1383.TestType(arg);
}
}
}
}

/**
* Tests issue 1383 https:/remkop/picocli/issues/1383
* Based on documentation for https://picocli.info/#_repeated_options
* <p>
* Allows for multiple java class arguments within an array to be used with
* a converter class
* @author @madfoal, @lind6
*/
@Test
public void testOneIssue1383RepeatedOptions() {
final String[] args = {FLAG_STRING, GOOD_STRING, FLAG_STRING, BAD_STRING};

final Issue1383 obj = new Issue1383();
try {
new CommandLine(obj).parseArgs(args);
} catch ( CommandLine.PicocliException e) {
assertEquals("Multiple Arguments with Type Converter Failure",errorMessage,e.getMessage());
}
}

/**
* Tests issue 1383 https:/remkop/picocli/issues/1383
* Based on documentation for https://picocli.info/#_repeated_options
* <p>
* Allows for multiple java class arguments within an array to be used with
* a converter class
* @author @madfoal, @lind6
*/
@Test
public void testOneIssue1383SplitRegex() {
final String[] args = {FLAG_STRING, GOOD_STRING+","+BAD_STRING};
final Issue1383 obj = new Issue1383();
try {
new CommandLine(obj).parseArgs(args);
} catch ( CommandLine.PicocliException e) {
assertEquals("Multiple Arguments with Type Converter Failure",errorMessage,e.getMessage());
}
}
}