Skip to content

Commit

Permalink
fix(ex.component): Fix extend method
Browse files Browse the repository at this point in the history
.

.
  • Loading branch information
vladimir-popov committed Jun 8, 2024
1 parent 02d2831 commit c561594
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lua/lualine/ex/component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lua/lualine/ex/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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' } })
Expand Down
53 changes: 53 additions & 0 deletions tests/component_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -67,6 +114,7 @@ describe('A child of the ex.component', function()
function Child:update_status()
return ''
end

local cmp = Child(u.opts())

-- when:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit c561594

Please sign in to comment.