summaryrefslogtreecommitdiff
path: root/test/test_format_command.vader
blob: 119c71385f5cffc2220ff387f75b191034d9848b (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
Before:
  silent! cd /testplugin/test
  silent file top/middle/bottom/dummy.txt

  function! CheckTempFile(filename) abort
    " Check every part of the temporary filename, except the random part.
    AssertEqual fnamemodify(tempname(), ':h'), fnamemodify(a:filename, ':h:h')
    AssertEqual 'dummy.txt', fnamemodify(a:filename, ':t')
  endfunction

  function! TestCreateFunc(buffer, temporary_file) abort
    return !empty(a:temporary_file)
  endfunction

After:
  unlet! g:result
  unlet! g:match

  delfunction CheckTempFile
  delfunction TestCreateFunc

Execute(FormatCommand should do nothing to basic command strings):
  AssertEqual
  \ ['', 'awesome-linter do something', 0],
  \ ale#command#FormatCommand(bufnr('%'), '', 'awesome-linter do something', 0, function('TestCreateFunc'))

Execute(FormatCommand should handle %%, and ignore other percents):
  AssertEqual
  \ ['', '% %%d %%f %x %', 0],
  \ ale#command#FormatCommand(bufnr('%'), '', '%% %%%d %%%f %x %', 0, function('TestCreateFunc'))

Execute(FormatCommand should convert %s to the current filename):
  AssertEqual
  \ [
  \   '',
  \   'foo ' . ale#Escape(expand('%:p')) . ' bar ' . ale#Escape(expand('%:p')),
  \   0,
  \ ],
  \ ale#command#FormatCommand(bufnr('%'), '', 'foo %s bar %s', 0, function('TestCreateFunc'))

Execute(FormatCommand should convert %t to a new temporary filename):
  let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %t', 0, function('TestCreateFunc'))

  call CheckTempFile(g:result[0])

  let g:match = matchlist(g:result[1], '\v^foo (.*) bar (.*)$')

  Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
  " The first item of the result should be a temporary filename, and it should
  " be the same as the escaped name in the command string.
  AssertEqual ale#Escape(g:result[0]), g:match[1]
  " The two temporary filenames formatted in should be the same.
  AssertEqual g:match[1], g:match[2]

Execute(FormatCommand should signal that files are created when temporary files are needed):
  AssertEqual
  \ 1,
  \ ale#command#FormatCommand(bufnr('%'), '', 'foo %t', 0, function('TestCreateFunc'))[2]

  AssertEqual
  \ 0,
  \ ale#command#FormatCommand(bufnr('%'), '', 'foo %s', 0, function('TestCreateFunc'))[2]

Execute(FormatCommand should let you combine %s and %t):
  let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo %t bar %s', 0, function('TestCreateFunc'))

  call CheckTempFile(g:result[0])

  let g:match = matchlist(g:result[1], '\v^foo (.*) bar (.*)$')

  Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
  " The first item of the result should be a temporary filename, and it should
  " be the same as the escaped name in the command string.
  AssertEqual ale#Escape(g:result[0]), g:match[1]
  " The second item should be equal to the original filename.
  AssertEqual ale#Escape(expand('%:p')), g:match[2]

Execute(FormatCommand should replace %e with the escaped executable):
  if has('win32')
    AssertEqual
    \ ['', 'foo foo', 0],
    \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, function('TestCreateFunc'))
    AssertEqual
    \ ['', '"foo bar"', 0],
    \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, function('TestCreateFunc'))
    AssertEqual
    \ ['', '%e %e', 0],
    \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, function('TestCreateFunc'))
  else
    AssertEqual
    \ ['', '''foo'' ''foo''', 0],
    \ ale#command#FormatCommand(bufnr('%'), 'foo', '%e %e', 0, function('TestCreateFunc'))
    AssertEqual
    \ ['', '''foo bar''', 0],
    \ ale#command#FormatCommand(bufnr('%'), 'foo bar', '%e', 0, function('TestCreateFunc'))
    AssertEqual
    \ ['', '%e %e', 0],
    \ ale#command#FormatCommand(bufnr('%'), '', '%e %e', 0, function('TestCreateFunc'))
  endif

Execute(EscapeCommandPart should escape all percent signs):
  AssertEqual '%%s %%t %%%% %%s %%t %%%%', ale#engine#EscapeCommandPart('%s %t %% %s %t %%')

Execute(EscapeCommandPart should pipe in temporary files appropriately):
  let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar', 1, function('TestCreateFunc'))

  call CheckTempFile(g:result[0])

  let g:match = matchlist(g:result[1], '\v^foo bar \< (.*)$')
  Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
  AssertEqual ale#Escape(g:result[0]), g:match[1]

  let g:result = ale#command#FormatCommand(bufnr('%'), '', 'foo bar %t', 1, function('TestCreateFunc'))

  call CheckTempFile(g:result[0])

  let g:match = matchlist(g:result[1], '\v^foo bar (.*)$')
  Assert !empty(g:match), 'No match found! Result was: ' . g:result[1]
  AssertEqual ale#Escape(g:result[0]), g:match[1]