Skip to content

Commit

Permalink
Merge pull request #180 from adrianocastro189/version-1.13.0/os3
Browse files Browse the repository at this point in the history
Add the CommandsHandler:addSettingsOperation() method
  • Loading branch information
adrianocastro189 authored Sep 10, 2024
2 parents 3bf3fc7 + 12d2705 commit 1501283
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 14 deletions.
41 changes: 41 additions & 0 deletions src/Commands/CommandsHandler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ local CommandsHandler = {}
To be accessible by the get operation, the setting must be registered in the
settings handler as accessible by command.
@TODO: Move the callback in this method to a separate function or class <2024.09.10>
@see Core.Settings.Setting.setAccessibleByCommand
@local
Expand Down Expand Up @@ -110,6 +112,8 @@ local CommandsHandler = {}
To be accessible by the set operation, the setting must be registered in the
settings handler as accessible by command.
@TODO: Move the callback in this method to a separate function or class <2024.09.10>
@see Core.Settings.Setting.setAccessibleByCommand
@local
Expand All @@ -128,6 +132,43 @@ local CommandsHandler = {}
end)
end

--[[--
Adds a settings operation to the commands handler.
The settings operation is a default operation that can be overridden in case the
addon wants to provide a custom settings command. This implementation prints a
list of all settings that are accessible by command and their descriptions.
To be listed by the settings operation, the setting must be registered in the
settings handler as accessible by command.
@TODO: Move the callback in this method to a separate function or class <2024.09.10>
@see Core.Settings.Setting.setAccessibleByCommand
@local
]]
function CommandsHandler:addSettingsOperation()
self:addOperation('settings', 'Lists all the setting ids that can be used by get or set', function ()
local introduction =
'Available settings, that can be retrieved with '..
self.__.output:color(self.slashCommand..' get {id}')..' '..
'and updated with '..
self.__.output:color(self.slashCommand..' set {id}')..' '..
'by replacing '..
self.__.output:color('{id}')..' '..
'with any of the ids listed below'

local helpContent = {introduction}

self.__.arr:each(self.__.settings:allAccessibleByCommand(), function (setting)
table.insert(helpContent, setting:getCommandHelpContent())
end)

self.__.output:out(helpContent)
end)
end

--[[--
Builds a help content that lists all available operations and their
descriptions.
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Settings/SettingGroup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ local SettingGroup = {}
]]
function SettingGroup:allAccessibleByCommand()
return self.__.arr:filter(self.settings, function(setting)
return setting:isAccessibleByCommand()
return setting.accessibleByCommand
end)
end

Expand Down Expand Up @@ -111,7 +111,7 @@ local SettingGroup = {}
]]
function SettingGroup:hasSettingsAccessibleByCommand()
return self.__.arr:any(self.settings, function(setting)
return setting:isAccessibleByCommand()
return setting.accessibleByCommand
end)
end

Expand Down
39 changes: 39 additions & 0 deletions tests/Commands/CommandsHandlerTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,43 @@ TestCase.new()
end,
})
:register()

-- @covers CommandsHandler settings operation
TestCase.new()
:setName('settings operation')
:setTestClass(TestCommandsHandler)
:setExecution(function(data)
local settingA = Spy
.new(__:new('Setting'))
:mockMethod('getCommandHelpContent', function() return 'setting-a' end)

local settingB = Spy
.new(__:new('Setting'))
:mockMethod('getCommandHelpContent', function() return 'setting-b' end)

local handler = __:new('CommandsHandler')

handler.__.settings = Spy
.new(handler.__.settings)
:mockMethod('allAccessibleByCommand', function() return {settingA, settingB} end)

handler.slashCommand = '/test'

handler.__.output = Spy
.new(handler.__.output)
:mockMethod('out')

handler:addSettingsOperation()

local callback = handler.operations['settings'].callback

callback()

handler.__.output:getMethod('out'):assertCalledOnceWith({
'Available settings, that can be retrieved with /test get {id} and updated with /test set {id} by replacing {id} with any of the ids listed below',
'setting-a',
'setting-b',
})
end)
:register()
-- end of TestCommandsHandler
24 changes: 12 additions & 12 deletions tests/Core/Setting/SettingGroupTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ TestCase.new()
local settingA = __:new('Setting')
local settingB = __:new('Setting')

settingA.isAccessibleByCommand = function() return false end
settingB.isAccessibleByCommand = function() return false end
settingA.accessibleByCommand = false
settingB.accessibleByCommand = false

return {
settings = { ['a'] = settingA, ['b'] = settingB },
Expand All @@ -84,8 +84,8 @@ TestCase.new()
local settingA = __:new('Setting')
local settingB = __:new('Setting')

settingA.isAccessibleByCommand = function() return true end
settingB.isAccessibleByCommand = function() return true end
settingA.accessibleByCommand = true
settingB.accessibleByCommand = true

return {
settings = { ['a'] = settingA, ['b'] = settingB },
Expand All @@ -96,8 +96,8 @@ TestCase.new()
local settingA = __:new('Setting')
local settingB = __:new('Setting')

settingA.isAccessibleByCommand = function() return false end
settingB.isAccessibleByCommand = function() return true end
settingA.accessibleByCommand = false
settingB.accessibleByCommand = true

return {
settings = { ['a'] = settingA, ['b'] = settingB },
Expand Down Expand Up @@ -225,8 +225,8 @@ TestCase.new()
local settingA = __:new('Setting')
local settingB = __:new('Setting')

settingA.isAccessibleByCommand = function() return false end
settingB.isAccessibleByCommand = function() return false end
settingA.accessibleByCommand = false
settingB.accessibleByCommand = false

return {
settings = { ['a'] = settingA, ['b'] = settingB },
Expand All @@ -237,8 +237,8 @@ TestCase.new()
local settingA = __:new('Setting')
local settingB = __:new('Setting')

settingA.isAccessibleByCommand = function() return true end
settingB.isAccessibleByCommand = function() return true end
settingA.accessibleByCommand = true
settingB.accessibleByCommand = true

return {
settings = { ['a'] = settingA, ['b'] = settingB },
Expand All @@ -249,8 +249,8 @@ TestCase.new()
local settingA = __:new('Setting')
local settingB = __:new('Setting')

settingA.isAccessibleByCommand = function() return false end
settingB.isAccessibleByCommand = function() return true end
settingA.accessibleByCommand = false
settingB.accessibleByCommand = true

return {
settings = { ['a'] = settingA, ['b'] = settingB },
Expand Down

0 comments on commit 1501283

Please sign in to comment.