Skip to content

Commit

Permalink
Added a few tests for CompletionMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 18, 2021
1 parent 74c97a2 commit 31229b0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2020, the original author or authors.
* Copyright (c) 2002-2021, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -4521,7 +4521,7 @@ else if (isSet(Option.RECOGNIZE_EXACT)) {
}
}

private CompletingParsedLine wrap(ParsedLine line) {
protected static CompletingParsedLine wrap(ParsedLine line) {
if (line instanceof CompletingParsedLine) {
return (CompletingParsedLine) line;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2002-2021, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.reader.impl;

import org.jline.reader.*;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class CompletionMatcherTest {

private CompletionMatcher compileCompletionMatcher(String line) {
CompletionMatcher completionMatcher = new CompletionMatcherImpl();
Parser parser = new DefaultParser();
completionMatcher.compile(new HashMap<>(), false, LineReaderImpl.wrap(parser.parse(line, line.length()))
, true, 0, "");
return completionMatcher;
}

@Test
public void uniqueCandidates() {
Candidate c = new Candidate("foo");
assertEquals("Expected only one element", 1, compileCompletionMatcher("").matches(Arrays.asList(c, c)).size());
}

@Test
public void test() {
List<Candidate> candidates = Arrays.asList(new Candidate("foo"), new Candidate("foobar"), new Candidate("bar"));
CompletionMatcher completionMatcher = compileCompletionMatcher("foo");
List<Candidate> matches = completionMatcher.matches(candidates);
assertEquals("Number of matches", 2, matches.size());
Candidate candidate = completionMatcher.exactMatch();
assertEquals("Exact match", "foo", (candidate != null ? candidate.value() : null));
assertEquals("Common prefix", "foo", completionMatcher.getCommonPrefix());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2020, the original author or authors.
* Copyright (c) 2002-2021, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand All @@ -12,12 +12,8 @@
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import static org.junit.Assert.assertEquals;


public class CompletionWithCustomMatcherTest extends ReaderTestSupport {

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

0 comments on commit 31229b0

Please sign in to comment.