Debian Codegraph @

tests/helpers.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3
4 assert = require('luassert')
5 local eq = assert.are.same
6
7 local M = {}
8
9 M.meths = setmetatable({}, {
10 __index = function(_, key)
11 return vim.api['nvim_' .. key]
12 end,
13 })
14
15 function M.init_component(component, opts)
16 if component == nil then
17 component = 'special.function_component'
18 else
19 opts.component_name = component
20 end
21 local comp = require('lualine.components.' .. component)
22 if type(comp) == 'table' then
23 comp = comp(opts)
24 elseif type(comp) == 'function' then
25 opts[1] = comp
26 comp = require('lualine.components.special.function_component')(opts)
27 end
28 return comp
29 end
30
31 -- Checks output of a component
32 M.assert_component = function(component, opts, result, is_active)
33 local comp = M.init_component(component, opts)
34 -- for testing global options
35 eq(result, comp:draw(opts.hl, is_active or true))
36 end
37
38 function M.assert_component_instance(comp, result)
39 eq(result, comp:draw(comp.options.hl))
40 end
41 -- sets defaults for component options
42 M.build_component_opts = function(opts)
43 if not opts then
44 opts = {}
45 end
46 if opts[1] == nil then
47 opts[1] = function()
48 return 'test'
49 end
50 end
51 if not opts.self then
52 opts.self = { section = 'c' }
53 end
54 if not opts.theme then
55 opts.theme = 'gruvbox'
56 end
57 if not opts.hl then
58 opts.hl = ''
59 end
60 if opts.icons_enabled == nil then
61 opts.icons_enabled = true
62 end
63 if not opts.component_separators then
64 opts.component_separators = { left = '', right = '' }
65 end
66 if not opts.section_separators then
67 opts.section_separators = { left = '', right = '' }
68 end
69 return opts
70 end
71
72 M.P = function(t)
73 print(vim.inspect(t))
74 end
75
76 function M.dedent(str, leave_indent)
77 if str == nil then
78 return nil
79 end
80 -- find minimum common indent across lines
81 local indent = nil
82 for line in str:gmatch('[^\n]+') do
83 local line_indent = line:match('^%s+') or ''
84 if indent == nil or #line_indent < #indent then
85 indent = line_indent
86 end
87 end
88 if indent == nil or #indent == 0 then
89 -- no minimum common indent
90 return str
91 end
92 local left_indent = (' '):rep(leave_indent or 0)
93 -- create a pattern for the indent
94 indent = indent:gsub('%s', '[ \t]')
95 -- strip it from the first line
96 str = str:gsub('^' .. indent, left_indent)
97 -- strip it from the remaining lines
98 str = str:gsub('[\n]' .. indent, '\n' .. left_indent)
99 return str
100 end
101
102 return M