|
| 1 | +package org.aesh.command.help; |
| 2 | + |
| 3 | +import org.aesh.command.Command; |
| 4 | +import org.aesh.command.CommandDefinition; |
| 5 | +import org.aesh.command.CommandException; |
| 6 | +import org.aesh.command.CommandResult; |
| 7 | +import org.aesh.command.activator.CommandActivator; |
| 8 | +import org.aesh.command.activator.OptionActivator; |
| 9 | +import org.aesh.command.completer.CompleterInvocation; |
| 10 | +import org.aesh.command.converter.ConverterInvocation; |
| 11 | +import org.aesh.command.impl.registry.AeshCommandRegistryBuilder; |
| 12 | +import org.aesh.command.invocation.CommandInvocation; |
| 13 | +import org.aesh.command.option.Option; |
| 14 | +import org.aesh.command.registry.CommandRegistry; |
| 15 | +import org.aesh.command.registry.CommandRegistryException; |
| 16 | +import org.aesh.command.settings.Settings; |
| 17 | +import org.aesh.command.settings.SettingsBuilder; |
| 18 | +import org.aesh.command.validator.ValidatorInvocation; |
| 19 | +import org.aesh.readline.ReadlineConsole; |
| 20 | +import org.aesh.terminal.utils.Config; |
| 21 | +import org.aesh.tty.TestConnection; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +public class AeshHelpCommandTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testCommandInvocationTest() throws IOException, InterruptedException, CommandRegistryException { |
| 30 | + TestConnection connection = new TestConnection(); |
| 31 | + |
| 32 | + CommandRegistry registry = AeshCommandRegistryBuilder.builder() |
| 33 | + .command(FooCommand.class) |
| 34 | + .command(BarCommand.class) |
| 35 | + .command(FooBarCommand.class) |
| 36 | + .create(); |
| 37 | + |
| 38 | + Settings<CommandInvocation, ConverterInvocation, CompleterInvocation, ValidatorInvocation, |
| 39 | + OptionActivator, CommandActivator> settings = |
| 40 | + SettingsBuilder.builder() |
| 41 | + .commandRegistry(registry) |
| 42 | + .enableOperatorParser(true) |
| 43 | + .connection(connection) |
| 44 | + .setPersistExport(false) |
| 45 | + .logging(true) |
| 46 | + .build(); |
| 47 | + |
| 48 | + ReadlineConsole console = new ReadlineConsole(settings); |
| 49 | + console.start(); |
| 50 | + connection.read("foo -h" + Config.getLineSeparator()); |
| 51 | + Thread.sleep(100); |
| 52 | + connection.assertBufferEndsWith("--ask ask me"+Config.getLineSeparator()+Config.getLineSeparator()); |
| 53 | + connection.clearOutputBuffer(); |
| 54 | + connection.read("bar -h" + Config.getLineSeparator()); |
| 55 | + Thread.sleep(100); |
| 56 | + connection.assertBufferEndsWith("--ask ask me"+Config.getLineSeparator()+Config.getLineSeparator()); |
| 57 | + connection.read("foobar -h" + Config.getLineSeparator()); |
| 58 | + Thread.sleep(100); |
| 59 | + connection.assertBufferEndsWith("--ask ask me"+Config.getLineSeparator()+Config.getLineSeparator()); |
| 60 | + |
| 61 | + console.stop(); |
| 62 | + } |
| 63 | + |
| 64 | + @CommandDefinition(name ="foo", generateHelp = true, description = "") |
| 65 | + private static class FooCommand implements Command { |
| 66 | + |
| 67 | + @Option(description = "my value") |
| 68 | + private String value; |
| 69 | + |
| 70 | + @Option(description = "ask me" ) |
| 71 | + private String ask; |
| 72 | + |
| 73 | + @Override |
| 74 | + public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { |
| 75 | + return CommandResult.SUCCESS; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @CommandDefinition(name ="bar", generateHelp = true, description = "") |
| 80 | + private static class BarCommand implements Command { |
| 81 | + |
| 82 | + @Option(description = "my value") |
| 83 | + private String value; |
| 84 | + |
| 85 | + @Option(description = "ask me", askIfNotSet = true) |
| 86 | + private String ask; |
| 87 | + |
| 88 | + @Override |
| 89 | + public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { |
| 90 | + return CommandResult.SUCCESS; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @CommandDefinition(name ="foobar", generateHelp = true, description = "") |
| 95 | + private static class FooBarCommand implements Command { |
| 96 | + |
| 97 | + @Option(description = "my value") |
| 98 | + private String value; |
| 99 | + |
| 100 | + @Option(description = "ask me", required = true) |
| 101 | + private String ask; |
| 102 | + |
| 103 | + @Override |
| 104 | + public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { |
| 105 | + return CommandResult.SUCCESS; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments