-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #377 from snuyanzin/JLINE3_376
Use History interface to navigate thgrough history, add a test indica…
- Loading branch information
Showing
2 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
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
67 changes: 67 additions & 0 deletions
67
builtins/src/test/java/org/jline/builtins/CommandsTest.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,67 @@ | ||
/* | ||
* Copyright (c) 2002-2018, the original author or authors. | ||
* | ||
* This software is distributable under the BSD license. See the terms of the | ||
* BSD license in the documentation provided with this software. | ||
* | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
package org.jline.builtins; | ||
|
||
import org.jline.reader.History; | ||
import org.jline.reader.LineReader; | ||
import org.jline.reader.LineReaderBuilder; | ||
import org.jline.reader.impl.history.DefaultHistory; | ||
import org.jline.terminal.Size; | ||
import org.jline.terminal.Terminal; | ||
import org.jline.terminal.TerminalBuilder; | ||
import org.junit.Test; | ||
|
||
import java.io.*; | ||
import java.nio.file.Files; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class CommandsTest { | ||
@Test | ||
public void testHistoryForFileWithMoreHistoryRecordsThanAtHistoryFileSize() { | ||
try { | ||
final ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
final File tmpHistoryFile = Files.createTempFile("tmpHistory", "temp").toFile(); | ||
tmpHistoryFile.deleteOnExit(); | ||
try (BufferedWriter bw = | ||
new BufferedWriter( | ||
new OutputStreamWriter( | ||
new FileOutputStream(tmpHistoryFile)))) { | ||
bw.write("1536743099591:SELECT \\n CURRENT_TIMESTAMP \\n as \\n c1;\n" | ||
+ "1536743104551:SELECT \\n 'asd' as \"sdf\", 4 \\n \\n as \\n c2;\\n\n" | ||
+ "1536743104551:SELECT \\n 'asd' \\n as \\n c2;\\n\n" | ||
+ "1536743104551:!/ 2\n" | ||
+ "1536743104551:SELECT \\n 2123 \\n as \\n c2 from dual;\\n\n" | ||
+ "1536743107526:!history\n" | ||
+ "1536743115431:SELECT \\n 2 \\n as \\n c2;\n" | ||
+ "1536743115431:SELECT \\n '213' \\n as \\n c1;\n" | ||
+ "1536743115431:!/ 8\n"); | ||
bw.flush(); | ||
} | ||
Terminal terminal = TerminalBuilder.builder().streams(System.in, System.out).build(); | ||
terminal.setSize(new Size(50, 30)); | ||
final History historyFromFile = new DefaultHistory(); | ||
final LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder() | ||
.terminal(terminal) | ||
.variable(LineReader.HISTORY_FILE, tmpHistoryFile.getAbsolutePath()) | ||
.history(historyFromFile); | ||
final LineReader lineReader = lineReaderBuilder.build(); | ||
historyFromFile.attach(lineReader); | ||
final int maxLines = 3; | ||
lineReader.setVariable(LineReader.HISTORY_FILE_SIZE, maxLines); | ||
lineReader.getHistory().save(); | ||
PrintStream out = new PrintStream(os, false); | ||
Commands.history(lineReader, out, out, new String[] {"-d"}); | ||
assertEquals(maxLines + 1, | ||
os.toString("UTF8").split("\\s+\\d{2}:\\d{2}:\\d{2}\\s+").length); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Test failed", e); | ||
} | ||
} | ||
} |