summaryrefslogtreecommitdiff
path: root/test/test_c_flag_parsing.vader
blob: 045554a35ab3e8b994f9c4ab19ed9b6c1621da81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
Before:
  Save g:ale_c_parse_makefile

  call ale#test#SetDirectory('/testplugin/test')

  let g:ale_c_parse_makefile = 1

After:
  Restore

  call ale#test#RestoreDirectory()

Execute(The CFlags parser should be able to parse include directives):
  call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')

  AssertEqual
  \ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'),
  \ ale#c#ParseCFlagsFromMakeOutput(bufnr(''), ['gcc -Isubdir -c file.c'])

  AssertEqual
  \ '-isystem ' . '/usr/include/dir',
  \ ale#c#ParseCFlagsFromMakeOutput(bufnr(''), ['gcc -isystem /usr/include/dir -c file.c'])

Execute(ParseCFlags should ignore -c and -o):
  call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')

  AssertEqual
  \ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'),
  \ ale#c#ParseCFlagsFromMakeOutput(bufnr(''), ['gcc -Isubdir -c file.c -o a.out'])

Execute(The CFlags parser should be able to parse macro directives):
  call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')

  AssertEqual
  \ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   . ' -DTEST=1',
  \ ale#c#ParseCFlagsFromMakeOutput(bufnr(''), ['gcc -Isubdir -DTEST=1 -c file.c'])

Execute(The CFlags parser should be able to parse macro directives with spaces):
  call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')

  AssertEqual
  \ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   . ' -DTEST=$(( 2 * 4 ))',
  \ ale#c#ParseCFlagsFromMakeOutput(bufnr(''), ['gcc -Isubdir -DTEST=$(( 2 * 4 )) -c file.c'])

Execute(The CFlags parser should be able to parse shell directives with spaces):
  call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')

  AssertEqual
  \ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   .  ' -DTEST=`date +%s`',
  \ ale#c#ParseCFlagsFromMakeOutput(bufnr(''), ['gcc -Isubdir -DTEST=`date +%s` -c file.c'])

Execute(ParseCFlags should be able to parse flags with relative paths):
  AssertEqual
  \ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')
  \   . ' -DTEST=`date +%s`',
  \ ale#c#ParseCFlags(
  \   ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
  \   'gcc -Isubdir  '
  \     .  '-I'. ale#path#Simplify('kernel/include')
  \     .  ' -DTEST=`date +%s` -c file.c'
  \ )

Execute(ParseCFlags should be able to parse -Dgoal):
  AssertEqual
  \ '-Dgoal=9'
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')
  \   . ' -DTEST=`date +%s`',
  \ ale#c#ParseCFlags(
  \   ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
  \   'gcc -Dgoal=9 -Isubdir '
  \     . '-I'. ale#path#Simplify('kernel/include')
  \     . ' -DTEST=`date +%s` -c file.c'
  \ )

Execute(ParseCFlags should handle paths with spaces in double quotes):
  AssertEqual
  \ '-Dgoal=9'
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/"dir with spaces"')
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')
  \   . ' -DTEST=`date +%s`',
  \ ale#c#ParseCFlags(
  \   ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
  \   'gcc -Dgoal=9 -Isubdir '
  \     . '-I"dir with spaces"' . ' -I'. ale#path#Simplify('kernel/include')
  \     . ' -DTEST=`date +%s` -c file.c'
  \ )

Execute(ParseCFlags should handle paths with spaces in single quotes):
  AssertEqual
  \ '-Dgoal=9'
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. "/test_c_projects/makefile_project/'dir with spaces'")
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')
  \   . ' -DTEST=`date +%s`',
  \ ale#c#ParseCFlags(
  \   ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
  \   'gcc -Dgoal=9 -Isubdir '
  \     . "-I'dir with spaces'" . ' -I'. ale#path#Simplify('kernel/include')
  \     . ' -DTEST=`date +%s` -c file.c'
  \ )

Execute(ParseCFlags should handle paths with minuses):
  AssertEqual
  \ '-Dgoal=9'
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/dir-with-dash')
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')
  \   . ' -DTEST=`date +%s`',
  \ ale#c#ParseCFlags(
  \   ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
  \   'gcc -Dgoal=9 -Isubdir '
  \     . ' -Idir-with-dash'
  \     . ' -I'. ale#path#Simplify('kernel/include')
  \     . ' -DTEST=`date +%s` -c file.c'
  \ )

Execute(ParseCFlags should handle -D with minuses):
  AssertEqual
  \ '-Dgoal=9'
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   . ' -Dmacro-with-dash'
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')
  \   . ' -DTEST=`date +%s`',
  \ ale#c#ParseCFlags(
  \   ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
  \   'gcc -Dgoal=9 -Isubdir '
  \     . '-Dmacro-with-dash '
  \     . ' -I'. ale#path#Simplify('kernel/include')
  \     . ' -DTEST=`date +%s` -c file.c'
  \ )

Execute(ParseCFlags should handle flags at the end of the line):
  AssertEqual
  \ '-Dgoal=9'
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')
  \   . ' ' . '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include'),
  \ ale#c#ParseCFlags(
  \   ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
  \   'gcc -Dgoal=9 -Isubdir '
  \     . ' -I'. ale#path#Simplify('kernel/include')
  \ )

