From 0ca3be8ca2ca1f4dba05fec81fbd0e96e13e168d Mon Sep 17 00:00:00 2001 From: Eric Bottard Date: Fri, 7 Feb 2014 12:47:10 +0100 Subject: [PATCH] Add failing tests --- .../shell/core/SimpleParser.java | 4 + .../shell/core/SimpleParserTests.java | 74 ++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/shell/core/SimpleParser.java b/src/main/java/org/springframework/shell/core/SimpleParser.java index 805e71f6..6bc93d84 100644 --- a/src/main/java/org/springframework/shell/core/SimpleParser.java +++ b/src/main/java/org/springframework/shell/core/SimpleParser.java @@ -434,6 +434,10 @@ public class SimpleParser implements Parser { return result; } + /** + * See whether 'buffer' could be an invocation of 'command', and if so, return the remaining part of the buffer. + * @param strictMatching true if ALL words of 'command' need to be matched + */ static String isMatch(final String buffer, final String command, final boolean strictMatching) { if ("".equals(buffer.trim())) { return ""; diff --git a/src/test/java/org/springframework/shell/core/SimpleParserTests.java b/src/test/java/org/springframework/shell/core/SimpleParserTests.java index cd4ed881..556bed91 100644 --- a/src/test/java/org/springframework/shell/core/SimpleParserTests.java +++ b/src/test/java/org/springframework/shell/core/SimpleParserTests.java @@ -23,6 +23,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertThat; @@ -60,7 +61,7 @@ public class SimpleParserTests { buffer = "f"; offset = parser.completeAdvanced(buffer, buffer.length(), candidates); - assertThat(candidates, hasItem(completionThat(is(equalTo("foo"))))); + assertThat(candidates, hasItem(completionThat(is(equalTo("foo "))))); } @Test @@ -266,6 +267,65 @@ public class SimpleParserTests { } + @Test + public void testAtEndOfKeyOption() { + parser.add(new MyCommands()); + parser.add(new StringCompletions(Arrays.asList("abd", "def"))); + + buffer = "fileMore"; + offset = parser.completeAdvanced(buffer, buffer.length(), candidates); + + assertThat(candidates, hasItem(completionThat(is(equalTo("fileMore --option "))))); + + // Do it again with a trailing space + buffer = "fileMore "; + candidates.clear(); + offset = parser.completeAdvanced(buffer, buffer.length(), candidates); + + assertThat(candidates, hasItem(completionThat(is(equalTo("fileMore --option "))))); + + } + + @Test + public void testAtEndOfKeyOptionWithEvenLongerKeyOption() { + parser.add(new MyCommands()); + parser.add(new StringCompletions(Arrays.asList("abd", "def"))); + + buffer = "file"; + offset = parser.completeAdvanced(buffer, buffer.length(), candidates); + + assertThat(candidates, hasItem(completionThat(is(equalTo("file "))))); + assertThat(candidates, hasItem(completionThat(is(equalTo("fileMore "))))); + + // Do it again with a trailing space + buffer = "file "; + candidates.clear(); + offset = parser.completeAdvanced(buffer, buffer.length(), candidates); + + // TODO + // assertThat(candidates, hasItem(completionThat(is(equalTo("file --option"))))); + // assertThat(candidates, not(hasItem(completionThat(startsWith("fileMore"))))); + + } + + @Test + public void testPrefixMatching() { + assertThat(SimpleParser.isMatch("hello", "hello", true), is(equalTo(""))); + assertThat(SimpleParser.isMatch("hello there", "hello", true), is(equalTo("there"))); + assertThat(SimpleParser.isMatch("hello there", "hello there", true), is(equalTo(""))); + assertThat(SimpleParser.isMatch("hell", "hello", true), is(equalTo(""))); + + assertThat(SimpleParser.isMatch("hello", "hello there", true), is(nullValue())); + assertThat(SimpleParser.isMatch("hello", "hello there", false), is(equalTo(""))); + + assertThat(SimpleParser.isMatch("hi", "hello", true), is(nullValue())); + + assertThat(SimpleParser.isMatch("hello", "hellothere", false), is(equalTo(""))); + assertThat(SimpleParser.isMatch("hello", "hellothere", true), is(equalTo(""))); + // TODO + // assertThat(SimpleParser.isMatch("hello ", "hellothere", true), is(nullValue())); + } + /** * Return a matcher that asserts that a completion, when added to {@link #buffer} at the given {@link #offset}, * indeed matches the provided matcher. @@ -332,6 +392,18 @@ public class SimpleParserTests { String option3) { } + + @CliCommand("file") + public void file(@CliOption(key = "option", mandatory = true) + String option) { + + } + + @CliCommand("fileMore") + public void fileMore(@CliOption(key = "option", mandatory = true) + String option) { + + } } public static class StringCompletions implements Converter {