-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When adding spaces in place of tabs, calculate from start of line
Maintain the length of the last line in AttributedStringBuilder so that future appends also calculate tab size correctly. If there are line breaks in a string, a tab character should add spaces up until the next tabstop from the beginning of the line not from the start of the string.
- Loading branch information
Showing
2 changed files
with
134 additions
and
3 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
105 changes: 105 additions & 0 deletions
105
terminal/src/test/java/org/jline/utils/AttributedStringBuilderTest.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,105 @@ | ||
package org.jline.utils; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class AttributedStringBuilderTest { | ||
private static String TAB_SIZE_ERR_MSG = "Incorrect tab size"; | ||
|
||
/** | ||
* Test single line with tabs in | ||
*/ | ||
@Test | ||
public void testTabSize() { | ||
AttributedStringBuilder sb; | ||
sb = new AttributedStringBuilder().tabs(4); | ||
sb.append("hello\tWorld"); | ||
assertEquals(TAB_SIZE_ERR_MSG, "hello World", sb.toString()); | ||
|
||
sb = new AttributedStringBuilder().tabs(5); | ||
sb.append("hello\tWorld"); | ||
assertEquals(TAB_SIZE_ERR_MSG, "hello World", sb.toString()); | ||
} | ||
|
||
/** | ||
* Test multiple lines with tabs in | ||
*/ | ||
@Test | ||
public void testSplitLineTabSize() { | ||
AttributedStringBuilder sb; | ||
sb = new AttributedStringBuilder().tabs(4); | ||
sb.append("hello\n\tWorld"); | ||
assertEquals(TAB_SIZE_ERR_MSG, "hello\n World", sb.toString()); | ||
|
||
sb = new AttributedStringBuilder().tabs(4); | ||
sb.append("hello\tWorld\n\tfoo\tbar"); | ||
assertEquals(TAB_SIZE_ERR_MSG, "hello World\n foo bar", sb.toString()); | ||
|
||
sb = new AttributedStringBuilder().tabs(5); | ||
sb.append("hello\n\tWorld"); | ||
assertEquals(TAB_SIZE_ERR_MSG, "hello\n World", sb.toString()); | ||
|
||
sb = new AttributedStringBuilder().tabs(5); | ||
sb.append("hello\tWorld\n\tfoo\tbar"); | ||
assertEquals(TAB_SIZE_ERR_MSG, "hello World\n foo bar", sb.toString()); | ||
} | ||
|
||
@Test | ||
public void testAppendToString() { | ||
AttributedStringBuilder sb; | ||
String expected = ""; | ||
sb = new AttributedStringBuilder().tabs(4); | ||
|
||
sb.append("hello"); expected += "hello"; | ||
sb.append("\tWorld"); expected += " World"; //append to first line | ||
assertEquals(TAB_SIZE_ERR_MSG, expected, sb.toString()); | ||
|
||
sb.append("\nfoo\tbar"); expected += "\nfoo bar"; //append new line | ||
assertEquals(TAB_SIZE_ERR_MSG, expected, sb.toString()); | ||
|
||
sb.append("lorem\tipsum"); expected += "lorem ipsum"; //append to second line | ||
assertEquals(TAB_SIZE_ERR_MSG, expected, sb.toString()); | ||
} | ||
|
||
@Test | ||
public void testFromAnsiWithTabs() { | ||
AttributedStringBuilder sb; | ||
String expected = ""; | ||
sb = new AttributedStringBuilder().tabs(4); | ||
|
||
sb.appendAnsi("hello\tWorld"); expected += "hello World"; | ||
assertEquals(TAB_SIZE_ERR_MSG, expected, sb.toString()); | ||
|
||
sb.appendAnsi("\033[38;5;120mgreen\tfoo\033[39m"); expected += "green foo"; | ||
assertEquals(TAB_SIZE_ERR_MSG, expected, sb.toString()); | ||
sb.appendAnsi("\n\033[38;5;120mbar\tbaz\033[39m"); expected += "\nbar baz"; | ||
assertEquals(TAB_SIZE_ERR_MSG, expected, sb.toString()); | ||
} | ||
|
||
/** | ||
* Test that tabs are not expanded in strings if tab size has not been set | ||
*/ | ||
@Test | ||
public void testUnsetTabSize() { | ||
AttributedStringBuilder sb; | ||
String expected = ""; | ||
sb = new AttributedStringBuilder(); | ||
|
||
sb.append("hello\tWorld"); expected += "hello\tWorld"; | ||
assertEquals(TAB_SIZE_ERR_MSG, expected, sb.toString()); | ||
} | ||
|
||
@Test(expected=IllegalStateException.class) | ||
public void testChangingExistingTabSize() throws Exception { | ||
AttributedStringBuilder sb = new AttributedStringBuilder(); | ||
sb.append("helloWorld"); | ||
sb.tabs(4); | ||
} | ||
|
||
@Test(expected=IllegalArgumentException.class) | ||
public void testNegativeTabSize() throws Exception { | ||
@SuppressWarnings("unused") | ||
AttributedStringBuilder sb = new AttributedStringBuilder().tabs(-1); | ||
} | ||
} |