Skip to content

Commit

Permalink
Feat: Add tests for configuration options (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
5-pebbles authored Sep 24, 2024
1 parent 08afa48 commit 457cf92
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 73 deletions.
1 change: 1 addition & 0 deletions lua/nordic/colors/nordic.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- The Nord palette: https://www.nordtheme.com/.
-- This file has a bunch of added colors.

-- NOTE: All hex codes must be uppercase (for testing)
local palette = {

none = 'NONE',
Expand Down
2 changes: 1 addition & 1 deletion lua/nordic/groups/integrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ function M.get_groups()
local bg
local fg
if O.ts_context.dark_background then
bg = C.black
bg = C.black1
fg = C.gray1
else
bg = C.gray1
Expand Down
2 changes: 1 addition & 1 deletion lua/nordic/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function M.load(opts)

-- Apply theme
local G = require('nordic.groups')
U.highlight(G.get_groups())
U.apply_highlights(G.get_groups())
G.set_term_colors()
end

Expand Down
20 changes: 4 additions & 16 deletions lua/nordic/tests/init.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
local U = require('nordic.utils')

M = {}

function M.assert_eq(left, right, message)
if not vim.deep_equal(left, right) then
local info = debug.getinfo(2)
local file_name = info.short_src
local line_number = info.currentline

print('Equal assertion failed at "' .. file_name .. ':' .. line_number .. '"')
print('Message: ' .. message)
print('Left:\n' .. vim.inspect(left))
print('Right:\n' .. vim.inspect(right))
end
end

function M.run_tests()
require('nordic.tests.options')
-- Ensures config resets
require('nordic').setup({})

require('nordic.tests.utils')
require('nordic.tests.options')
end

return M
223 changes: 171 additions & 52 deletions lua/nordic/tests/options.lua
Original file line number Diff line number Diff line change
@@ -1,55 +1,174 @@
-- First test the default config, and then test different variations of the config.
local base_palette = require('nordic.colors.nordic');

local config = require('nordic.config').options
local assert_eq = require('nordic.utils').assert_eq
local load = require('nordic').load
local get_highlight = require('nordic.utils').get_highlight

local function flip_string(string)
local switch = {
['light'] = 'dark',
['dark'] = 'light',
['flat'] = 'classic',
['classic'] = 'flat',
}
return switch[string]
end

load(config)

config.on_palette = function(palette)
palette.black0 = '#000000'
end

config.after_palette = function(palette)
local U = require('nordic.utils')
palette.bg_visual = U.blend(palette.orange.base, palette.bg, 0.15)
end

config.on_highlight = function(highlights, palette)
highlights.TelescopePromptTitle = {
fg = palette.red.bright,
bg = palette.green.base,
italic = true,
underline = true,
sp = palette.yellow.dim,
undercurl = false,
}
end

-- Flip all fields
config.bold_keywords = not config.bold_keywords
config.italic_comments = not config.italic_comments
config.transparent.bg = not config.transparent.bg
config.transparent.float = not config.transparent.float
config.bright_border = not config.bright_border
config.reduced_blue = not config.reduced_blue
config.swap_backgrounds = not config.swap_backgrounds
config.cursorline.bold = not config.cursorline.bold
config.cursorline.bold_number = not config.cursorline.bold_number
config.cursorline.theme = flip_string(config.cursorline.theme)
config.cursorline.blend = 0
config.noice.style = flip_string(config.noice.style)
config.telescope.style = flip_string(config.telescope.style)
config.leap.dim_backdrop = not config.leap.dim_backdrop
config.ts_context.dark_background = not config.ts_context.dark_background

load(config)
load({})

-- Tests for changes in palette should check highlights (to make sure everything is applied)

-- on_palette
assert_eq(get_highlight('Normal').bg, base_palette.gray0,
'on_palette: all highlights that use `gray0` should be `gray0` by default')
load({ on_palette = function(palette) palette.gray0 = '#FFFFFF' end })
assert_eq(get_highlight('Normal').bg, '#FFFFFF',
'on_platte: changing a color should cascade to all highlights that use it')
load({ on_palette = function(_) end })
assert_eq(get_highlight('Normal').bg, base_palette.gray0,
'on_palette: reloading should revert the palette to its original state')

-- after_palette
assert_eq(get_highlight('Normal').bg, base_palette.gray0,
'after_palette: all highlights that use `bg` should be `gray0` by default')
load({ after_palette = function(palette) palette.bg = '#FFFFFF' end })
assert_eq(get_highlight('Normal').bg, '#FFFFFF',
'after_platte: changing a color should cascade to all highlights that use it')
load({ after_palette = function(_) end })
assert_eq(get_highlight('Normal').bg, base_palette.gray0,
'after_palette: reloading should revert the palette to its original state')

-- on_highlight
assert_eq(get_highlight('Normal').bg, base_palette.gray0,
'on_highlight: `Normal` should be `gray0` by default')
load({ on_highlight = function(highlights, _) highlights['Normal'].bg = '#FFFFFF' end })
assert_eq(get_highlight('Normal').bg, '#FFFFFF',
'on_highlight: changing a highlight should actually change the highlight')
load({ on_highlight = function(_, _) end })
assert_eq(get_highlight('Normal').bg, base_palette.gray0,
'on_highlight: reloading should revert all highlights to their original state')

-- bold_keywords
assert_eq(get_highlight('Keyword').bold, nil,
'bold_keywords: highlight `Keyword` should not be bold by default')
load({ bold_keywords = true })
assert_eq(get_highlight('Keyword').bold, true,
'bold_keywords: highlight `Keyword` should be bold if `bold_keywords` is true')
load({ bold_keywords = false })

-- italic_comments
assert_eq(get_highlight('Comment').italic, true,
'italic_comments: highlight `Comments` should be italic by default')
load({ italic_comments = false })
assert_eq(get_highlight('Comment').italic, nil,
'italic_comments: highlight `Comments` should not be italic if `italic_comments` is false')
load({ italic_comments = true })

-- transparent
-- bg
assert_eq(get_highlight('Normal').bg ~= nil, true,
'transparent: highlight `Normal.bg` should not be `nil` by default')
load({ transparent = { bg = true } })
assert_eq(get_highlight('Normal').bg, nil,
'transparent: highlight `Normal.bg` should be `nil` if `transparent.bg` is true')
load({ transparent = { bg = false } })
-- float
assert_eq(get_highlight('NormalFloat').bg ~= nil, true,
'transparent: highlight `NormalFloat.bg` should not be `nil` by default')
load({ transparent = { float = true } })
assert_eq(get_highlight('NormalFloat').bg, nil,
'transparent: highlight `NormalFloat.bg` should be `nil` if `transparent.float` is true')
load({ transparent = { float = false } })

-- bright_border
assert_eq(get_highlight('WinSeparator').fg, base_palette.black0,
'bright_border: all highlights that use `border_fg` should be `black0` by default')
load({ bright_border = true })
-- NOTE: This will fail if the wrong white0 variant is used
assert_eq(get_highlight('WinSeparator').fg, base_palette.white0_reduce_blue,
'bright_border: all highlights that use `border_fg` should be `white0_reduce_blue` if `bright_border` is true')
load({ bright_border = false })

-- reduced_blue
assert_eq(get_highlight('Normal').fg, base_palette.white0_reduce_blue,
'reduced_blue: all highlights that use `white0` should be `white0_reduce_blue` by default')
load({ reduced_blue = false })
assert_eq(get_highlight('Normal').fg, base_palette.white0_normal,
'reduced_blue: all highlights that use `white0` should be `white0_normal` if `reduced_blue` is false')
load({ reduced_blue = true })

-- swap_backgrounds
-- NOTE: This will fail if any transparent settings are set
assert_eq(get_highlight('Normal').bg, base_palette.gray0,
'swap_backgrounds: all highlights that use `bg` should be `gray0` by default')
assert_eq(get_highlight('NormalFloat').bg, base_palette.black1,
'swap_backgrounds: all highlights that use `bg_float` should be `black1` by default')
load({ swap_backgrounds = true })
assert_eq(get_highlight('Normal').bg, base_palette.black1,
'swap_backgrounds: all highlights that use `bg` should be `black1` if `swap_backgrounds` is true')
assert_eq(get_highlight('NormalFloat').bg, base_palette.gray0,
'swap_backgrounds: all highlights that use `bg_float` should be `gray0` if `swap_backgrounds` is true')
load({ swap_backgrounds = false })

-- cursorline
-- bold
assert_eq(get_highlight('CursorLine').bold, nil,
'cursorline: highlight `CursorLine` should not be bold by default')
load({ cursorline = { bold = true } })
assert_eq(get_highlight('CursorLine').bold, true,
'cursorline: highlight `CursorLine` should be bold if `cursorline.bold` is true')
load({ cursorline = { bold = false } })
-- bold_number
assert_eq(get_highlight('CursorLineNr').bold, true,
'cursorline: highlight `CursorLineNr` should be bold by default')
load({ cursorline = { bold_number = false } })
assert_eq(get_highlight('CursorLineNr').bold, nil,
'cursorline: highlight `CursorLineNr` should not be bold if `cursorline.bold_number` is false')
load({ cursorline = { bold_number = true } })
-- theme
load({ cursorline = { blend = 1 } })
assert_eq(get_highlight('CursorLine').bg, base_palette.black0,
'cursorline: highlight `CursorLine` should be `black0` with the default theme')
load({ cursorline = { theme = 'light' } })
assert_eq(get_highlight('CursorLine').bg, base_palette.gray2,
'cursorline: highlight `CursorLine` should be `gray2` if `cursorline.theme` is `light`')
load({ cursorline = { theme = 'dark' } })
assert_eq(get_highlight('CursorLine').bg, base_palette.black0,
'cursorline: highlight `CursorLine` should be `black0` if `cursorline.theme` is `dark`')
-- blend
load({ cursorline = { blend = 1 } })
assert_eq(get_highlight('CursorLine').bg, base_palette.black0,
'cursorline: highlight `CursorLine` should be `black0` if `cursorline.blend` is 1')
load({ cursorline = { blend = 0.5 } })
local blend = require('nordic.utils').blend
assert_eq(get_highlight('CursorLine').bg, blend(base_palette.black0, base_palette.gray0, 0.5),
'cursorline: highlight `CursorLine` should be `#1F232C` if `cursorline.blend` is 0.5')
load({ cursorline = { blend = 0 } })
assert_eq(get_highlight('CursorLine').bg, base_palette.gray0,
'cursorline: highlight `CursorLine` should be `gray0` if `cursorline.blend` is 0')

-- noice.style
assert_eq(get_highlight('NoiceCmdline').bg, base_palette.gray0,
'noice: highlight `NoiceCmdline` should be `gray0` by default')
load({ noice = { style = 'flat' } })
assert_eq(get_highlight('NoiceCmdline').bg, base_palette.black0,
'noice: highlight `NoiceCmdline` should be `black0` if `noice.style` is `flat`')
load({ noice = { style = 'classic' } })
assert_eq(get_highlight('NoiceCmdline').bg, base_palette.gray0,
'noice: highlight `NoiceCmdline` should be `gray0` if `noice.style` is `classic`')

-- telescope.style
assert_eq(get_highlight('TelescopeNormal').bg, base_palette.black1,
'telescope: highlight `TelescopeNormal` should be `black1` by default')
load({ telescope = { style = 'classic' } })
assert_eq(get_highlight('TelescopeNormal').bg, base_palette.gray0,
'telescope: highlight `TelescopeNormal` should be `gray0` if `telescope.style` is `classic`')
load({ telescope = { style = 'flat' } })
assert_eq(get_highlight('TelescopeNormal').bg, base_palette.black1,
'telescope: highlight `TelescopeNormal` should be `black1` if `telescope.style` is `flat`')

-- leap.dim_backdrop
assert_eq(get_highlight('LeapBackdrop').fg, nil,
'leap: highlight `LeapBackdrop` should be `nil` by default')
load({ leap = { dim_backdrop = true } })
assert_eq(get_highlight('LeapBackdrop').fg, base_palette.gray4,
'leap: highlight `LeapBackdrop` should be `gray4` if `leap.dim_backdrop` is true')
load({ leap = { dim_backdrop = false } })

-- ts_context.dark_background
assert_eq(get_highlight('TreesitterContext').bg, base_palette.black1,
'ts_context: highlight `TreesitterContext` should be `black1` by default')
load({ ts_context = { dark_background = false } })
assert_eq(get_highlight('TreesitterContext').bg, base_palette.gray1,
'ts_context: highlight `TreesitterContext` should be `gray1` if `ts_context.dark_background` is false')
load({ ts_context = { dark_background = true } })
2 changes: 1 addition & 1 deletion lua/nordic/tests/utils.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local assert_eq = require('nordic.tests').assert_eq
local assert_eq = require('nordic.utils').assert_eq
local U = require('nordic.utils')

local t1, t2, nested
Expand Down
30 changes: 28 additions & 2 deletions lua/nordic/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@ function M.loaded()
return vim.g.colors_name == M.NAME
end

function M.highlight(table)
for group, config in pairs(table) do
function M.apply_highlights(groups)
for group, config in pairs(groups) do
vim.api.nvim_set_hl(0, group, config)
end
end

function M.get_highlight(group)
local function hexify(value)
if type(value) == 'number' then
return string.format('#%X', value)
elseif type(value) == 'table' then
return vim.tbl_map(hexify, value)
end
return value
end

return hexify(vim.api.nvim_get_hl(0, { name = group, create = false }))
end


function M.none()
return 'NONE'
end
Expand Down Expand Up @@ -76,4 +90,16 @@ function M.blend(foreground, background, alpha)
return M.rgb_to_hex(blend_channel(fg[1], bg[1]), blend_channel(fg[2], bg[2]), blend_channel(fg[3], bg[3]))
end

function M.assert_eq(left, right, message)
if not vim.deep_equal(left, right) then
local info = debug.getinfo(2)
local file_name = info.short_src
local line_number = info.currentline
print('Equal assertion failed at "' .. file_name .. ':' .. line_number .. '"')
print('Message: ' .. message)
print('Left:\n' .. vim.inspect(left))
print('Right:\n' .. vim.inspect(right))
end
end

return M

0 comments on commit 457cf92

Please sign in to comment.