SHL-98: check option availability in case insensitive way

Add unit test, format code
This commit is contained in:
Bay Batu
2014-09-17 01:11:00 +03:00
committed by Eric Bottard
parent 880bc35d55
commit e16ceca495
3 changed files with 40 additions and 12 deletions

2
.gitignore vendored
View File

@@ -11,3 +11,5 @@ target/
/.idea /.idea
/*.iml /*.iml
/*.ipr
/*.iws

View File

@@ -407,12 +407,12 @@ public class SimpleParser implements Parser {
Set<String> cliOptionKeySet = new LinkedHashSet<String>(); Set<String> cliOptionKeySet = new LinkedHashSet<String>();
for (CliOption cliOption : cliOptions) { for (CliOption cliOption : cliOptions) {
for (String key : cliOption.key()) { for (String key : cliOption.key()) {
cliOptionKeySet.add(key.toLowerCase()); cliOptionKeySet.add(key);
} }
} }
Set<String> unavailableOptions = new LinkedHashSet<String>(); Set<String> unavailableOptions = new LinkedHashSet<String>();
for (String suppliedOption : options.keySet()) { for (String suppliedOption : options.keySet()) {
if (!cliOptionKeySet.contains(suppliedOption.toLowerCase())) { if (!cliOptionKeySet.contains(suppliedOption)) {
unavailableOptions.add(suppliedOption); unavailableOptions.add(suppliedOption);
} }
} }
@@ -499,7 +499,8 @@ public class SimpleParser implements Parser {
String bufferToReturn = null; String bufferToReturn = null;
String lastWord = null; String lastWord = null;
next_buffer_loop: for (int bufferIndex = 0; bufferIndex < buffer.length(); bufferIndex++) { next_buffer_loop:
for (int bufferIndex = 0; bufferIndex < buffer.length(); bufferIndex++) {
String bufferSoFarIncludingThis = buffer.substring(0, bufferIndex + 1); String bufferSoFarIncludingThis = buffer.substring(0, bufferIndex + 1);
String bufferRemaining = buffer.substring(bufferIndex + 1); String bufferRemaining = buffer.substring(bufferIndex + 1);
@@ -649,7 +650,7 @@ public class SimpleParser implements Parser {
for (String value : cmd.value()) { for (String value : cmd.value()) {
if (buffer.startsWith(value) || value.startsWith(buffer)) { if (buffer.startsWith(value) || value.startsWith(buffer)) {
results.add(new Completion(value)); // no space at the end, as there's no need to continue the results.add(new Completion(value)); // no space at the end, as there's no need to continue the
// command further // command further
} }
} }
candidates.addAll(results); candidates.addAll(results);
@@ -750,7 +751,7 @@ public class SimpleParser implements Parser {
// option key/value pair) // option key/value pair)
if (lastOptionKey == null if (lastOptionKey == null
|| (!"".equals(lastOptionKey) && !"".equals(lastOptionValue) && translated.endsWith(" ") && !tokenizer || (!"".equals(lastOptionKey) && !"".equals(lastOptionValue) && translated.endsWith(" ") && !tokenizer
.openingQuotesHaveNotBeenClosed())) { .openingQuotesHaveNotBeenClosed())) {
// We have either NEVER specified an option key/value pair // We have either NEVER specified an option key/value pair
// OR we have specified a full option key/value pair // OR we have specified a full option key/value pair
@@ -1002,7 +1003,7 @@ public class SimpleParser implements Parser {
} }
public void obtainHelp( public void obtainHelp(
@CliOption(key = { "", "command" }, optionContext = "availableCommands", help = "Command name to provide help for") @CliOption(key = {"", "command"}, optionContext = "availableCommands", help = "Command name to provide help for")
String buffer) { String buffer) {
synchronized (mutex) { synchronized (mutex) {
if (buffer == null) { if (buffer == null) {

View File

@@ -37,6 +37,8 @@ import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.shell.converters.StringConverter;
import org.springframework.shell.core.annotation.CliCommand; import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption; import org.springframework.shell.core.annotation.CliOption;
import org.springframework.shell.event.ParseResult; import org.springframework.shell.event.ParseResult;
@@ -428,6 +430,17 @@ public class SimpleParserTests {
assertThat(result, nullValue(ParseResult.class)); assertThat(result, nullValue(ParseResult.class));
} }
@Test
public void testMixedCaseOptions_SHL_98() {
parser.add(new MixedCaseOptions());
parser.add(new StringConverter());
ParseResult result = parser.parse("foo --upPer bar");
assertThat(result.getMethod().getName(), equalTo("foo"));
result = parser.parse("foo --upper fizz");
assertThat(result, nullValue());
}
/** /**
* Return a matcher that asserts that a completion, when added to {@link #buffer} at the given {@link #offset}, * Return a matcher that asserts that a completion, when added to {@link #buffer} at the given {@link #offset},
* indeed matches the provided matcher. * indeed matches the provided matcher.
@@ -523,6 +536,18 @@ public class SimpleParserTests {
} }
} }
public static class MixedCaseOptions implements CommandMarker {
@CliCommand(value = "foo")
public String foo(@CliOption(key = "upPer")
String arg) {
return "foo " + arg;
}
}
public static class StringCompletions implements Converter<String> { public static class StringCompletions implements Converter<String> {
private final List<String> completions; private final List<String> completions;