Skip to content

Commit 3d39ef3

Browse files
committed
Discard empty args
- Take 2 of #763 - Revert changes in 06e89dc what comes for `ExtendedDefaultParser`. - Discard empty args in a `Shell` coming from `ExtendedDefaultParser` - `ExtendedDefaultParserTests` has more tests, some commented out, to see some differences for jline default parser impl. Something to get handled in #517
1 parent 042d56e commit 3d39ef3

File tree

3 files changed

+77
-25
lines changed

3 files changed

+77
-25
lines changed

spring-shell-core/src/main/java/org/springframework/shell/Shell.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ protected Object evaluate(Input input) {
195195
return NO_INPUT;
196196
}
197197

198-
List<String> words = input.words();
198+
// List<String> words = input.words();
199+
// gh-763
200+
List<String> words = input.words().stream().filter(w -> w.length() > 0).collect(Collectors.toList());
199201
String line = words.stream().collect(Collectors.joining(" ")).trim();
200202
String command = findLongestCommand(line, false);
201203

spring-shell-core/src/main/java/org/springframework/shell/jline/ExtendedDefaultParser.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,10 @@ else if (!isEscapeChar(line, i)) {
126126
}
127127

128128
if (current.length() > 0 || (line != null && cursor == line.length())) {
129-
if (current.length() > 0) {
130-
words.add(current.toString());
131-
}
129+
words.add(current.toString());
132130
}
133131