Execute(FlagsFromCompileCommands should tolerate empty values):
  AssertEqual '', ale#c#FlagsFromCompileCommands(bufnr(''), '')

Execute(ParseCompileCommandsFlags should tolerate empty values):
  AssertEqual '', ale#c#ParseCompileCommandsFlags(bufnr(''), {}, {})

Execute(ParseCompileCommandsFlags should parse some basic flags):
  silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.c'))

  AssertEqual
  \ '-I/usr/include/xmms2',
  \ ale#c#ParseCompileCommandsFlags(bufnr(''), { "xmms2-mpris.c": [
  \   {
  \     'directory': '/foo/bar/xmms2-mpris',
  \     'command': '/usr/bin/cc  -I' . '/usr/include/xmms2'
  \       . '    -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
  \       . '   -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
  \     'file': '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
  \   },
  \ ] }, {})

Execute(ParseCompileCommandsFlags should tolerate items without commands):
  silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.c'))

  AssertEqual
  \ '',
  \ ale#c#ParseCompileCommandsFlags(bufnr(''), { "xmms2-mpris.c": [
  \   {
  \     'directory': '/foo/bar/xmms2-mpris',
  \     'file': '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
  \   },
  \ ] }, {})

Execute(ParseCompileCommandsFlags should fall back to files in the same directory):
  silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.c'))

  AssertEqual
  \ '-I/usr/include/xmms2',
  \ ale#c#ParseCompileCommandsFlags(bufnr(''), {}, { "src": [
  \   {
  \     'directory': '/foo/bar/xmms2-mpris',
  \     'command': '/usr/bin/cc  -I' . '/usr/include/xmms2'
  \       . '    -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
  \       . '   -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
  \     'file': (has('win32') ? 'C:' : '') . '/foo/bar/xmms2-mpris/src/xmms2-other.c',
  \   },
  \ ] })

Execute(ParseCompileCommandsFlags should take commands from matching .c files for .h files):
  silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.h'))

  AssertEqual
  \ '-I/usr/include/xmms2',
  \ ale#c#ParseCompileCommandsFlags(
  \   bufnr(''),
  \   {
  \     'xmms2-mpris.c': [
  \       {
  \         'directory': '/foo/bar/xmms2-mpris',
  \         'file': (has('win32') ? 'C:' : '') . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
  \         'command': '/usr/bin/cc  -I' . '/usr/include/xmms2'
  \         . '    -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
  \         . '   -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
  \       },
  \     ],
  \   },
  \   {
  \   },
  \ )

Execute(ParseCompileCommandsFlags should take commands from matching .cpp files for .hpp files):
  silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.hpp'))

  AssertEqual
  \ '-I/usr/include/xmms2',
  \ ale#c#ParseCompileCommandsFlags(
  \   bufnr(''),
  \   {
  \     'xmms2-mpris.cpp': [
  \       {
  \         'directory': '/foo/bar/xmms2-mpris',
  \         'file': (has('win32') ? 'C:' : '') . '/foo/bar/xmms2-mpris/src/xmms2-mpris.cpp',
  \         'command': '/usr/bin/cc  -I' . '/usr/include/xmms2'
  \         . '    -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
  \         . '   -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.cpp',
  \       },
  \     ],
  \   },
  \   {
  \   },
  \ )

Execute(ParseCompileCommandsFlags should take commands from matching .cpp files for .h files):
  silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.h'))

  AssertEqual
  \ '-I/usr/include/xmms2',
  \ ale#c#ParseCompileCommandsFlags(
  \   bufnr(''),
  \   {
  \     'xmms2-mpris.cpp': [
  \       {
  \         'directory': '/foo/bar/xmms2-mpris',
  \         'file': (has('win32') ? 'C:' : '') . '/foo/bar/xmms2-mpris/src/xmms2-mpris.cpp',
  \         'command': '/usr/bin/cc  -I' . '/usr/include/xmms2'
  \         . '    -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
  \         . '   -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.cpp',
  \       },
  \     ],
  \   },
  \   {
  \   },
  \ )

Execute(ParseCompileCommandsFlags should not take commands from .c files for .h files with different names):
  silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/other.h'))

  AssertEqual
  \ '',
  \ ale#c#ParseCompileCommandsFlags(
  \   bufnr(''),
  \   {
  \     'xmms2-mpris.c': [
  \       {
  \         'directory': '/foo/bar/xmms2-mpris',
  \         'file': (has('win32') ? 'C:' : '') . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
  \         'command': '/usr/bin/cc  -I' . '/usr/include/xmms2'
  \         . '    -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
  \         . '   -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
  \       },
  \     ],
  \   },
  \   {
  \   },
  \ )

Execute(ParseCFlags should handle parenthesis and quotes):
  AssertEqual
  \ '-Dgoal=9 -Dtest1="('' '')" file1.o -Dtest2=''(` `)'' file2.o -Dtest3=`(" ")` file3.o',
  \ ale#c#ParseCFlags(
  \   ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
  \   'gcc -Dgoal=9 '
  \     . '-Dtest1="('' '')" file1.o '
  \     . '-Dtest2=''(` `)'' file2.o '
  \     . '-Dtest3=`(" ")` file3.o '
  \ )