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 21 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
4 changes: 2 additions & 2 deletions lua/nordic/groups/init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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(native, integrations)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this should not be a merge inplace? We do not want to modify native here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works because get_groups creates a new table each time; however, I agree it's not very clear. merge and merge_inplace do almost the same thing, so it feels weird to have both.

I could revert that commit or I could make it more clear with something like this:

local native = require('nordic.groups.native').get_groups()
local integrations = require('nordic.groups.integrations').get_groups()

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

Which ever you think is better.

Copy link
Owner

@AlexvZyl AlexvZyl Jun 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge and merge inplace are different enough that we would want both, it is very common to have both.

So I think we should:

  • Keep both.
  • If you want to keep using merge_inplace in this scenario, please change it the way you suggested. Just a bit more clear what is going on.

Copy link
Collaborator Author

@5-pebbles 5-pebbles Jun 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in: 8c8fff4

-- Apply on_highlight
local palette = require('nordic.colors')
Expand Down
51 changes: 51 additions & 0 deletions lua/nordic/tests/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local U = require('nordic.utils')

M = {}

local function pretty_string(value)
AlexvZyl marked this conversation as resolved.
Show resolved Hide resolved
if not U.is_table(value) then return tostring(value) end
local output = '{'
for k, v in pairs(value) do
output = output .. '' .. tostring(k) .. ': ' .. pretty_string(v) .. ', '
end
if #output == 1 then
return output .. '}'
else
return output:sub(1, -3) .. '}'
end
end

function M.assert_eq(left, right, message)
AlexvZyl marked this conversation as resolved.
Show resolved Hide resolved
local function recursive_eq(value1, value2)
if not U.is_table(value1) or not U.is_table(value2) then
return value1 == value2
end

-- I think this can be done faster but its just for tests...
AlexvZyl marked this conversation as resolved.
Show resolved Hide resolved
AlexvZyl marked this conversation as resolved.
Show resolved Hide resolved
for k, v in pairs(value1) do
if not recursive_eq(v, value2[k]) then return false end
end
for k, v in pairs(value2) do
if not recursive_eq(v, value1[k]) then return false end
end
return true
end

if not recursive_eq(left, right) then
local info = debug.getinfo(2)
local file_name = info.short_src
local line_number = info.currentline
print('\nassertion `left == right` failed' ..
((message and ': ' .. message) or '') ..
'\n left: ' ..
pretty_string(left) ..
'\n right: ' .. pretty_string(right) .. '\nat ' .. tostring(file_name) .. ':' .. tostring(line_number))
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')

-- none
assert_eq(U.none(), 'NONE', 'utils.none() should return "NONE"')

-- is_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')

-- is_table
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')

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

local t1 = {a = 1}
local t2 = {b = 2}
assert_eq(U.merge_inplace(t1, t2) == t1, true, 'U.merge_inplace(t1, t2) should return t1')

local t1 = {a = 1, b = 3}
local 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')

local t1 = {a = 1, d = {x = 10}}
local 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')

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

local nested = { y = 20 }
local t1 = {a = 1}
local 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')

-- hex_to_rgb
assert_eq({ U.hex_to_rgb('#191D24') }, {25, 29, 36}, 'U.hex_to_rgb("#191D24") should return 25, 29, 36')

-- rgb_to_hex
assert_eq(U.rgb_to_hex(25, 29, 36), '#191D24', 'U.rgb_to_hex(25, 29, 36) should return "#191D24"')

-- blend
assert_eq(U.blend('#FFFFFF', '#000000', 0.5), '#808080', 'U.blend("#FFFFFF", ""#000000", 0.5) should return "#808080"')
114 changes: 22 additions & 92 deletions lua/nordic/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,38 @@ 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_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
return vim.tbl_deep_extend('force', table1, table2)
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)
-- 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

return t1
end

function M.hex_to_rgb(str)
Expand All @@ -46,82 +51,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