Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add testing for utils & delete extraneous code; #145

Merged
merged 27 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1f04c6d
feat(tests): separate test modules;
5-pebbles Jun 12, 2024
cda0836
feat(tests): add assert_eq to test module;
5-pebbles Jun 12, 2024
5f3658a
ci(tests): echo tests output on complete;
5-pebbles Jun 12, 2024
2261642
fix(tests): remove intentional failures;
5-pebbles Jun 12, 2024
c117dae
feat(tests): assert_eq works on tables now;
5-pebbles Jun 14, 2024
21c2ca9
feat(tests): add tests for none, is_none, & hex_to_rgb;
5-pebbles Jun 15, 2024
e7d742a
feat(tests): add tests & fix merge;
5-pebbles Jun 15, 2024
f5ff09c
fix(tests): fix bad module name in error messages;
5-pebbles Jun 15, 2024
289870e
feat(tests): add tests for merge_inplace;
5-pebbles Jun 15, 2024
706f874
fix(utils): merge_inplace clones values in t1 nested tables;
5-pebbles Jun 15, 2024
7c38ee1
feat(utils): add is_table to utils;
5-pebbles Jun 15, 2024
ae12e29
feat(tests): add tests for rgb_to_hex;
5-pebbles Jun 15, 2024
be45c60
fix(utils): rgb_to_hex returns uppercase like everything else;
5-pebbles Jun 15, 2024
a109c94
feat(tests): readable table asserts;
5-pebbles Jun 15, 2024
a741e90
feat(tests): add tests for rgb_to_hsv;
5-pebbles Jun 15, 2024
e627217
fix(utils): hsv_to_rgb function was misnamed;
5-pebbles Jun 15, 2024
ea111b0
feat(tests): add tests for hsv_to_rgb;
5-pebbles Jun 15, 2024
7876f49
rm(utils): rgb_to_hsv, hsv_to_rgb, darken, & lighten;
5-pebbles Jun 15, 2024
9d89242
rm(utils): replace merge with merge_inplace;
5-pebbles Jun 15, 2024
3349c8b
feat(tests): add tests for blend;
5-pebbles Jun 15, 2024
4f5b445
feat(tests): add test for merge_inplace returning a reference;
5-pebbles Jun 15, 2024
8c8fff4
feat(utils): re-add merge & cleanup merge_inplace usage;
5-pebbles Jun 16, 2024
4bcea92
fix(tests): replace pretty_string with vim.inspect();
5-pebbles Aug 31, 2024
34f5427
fix(tests): replace recursive_eq with vim.deep_equal();
5-pebbles Aug 31, 2024
ce6062a
ref(tests): cleanup and organising
AlexvZyl Sep 1, 2024
e10e668
ref(tests): improve equal assert message
AlexvZyl Sep 1, 2024
265ba41
ref(tests): Correct test...
AlexvZyl Sep 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
AlexvZyl marked this conversation as resolved.
Show resolved Hide resolved
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)
AlexvZyl marked this conversation as resolved.
Show resolved Hide resolved
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
Loading