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

4
.gitignore vendored
View File

@@ -10,4 +10,6 @@ target/
/*.log
/.idea
/*.iml
/*.iml
/*.ipr
/*.iws

View File

@@ -45,7 +45,7 @@ import org.springframework.util.StringUtils;
/**
* Default implementation of {@link Parser}.
*
*
* @author Ben Alex
* @since 1.0
*/
@@ -81,7 +81,7 @@ public class SimpleParser implements Parser {
/**
* get all mandatory options keys. For the options with multiple keys, the keys will be in one row.
*
*
* @param cliOptions options
* @return mandatory options keys
*/
@@ -91,7 +91,7 @@ public class SimpleParser implements Parser {
/**
* get all options key.
*
*
* @param cliOptions
* @param includeOptionalOptions
* @return options keys
@@ -393,7 +393,7 @@ public class SimpleParser implements Parser {
/**
* Normalises the given raw user input string ready for parsing
*
*
* @param rawInput the string to normalise; can't be <code>null</code>
* @return a non-<code>null</code> string
*/
@@ -407,12 +407,12 @@ public class SimpleParser implements Parser {
Set<String> cliOptionKeySet = new LinkedHashSet<String>();
for (CliOption cliOption : cliOptions) {
for (String key : cliOption.key()) {
cliOptionKeySet.add(key.toLowerCase());
cliOptionKeySet.add(key);
}
}
Set<String> unavailableOptions = new LinkedHashSet<String>();
for (String suppliedOption : options.keySet()) {
if (!cliOptionKeySet.contains(suppliedOption.toLowerCase())) {
if (!cliOptionKeySet.contains(suppliedOption)) {
unavailableOptions.add(suppliedOption);
}
}
@@ -499,7 +499,8 @@ public class SimpleParser implements Parser {
String bufferToReturn = 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 bufferRemaining = buffer.substring(bufferIndex + 1);
@@ -649,7 +650,7 @@ public class SimpleParser implements Parser {
for (String value : cmd.value()) {
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
// command further
// command further
}
}
candidates.addAll(results);
@@ -750,7 +751,7 @@ public class SimpleParser implements Parser {
// option key/value pair)
if (lastOptionKey == null
|| (!"".equals(lastOptionKey) && !"".equals(lastOptionValue) && translated.endsWith(" ") && !tokenizer
.openingQuotesHaveNotBeenClosed())) {
.openingQuotesHaveNotBeenClosed())) {
// We have either NEVER specified an option key/value pair
// OR we have specified a full option key/value pair
@@ -978,7 +979,7 @@ public class SimpleParser implements Parser {
/**
* populate completion for mandatory options
*
*
* @param translated user's input
* @param unspecified unspecified options
* @param value the option key
@@ -1002,7 +1003,7 @@ public class SimpleParser implements Parser {
}
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) {
synchronized (mutex) {
if (buffer == null) {

View File

@@ -37,6 +37,8 @@ import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.shell.converters.StringConverter;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.shell.event.ParseResult;
@@ -428,6 +430,17 @@ public class SimpleParserTests {
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},
* 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> {
private final List<String> completions;