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 18 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
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
63 changes: 63 additions & 0 deletions lua/nordic/tests/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local assert_eq = require('nordic.tests').assert_eq
local U = require('nordic.utils')

local function round(value, places)
return tonumber(string.format("%." .. (places or 0) .. "f", value))
end

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

-- 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, 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"')
106 changes: 22 additions & 84 deletions lua/nordic/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,41 @@ 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
-- TODO what about replacing this with merge_inplace
if not table1 then
return table2 or {}
elseif not table2 then
return table1
end
return vim.tbl_deep_extend('force', table1, table2)
end
AlexvZyl marked this conversation as resolved.
Show resolved Hide resolved

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

-- merge
for k, v in pairs(table2) do
if type(v) == 'table' then
if type(table1[k]) ~= 'table' then table1[k] = {} end
if M.is_table(v) then
if not M.is_table(table1[k]) then table1[k] = {} end
M.merge_inplace(table1[k], v)
else
table1[k] = v
Expand All @@ -46,82 +59,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