1package goutils 2 3import ( 4 "fmt" 5 "testing" 6) 7 8// ****************************** TESTS ******************************************** 9 10func TestWrapNormalWord(t *testing.T) { 11 12 in := "Bob Manuel Bob Manuel" 13 out := "Bob Manuel\nBob Manuel" 14 wrapLength := 10 15 16 if x := Wrap(in, wrapLength); x != out { 17 t.Errorf("Wrap(%v) = %v, want %v", in, x, out) 18 } 19} 20 21func TestWrapNormalWord2(t *testing.T) { 22 23 in := "Here is one line of text that is going to be wrapped after 20 columns." 24 out := "Here is one line of\ntext that is going\nto be wrapped after\n20 columns." 25 wrapLength := 20 26 27 if x := Wrap(in, wrapLength); x != out { 28 t.Errorf("Wrap(%v) = %v, want %v", in, x, out) 29 } 30} 31 32func TestWrapNormalWord3(t *testing.T) { 33 34 in := "Click here to jump to the commons website - http://commons.apache.org" 35 out := "Click here to jump\nto the commons\nwebsite -\nhttp://commons.apache.org" 36 wrapLength := 20 37 38 if x := Wrap(in, wrapLength); x != out { 39 t.Errorf("Wrap(%v) = %v, want %v", in, x, out) 40 } 41} 42 43func TestWrapNormalWord4(t *testing.T) { 44 45 in := "Click here, http://commons.apache.org, to jump to the commons website" 46 out := "Click here,\nhttp://commons.apache.org,\nto jump to the\ncommons website" 47 wrapLength := 20 48 49 if x := Wrap(in, wrapLength); x != out { 50 t.Errorf("Wrap(%v) = %v, want %v", in, x, out) 51 } 52} 53 54 55func TestNonWrapLongWord(t *testing.T) { 56 in := "123456789_123456789_123456789_123456789_1234567890" 57 out := "123456789_123456789_123456789_123456789_1234567890" 58 wrapLength := 11 59 newLineStr := "\n" 60 wrapLongWords := false 61 62 if x := WrapCustom(in, wrapLength, newLineStr, wrapLongWords); x != out { 63 t.Errorf("Wrap(%v) = %v, want %v", in, x, out) 64 } 65} 66 67func TestWrapLongWord(t *testing.T) { 68 in := "123456789_123456789_123456789_123456789_1234567890" 69 out := "123456789_1\n23456789_12\n3456789_123\n456789_1234\n567890" 70 wrapLength := 11 71 newLineStr := "\n" 72 wrapLongWords := true 73 74 if x := WrapCustom(in, wrapLength, newLineStr, wrapLongWords); x != out { 75 t.Errorf("Wrap(%v) = %v, want %v", in, x, out) 76 } 77} 78 79func TestWrapCustomLongWordFalse(t *testing.T) { 80 81 in := "BobManuelBob Bob" 82 out := "BobManuelBob<br\\>Bob" 83 wrapLength := 10 84 newLineStr := "<br\\>" 85 wrapLongWords := false 86 87 if x := WrapCustom(in, wrapLength, newLineStr, wrapLongWords); x != out { 88 t.Errorf("Wrap(%v) = %v, want %v", in, x, out) 89 } 90} 91 92func TestWrapCustomLongWordTrue(t *testing.T) { 93 94 in := "BobManuelBob Bob" 95 out := "BobManuelB<br\\>ob Bob" 96 wrapLength := 10 97 newLineStr := "<br\\>" 98 wrapLongWords := true 99 100 if x := WrapCustom(in, wrapLength, newLineStr, wrapLongWords); x != out { 101 t.Errorf("WrapCustom(%v) = %v, want %v", in, x, out) 102 } 103} 104 105func TestCapitalize(t *testing.T) { 106 107 // Test 1: Checks if function works with 1 parameter, and default whitespace delimiter 108 in := "test is going.well.thank.you.for inquiring" 109 out := "Test Is Going.well.thank.you.for Inquiring" 110 111 if x := Capitalize(in); x != out { 112 t.Errorf("Capitalize(%v) = %v, want %v", in, x, out) 113 } 114 115 // Test 2: Checks if function works with both parameters, with param 2 containing whitespace and '.' 116 out = "Test Is Going.Well.Thank.You.For Inquiring" 117 delimiters := []rune{' ', '.'} 118 119 if x := Capitalize(in, delimiters...); x != out { 120 t.Errorf("Capitalize(%v) = %v, want %v", in, x, out) 121 } 122} 123 124func TestCapitalizeFully(t *testing.T) { 125 126 // Test 1 127 in := "tEsT iS goiNG.wELL.tHaNk.yOU.for inqUIrING" 128 out := "Test Is Going.well.thank.you.for Inquiring" 129 130 if x := CapitalizeFully(in); x != out { 131 t.Errorf("CapitalizeFully(%v) = %v, want %v", in, x, out) 132 } 133 134 // Test 2 135 out = "Test Is Going.Well.Thank.You.For Inquiring" 136 delimiters := []rune{' ', '.'} 137 138 if x := CapitalizeFully(in, delimiters...); x != out { 139 t.Errorf("CapitalizeFully(%v) = %v, want %v", in, x, out) 140 } 141} 142 143func TestUncapitalize(t *testing.T) { 144 145 // Test 1: Checks if function works with 1 parameter, and default whitespace delimiter 146 in := "This Is A.Test" 147 out := "this is a.Test" 148 149 if x := Uncapitalize(in); x != out { 150 t.Errorf("Uncapitalize(%v) = %v, want %v", in, x, out) 151 } 152 153 // Test 2: Checks if function works with both parameters, with param 2 containing whitespace and '.' 154 out = "this is a.test" 155 delimiters := []rune{' ', '.'} 156 157 if x := Uncapitalize(in, delimiters...); x != out { 158 t.Errorf("Uncapitalize(%v) = %v, want %v", in, x, out) 159 } 160} 161 162func TestSwapCase(t *testing.T) { 163 164 in := "This Is A.Test" 165 out := "tHIS iS a.tEST" 166 167 if x := SwapCase(in); x != out { 168 t.Errorf("SwapCase(%v) = %v, want %v", in, x, out) 169 } 170} 171 172func TestInitials(t *testing.T) { 173 174 // Test 1 175 in := "John Doe.Ray" 176 out := "JD" 177 178 if x := Initials(in); x != out { 179 t.Errorf("Initials(%v) = %v, want %v", in, x, out) 180 } 181 182 // Test 2 183 out = "JDR" 184 delimiters := []rune{' ', '.'} 185 186 if x := Initials(in, delimiters...); x != out { 187 t.Errorf("Initials(%v) = %v, want %v", in, x, out) 188 } 189 190} 191 192// ****************************** EXAMPLES ******************************************** 193 194func ExampleWrap() { 195 196 in := "Bob Manuel Bob Manuel" 197 wrapLength := 10 198 199 fmt.Println(Wrap(in, wrapLength)) 200 // Output: 201 // Bob Manuel 202 // Bob Manuel 203} 204 205func ExampleWrapCustom_1() { 206 207 in := "BobManuelBob Bob" 208 wrapLength := 10 209 newLineStr := "<br\\>" 210 wrapLongWords := false 211 212 fmt.Println(WrapCustom(in, wrapLength, newLineStr, wrapLongWords)) 213 // Output: 214 // BobManuelBob<br\>Bob 215} 216 217func ExampleWrapCustom_2() { 218 219 in := "BobManuelBob Bob" 220 wrapLength := 10 221 newLineStr := "<br\\>" 222 wrapLongWords := true 223 224 fmt.Println(WrapCustom(in, wrapLength, newLineStr, wrapLongWords)) 225 // Output: 226 // BobManuelB<br\>ob Bob 227} 228 229func ExampleCapitalize() { 230 231 in := "test is going.well.thank.you.for inquiring" // Compare input to CapitalizeFully example 232 delimiters := []rune{' ', '.'} 233 234 fmt.Println(Capitalize(in)) 235 fmt.Println(Capitalize(in, delimiters...)) 236 // Output: 237 // Test Is Going.well.thank.you.for Inquiring 238 // Test Is Going.Well.Thank.You.For Inquiring 239} 240 241func ExampleCapitalizeFully() { 242 243 in := "tEsT iS goiNG.wELL.tHaNk.yOU.for inqUIrING" // Notice scattered capitalization 244 delimiters := []rune{' ', '.'} 245 246 fmt.Println(CapitalizeFully(in)) 247 fmt.Println(CapitalizeFully(in, delimiters...)) 248 // Output: 249 // Test Is Going.well.thank.you.for Inquiring 250 // Test Is Going.Well.Thank.You.For Inquiring 251} 252 253func ExampleUncapitalize() { 254 255 in := "This Is A.Test" 256 delimiters := []rune{' ', '.'} 257 258 fmt.Println(Uncapitalize(in)) 259 fmt.Println(Uncapitalize(in, delimiters...)) 260 // Output: 261 // this is a.Test 262 // this is a.test 263} 264 265func ExampleSwapCase() { 266 267 in := "This Is A.Test" 268 fmt.Println(SwapCase(in)) 269 // Output: 270 // tHIS iS a.tEST 271} 272 273func ExampleInitials() { 274 275 in := "John Doe.Ray" 276 delimiters := []rune{' ', '.'} 277 278 fmt.Println(Initials(in)) 279 fmt.Println(Initials(in, delimiters...)) 280 // Output: 281 // JD 282 // JDR 283} 284