summaryrefslogtreecommitdiff
path: root/test/test_nvim_api_highlight.vader
blob: 829c3ad74c72423b079f9ad66949ee48ef6a2ff0 (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
Before:
  Save g:ale_enabled
  Save g:ale_set_signs

  let g:nvim_buf_clear_namespace_calls = []
  let g:nvim_buf_add_highlight_calls = []

  call ale#test#SetDirectory('/testplugin/test/highlight')
  call ale#test#SetFilename('dummy.txt')

  runtime autoload/ale/highlight.vim

  let g:ale_set_signs = 1
  let g:ale_enabled = 1
  let b:ale_nvim_highlight_id = 42
  let b:ale_highlight_items = []

  function! ale#highlight#nvim_buf_clear_namespace(...) abort
    call add(g:nvim_buf_clear_namespace_calls, a:000)
  endfunction

  function! ale#highlight#nvim_buf_add_highlight(...) abort
    call add(g:nvim_buf_add_highlight_calls, a:000)
    return 42 " returns namespace id
  endfunction

After:
  Restore

  unlet! b:ale_enabled
  unlet! b:ale_nvim_highlight_id
  unlet! b:ale_highlight_items

  unlet! g:nvim_buf_clear_namespace_calls
  unlet! g:nvim_buf_add_highlight_calls

  call ale#test#RestoreDirectory()
  call ale#linter#Reset()

  runtime autoload/ale/highlight.vim

Given foobar (Some imaginary filetype):
  <contents>

Execute(Check usage of nvim_buf_clear_namespace):
  if ale#highlight#HasNeovimApi()
    call ale#highlight#SetHighlights(bufnr(''), [])

    AssertEqual 1, len(g:nvim_buf_clear_namespace_calls)
    AssertEqual
    \ [[bufnr(''), 42, 0, -1]],
    \ g:nvim_buf_clear_namespace_calls
  endif

Execute(Check usage of nvim_buf_add_highlight / single char):
  if ale#highlight#HasNeovimApi()
    call ale#highlight#SetHighlights(bufnr(''), [
    \ {
    \   'bufnr': bufnr(''),
    \   'type': 'E',
    \   'lnum': 2,
    \   'col': 4,
    \ }
    \])

    " Highlights are cleared on update
    AssertEqual 1, len(g:nvim_buf_clear_namespace_calls)
    AssertEqual [[bufnr(''), 42, 0, -1]], g:nvim_buf_clear_namespace_calls

    " Should highlight a single char by lnum, col
    AssertEqual 1, len(g:nvim_buf_add_highlight_calls)
    AssertEqual
    \ [[bufnr(''), 42, 'ALEError', 1, 3, 4]],
    \ g:nvim_buf_add_highlight_calls
  endif

Execute(Check usage of nvim_buf_add_highlight / single line span):
  if ale#highlight#HasNeovimApi()
    call ale#highlight#SetHighlights(bufnr(''), [
    \ {
    \   'bufnr': bufnr(''),
    \   'type': 'E',
    \   'lnum': 2,
    \   'col': 4,
    \   'end_lnum': 2,
    \   'end_col': 10,
    \ }
    \])

    " Highlights are cleared on update
    AssertEqual 1, len(g:nvim_buf_clear_namespace_calls)
    AssertEqual [[bufnr(''), 42, 0, -1]], g:nvim_buf_clear_namespace_calls

    " Should highlight a span between col and end_col on lnum
    AssertEqual 1, len(g:nvim_buf_add_highlight_calls)
    AssertEqual
    \ [[bufnr(''), 42, 'ALEError', 1, 3, 10]],
    \ g:nvim_buf_add_highlight_calls
  endif

Execute(Check usage of nvim_buf_add_highlight / multiple lines span):
  if ale#highlight#HasNeovimApi()
    call ale#highlight#SetHighlights(bufnr(''), [
    \ {
    \   'bufnr': bufnr(''),
    \   'type': 'E',
    \   'lnum': 2,
    \   'col': 4,
    \   'end_lnum': 5,
    \   'end_col': 10,
    \ }
    \])

    " Highlights are cleared on update
    AssertEqual 1, len(g:nvim_buf_clear_namespace_calls)
    AssertEqual [[bufnr(''), 42, 0, -1]], g:nvim_buf_clear_namespace_calls

    " Should highlight all lines from lnum till end_lnum
    AssertEqual 4, len(g:nvim_buf_add_highlight_calls)
    AssertEqual
    \ [
    \   [bufnr(''), 42, 'ALEError', 1, 3, -1],
    \   [bufnr(''), 42, 'ALEError', 2, 0, -1],
    \   [bufnr(''), 42, 'ALEError', 3, 0, -1],
    \   [bufnr(''), 42, 'ALEError', 4, 0, 10]
    \ ],
    \ g:nvim_buf_add_highlight_calls
  endif

Execute(Check usage of nvim_buf_add_highlight / line highights):
  let g:ale_set_signs = 0

  if ale#highlight#HasNeovimApi()
    call ale#highlight#SetHighlights(bufnr(''), [
    \ {
    \   'bufnr': bufnr(''),
    \   'type': 'E',
    \   'lnum': 2,
    \   'col': 4,
    \   'end_lnum': 5,
    \   'end_col': 10,
    \ }
    \])

    " Highlights are cleared on update
    AssertEqual 1, len(g:nvim_buf_clear_namespace_calls)
    AssertEqual [[bufnr(''), 42, 0, -1]], g:nvim_buf_clear_namespace_calls

    " Now the last highlight should be put on the entire line
    AssertEqual 5, len(g:nvim_buf_add_highlight_calls)
    AssertEqual
    \ [
    \   [bufnr(''), 42, 'ALEError', 1, 3, -1],
    \   [bufnr(''), 42, 'ALEError', 2, 0, -1],
    \   [bufnr(''), 42, 'ALEError', 3, 0, -1],
    \   [bufnr(''), 42, 'ALEError', 4, 0, 10],
    \   [bufnr(''), 42, 'ALEErrorLine', 1, 0, -1]
    \ ],
    \ g:nvim_buf_add_highlight_calls
  endif