Debian Codegraph @

tests/statusline.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3
4 --- ## Testing module for lualines statusline
5 ---
6 --- ###Uses:
7 ---
8 --- Create a new instance with status width 120 & for active statusline
9 --- like following.
10 ---
11 --- ``lua
12 --- local statusline = require('tests.statusline').new(120, 'active')
13 --- ```
14 ---
15 --- To create a new instance with status width 80 & for inactive statusline use following.
16 ---
17 --- ``lua
18 --- local statusline = require('tests.statusline').new(120, 'inactive')
19 --- ```
20 ---
21 --- Now setup the state you want to test.
22 --- To test you'll call `expect` method on statusline for example.
23 ---
24 --- To create a new instance with status width 80 & tabline
25 ---
26 --- ``lua
27 --- local statusline = require('tests.statusline').new(120, 'tabline')
28 --- ```
29 ---
30 --- Now setup the state you want to test.
31 --- To test you'll call `expect` method on statusline for example.
32 ---
33 --- ``lua
34 --- statusline:expect([===[
35 --- highlights = {
36 --- 1: lualine_c_inactive = { bg = "#3c3836", fg = "#a89984" }
37 --- }
38 --- |{1: [No Name] }
39 --- {1: }
40 --- {1: 0:1 }|
41 ---
42 ---]===])
43 --- ```
44 ---
45 --- For more flexibility you can match a pattern in expect block.
46 --- ``lua
47 --- statusline:expect([===[
48 --- highlights = {
49 --- 1: lualine_a_tabs_inactive = { bg = "#3c3836", bold = true, fg = "#a89984" }
50 --- 2: lualine_transitional_lualine_a_tabs_inactive_to_lualine_a_tabs_active = { bg = "#a89984", fg = "#3c3836" }
51 --- 3: lualine_a_tabs_active = { bg = "#a89984", bold = true, fg = "#282828" }
52 --- 4: lualine_transitional_lualine_a_tabs_active_to_lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
53 --- 5: lualine_c_normal = { bg = "#3c3836", fg = "#a89984" }
54 --- }
55 --- {MATCH:|{1: %d+ }}
56 --- {MATCH:{1: %d+ }}
57 --- {2:}
58 --- {MATCH:{3: %d+ }}
59 --- {4:}
60 --- {MATCH:{5:%s+}|}
61 ---
62 ---]===])
63 --- ```
64 ---
65 --- An easy way to create an expect block is to call `snapshot` method
66 --- on statusline where you'll call expect and run the test. It will print
67 --- an expect block based on the state of statusline. You can copy it and
68 --- replace the snapshot call with the expect call.
69 ---
70 --- ``lua
71 --- statusline:snapshot()
72 --- ```
73
74 local helpers = require('tests.helpers')
75 local stub = require('luassert.stub')
76
77 local M = {}
78
79 local function eval_stl(stl_expr, width, eval_type)
80 local stl_buf, hl_list, stl_eval_res
81 if stl_expr == nil then
82 return nil
83 end
84 stl_eval_res = vim.api.nvim_eval_statusline(
85 stl_expr,
86 { maxwidth = width, highlights = true, fillchar = ' ', use_tabline = (eval_type == 'tabline') }
87 )
88 stl_buf, hl_list = stl_eval_res.str, stl_eval_res.highlights
89
90 local hl_map = {}
91
92 local buf = { 'highlights = {' }
93 local hl_id = 1
94 for _, hl in ipairs(hl_list) do
95 local hl_name = hl.group
96 if not hl_map[hl_name] then
97 hl_map[hl_name] = require('lualine.utils.utils').extract_highlight_colors(hl_name) or {}
98 table.insert(
99 buf,
100 string.format(' %4d: %s = %s', hl_id, hl_name, vim.inspect(hl_map[hl_name], { newline = ' ', indent = '' }))
101 )
102 hl_map[hl_name].id = hl_id
103 hl_id = hl_id + 1
104 end
105 end
106 table.insert(buf, '}')
107
108 local stl = {}
109 for i = 1, #hl_list do
110 local start, finish = hl_list[i].start, hl_list[i + 1] and hl_list[i + 1].start or #stl_buf
111 if start ~= finish then
112 table.insert(
113 stl,
114 string.format('{%d:%s}', hl_map[hl_list[i].group].id, vim.fn.strpart(stl_buf, start, finish - start))
115 )
116 end
117 end
118 table.insert(buf, '|' .. table.concat(stl, '\n') .. '|')
119 table.insert(buf, '')
120 return table.concat(buf, '\n')
121 end
122
123 function M:expect_expr(expect, expr)
124 if expr == nil then
125 -- test if both are nil when running expect against nil
126 assert.are.same(expect, nil)
127 return
128 end
129 local actual = eval_stl(expr, self.width, self.type)
130 if expect == nil then
131 assert.are.same(expect, actual)
132 return
133 end
134 expect = helpers.dedent(expect)
135 local matched = true
136 local errmsg = {}
137 if expect ~= actual then
138 expect = expect ~= nil and vim.split(expect, '\n')
139 actual = vim.split(actual, '\n')
140 if expect[#expect] == '' then
141 expect[#expect] = nil
142 end
143 if actual[#actual] == '' then
144 actual[#actual] = nil
145 end
146 for i = 1, math.max(#expect, #actual) do
147 if expect[i] and actual[i] then
148 local match_pat = expect[i]:match('{MATCH:(.*)}')
149 if expect[i] == actual[i] or (match_pat and actual[i]:match(match_pat)) then
150 expect[i] = string.rep(' ', 2) .. expect[i]
151 actual[i] = string.rep(' ', 2) .. actual[i]
152 goto loop_end
153 end
154 end
155 matched = false
156 if expect[i] then
157 expect[i] = '*' .. string.rep(' ', 1) .. expect[i]
158 end
159 if actual[i] then
160 actual[i] = '*' .. string.rep(' ', 1) .. actual[i]
161 end
162 ::loop_end::
163 end
164 end
165 if not matched then
166 table.insert(errmsg, 'Unexpected statusline')
167 table.insert(errmsg, 'Expected:')
168 table.insert(errmsg, table.concat(expect, '\n') .. '\n')
169 table.insert(errmsg, 'Actual:')
170 table.insert(errmsg, table.concat(actual, '\n'))
171 end
172 assert(matched, table.concat(errmsg, '\n'))
173 end
174
175 function M:snapshot_expr(expr)
176 local type_map = {
177 active = 'statusline',
178 inactive = 'inactive_statusline',
179 tabline = 'tabline',
180 }
181 if expr == nil then
182 print((type_map[self.type] or 'statusline') .. ':expect(nil)')
183 return
184 end
185 print((type_map[self.type] or 'statusline') .. ':expect([===[')
186 print(eval_stl(expr, self.width, self.type) .. ']===])')
187 end
188
189 function M:snapshot()
190 local utils = require('lualine.utils.utils')
191 stub(utils, 'is_focused')
192 utils.is_focused.returns(self.type ~= 'inactive')
193 local expr
194 if self.type == 'inactive' then
195 expr = require('lualine').statusline(false)
196 elseif self.type == 'tabline' then
197 expr = require('lualine').tabline()
198 else
199 expr = require('lualine').statusline(true)
200 end
201 self:snapshot_expr(expr)
202 utils.is_focused:revert()
203 end
204
205 function M:expect(result)
206 local utils = require('lualine.utils.utils')
207 stub(utils, 'is_focused')
208 utils.is_focused.returns(self.type ~= 'inactive')
209 local expr
210 if self.type == 'inactive' then
211 expr = require('lualine').statusline(false)
212 elseif self.type == 'tabline' then
213 expr = require('lualine').tabline()
214 else
215 expr = require('lualine').statusline(true)
216 end
217 self:expect_expr(result, expr)
218 utils.is_focused:revert()
219 end
220
221 function M.new(_, width, eval_type)
222 if type(_) ~= 'table' then
223 eval_type = width
224 width = _
225 end
226 local self = {}
227 self.width = width or 120
228 self.type = eval_type
229 if self.type == nil then
230 self.type = 'active'
231 end
232 return setmetatable(self, {
233 __index = M,
234 __call = function(_, ...)
235 M.new(...)
236 end,
237 })
238 end
239
240 return M.new()