1" Test for joining lines. 2 3func Test_join_with_count() 4 new 5 call setline(1, ['one', 'two', 'three', 'four']) 6 normal J 7 call assert_equal('one two', getline(1)) 8 %del 9 call setline(1, ['one', 'two', 'three', 'four']) 10 normal 10J 11 call assert_equal('one two three four', getline(1)) 12 13 call setline(1, ['one', '', 'two']) 14 normal J 15 call assert_equal('one', getline(1)) 16 17 call setline(1, ['one', ' ', 'two']) 18 normal J 19 call assert_equal('one', getline(1)) 20 21 call setline(1, ['one', '', '', 'two']) 22 normal JJ 23 call assert_equal('one', getline(1)) 24 25 call setline(1, ['one', ' ', ' ', 'two']) 26 normal JJ 27 call assert_equal('one', getline(1)) 28 29 call setline(1, ['one', '', '', 'two']) 30 normal 2J 31 call assert_equal('one', getline(1)) 32 33 quit! 34endfunc 35 36" Tests for setting the '[,'] marks when joining lines. 37func Test_join_marks() 38 enew 39 call append(0, [ 40 \ "\t\tO sodales, ludite, vos qui", 41 \ "attamen consulite per voster honur. Tua pulchra " . 42 \ "facies me fay planszer milies", 43 \ "", 44 \ "This line.", 45 \ "Should be joined with the next line", 46 \ "and with this line"]) 47 48 normal gg0gqj 49 call assert_equal([0, 1, 1, 0], getpos("'[")) 50 call assert_equal([0, 2, 1, 0], getpos("']")) 51 52 /^This line/;'}-join 53 call assert_equal([0, 4, 11, 0], getpos("'[")) 54 call assert_equal([0, 4, 67, 0], getpos("']")) 55 enew! 56endfunc 57 58" Test for joining lines and marks in them 59" in compatible and nocompatible modes 60" and with 'joinspaces' set or not 61" and with 'cpoptions' flag 'j' set or not 62func Test_join_spaces_marks() 63 new 64 " Text used for the test 65 insert 66asdfasdf. 67asdf 68asdfasdf. 69asdf 70asdfasdf. 71asdf 72asdfasdf. 73asdf 74asdfasdf. 75asdf 76asdfasdf. 77asdf 78asdfasdf. 79asdf 80asdfasdf 81asdf 82asdfasdf 83asdf 84asdfasdf 85asdf 86asdfasdf 87asdf 88asdfasdf 89asdf 90asdfasdf 91asdf 92asdfasdf 93asdf 94zx cvn. 95as dfg? 96hjkl iop! 97ert 98zx cvn. 99as dfg? 100hjkl iop! 101ert 102. 103 let text = getline(1, '$') 104 normal gg 105 106 set nojoinspaces 107 set cpoptions-=j 108 normal JjJjJjJjJjJjJjJjJjJjJjJjJjJ 109 normal j05lmx 110 normal 2j06lmy 111 normal 2k4Jy3l$p 112 normal `xyl$p 113 normal `yy2l$p 114 115 set cpoptions+=j 116 normal j05lmx 117 normal 2j06lmy 118 normal 2k4Jy3l$p 119 normal `xyl$p 120 normal `yy2l$p 121 122 " Expected output 123 let expected =<< trim [DATA] 124 asdfasdf. asdf 125 asdfasdf. asdf 126 asdfasdf. asdf 127 asdfasdf. asdf 128 asdfasdf. asdf 129 asdfasdf. asdf 130 asdfasdf. asdf 131 asdfasdf asdf 132 asdfasdf asdf 133 asdfasdf asdf 134 asdfasdf asdf 135 asdfasdf asdf 136 asdfasdf asdf 137 asdfasdf asdf 138 zx cvn. as dfg? hjkl iop! ert ernop 139 zx cvn. as dfg? hjkl iop! ert ernop 140 [DATA] 141 142 call assert_equal(expected, getline(1, '$')) 143 144 enew! 145 call append(0, text) 146 normal gg 147 148 set cpoptions-=j 149 set joinspaces 150 normal JjJjJjJjJjJjJjJjJjJjJjJjJjJ 151 normal j05lmx 152 normal 2j06lmy 153 normal 2k4Jy3l$p 154 normal `xyl$p 155 normal `yy2l$p 156 157 set cpoptions+=j 158 normal j05lmx 159 normal 2j06lmy 160 normal 2k4Jy3l$p 161 normal `xyl$p 162 normal `yy2l$p 163 164 " Expected output 165 let expected =<< trim [DATA] 166 asdfasdf. asdf 167 asdfasdf. asdf 168 asdfasdf. asdf 169 asdfasdf. asdf 170 asdfasdf. asdf 171 asdfasdf. asdf 172 asdfasdf. asdf 173 asdfasdf asdf 174 asdfasdf asdf 175 asdfasdf asdf 176 asdfasdf asdf 177 asdfasdf asdf 178 asdfasdf asdf 179 asdfasdf asdf 180 zx cvn. as dfg? hjkl iop! ert enop 181 zx cvn. as dfg? hjkl iop! ert ernop 182 183 [DATA] 184 185 call assert_equal(expected, getline(1, '$')) 186 187 enew! 188 call append(0, text) 189 normal gg 190 191 set cpoptions-=j 192 set nojoinspaces 193 set compatible 194 195 normal JjJjJjJjJjJjJjJjJjJjJjJjJjJ 196 normal j4Jy3l$pjdG 197 198 " Expected output 199 let expected =<< trim [DATA] 200 asdfasdf. asdf 201 asdfasdf. asdf 202 asdfasdf. asdf 203 asdfasdf. asdf 204 asdfasdf. asdf 205 asdfasdf. asdf 206 asdfasdf. asdf 207 asdfasdf asdf 208 asdfasdf asdf 209 asdfasdf asdf 210 asdfasdf asdf 211 asdfasdf asdf 212 asdfasdf asdf 213 asdfasdf asdf 214 zx cvn. as dfg? hjkl iop! ert a 215 [DATA] 216 217 call assert_equal(expected, getline(1, '$')) 218 219 set nocompatible 220 set cpoptions&vim 221 set joinspaces&vim 222 close! 223endfunc 224 225" Test for joining lines with comments 226func Test_join_lines_with_comments() 227 new 228 229 " Text used by the test 230 insert 231{ 232 233/* 234* Make sure the previous comment leader is not removed. 235*/ 236 237/* 238* Make sure the previous comment leader is not removed. 239*/ 240 241// Should the next comment leader be left alone? 242// Yes. 243 244// Should the next comment leader be left alone? 245// Yes. 246 247/* Here the comment leader should be left intact. */ 248// And so should this one. 249 250/* Here the comment leader should be left intact. */ 251// And so should this one. 252 253if (condition) // Remove the next comment leader! 254// OK, I will. 255action(); 256 257if (condition) // Remove the next comment leader! 258// OK, I will. 259action(); 260} 261. 262 263 call cursor(2, 1) 264 set comments=s1:/*,mb:*,ex:*/,:// 265 set nojoinspaces fo=j 266 set backspace=eol,start 267 268 .,+3join 269 exe "normal j4J\<CR>" 270 .,+2join 271 exe "normal j3J\<CR>" 272 .,+2join 273 exe "normal j3J\<CR>" 274 .,+2join 275 exe "normal jj3J\<CR>" 276 277 " Expected output 278 let expected =<< trim [CODE] 279 { 280 /* Make sure the previous comment leader is not removed. */ 281 /* Make sure the previous comment leader is not removed. */ 282 // Should the next comment leader be left alone? Yes. 283 // Should the next comment leader be left alone? Yes. 284 /* Here the comment leader should be left intact. */ // And so should this one. 285 /* Here the comment leader should be left intact. */ // And so should this one. 286 if (condition) // Remove the next comment leader! OK, I will. 287 action(); 288 if (condition) // Remove the next comment leader! OK, I will. 289 action(); 290 } 291 [CODE] 292 293 call assert_equal(expected, getline(1, '$')) 294 295 set comments&vim 296 set joinspaces&vim 297 set fo&vim 298 set backspace&vim 299 close! 300endfunc 301 302" Test for joining lines with different comment leaders 303func Test_join_comments_2() 304 new 305 306 insert 307{ 308 309/* 310 * Make sure the previous comment leader is not removed. 311 */ 312 313/* 314 * Make sure the previous comment leader is not removed. 315 */ 316 317/* List: 318 * - item1 319 * foo bar baz 320 * foo bar baz 321 * - item2 322 * foo bar baz 323 * foo bar baz 324 */ 325 326/* List: 327 * - item1 328 * foo bar baz 329 * foo bar baz 330 * - item2 331 * foo bar baz 332 * foo bar baz 333 */ 334 335// Should the next comment leader be left alone? 336// Yes. 337 338// Should the next comment leader be left alone? 339// Yes. 340 341/* Here the comment leader should be left intact. */ 342// And so should this one. 343 344/* Here the comment leader should be left intact. */ 345// And so should this one. 346 347if (condition) // Remove the next comment leader! 348 // OK, I will. 349 action(); 350 351if (condition) // Remove the next comment leader! 352 // OK, I will. 353 action(); 354 355int i = 7 /* foo *// 3 356 // comment 357 ; 358 359int i = 7 /* foo *// 3 360 // comment 361 ; 362 363># Note that the last character of the ending comment leader (left angle 364 # bracket) is a comment leader itself. Make sure that this comment leader is 365 # not removed from the next line #< 366< On this line a new comment is opened which spans 2 lines. This comment should 367< retain its comment leader. 368 369># Note that the last character of the ending comment leader (left angle 370 # bracket) is a comment leader itself. Make sure that this comment leader is 371 # not removed from the next line #< 372< On this line a new comment is opened which spans 2 lines. This comment should 373< retain its comment leader. 374 375} 376. 377 378 call cursor(2, 1) 379 set comments=sO:*\ -,mO:*\ \ ,exO:*/ 380 set comments+=s1:/*,mb:*,ex:*/,:// 381 set comments+=s1:>#,mb:#,ex:#<,:< 382 set cpoptions-=j joinspaces fo=j 383 set backspace=eol,start 384 385 .,+3join 386 exe "normal j4J\<CR>" 387 .,+8join 388 exe "normal j9J\<CR>" 389 .,+2join 390 exe "normal j3J\<CR>" 391 .,+2join 392 exe "normal j3J\<CR>" 393 .,+2join 394 exe "normal jj3J\<CR>j" 395 .,+2join 396 exe "normal jj3J\<CR>j" 397 .,+5join 398 exe "normal j6J\<CR>" 399 exe "normal oSome code!\<CR>// Make sure backspacing does not remove this comment leader.\<Esc>0i\<C-H>\<Esc>" 400 401 " Expected output 402 let expected =<< trim [CODE] 403 { 404 /* Make sure the previous comment leader is not removed. */ 405 /* Make sure the previous comment leader is not removed. */ 406 /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */ 407 /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */ 408 // Should the next comment leader be left alone? Yes. 409 // Should the next comment leader be left alone? Yes. 410 /* Here the comment leader should be left intact. */ // And so should this one. 411 /* Here the comment leader should be left intact. */ // And so should this one. 412 if (condition) // Remove the next comment leader! OK, I will. 413 action(); 414 if (condition) // Remove the next comment leader! OK, I will. 415 action(); 416 int i = 7 /* foo *// 3 // comment 417 ; 418 int i = 7 /* foo *// 3 // comment 419 ; 420 ># Note that the last character of the ending comment leader (left angle bracket) is a comment leader itself. Make sure that this comment leader is not removed from the next line #< < On this line a new comment is opened which spans 2 lines. This comment should retain its comment leader. 421 ># Note that the last character of the ending comment leader (left angle bracket) is a comment leader itself. Make sure that this comment leader is not removed from the next line #< < On this line a new comment is opened which spans 2 lines. This comment should retain its comment leader. 422 423 Some code!// Make sure backspacing does not remove this comment leader. 424 } 425 [CODE] 426 427 call assert_equal(expected, getline(1, '$')) 428 close! 429endfunc 430 431func Test_join_lines() 432 new 433 call setline(1, ['a', 'b', '', 'c', 'd']) 434 %join 435 call assert_equal('a b c d', getline(1)) 436 call setline(1, ['a', 'b', '', 'c', 'd']) 437 normal 5J 438 call assert_equal('a b c d', getline(1)) 439 call setline(1, ['a', 'b', 'c']) 440 2,2join 441 call assert_equal(['a', 'b', 'c'], getline(1, '$')) 442 call assert_equal(2, line('.')) 443 2join 444 call assert_equal(['a', 'b c'], getline(1, '$')) 445 bwipe! 446endfunc 447 448" vim: shiftwidth=2 sts=2 expandtab 449