-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
cli/base/src/main/java/de/cotto/bitbook/cli/TerminalSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package de.cotto.bitbook.cli; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.jline.reader.LineReader; | ||
import org.jline.terminal.Terminal; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.PrintWriter; | ||
import java.util.Optional; | ||
|
||
@Component | ||
public class TerminalSupport { | ||
private final LineReader lineReader; | ||
private final PromptChangeListener promptChangeListener; | ||
private final PrintWriter writer; | ||
|
||
public TerminalSupport(Terminal terminal, LineReader lineReader, PromptChangeListener promptChangeListener) { | ||
this.lineReader = lineReader; | ||
this.promptChangeListener = promptChangeListener; | ||
writer = terminal.writer(); | ||
} | ||
|
||
public void write(String text) { | ||
writer.println(text); | ||
writer.flush(); | ||
} | ||
|
||
public Optional<String> request(String prompt) { | ||
String answer = lineReader.readLine(prompt + " "); | ||
if (StringUtils.isBlank(answer)) { | ||
return Optional.empty(); | ||
} else { | ||
return Optional.of(answer); | ||
} | ||
} | ||
|
||
public void changePrompt(String newPrompt) { | ||
promptChangeListener.changePrompt(newPrompt); | ||
} | ||
|
||
public void changePromptToDefault() { | ||
promptChangeListener.changePromptToDefault(); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
cli/base/src/test/java/de/cotto/bitbook/cli/TerminalSupportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package de.cotto.bitbook.cli; | ||
|
||
import org.jline.reader.LineReader; | ||
import org.jline.terminal.Terminal; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InOrder; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.io.PrintWriter; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class TerminalSupportTest { | ||
private TerminalSupport terminalSupport; | ||
|
||
@Mock | ||
private Terminal terminal; | ||
|
||
@Mock | ||
private LineReader lineReader; | ||
|
||
@Mock | ||
private PromptChangeListener promptChangeListener; | ||
|
||
@Mock | ||
private PrintWriter writer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
when(terminal.writer()).thenReturn(writer); | ||
terminalSupport = new TerminalSupport(terminal, lineReader, promptChangeListener); | ||
} | ||
|
||
@Test | ||
void write() { | ||
terminalSupport.write("x"); | ||
InOrder inOrder = Mockito.inOrder(writer); | ||
inOrder.verify(writer).println("x"); | ||
inOrder.verify(writer).flush(); | ||
} | ||
|
||
@Test | ||
void request_prompt() { | ||
terminalSupport.request("?"); | ||
verify(lineReader).readLine("? "); | ||
} | ||
|
||
@Test | ||
void request_empty() { | ||
when(lineReader.readLine(anyString())).thenReturn(""); | ||
assertThat(terminalSupport.request("?")).isEmpty(); | ||
} | ||
|
||
@Test | ||
void request_blank() { | ||
when(lineReader.readLine(anyString())).thenReturn("\t \r\n"); | ||
assertThat(terminalSupport.request("?")).isEmpty(); | ||
} | ||
|
||
@Test | ||
void request() { | ||
when(lineReader.readLine(anyString())).thenReturn("hello"); | ||
assertThat(terminalSupport.request("?")).contains("hello"); | ||
} | ||
|
||
@Test | ||
void changePrompt() { | ||
terminalSupport.changePrompt("x"); | ||
verify(promptChangeListener).changePrompt("x"); | ||
} | ||
|
||
@Test | ||
void changePromptToDefault() { | ||
terminalSupport.changePromptToDefault(); | ||
verify(promptChangeListener).changePromptToDefault(); | ||
} | ||
} |