1 /* 2 * Copyright (C) 2016 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.javapoet; 17 18 import org.junit.Test; 19 import org.junit.runner.RunWith; 20 import org.junit.runners.JUnit4; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 @RunWith(JUnit4.class) 25 public final class LineWrapperTest { wrap()26 @Test public void wrap() throws Exception { 27 StringBuffer out = new StringBuffer(); 28 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 29 lineWrapper.append("abcde"); 30 lineWrapper.wrappingSpace(2); 31 lineWrapper.append("fghij"); 32 lineWrapper.close(); 33 assertThat(out.toString()).isEqualTo("abcde\n fghij"); 34 } 35 noWrap()36 @Test public void noWrap() throws Exception { 37 StringBuffer out = new StringBuffer(); 38 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 39 lineWrapper.append("abcde"); 40 lineWrapper.wrappingSpace(2); 41 lineWrapper.append("fghi"); 42 lineWrapper.close(); 43 assertThat(out.toString()).isEqualTo("abcde fghi"); 44 } 45 zeroWidthNoWrap()46 @Test public void zeroWidthNoWrap() throws Exception { 47 StringBuffer out = new StringBuffer(); 48 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 49 lineWrapper.append("abcde"); 50 lineWrapper.zeroWidthSpace(2); 51 lineWrapper.append("fghij"); 52 lineWrapper.close(); 53 assertThat(out.toString()).isEqualTo("abcdefghij"); 54 } 55 nospaceWrapMax()56 @Test public void nospaceWrapMax() throws Exception { 57 StringBuffer out = new StringBuffer(); 58 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 59 lineWrapper.append("abcde"); 60 lineWrapper.zeroWidthSpace(2); 61 lineWrapper.append("fghijk"); 62 lineWrapper.close(); 63 assertThat(out.toString()).isEqualTo("abcde\n fghijk"); 64 } 65 multipleWrite()66 @Test public void multipleWrite() throws Exception { 67 StringBuffer out = new StringBuffer(); 68 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 69 lineWrapper.append("ab"); 70 lineWrapper.wrappingSpace(1); 71 lineWrapper.append("cd"); 72 lineWrapper.wrappingSpace(1); 73 lineWrapper.append("ef"); 74 lineWrapper.wrappingSpace(1); 75 lineWrapper.append("gh"); 76 lineWrapper.wrappingSpace(1); 77 lineWrapper.append("ij"); 78 lineWrapper.wrappingSpace(1); 79 lineWrapper.append("kl"); 80 lineWrapper.wrappingSpace(1); 81 lineWrapper.append("mn"); 82 lineWrapper.wrappingSpace(1); 83 lineWrapper.append("op"); 84 lineWrapper.wrappingSpace(1); 85 lineWrapper.append("qr"); 86 lineWrapper.close(); 87 assertThat(out.toString()).isEqualTo("ab cd ef\n gh ij kl\n mn op qr"); 88 } 89 fencepost()90 @Test public void fencepost() throws Exception { 91 StringBuffer out = new StringBuffer(); 92 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 93 lineWrapper.append("abcde"); 94 lineWrapper.append("fghij"); 95 lineWrapper.wrappingSpace(2); 96 lineWrapper.append("k"); 97 lineWrapper.append("lmnop"); 98 lineWrapper.close(); 99 assertThat(out.toString()).isEqualTo("abcdefghij\n klmnop"); 100 } 101 fencepostZeroWidth()102 @Test public void fencepostZeroWidth() throws Exception { 103 StringBuffer out = new StringBuffer(); 104 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 105 lineWrapper.append("abcde"); 106 lineWrapper.append("fghij"); 107 lineWrapper.zeroWidthSpace(2); 108 lineWrapper.append("k"); 109 lineWrapper.append("lmnop"); 110 lineWrapper.close(); 111 assertThat(out.toString()).isEqualTo("abcdefghij\n klmnop"); 112 } 113 overlyLongLinesWithoutLeadingSpace()114 @Test public void overlyLongLinesWithoutLeadingSpace() throws Exception { 115 StringBuffer out = new StringBuffer(); 116 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 117 lineWrapper.append("abcdefghijkl"); 118 lineWrapper.close(); 119 assertThat(out.toString()).isEqualTo("abcdefghijkl"); 120 } 121 overlyLongLinesWithLeadingSpace()122 @Test public void overlyLongLinesWithLeadingSpace() throws Exception { 123 StringBuffer out = new StringBuffer(); 124 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 125 lineWrapper.wrappingSpace(2); 126 lineWrapper.append("abcdefghijkl"); 127 lineWrapper.close(); 128 assertThat(out.toString()).isEqualTo("\n abcdefghijkl"); 129 } 130 overlyLongLinesWithLeadingZeroWidth()131 @Test public void overlyLongLinesWithLeadingZeroWidth() throws Exception { 132 StringBuffer out = new StringBuffer(); 133 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 134 lineWrapper.zeroWidthSpace(2); 135 lineWrapper.append("abcdefghijkl"); 136 lineWrapper.close(); 137 assertThat(out.toString()).isEqualTo("abcdefghijkl"); 138 } 139 noWrapEmbeddedNewlines()140 @Test public void noWrapEmbeddedNewlines() throws Exception { 141 StringBuffer out = new StringBuffer(); 142 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 143 lineWrapper.append("abcde"); 144 lineWrapper.wrappingSpace(2); 145 lineWrapper.append("fghi\njklmn"); 146 lineWrapper.append("opqrstuvwxy"); 147 lineWrapper.close(); 148 assertThat(out.toString()).isEqualTo("abcde fghi\njklmnopqrstuvwxy"); 149 } 150 wrapEmbeddedNewlines()151 @Test public void wrapEmbeddedNewlines() throws Exception { 152 StringBuffer out = new StringBuffer(); 153 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 154 lineWrapper.append("abcde"); 155 lineWrapper.wrappingSpace(2); 156 lineWrapper.append("fghij\nklmn"); 157 lineWrapper.append("opqrstuvwxy"); 158 lineWrapper.close(); 159 assertThat(out.toString()).isEqualTo("abcde\n fghij\nklmnopqrstuvwxy"); 160 } 161 noWrapEmbeddedNewlines_ZeroWidth()162 @Test public void noWrapEmbeddedNewlines_ZeroWidth() throws Exception { 163 StringBuffer out = new StringBuffer(); 164 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 165 lineWrapper.append("abcde"); 166 lineWrapper.zeroWidthSpace(2); 167 lineWrapper.append("fghij\nklmn"); 168 lineWrapper.append("opqrstuvwxyz"); 169 lineWrapper.close(); 170 assertThat(out.toString()).isEqualTo("abcdefghij\nklmnopqrstuvwxyz"); 171 } 172 wrapEmbeddedNewlines_ZeroWidth()173 @Test public void wrapEmbeddedNewlines_ZeroWidth() throws Exception { 174 StringBuffer out = new StringBuffer(); 175 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 176 lineWrapper.append("abcde"); 177 lineWrapper.zeroWidthSpace(2); 178 lineWrapper.append("fghijk\nlmn"); 179 lineWrapper.append("opqrstuvwxy"); 180 lineWrapper.close(); 181 assertThat(out.toString()).isEqualTo("abcde\n fghijk\nlmnopqrstuvwxy"); 182 } 183 noWrapMultipleNewlines()184 @Test public void noWrapMultipleNewlines() throws Exception { 185 StringBuffer out = new StringBuffer(); 186 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 187 lineWrapper.append("abcde"); 188 lineWrapper.wrappingSpace(2); 189 lineWrapper.append("fghi\nklmnopq\nr"); 190 lineWrapper.wrappingSpace(2); 191 lineWrapper.append("stuvwxyz"); 192 lineWrapper.close(); 193 assertThat(out.toString()).isEqualTo("abcde fghi\nklmnopq\nr stuvwxyz"); 194 } 195 wrapMultipleNewlines()196 @Test public void wrapMultipleNewlines() throws Exception { 197 StringBuffer out = new StringBuffer(); 198 LineWrapper lineWrapper = new LineWrapper(out, " ", 10); 199 lineWrapper.append("abcde"); 200 lineWrapper.wrappingSpace(2); 201 lineWrapper.append("fghi\nklmnopq\nrs"); 202 lineWrapper.wrappingSpace(2); 203 lineWrapper.append("tuvwxyz1"); 204 lineWrapper.close(); 205 assertThat(out.toString()).isEqualTo("abcde fghi\nklmnopq\nrs\n tuvwxyz1"); 206 } 207 } 208