diff --git a/lua/lualine/ex/component.lua b/lua/lualine/ex/component.lua index 2d20516..ae2c3ba 100644 --- a/lua/lualine/ex/component.lua +++ b/lua/lualine/ex/component.lua @@ -16,15 +16,19 @@ local log = require('plenary.log').new({ plugin = 'ex.component' }) ---@field default_options table ---@field options ExComponentOptions -local Ex = require('lualine.component'):extend() +local LC = require('lualine.component') +-- extends all methods from the `lualine.component`: +local Ex = LC:extend() +-- override the `extend` method to be able to receive the default options: function Ex:extend(default_options) - local cls = self.super.extend(self) - cls.default_options = ex.merge(vim.deepcopy(default_options or {}), { + local cls = LC.extend(self) + local parent_default_options = ex.merge(vim.deepcopy(self.default_options or {}), { disabled_color = { fg = 'grey' }, disabled_icon_color = { fg = 'grey' }, draw_empty = true, }) + cls.default_options = vim.tbl_extend('force', parent_default_options, default_options or {}) return cls end diff --git a/lua/lualine/ex/init.lua b/lua/lualine/ex/init.lua index a80bf90..a6fe221 100644 --- a/lua/lualine/ex/init.lua +++ b/lua/lualine/ex/init.lua @@ -30,6 +30,8 @@ end ---@type fun(dest: table, source: table): table --- Puts all absent key-value pairs from the {source} to the {dest}. +---@param dest table a table to which data should be added. +---@param source table a table from which a data should be copied. ---@return table dest with added pairs. M.merge = function(dest, source, already_visited) vim.validate({ dest = { dest, 'table' }, source = { source, 'table' } }) diff --git a/tests/component_spec.lua b/tests/component_spec.lua index 85e27e9..7df4a53 100644 --- a/tests/component_spec.lua +++ b/tests/component_spec.lua @@ -7,6 +7,52 @@ local same = assert.are.same local l = require('tests.ex.lualine') local t = require('tests.ex.busted') --:ignore_all_tests() +describe('ex.component', function() + it('should have the same methods as lualine.component', function() + local Test = require('lualine.ex.component'):extend() + -- check only few: + assert(type(Test.update_status) == 'function') + assert(type(Test.draw) == 'function') + end) + it('child should have the same methods as ex.component', function() + local Test = require('lualine.ex.component'):extend() + -- check only few: + assert(type(Test.pre_init) == 'function') + assert(type(Test.post_init) == 'function') + end) + it('child`s default options should include parent`s', function() + -- given: + local Test = require('lualine.ex.component'):extend({ parent_opt = true }) + -- when: + local Child = Test:extend({ child_opt = true }) + -- then: + local clue = vim.inspect(Child.default_options) + assert(Child.default_options.parent_opt, clue) + assert(Child.default_options.child_opt, clue) + end) + it('child`s default options should override parent`s', function() + -- given: + local Test = require('lualine.ex.component'):extend({ opt = false }) + -- when: + local Child = Test:extend({ opt = true }) + -- then: + local clue = vim.inspect(Child.default_options) + assert(Child.default_options.opt, clue) + end) + it('instance of the child should have the same methods as a parent', function() + -- given: + local Test = require('lualine.ex.component'):extend() + function Test:test() + return true + end + + local Child = Test:extend() + -- when: + local child = Child:new(u.opts({ icon = { align = 'right' } })) + -- then: + assert(type(child.test) == 'function') + end) +end) describe('A child of the ex.component', function() it('should have the passed default options as a property', function() -- given: @@ -46,6 +92,7 @@ describe('A child of the ex.component', function() function Ex:post_init() passed_opts = self.options end + -- when: Ex(init_opts) -- then: @@ -67,6 +114,7 @@ describe('A child of the ex.component', function() function Child:update_status() return '' end + local cmp = Child(u.opts()) -- when: @@ -88,6 +136,7 @@ describe('A child of the ex.component', function() function Child:update_status() return 'some_text' end + local cmp = Child(u.opts()) -- when: @@ -103,9 +152,11 @@ describe('A child of the ex.component', function() function Child:update_status() return 'some_text' end + function Child:is_enabled() return false end + local cmp = Child(u.opts()) -- when: @@ -130,9 +181,11 @@ describe('A child of the ex.component', function() function Child:update_status() return 'some_text' end + function Child:is_enabled() return is_enabled end + local cmp = Child(u.opts()) -- when: