robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
c1ac55ba45
master
1" Tests for the various flags in the 'comments' option 2 3" Test for the 'n' flag in 'comments' 4func Test_comment_nested () 5new 6setlocal comments = n:>fo += ro 7exe "normal i> B\nD\<C-C>ggOA\<C-C>joC\<C-C>Go\<BS>>>> F\nH" 8exe "normal 5GOE\<C-C>6GoG" 9let expected =<< trim END 10> A 11> B 12> C 13> D 14>>>> E 15>>>> F 16>>>> G 17>>>> H 18END 19call assert_equal ( expected , getline ( 1 , '$' )) 20bw ! 21endfunc 22 23" Test for the 'b' flag in 'comments' 24func Test_comment_blank () 25new 26setlocal comments = b:*fo += ro 27exe "normal i* E\nF\n\<BS>G\nH\<C-C>ggOC\<C-C>O\<BS>B\<C-C>OA\<C-C>2joD" 28let expected =<< trim END 29A 30*B 31* C 32* D 33* E 34* F 35*G 36H 37END 38call assert_equal ( expected , getline ( 1 , '$' )) 39bw ! 40endfunc 41 42" Test for the 'f' flag in 'comments' (only the first line has a comment 43" string) 44func Test_comment_firstline () 45new 46setlocal comments = f:-fo += ro 47exe "normal i- B\nD\<C-C>ggoC\<C-C>ggOA\<C-C>" 48call assert_equal ([ 'A' , '- B' , ' C' , ' D' ], getline ( 1 , '$' )) 49 %d 50setlocal comments = :- 51exe "normal i- B\nD\<C-C>ggoC\<C-C>ggOA\<C-C>" 52call assert_equal ([ '- A' , '- B' , '- C' , '- D' ], getline ( 1 , '$' )) 53bw ! 54endfunc 55 56" Test for the 's', 'm' and 'e' flags in 'comments' 57" Test for automatically adding comment leaders in insert mode 58func Test_comment_threepiece () 59new 60setlocal expandtab 61call setline ( 1 , [ "\t/*" ]) 62setlocal formatoptions = croql 63call cursor ( 1 , 3 ) 64call feedkeys ( "A\<cr>\<cr>/" , 'tnix' ) 65call assert_equal ([ "\t/*" , " *" , " */" ], getline ( 1 , '$' )) 66 67" If a comment ends in a single line, then don't add it in the next line 68 %d 69call setline ( 1 , '/* line1 */' ) 70call feedkeys ( "A\<CR>next line" , 'xt' ) 71call assert_equal ([ '/* line1 */' , 'next line' ], getline ( 1 , '$' )) 72 73 %d 74" Copy the trailing indentation from the leader comment to a new line 75setlocal autoindent noexpandtab 76call feedkeys ( "a\t/*\tone\ntwo\n/" , 'xt' ) 77call assert_equal ([ "\t/*\tone" , "\t *\ttwo" , "\t */" ], getline ( 1 , '$' )) 78bw ! 79endfunc 80 81" Test for the 'r' flag in 'comments' (right align comment) 82func Test_comment_rightalign () 83new 84setlocal comments = sr:/***,m:**,ex-2:******/fo += ro 85exe "normal i=\<C-C>o\t /***\nD\n/" 86exe "normal 2GOA\<C-C>joB\<C-C>jOC\<C-C>joE\<C-C>GOF\<C-C>joG" 87let expected =<< trim END 88= 89A 90/*** 91** B 92** C 93** D 94** E 95** F 96******/ 97G 98END 99call assert_equal ( expected , getline ( 1 , '$' )) 100bw ! 101endfunc 102 103" Test for the 'O' flag in 'comments' 104func Test_comment_O () 105new 106setlocal comments = Ob:*fo += ro 107exe "normal i* B\nD\<C-C>kOA\<C-C>joC" 108let expected =<< trim END 109A 110* B 111* C 112* D 113END 114call assert_equal ( expected , getline ( 1 , '$' )) 115bw ! 116endfunc 117 118" Test for using a multibyte character as a comment leader 119func Test_comment_multibyte_leader () 120new 121let t =<< trim END 122{ 123X 124Xa 125XaY 126XY 127XYZ 128X Y 129X YZ 130XX 131XXa 132XXY 133} 134END 135call setline ( 1 , t ) 136call cursor ( 2 , 1 ) 137 138set tw = 2 fo = cqmcomments = n:X 139exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq" 140let t =<< trim END 141X 142Xa 143XaY 144XY 145XYZ 146X Y 147X YZ 148XX 149XXa 150XXY 151END 152exe "normal o\n" . join ( t , "\n" ) 153 154let expected =<< trim END 155{ 156X 157Xa 158Xa 159XY 160XY 161XY 162XZ 163X Y 164X Y 165X Z 166XX 167XXa 168XXY 169 170X 171Xa 172Xa 173XY 174XY 175XY 176XZ 177X Y 178X Y 179X Z 180XX 181XXa 182XXY 183} 184END 185call assert_equal ( expected , getline ( 1 , '$' )) 186 187set tw & fo & comments & 188bw ! 189endfunc 190 191" Test for a space character in 'comments' setting 192func Test_comment_space () 193new 194setlocal comments = b:\ >fo += ro 195exe "normal i> B\nD\<C-C>ggOA\<C-C>joC" 196exe "normal Go > F\nH\<C-C>kOE\<C-C>joG" 197let expected =<< trim END 198A 199> B 200C 201D 202> E 203> F 204> G 205> H 206END 207call assert_equal ( expected , getline ( 1 , '$' )) 208bw ! 209endfunc 210 211" Test for formatting lines with and without comments 212func Test_comment_format_lines () 213new 214call setline ( 1 , [ 'one' , '/* two */' , 'three' ]) 215normal gggqG 216call assert_equal ([ 'one' , '/* two */' , 'three' ], getline ( 1 , '$' )) 217bw ! 218endfunc 219 220" Test for using 'a' in 'formatoptions' with comments 221func Test_comment_autoformat () 222new 223setlocal formatoptions += a 224call feedkeys ( "a- one\n- two\n" , 'xt' ) 225call assert_equal ([ '- one' , '- two' , '' ], getline ( 1 , '$' )) 226 227 %d 228call feedkeys ( "a\none\n" , 'xt' ) 229call assert_equal ([ '' , 'one' , '' ], getline ( 1 , '$' )) 230 231setlocal formatoptions += aw 232 %d 233call feedkeys ( "aone \ntwo\n" , 'xt' ) 234call assert_equal ([ 'one two' , '' ], getline ( 1 , '$' )) 235 236 %d 237call feedkeys ( "aone\ntwo\n" , 'xt' ) 238call assert_equal ([ 'one' , 'two' , '' ], getline ( 1 , '$' )) 239 240set backspace = indent,eol,start 241 %d 242call feedkeys ( "aone \n\<BS>" , 'xt' ) 243call assert_equal ([ 'one' ], getline ( 1 , '$' )) 244set backspace & 245 246bw ! 247endfunc 248 249" Test for joining lines with comments ('j' flag in 'formatoptions') 250func Test_comment_join_lines_fo_j () 251new 252setlocal fo += jcomments = :// 253call setline ( 1 , [ 'i++; // comment1' , ' // comment2' ]) 254normal J 255call assert_equal ( 'i++; // comment1 comment2' , getline ( 1 )) 256setlocal fo -= j 257call setline ( 1 , [ 'i++; // comment1' , ' // comment2' ]) 258normal J 259call assert_equal ( 'i++; // comment1 // comment2' , getline ( 1 )) 260" Test with nested comments 261setlocal fo += jcomments = n:>,n:) 262call setline ( 1 , [ 'i++; > ) > ) comment1' , ' > ) comment2' ]) 263normal J 264call assert_equal ( 'i++; > ) > ) comment1 comment2' , getline ( 1 )) 265bw ! 266endfunc 267 268" Test for formatting lines where only the first line has a comment. 269func Test_comment_format_firstline_comment () 270new 271setlocal formatoptions = tcq 272call setline ( 1 , [ '- one two' , 'three' ]) 273normal gggqG 274call assert_equal ([ '- one two three' ], getline ( 1 , '$' )) 275 276 %d 277call setline ( 1 , [ '- one' , '- two' ]) 278normal gggqG 279call assert_equal ([ '- one' , '- two' ], getline ( 1 , '$' )) 280bw ! 281endfunc 282 283" vim: shiftwidth=2 sts=2 expandtab