| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import static junit.framework.Assert.assertEquals; |
| 20 |
|
| 21 |
import androidx.test.runner.AndroidJUnit4; |
| 22 |
|
| 23 |
import org.junit.Before; |
| 24 |
import org.junit.Test; |
| 25 |
import org.junit.runner.RunWith; |
| 26 |
|
| 27 |
import java.io.ByteArrayOutputStream; |
| 28 |
import java.io.PrintWriter; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
@RunWith(AndroidJUnit4.class) |
| 34 |
public class IndentingPrintWriterTest { |
| 35 |
|
| 36 |
private ByteArrayOutputStream mStream; |
| 37 |
private PrintWriter mWriter; |
| 38 |
|
| 39 |
@Before |
| 40 |
public void setUp() throws Exception { |
| 41 |
mStream = new ByteArrayOutputStream(); |
| 42 |
mWriter = new PrintWriter(mStream); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void testMultipleIndents() throws Exception { |
| 47 |
final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, " "); |
| 48 |
|
| 49 |
pw.print("Hello"); |
| 50 |
pw.increaseIndent(); |
| 51 |
pw.println(); |
| 52 |
pw.print("World"); |
| 53 |
pw.increaseIndent(); |
| 54 |
pw.println(); |
| 55 |
pw.print("And"); |
| 56 |
pw.decreaseIndent(); |
| 57 |
pw.println(); |
| 58 |
pw.print("Goodbye"); |
| 59 |
pw.decreaseIndent(); |
| 60 |
pw.println(); |
| 61 |
pw.print("World"); |
| 62 |
pw.println(); |
| 63 |
|
| 64 |
pw.flush(); |
| 65 |
assertEquals("Hello\n World\n And\n Goodbye\nWorld\n", mStream.toString()); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void testAdjustIndentAfterNewline() throws Exception { |
| 70 |
final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, " "); |
| 71 |
|
| 72 |
pw.println("Hello"); |
| 73 |
pw.increaseIndent(); |
| 74 |
pw.println("World"); |
| 75 |
|
| 76 |
pw.flush(); |
| 77 |
assertEquals("Hello\n World\n", mStream.toString()); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void testWrapping() throws Exception { |
| 82 |
final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, "", 10); |
| 83 |
|
| 84 |
pw.print("dog "); |
| 85 |
pw.print("cat "); |
| 86 |
pw.print("cow "); |
| 87 |
pw.print("meow "); |
| 88 |
|
| 89 |
pw.flush(); |
| 90 |
assertEquals("dog cat \ncow meow ", mStream.toString()); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void testWrappingIndented() throws Exception { |
| 95 |
final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, " ", 10); |
| 96 |
|
| 97 |
pw.increaseIndent(); |
| 98 |
pw.print("dog "); |
| 99 |
pw.print("meow "); |
| 100 |
pw.print("a "); |
| 101 |
pw.print("b "); |
| 102 |
pw.print("cow "); |
| 103 |
|
| 104 |
pw.flush(); |
| 105 |
assertEquals(" dog \n meow \n a b \n cow ", mStream.toString()); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void testWrappingEmbeddedNewlines() throws Exception { |
| 110 |
final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, " ", 10); |
| 111 |
|
| 112 |
pw.increaseIndent(); |
| 113 |
pw.print("Lorem ipsum \ndolor sit \namet, consectetur \nadipiscing elit."); |
| 114 |
|
| 115 |
pw.flush(); |
| 116 |
assertEquals(" Lorem ip\n sum \n dolor si\n t \n amet, co\n" |
| 117 |
+ " nsectetu\n r \n adipisci\n ng elit.\n", mStream.toString()); |
| 118 |
} |
| 119 |
|
| 120 |
@Test |
| 121 |
public void testWrappingSingleGiant() throws Exception { |
| 122 |
final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, " ", 10); |
| 123 |
|
| 124 |
pw.increaseIndent(); |
| 125 |
pw.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit."); |
| 126 |
|
| 127 |
pw.flush(); |
| 128 |
assertEquals(" Lorem ip\n sum dolo\n r sit am\n et, cons\n" |
| 129 |
+ " ectetur \n adipisci\n ng elit.\n", mStream.toString()); |
| 130 |
} |
| 131 |
|
| 132 |
@Test |
| 133 |
public void testWrappingPrefixedGiant() throws Exception { |
| 134 |
final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, " ", 10); |
| 135 |
|
| 136 |
pw.increaseIndent(); |
| 137 |
pw.print("foo"); |
| 138 |
pw.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit."); |
| 139 |
|
| 140 |
pw.flush(); |
| 141 |
assertEquals(" foo\n Lorem ip\n sum dolo\n r sit am\n et, cons\n" |
| 142 |
+ " ectetur \n adipisci\n ng elit.\n", mStream.toString()); |
| 143 |
} |
| 144 |
|
| 145 |
@Test |
| 146 |
public void testArrayValue() { |
| 147 |
final IndentingPrintWriter pw = new IndentingPrintWriter(mWriter, " "); |
| 148 |
pw.increaseIndent(); |
| 149 |
pw.print("strings", new String[]{"a", "b", "c"}); |
| 150 |
pw.print("ints", new int[]{1, 2}); |
| 151 |
pw.flush(); |
| 152 |
|
| 153 |
assertEquals(" strings=[a, b, c] ints=[1, 2] ", mStream.toString()); |
| 154 |
} |
| 155 |
} |