134-
if (line != null && cursor == line.length() && words.size() > 0) {
132+
if (line != null && cursor == line.length()) {
135133
wordIndex = words.size() - 1;
136134
wordCursor = words.get(words.size() - 1).length();
137135
}

spring-shell-core/src/test/java/org/springframework/shell/jline/ExtendedDefaultParserTests.java

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,90 @@
1515
*/
1616
package org.springframework.shell.jline;
1717

18-
import org.junit.jupiter.api.Test;
18+
import java.util.stream.Stream;
19+
20+
import org.jline.reader.ParsedLine;
21+
import org.jline.reader.impl.DefaultParser;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.Arguments;
24+
import org.junit.jupiter.params.provider.MethodSource;
1925

2026
import static org.assertj.core.api.Assertions.assertThat;
2127

2228
public class ExtendedDefaultParserTests {
2329

24-
@Test
25-
void wordsParsing() {
26-
ExtendedDefaultParser parser = new ExtendedDefaultParser();
30+
private final ExtendedDefaultParser springParser = new ExtendedDefaultParser();
31+
private final DefaultParser jlineParser = new DefaultParser();
32+
33+
static Stream<Arguments> args() {
34+
// cursor words wordIndex wordCursor line
35+
// We've left these tests here to show issues
36+
// differences between DefaultParser and
37+
// ExtendedDefaultParser. relates gh517
38+
return Stream.of(
39+
Arguments.of(0, 1, 0, 0, "one"),
40+
Arguments.of(3, 1, 0, 3, "one "),
41+
Arguments.of(4, 2, 1, 0, "one "),
2742

28-
assertThat(parser.parse("one", 0).words()).hasSize(1);
29-
assertThat(parser.parse("one", 3).words()).hasSize(1);
43+
Arguments.of(0, 2, 0, 0, "one two"),
44+
Arguments.of(1, 2, 0, 1, "one two"),
45+
Arguments.of(2, 2, 0, 2, "one two"),
46+
Arguments.of(3, 2, 0, 3, "one two"),
47+
Arguments.of(7, 2, 1, 3, "one two"),
48+
Arguments.of(7, 2, 1, 3, "one two "),
49+
Arguments.of(8, 3, 2, 0, "one two "),
3050

31-
assertThat(parser.parse("one two", 0).words()).hasSize(2);
32-
assertThat(parser.parse("one two", 7).words()).hasSize(2);
51+
Arguments.of(0, 1, 0, 0, "'one'"),
52+
// Arguments.of(5, 1, 0, 3, "'one' "),
53+
Arguments.of(6, 2, 1, 0, "'one' "),
3354

34-
assertThat(parser.parse("'one'", 0).words()).hasSize(1);
35-
assertThat(parser.parse("'one'", 5).words()).hasSize(1);
55+
Arguments.of(0, 1, 0, 0, "'one'"),
56+
Arguments.of(1, 1, 0, 0, "'one'"),
57+
Arguments.of(2, 1, 0, 1, "'one'"),
58+
Arguments.of(3, 1, 0, 2, "'one'"),
59+
Arguments.of(4, 1, 0, 3, "'one'"),
60+
// Arguments.of(5, 1, 0, 3, "'one'"),
3661

37-
assertThat(parser.parse("'one' two", 0).words()).hasSize(2);
38-
assertThat(parser.parse("'one' two", 9).words()).hasSize(2);
62+
Arguments.of(0, 1, 0, 0, "'one' "),
63+
Arguments.of(1, 1, 0, 0, "'one' "),
64+
Arguments.of(2, 1, 0, 1, "'one' "),
65+
Arguments.of(3, 1, 0, 2, "'one' "),
66+
Arguments.of(4, 1, 0, 3, "'one' "),
67+
// Arguments.of(5, 1, 0, 3, "'one' "),
68+
Arguments.of(6, 2, 1, 0, "'one' "),
3969

40-
assertThat(parser.parse("one 'two'", 0).words()).hasSize(2);
41-
assertThat(parser.parse("one 'two'", 9).words()).hasSize(2);
70+
Arguments.of(0, 1, 0, 0, "\"one\""),
71+
Arguments.of(1, 1, 0, 0, "\"one\""),
72+
Arguments.of(2, 1, 0, 1, "\"one\""),
73+
Arguments.of(3, 1, 0, 2, "\"one\""),
74+
Arguments.of(4, 1, 0, 3, "\"one\""),
75+
// Arguments.of(5, 1, 0, 3, "\"one\""),
4276

43-
assertThat(parser.parse("\"one\"", 0).words()).hasSize(1);
44-
assertThat(parser.parse("\"one\"", 5).words()).hasSize(1);
77+
Arguments.of(0, 1, 0, 0, "\"one\" "),
78+
Arguments.of(1, 1, 0, 0, "\"one\" "),
79+
Arguments.of(2, 1, 0, 1, "\"one\" "),
80+
Arguments.of(3, 1, 0, 2, "\"one\" "),
81+
Arguments.of(4, 1, 0, 3, "\"one\" "),
82+
// Arguments.of(5, 1, 0, 3, "\"one\" "),
83+
Arguments.of(6, 2, 1, 0, "\"one\" ")
84+
);
85+
}
4586

46-
assertThat(parser.parse("\"one\" two", 0).words()).hasSize(2);
47-
assertThat(parser.parse("\"one\" two", 9).words()).hasSize(2);
87+
@ParameterizedTest
88+
@MethodSource("args")
89+
void testSpringExtendedDefaultParser(int cursor, int words, int wordIndex, int wordCursor, String line) {
90+
ParsedLine parse = springParser.parse(line, cursor);
91+
assertThat(parse.words()).as("words").hasSize(words);
92+
assertThat(parse.wordIndex()).as("wordIndex").isEqualTo(wordIndex);
93+
assertThat(parse.wordCursor()).as("wordCursor").isEqualTo(wordCursor);
94+
}
4895

49-
assertThat(parser.parse("one \"two\"", 0).words()).hasSize(2);
50-
assertThat(parser.parse("one \"two\"", 9).words()).hasSize(2);
96+
@ParameterizedTest
97+
@MethodSource("args")
98+
void testJlineDefaultParser(int cursor, int words, int wordIndex, int wordCursor, String line) {
99+
ParsedLine parse = jlineParser.parse(line, cursor);
100+
assertThat(parse.words()).as("words").hasSize(words);
101+
assertThat(parse.wordIndex()).as("wordIndex").isEqualTo(wordIndex);
102+
assertThat(parse.wordCursor()).as("wordCursor").isEqualTo(wordCursor);
51103
}
52104
}

0 commit comments

Comments
 (0)