Skip to content

Commit

Permalink
Tests for #84
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jan 4, 2017
1 parent f6f2c95 commit 691e876
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2016, the original author or authors.
* Copyright (c) 2002-2017, 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.
Expand Down Expand Up @@ -212,6 +212,9 @@ public TestBuffer ctrl(char let) {
return append(KeyMap.ctrl(let));
}

public TestBuffer alt(char let) {
return append(KeyMap.alt(let));
}
public TestBuffer enter() {
return ctrl('J');
}
Expand Down
66 changes: 66 additions & 0 deletions reader/src/test/java/org/jline/reader/impl/WidgetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2002-2017, 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.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package org.jline.reader.impl;

import org.jline.keymap.KeyMap;
import org.jline.reader.LineReader;
import org.jline.reader.Reference;
import org.jline.reader.Widget;
import org.jline.utils.InputStreamReader;
import org.junit.Test;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.Reader;
import java.nio.charset.Charset;

import static org.junit.Assert.assertEquals;

public class WidgetTest extends ReaderTestSupport {

@Test
public void testCustomWidget() throws IOException {
reader.getKeyMaps().get(reader.getKeyMap())
.bind(new Reference("custom-widget"), "\n");
reader.getWidgets().put("custom-widget", () -> {
if (reader.getBuffer().cursor() == reader.getBuffer().length()) {
reader.callWidget(LineReader.ACCEPT_LINE);
} else {
reader.getBuffer().write('\n');
}
return true;
});

TestBuffer b = new TestBuffer()
.append("foo bar")
.left(3)
.enter()
.right(3)
.enter();
assertLine("foo \nbar", b, false);
}

@Test
public void testCustomWidget2() throws IOException {
reader.getKeyMaps().get(reader.getKeyMap())
.bind(new Reference(LineReader.SELF_INSERT), "\n");
reader.getKeyMaps().get(reader.getKeyMap())
.bind(new Reference(LineReader.ACCEPT_LINE), KeyMap.alt('\n'));

TestBuffer b = new TestBuffer()
.append("foo bar")
.left(3)
.enter()
.right(3)
.alt('\n');
assertLine("foo \nbar", b, false);
}

}

0 comments on commit 691e876

Please sign in to comment.