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