@Test
public void testCompletion() throws Exception {

    TestConnection connection = new TestConnection(false);
    // Build dynamic command.
    DynCommand1 cmd = new DynCommand1();
    DynamicOptionsProvider provider = new DynamicOptionsProvider();
    MapProcessedCommandBuilder builder = MapProcessedCommandBuilder.builder();
    builder.command(cmd);
    // Retrieve dynamic options during completion.
    builder.lookupAtCompletionOnly(true);
    builder.name("dyn1");
    builder.optionProvider(provider);
    CommandRegistry registry = AeshCommandRegistryBuilder.builder()
            .command(builder.create())
            .create();
    Settings settings = SettingsBuilder.builder()
            .logging(true)
            .connection(connection)
            .commandRegistry(registry)
            .build();
    ReadlineConsole console = new ReadlineConsole(settings);
    console.setPrompt(new Prompt(""));
    console.start();
    // First test without any dynamic option provided.
    connection.clearOutputBuffer();
    connection.read("d");
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 ", connection.getOutputBuffer());
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 ", connection.getOutputBuffer());
    connection.read("--");
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 --", connection.getOutputBuffer());
    // Then add dynamic options
    provider.options = getOptions();
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 --opt-dyn", connection.getOutputBuffer());
    connection.read("1");
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 --opt-dyn1-withvalue=", connection.getOutputBuffer());
    connection.read("cdcsdc ");
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 --opt-dyn1-withvalue=cdcsdc --opt-dyn", connection.getOutputBuffer());
    connection.read("2");
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 --opt-dyn1-withvalue=cdcsdc --opt-dyn2-withvalue=", connection.getOutputBuffer());
    connection.read("xxx ");
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 --opt-dyn1-withvalue=cdcsdc --opt-dyn2-withvalue=xxx --opt-dyn3-novalue ", connection.getOutputBuffer());
    // No completion if the options already exist in the buffer.
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 --opt-dyn1-withvalue=cdcsdc --opt-dyn2-withvalue=xxx --opt-dyn3-novalue ", connection.getOutputBuffer());
    // Execute command.
    connection.read(Config.getLineSeparator());
    connection.clearOutputBuffer();
    Thread.sleep(200);
    {
        String val = (String) cmd.options.get("opt-dyn1-withvalue");
        assertEquals("cdcsdc", val);
    }
    {
        String val = (String) cmd.options.get("opt-dyn2-withvalue");
        assertEquals("xxx", val);
    }
    assertTrue(cmd.contains("opt-dyn3-novalue"));
    // Invalid option
    connection.read("dyn1 --opt-dyn3-novalue--");
    connection.read(completeChar.getFirstValue());
    assertEquals("dyn1 --opt-dyn3-novalue--", connection.getOutputBuffer());
}