Skip to content

Commit

Permalink
CI: Add testing for utils & delete extraneous code (#145)
Browse files Browse the repository at this point in the history
Co-authored-by: AlexvZyl <[email protected]>
  • Loading branch information
5-pebbles and AlexvZyl authored Sep 1, 2024
1 parent 9f3cff8 commit 45a5812
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 95 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ jobs:
shell: bash
run: |
nvim --version
OUTPUT=$(nvim --headless -c "lua require 'nordic.tests.options'" -c 'q' 2>&1);
OUTPUT=$(nvim --headless -c "lua require('nordic.tests').run_tests()" -c 'q' 2>&1);
if [[ -n "$OUTPUT" ]]
then
exit 1
echo "::error title='Checks failed'::$OUTPUT" && exit 1
fi
7 changes: 5 additions & 2 deletions lua/nordic/groups/init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
local merge = require('nordic.utils').merge
local merge_inplace = require('nordic.utils').merge_inplace

local M = {}

function M.get_groups()
local native = require('nordic.groups.native').get_groups()
local integrations = require('nordic.groups.integrations').get_groups()
local groups = merge(native, integrations)

local groups = {}
merge_inplace(groups, native)
merge_inplace(groups, integrations)

-- Apply on_highlight
local palette = require('nordic.colors')
Expand Down
23 changes: 23 additions & 0 deletions lua/nordic/tests/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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')
require('nordic.tests.utils')
end

return M
56 changes: 56 additions & 0 deletions lua/nordic/tests/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
local assert_eq = require('nordic.tests').assert_eq
local U = require('nordic.utils')

local t1, t2, nested

-- Types.

assert_eq(U.none(), 'NONE', 'utils.none() should return "NONE"')
assert_eq(U.is_none('NONE'), true, 'U.is_none("NONE") should return true')
assert_eq(U.is_none('none'), true, 'U.is_none("none") should return true')
assert_eq(U.is_none('nil'), false, 'U.is_none("nil") should return false')
assert_eq(U.is_table('string'), false, 'U.is_table("string") should return false')
assert_eq(U.is_table(4), false, 'U.is_table(4) should return false')
assert_eq(U.is_table({}), true, 'U.is_table({}) should return true')

-- Table.

t1 = { a = 1 }
t2 = { b = 2 }
U.merge_inplace(t1, t2)
assert_eq(t1, { a = 1, b = 2 }, 'U.merge_inplace(t1, t2) basic merge')

t1 = { a = 1, b = 3 }
t2 = { b = 2, c = 4 }
U.merge_inplace(t1, t2)
assert_eq(t1, { a = 1, b = 2, c = 4 }, 'U.merge_inplace(t1, t2) overwriting values')

t1 = { a = 1, d = { x = 10 } }
t2 = { d = { y = 20 }, e = 5 }
U.merge_inplace(t1, t2)
assert_eq(t1, { a = 1, d = { x = 10, y = 20 }, e = 5 }, 'U.merge_inplace(t1, t2) nested tables')

nested = { y = 20 }
t1 = { d = nested, e = 5 }
t2 = { a = 1 }
U.merge_inplace(t1, t2)
assert_eq(t1['d'] ~= nested, true, 'U.merge_inplace(t1, t2) copy t1 nested values')

nested = { y = 20 }
t1 = { a = 1 }
t2 = { d = nested, e = 5 }
U.merge_inplace(t1, t2)
assert_eq(t1['d'] ~= nested, true, 'U.merge_inplace(t1, t2) copy t2 nested values')

assert_eq(U.merge({}, {}), {}, 'U.merge({}, {}) should return an empty table')
assert_eq(U.merge(nil, nil), {}, 'U.merge(nil, nil) should return an empty table')
assert_eq(U.merge(nil, { a = 1 }), { a = 1 }, 'U.merge(nil, {a = 1}) should return {a = 1}')
assert_eq(U.merge({ a = 1 }, nil), { a = 1 }, 'U.merge({a = 1}, nil) should return {a = 1}')
assert_eq(U.merge({ a = 1, b = 3 }, { b = 2, c = 4 }), { a = 1, b = 2, c = 4 },
'U.merge({a = 1, b = 3}, {b = 2, c = 4}) should return {a = 1, b = 2, c = 4}')

-- Colors.

assert_eq({ U.hex_to_rgb('#191D24') }, { 25, 29, 36 }, 'U.hex_to_rgb("#191D24") should return 25, 29, 36')
assert_eq(U.rgb_to_hex(25, 29, 36), '#191D24', 'U.rgb_to_hex(25, 29, 36) should return "#191D24"')
assert_eq(U.blend('#FFFFFF', '#000000', 0.5), '#808080', 'U.blend("#FFFFFF", ""#000000", 0.5) should return "#808080"')
119 changes: 28 additions & 91 deletions lua/nordic/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,43 @@ function M.highlight(table)
end
end

function M.none()
return 'NONE'
end

function M.is_none(string)
return string == 'NONE' or string == 'none'
end

function M.none()
return 'NONE'
function M.is_table(value)
return type(value) == 'table'
end

function M.merge(table1, table2)
if table1 == table2 == nil then return {} end
if table1 == nil then
return table2
elseif table2 == nil then
return table1
function M.merge(t1, t2)
if not t1 then
return t2 or {}
elseif not t2 then
return t1
end
return vim.tbl_deep_extend('force', table1, table2)
return vim.tbl_deep_extend('force', t1, t2)
end

function M.merge_inplace(table1, table2)
for k, v in pairs(table2) do
if type(v) == 'table' then
if type(table1[k]) ~= 'table' then table1[k] = {} end
M.merge_inplace(table1[k], v)
function M.merge_inplace(t1, t2)
-- clone values
for k, v in pairs(t1) do
if M.is_table(v) then
t1[k] = {}
M.merge_inplace(t1[k], v)
end
end

-- merge
for k, v in pairs(t2) do
if M.is_table(v) then
if not M.is_table(t1[k]) then t1[k] = {} end
M.merge_inplace(t1[k], v)
else
table1[k] = v
t1[k] = v
end
end
end
Expand All @@ -46,82 +58,7 @@ function M.hex_to_rgb(str)
end

function M.rgb_to_hex(r, g, b)
return '#' .. string.format('%x', r) .. string.format('%x', g) .. string.format('%x', b)
end

function M.rgb_to_hsv(r, g, b)
r, g, b = r / 255, g / 255, b / 255
local max, min = math.max(r, g, b), math.min(r, g, b)

local h, s, v
v = max

local d = max - min
if max == 0 then
s = 0
else
s = d / max
end

if max == min then
h = 0
else
if max == r then
h = (g - b) / d
if g < b then h = h + 6 end
elseif max == g then
h = (b - r) / d + 2
elseif max == b then
h = (r - g) / d + 4
end
h = h / 6
end

return h, s, v
end

function M.hsv_to_rbg(h, s, v)
local r, g, b

local i = math.floor(h * 6)
local f = h * 6 - i
local p = v * (1 - s)
local q = v * (1 - f * s)
local t = v * (1 - (1 - f) * s)

i = i % 6

if i == 0 then
r, g, b = v, t, p
elseif i == 1 then
r, g, b = q, v, p
elseif i == 2 then
r, g, b = p, v, t
elseif i == 3 then
r, g, b = p, q, v
elseif i == 4 then
r, g, b = t, p, v
elseif i == 5 then
r, g, b = v, p, q
end

return r * 255, g * 255, b * 255
end

function M.darken(hex, amount)
local r, g, b = M.hex_to_rgb(hex)
local h, s, v = M.rgb_to_hsv(r, g, b)
v = v * ((1 - amount) / 1)
r, g, b = M.hsv_to_rbg(h, s, v)
return M.rgb_to_hex(r, g, b)
end

function M.lighten(hex, amount)
local r, g, b = M.hex_to_rgb(hex)
local h, s, v = M.rgb_to_hsv(r, g, b)
v = v * (1 + amount)
r, g, b = M.hsv_to_rbg(h, s, v)
return M.rgb_to_hex(r, g, b)
return '#' .. string.format('%X', r) .. string.format('%X', g) .. string.format('%X', b)
end

-- Adapted from @folke/tokyonight.nvim.
Expand Down

0 comments on commit 45a5812

Please sign in to comment.