Skip to content

Commit

Permalink
Merge pull request #536 from Marcono1234/AttributedStringBuilder-appe…
Browse files Browse the repository at this point in the history
…nd-null

Fix AttributedStringBuilder.append not handling null correctly
  • Loading branch information
mattirn committed May 24, 2020
2 parents 6924e3e + a1551e7 commit 1ccf81c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ public AttributedString subSequence(int start, int end) {

@Override
public AttributedStringBuilder append(CharSequence csq) {
if (csq == null) {
csq = "null"; // Required by Appendable.append
}
return append(new AttributedString(csq, current));
}

@Override
public AttributedStringBuilder append(CharSequence csq, int start, int end) {
if (csq == null) {
csq = "null"; // Required by Appendable.append
}
return append(csq.subSequence(start, end));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
package org.jline.utils;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import static org.junit.Assert.*;

public class AttributedStringBuilderTest {
private static String TAB_SIZE_ERR_MSG = "Incorrect tab size";
private static String TAB_SIZE_ERR_MSG = "Incorrect tab size";

/**
* Test single line with tabs in
*/
/**
* Test single line with tabs in
*/
@Test
public void testTabSize() {
AttributedStringBuilder sb;
Expand All @@ -39,7 +38,6 @@ public void testTabSize() {
sb = new AttributedStringBuilder().tabs(Arrays.asList(6,13));
sb.append("one\ttwo\tthree\tfour");
assertEquals(TAB_SIZE_ERR_MSG, "one two three four", sb.toString());

}

/**
Expand Down Expand Up @@ -82,6 +80,24 @@ public void testAppendToString() {
assertEquals(TAB_SIZE_ERR_MSG, expected, sb.toString());
}

/**
* Test that methods overriding {@code Appendable.append} correctly handle
* {@code null -> "null"}.
*/
@Test
public void testAppendNullToString() {
AttributedStringBuilder sb = new AttributedStringBuilder();
String expected = "";

sb.append("foo"); expected += "foo";
sb.append((CharSequence) null); expected += "null";
assertEquals(expected, sb.toString());

sb.append("bar"); expected += "bar";
sb.append((CharSequence) null, 1, 3); expected += "ul"; // Indices apply to "null"
assertEquals(expected, sb.toString());
}

@Test
public void testFromAnsiWithTabs() {
AttributedStringBuilder sb;
Expand Down

0 comments on commit 1ccf81c

Please sign in to comment.