test_join.vim (10109B)
1 " Test for joining lines. 2 3 func 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! 34 endfunc 35 36 " Tests for setting the '[,'] marks when joining lines. 37 func 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! 56 endfunc 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 62 func Test_join_spaces_marks() 63 new 64 " Text used for the test 65 insert 66 asdfasdf. 67 asdf 68 asdfasdf. 69 asdf 70 asdfasdf. 71 asdf 72 asdfasdf. 73 asdf 74 asdfasdf. 75 asdf 76 asdfasdf. 77 asdf 78 asdfasdf. 79 asdf 80 asdfasdf 81 asdf 82 asdfasdf 83 asdf 84 asdfasdf 85 asdf 86 asdfasdf 87 asdf 88 asdfasdf 89 asdf 90 asdfasdf 91 asdf 92 asdfasdf 93 asdf 94 zx cvn. 95 as dfg? 96 hjkl iop! 97 ert 98 zx cvn. 99 as dfg? 100 hjkl iop! 101 ert 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 throw 'skipped: Nvim does not support "set compatible" or "set cpoptions+=j"' 144 145 enew! 146 call append(0, text) 147 normal gg 148 149 set cpoptions-=j 150 set joinspaces 151 normal JjJjJjJjJjJjJjJjJjJjJjJjJjJ 152 normal j05lmx 153 normal 2j06lmy 154 normal 2k4Jy3l$p 155 normal `xyl$p 156 normal `yy2l$p 157 158 set cpoptions+=j 159 normal j05lmx 160 normal 2j06lmy 161 normal 2k4Jy3l$p 162 normal `xyl$p 163 normal `yy2l$p 164 165 " Expected output 166 let expected =<< trim [DATA] 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 asdfasdf asdf 181 zx cvn. as dfg? hjkl iop! ert enop 182 zx cvn. as dfg? hjkl iop! ert ernop 183 184 [DATA] 185 186 call assert_equal(expected, getline(1, '$')) 187 188 enew! 189 call append(0, text) 190 normal gg 191 192 set cpoptions-=j 193 set nojoinspaces 194 set compatible 195 196 normal JjJjJjJjJjJjJjJjJjJjJjJjJjJ 197 normal j4Jy3l$pjdG 198 199 " Expected output 200 let expected =<< trim [DATA] 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 asdfasdf asdf 215 zx cvn. as dfg? hjkl iop! ert a 216 [DATA] 217 218 call assert_equal(expected, getline(1, '$')) 219 220 set nocompatible 221 set cpoptions&vim 222 set joinspaces&vim 223 close! 224 endfunc 225 226 " Test for joining lines with comments 227 func Test_join_lines_with_comments() 228 new 229 230 " Text used by the test 231 insert 232 { 233 234 /* 235 * Make sure the previous comment leader is not removed. 236 */ 237 238 /* 239 * Make sure the previous comment leader is not removed. 240 */ 241 242 // Should the next comment leader be left alone? 243 // Yes. 244 245 // Should the next comment leader be left alone? 246 // Yes. 247 248 /* Here the comment leader should be left intact. */ 249 // And so should this one. 250 251 /* Here the comment leader should be left intact. */ 252 // And so should this one. 253 254 if (condition) // Remove the next comment leader! 255 // OK, I will. 256 action(); 257 258 if (condition) // Remove the next comment leader! 259 // OK, I will. 260 action(); 261 } 262 . 263 264 call cursor(2, 1) 265 set comments=s1:/*,mb:*,ex:*/,:// 266 set nojoinspaces fo=j 267 set backspace=eol,start 268 269 .,+3join 270 exe "normal j4J\<CR>" 271 .,+2join 272 exe "normal j3J\<CR>" 273 .,+2join 274 exe "normal j3J\<CR>" 275 .,+2join 276 exe "normal jj3J\<CR>" 277 278 " Expected output 279 let expected =<< trim [CODE] 280 { 281 /* Make sure the previous comment leader is not removed. */ 282 /* Make sure the previous comment leader is not removed. */ 283 // Should the next comment leader be left alone? Yes. 284 // Should the next comment leader be left alone? Yes. 285 /* Here the comment leader should be left intact. */ // And so should this one. 286 /* Here the comment leader should be left intact. */ // And so should this one. 287 if (condition) // Remove the next comment leader! OK, I will. 288 action(); 289 if (condition) // Remove the next comment leader! OK, I will. 290 action(); 291 } 292 [CODE] 293 294 call assert_equal(expected, getline(1, '$')) 295 296 set comments&vim 297 set joinspaces&vim 298 set fo&vim 299 set backspace&vim 300 close! 301 endfunc 302 303 " Test for joining lines with different comment leaders 304 func Test_join_comments_2() 305 new 306 307 insert 308 { 309 310 /* 311 * Make sure the previous comment leader is not removed. 312 */ 313 314 /* 315 * Make sure the previous comment leader is not removed. 316 */ 317 318 /* List: 319 * - item1 320 * foo bar baz 321 * foo bar baz 322 * - item2 323 * foo bar baz 324 * foo bar baz 325 */ 326 327 /* List: 328 * - item1 329 * foo bar baz 330 * foo bar baz 331 * - item2 332 * foo bar baz 333 * foo bar baz 334 */ 335 336 // Should the next comment leader be left alone? 337 // Yes. 338 339 // Should the next comment leader be left alone? 340 // Yes. 341 342 /* Here the comment leader should be left intact. */ 343 // And so should this one. 344 345 /* Here the comment leader should be left intact. */ 346 // And so should this one. 347 348 if (condition) // Remove the next comment leader! 349 // OK, I will. 350 action(); 351 352 if (condition) // Remove the next comment leader! 353 // OK, I will. 354 action(); 355 356 int i = 7 /* foo *// 3 357 // comment 358 ; 359 360 int i = 7 /* foo *// 3 361 // comment 362 ; 363 364 ># Note that the last character of the ending comment leader (left angle 365 # bracket) is a comment leader itself. Make sure that this comment leader is 366 # not removed from the next line #< 367 < On this line a new comment is opened which spans 2 lines. This comment should 368 < retain its comment leader. 369 370 ># Note that the last character of the ending comment leader (left angle 371 # bracket) is a comment leader itself. Make sure that this comment leader is 372 # not removed from the next line #< 373 < On this line a new comment is opened which spans 2 lines. This comment should 374 < retain its comment leader. 375 376 } 377 . 378 379 call cursor(2, 1) 380 set comments=sO:*\ -,mO:*\ \ ,exO:*/ 381 set comments+=s1:/*,mb:*,ex:*/,:// 382 set comments+=s1:>#,mb:#,ex:#<,:< 383 set cpoptions-=j joinspaces fo=j 384 set backspace=eol,start 385 386 .,+3join 387 exe "normal j4J\<CR>" 388 .,+8join 389 exe "normal j9J\<CR>" 390 .,+2join 391 exe "normal j3J\<CR>" 392 .,+2join 393 exe "normal j3J\<CR>" 394 .,+2join 395 exe "normal jj3J\<CR>j" 396 .,+2join 397 exe "normal jj3J\<CR>j" 398 .,+5join 399 exe "normal j6J\<CR>" 400 exe "normal oSome code!\<CR>// Make sure backspacing does not remove this comment leader.\<Esc>0i\<C-H>\<Esc>" 401 402 " Expected output 403 let expected =<< trim [CODE] 404 { 405 /* Make sure the previous comment leader is not removed. */ 406 /* Make sure the previous comment leader is not removed. */ 407 /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */ 408 /* List: item1 foo bar baz foo bar baz item2 foo bar baz foo bar baz */ 409 // Should the next comment leader be left alone? Yes. 410 // Should the next comment leader be left alone? Yes. 411 /* Here the comment leader should be left intact. */ // And so should this one. 412 /* Here the comment leader should be left intact. */ // And so should this one. 413 if (condition) // Remove the next comment leader! OK, I will. 414 action(); 415 if (condition) // Remove the next comment leader! OK, I will. 416 action(); 417 int i = 7 /* foo *// 3 // comment 418 ; 419 int i = 7 /* foo *// 3 // comment 420 ; 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 ># 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. 423 424 Some code!// Make sure backspacing does not remove this comment leader. 425 } 426 [CODE] 427 428 call assert_equal(expected, getline(1, '$')) 429 close! 430 endfunc 431 432 func Test_join_lines() 433 new 434 call setline(1, ['a', 'b', '', 'c', 'd']) 435 %join 436 call assert_equal('a b c d', getline(1)) 437 call setline(1, ['a', 'b', '', 'c', 'd']) 438 normal 5J 439 call assert_equal('a b c d', getline(1)) 440 call setline(1, ['a', 'b', 'c']) 441 2,2join 442 call assert_equal(['a', 'b', 'c'], getline(1, '$')) 443 call assert_equal(2, line('.')) 444 2join 445 call assert_equal(['a', 'b c'], getline(1, '$')) 446 bwipe! 447 endfunc