Skip to content

Commit 31229b0

Browse files
committed
Added a few tests for CompletionMatcher
1 parent 74c97a2 commit 31229b0

3 files changed

Lines changed: 49 additions & 9 deletions

File tree

reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002-2020, the original author or authors.
2+
* Copyright (c) 2002-2021, the original author or authors.
33
*
44
* This software is distributable under the BSD license. See the terms of the
55
* BSD license in the documentation provided with this software.
@@ -4521,7 +4521,7 @@ else if (isSet(Option.RECOGNIZE_EXACT)) {
45214521
}
45224522
}
45234523

4524-
private CompletingParsedLine wrap(ParsedLine line) {
4524+
protected static CompletingParsedLine wrap(ParsedLine line) {
45254525
if (line instanceof CompletingParsedLine) {
45264526
return (CompletingParsedLine) line;
45274527
} else {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2002-2021, the original author or authors.
3+
*
4+
* This software is distributable under the BSD license. See the terms of the
5+
* BSD license in the documentation provided with this software.
6+
*
7+
* https://opensource.org/licenses/BSD-3-Clause
8+
*/
9+
package org.jline.reader.impl;
10+
11+
import org.jline.reader.*;
12+
import org.junit.Test;
13+
14+
import java.util.Arrays;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
public class CompletionMatcherTest {
21+
22+
private CompletionMatcher compileCompletionMatcher(String line) {
23+
CompletionMatcher completionMatcher = new CompletionMatcherImpl();
24+
Parser parser = new DefaultParser();
25+
completionMatcher.compile(new HashMap<>(), false, LineReaderImpl.wrap(parser.parse(line, line.length()))
26+
, true, 0, "");
27+
return completionMatcher;
28+
}
29+
30+
@Test
31+
public void uniqueCandidates() {
32+
Candidate c = new Candidate("foo");
33+
assertEquals("Expected only one element", 1, compileCompletionMatcher("").matches(Arrays.asList(c, c)).size());
34+
}
35+
36+
@Test
37+
public void test() {
38+
List<Candidate> candidates = Arrays.asList(new Candidate("foo"), new Candidate("foobar"), new Candidate("bar"));
39+
CompletionMatcher completionMatcher = compileCompletionMatcher("foo");
40+
List<Candidate> matches = completionMatcher.matches(candidates);
41+
assertEquals("Number of matches", 2, matches.size());
42+
Candidate candidate = completionMatcher.exactMatch();
43+
assertEquals("Exact match", "foo", (candidate != null ? candidate.value() : null));
44+
assertEquals("Common prefix", "foo", completionMatcher.getCommonPrefix());
45+
}
46+
}

reader/src/test/java/org/jline/reader/impl/CompletionWithCustomMatcherTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002-2020, the original author or authors.
2+
* Copyright (c) 2002-2021, the original author or authors.
33
*
44
* This software is distributable under the BSD license. See the terms of the
55
* BSD license in the documentation provided with this software.
@@ -12,12 +12,8 @@
1212
import org.junit.Test;
1313

1414
import java.io.IOException;
15-
import java.util.Arrays;
1615
import java.util.Map;
1716

18-
import static org.junit.Assert.assertEquals;
19-
20-
2117
public class CompletionWithCustomMatcherTest extends ReaderTestSupport {
2218

2319
@Test
@@ -33,8 +29,6 @@ public void compile(Map<LineReader.Option, Boolean> options, boolean prefix, Com
3329
// add custom matcher before typo matcher
3430
int pos = matchers.size() + (LineReader.Option.COMPLETE_MATCHER_TYPO.isSet(options) ? -1 : 0);
3531
matchers.add(pos, simpleMatcher(candidate -> camelMatch(line.word(), 0, candidate, 0)));
36-
Candidate c = new Candidate(line.word());
37-
assertEquals("Expected only one element", 1, matches(Arrays.asList(c, c)).size());
3832
}
3933
}
4034
});

0 commit comments

Comments
 (0)