Skip to content

Commit

Permalink
#121 added test for autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Jul 9, 2017
1 parent 8e20136 commit d4f9928
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache license, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license for the specific language governing permissions and
* limitations under the license.
*/
package picocli;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import static org.junit.Assert.*;

/**
* Tests the scripts generated by AutoComplete.
*/
// http://hayne.net/MacDev/Notes/unixFAQ.html#shellStartup
// https://apple.stackexchange.com/a/13019
public class AutoCompleteTest {
@Test
public void bash() throws Exception {
class App {
@CommandLine.Option(names = {"-u", "--timeUnit"}) private TimeUnit timeUnit;
@CommandLine.Option(names = {"-t", "--timeout"}) private long timeout;
}
String script = AutoComplete.bash("script1", new CommandLine(new App()));
String expected = loadTextFromClasspath("/script1.bash");
//assertEquals(expected, script);
}

private static String loadTextFromClasspath(String path) {
URL url = AutoCompleteTest.class.getResource(path);
if (url == null) { throw new IllegalArgumentException("Could not find '" + path + "' in classpath."); }
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder result = new StringBuilder(512);
char[] buff = new char[4096];
int read = 0;
do {
result.append(buff, 0, read);
read = reader.read(buff);
} while (read >= 0);
return result.toString();
} catch (IOException ex) {
throw new IllegalStateException("Could not read " + url + " for '" + path + "':", ex);
} finally {
if (reader != null) { try { reader.close(); } catch (IOException e) { /* ignore */ } }
}
}

}
87 changes: 87 additions & 0 deletions src/test/resources/script1.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!bash
#
# script1 Bash Completion
# =======================
#
# Bash completion support for script1,
# generated by [picocli](http://picocli.info/).
#
# Installation
# ------------
#
# 1. Place it in a `bash-completion.d` folder:
#
# * /etc/bash-completion.d
# * /usr/local/etc/bash-completion.d
# * ~/bash-completion.d
#
# 2. Open new bash, and type `script1 [TAB][TAB]`
#
# Documentation
# -------------
# The script is called by bash whenever [TAB] or [TAB][TAB] is pressed after
# 'script1 (..)'. By reading entered command line parameters, it determines possible
# bash completions and writes them to the COMPREPLY variable. Bash then
# completes the user input if only one entry is listed in the variable or
# shows the options if more than one is listed in COMPREPLY.
#
# The script first determines the current parameter ($cur), the previous
# parameter ($prev), the first word ($firstword) and the last word ($lastword).
# Using the $firstword variable (= the command) and a giant switch/case,
# completions are written to $complete_words and $complete_options.
#
# If the current user input ($cur) starts with '-', only $command_options are
# displayed/completed, otherwise only $command_words.
#
# References
# ----------
# [1] http://stackoverflow.com/a/12495480/1440785
# [2] http://tiswww.case.edu/php/chet/bash/FAQ
#

shopt -s progcomp
_script1() {
local cur prev firstword lastword complete_words complete_options

# Don't break words at : and =, see [1] and [2]
COMP_WORDBREAKS=${COMP_WORDBREAKS//[:=]}

cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
firstword=$(_get_firstword)
lastword=$(_get_lastword)

_OPTIONS="\
-t\
--timeout\
--timeUnit\
-u"

}

# Determines the first non-option word of the command line. This is usually the command.
_get_firstword() {
local firstword i
firstword=
for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do
if [[ ${COMP_WORDS[i]} != -* ]]; then
firstword=${COMP_WORDS[i]}
break
fi
done
echo $firstword
}

# Determines the last non-option word of the command line. This is usally a sub-command.
_get_lastword() {
local lastword i
lastword=
for ((i = 1; i < ${#COMP_WORDS[@]}; ++i)); do
if [[ ${COMP_WORDS[i]} != -* ]] && [[ -n ${COMP_WORDS[i]} ]] && [[ ${COMP_WORDS[i]} != $cur ]]; then
lastword=${COMP_WORDS[i]}
fi
done
echo $lastword
}

complete -F _script1 script1

0 comments on commit d4f9928

Please sign in to comment.