Skip to content

Commit

Permalink
implemented issue #28 - Replace beanshell standard help system with a…
Browse files Browse the repository at this point in the history
… more extensible help system
  • Loading branch information
stefanofornari committed Dec 5, 2018
1 parent e992b76 commit 17c400c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion changeslog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20181130
20181205
--------
- implemented issue #28 - Replace beanshell standard help system with a more extensible help system

Expand Down
3 changes: 2 additions & 1 deletion src/main/bshell/help/bsh/commands/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ help() shows an output similar to:
help(name)
----------

A variation of the help() that displays all help files ending with {name}.txt
A variation of the help() that displays all help files named {name}.txt
(regardless the subdir they are in).

For example, given bshell.help set to /opt/bshell/help and the following
directory structure
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/ste/bshell/commands/help.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public class help {

public static final String HELP = "bshell.help";

public static void invoke(final Interpreter bsh, CallStack callstack)
public static void invoke(final Interpreter bsh, final CallStack callstack)
throws EvalError {
invoke(bsh, callstack, null);
}

public static void invoke(final Interpreter bsh, final CallStack callstack, final String name)
throws EvalError {
if (bsh.get(HELP) == null) {
throw new EvalError("please set bsh.help to an existing and readable directory", null, callstack);
Expand All @@ -56,7 +61,8 @@ public static void invoke(final Interpreter bsh, CallStack callstack)
.filter(new Predicate<Path>() {
@Override
public boolean test(Path p) {
return p.toString().endsWith(".txt");
return (name == null) ? p.toString().endsWith(".txt")
: String.valueOf(p.getFileName()).equals(name + ".txt");
}
}).forEach(new Consumer<Path>() {
@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/init.bsh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
importCommands("ste/bshell/commands");

printBanner() {
print("Welcome to bshell ${version}\n");
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/bshell/help/org/cls.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cls()
-----

description for cls
40 changes: 28 additions & 12 deletions src/test/java/ste/bshell/commands/BugFree_help.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,14 @@ public void no_data_prints_all_command_help() throws Exception {
bsh.set("bshell.help", "src/test/bshell/help");
help.invoke(bsh, null);
then(STDOUT.getLog())
.contains("load\n")
.contains("\n====\n")
.contains("load(filename)\n")
.contains("\n--------------")
.contains("load\n====\n")
.contains("load(filename)\n--------------")
.contains("description for load")
.contains("com.cls\n")
.contains("\n=======\n")
.contains("cls()")
.contains("\n-----")
.contains("com.cls\n=======\n")
.contains("cls()\n-----")
.contains("description for cls")
.contains("com.acme.app.filter\n")
.contains("\n===================\n")
.contains("filter(dataset, column, value)")
.contains("\n------------------------------")
.contains("com.acme.app.filter\n===================\n")
.contains("filter(dataset, column, value)\n------------------------------")
.contains("description for load");
}

Expand Down Expand Up @@ -101,4 +95,26 @@ public void error_if_help_dir_is_not_valid() throws Exception {
}
}

@Test
public void not_existing_command_name() throws Exception {
final Interpreter bsh = new Interpreter();
bsh.set("bshell.help", "src/test/bshell/help");
help.invoke(bsh, null, "none");
then(STDOUT.getLog()).isEmpty();
}

@Test
public void existing_command_name() throws Exception {
final Interpreter bsh = new Interpreter();
bsh.set("bshell.help", "src/test/bshell/help");
help.invoke(bsh, null, "filter");
then(STDOUT.getLog())
.contains("com.acme.app.filter\n===================\n");

STDOUT.clearLog();
help.invoke(bsh, null, "cls");
then(STDOUT.getLog())
.contains("com.cls\n=======\n")
.contains("\n\norg.cls\n=======\n");
}
}

0 comments on commit 17c400c

Please sign in to comment.