Skip to content

Commit

Permalink
chore(tests): add tests for utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Jul 1, 2024
1 parent 85994c7 commit 6eef36d
Show file tree
Hide file tree
Showing 8 changed files with 506 additions and 93 deletions.
7 changes: 6 additions & 1 deletion lua/vgit/core/utils/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ local date = {}

function date.format(time, format)
format = format or '%d %b %Y'
local timestamp = tonumber(time)

return os.date(format, tonumber(time))
if not timestamp or timestamp == 0 then
timestamp = os.time()
end

return os.date(format, timestamp)
end

function date.age(current_time)
Expand Down
5 changes: 4 additions & 1 deletion lua/vgit/core/utils/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ function list.join(l, with)
local result = ''

for i = 1, #l do
result = result .. l[i] .. with
result = result .. l[i]
if i ~= #l then
result = result .. with
end
end

return result
Expand Down
11 changes: 3 additions & 8 deletions lua/vgit/core/utils/object.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ function object.merge(...)
end

function object.pairs(o)
local pairs = {}
local p = {}

for key, component in pairs(o) do
pairs[#pairs + 1] = { key, component }
p[#p + 1] = { key, component }
end

return pairs
return p
end

function object.keys(o)
Expand All @@ -97,18 +97,13 @@ function object.values(o)
return values
end

function object.clone_deep(config_object)
return vim.tbl_deep_extend('force', {}, config_object)
end

function object.clone(config_object)
return vim.tbl_extend('force', {}, config_object)
end

function object.each(o, callback)
for key, value in pairs(o) do
local break_loop = callback(value, key)

if break_loop then return end
end
end
Expand Down
117 changes: 34 additions & 83 deletions tests/unit/core/utils_spec.lua → tests/unit/core/utils_date_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,40 @@ local utils = require('vgit.core.utils')

local eq = assert.are.same

describe('utils:', function()
describe('utils.date:', function()
describe('date.format', function()
it('should format the date using the default format', function()
local time = 1609477200 -- January 1, 2021
local formatted_date = utils.date.format(time)
assert.are.equal(formatted_date, '01 Jan 2021')
end)

it('should format the date using a custom format', function()
local current_time = os.time({ year = 2021, month = 1, day = 1, hour = 5, min = 0, sec = 0 })
local formatted_date = utils.date.format(current_time, '%Y-%m-%d %H:%M:%S')
eq(formatted_date, '2021-01-01 05:00:00')
end)

it('should handle invalid time input gracefully', function()
local invalid_time = 'invalid'
local formatted_date = utils.date.format(invalid_time)
local expected_date = os.date('%d %b %Y', os.time()) -- Use the current date
assert.are.equal(formatted_date, expected_date)
end)

it('should handle nil time input gracefully', function()
local formatted_date = utils.date.format(nil)
local expected_date = os.date('%d %b %Y', os.time()) -- Use the current date
assert.are.equal(formatted_date, expected_date)
end)

it('should handle nil format input gracefully', function()
local time = 1609477200 -- January 1, 2021
local formatted_date = utils.date.format(time, nil)
assert.are.equal(formatted_date, '01 Jan 2021')
end)
end)

describe('age', function()
before_each(function()
os.time = mock(os.time, true)
Expand Down Expand Up @@ -155,86 +188,4 @@ describe('utils:', function()
eq(age.display, '5 years ago')
end)
end)

describe('round', function()
it('should round pi to 3', function()
eq(utils.math.round(3.14159265359), 3)
end)
end)

describe('strip_substring', function()
it('should remove "/bar" from "foo/bar/baz"', function()
eq(utils.str.strip('foo/bar/baz', '/bar'), 'foo/baz')
end)

it('should remove "foo/baz" from "foo/bar/baz"', function()
eq(utils.str.strip('foo/bar/baz', '/bar'), 'foo/baz')
end)

it('should remove "helix-core/src" from "helix-core/src/comment.rs"', function()
eq(utils.str.strip('helix-core/src/comment.rs', 'helix-core/src'), '/comment.rs')
end)

it('should remove "helix-core/src/" from "helix-core/src/comment.rs"', function()
eq(utils.str.strip('helix-core/src/comment.rs', 'helix-core/src/'), 'comment.rs')
end)

it('should remove "x-c" from "helix-core"', function()
eq(utils.str.strip('helix-core', 'x-c'), 'heliore')
end)

it('should remove "ab" from "ababababa"', function()
eq(utils.str.strip('ababababa', 'ab'), 'abababa')
end)

it('should remove "ababababa" from "abababab"', function()
eq(utils.str.strip('ababababa', 'abababab'), 'a')
end)

it('should remove "ababababa" from "ababababa"', function()
eq(utils.str.strip('ababababa', 'ababababa'), '')
end)

it('should not remove "lua/" from "vgit.lua"', function()
eq(utils.str.strip('vgit.lua', 'lua/'), 'vgit.lua')
end)
end)

describe('object.assign', function()
it('should assign attributes in b into a regardless of if a has any of the attributes', function()
local a = {}
local b = {
config = {
line_number = {
enabled = false,
width = 10,
},
},
}
local c = utils.object.assign(a, b)

eq(c, b)
end)

it('should handle nested object assignment', function()
local a = {
config = {
line_number = {
width = 20,
},
},
}
local b = {
config = {
line_number = {
enabled = false,
width = 10,
},
},
}
local c = utils.object.assign(a, b)

eq(c, b)
end)
end)
end)
151 changes: 151 additions & 0 deletions tests/unit/core/utils_list_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
local list = require('vgit.core.utils.list')

local eq = assert.are.same

describe('list:', function()
describe('list.is_list', function()
it('should return true for a list', function()
local l = { 1, 2, 3 }
local result = list.is_list(l)
eq(result, true)
end)

it('should return false for a non-list', function()
local l = { key = 'value' }
local result = list.is_list(l)
eq(result, false)
end)
end)

describe('list.is_empty', function()
it('should return true for an empty list', function()
local l = {}
local result = list.is_empty(l)
eq(result, true)
end)

it('should return false for a non-empty list', function()
local l = { 1, 2, 3 }
local result = list.is_empty(l)
eq(result, false)
end)
end)

describe('list.join', function()
it('should join elements of a list with a separator', function()
local l = { 'a', 'b', 'c' }
local result = list.join(l, ',')
eq(result, 'a,b,c')
end)
end)

describe('list.pick', function()
it('should return the specified item from the list if found', function()
local l = { 'apple', 'banana', 'cherry' }
local result = list.pick(l, 'banana')
eq(result, 'banana')
end)

it('should return the first item if the specified item is not found', function()
local l = { 'apple', 'banana', 'cherry' }
local result = list.pick(l, 'pear')
eq(result, 'apple')
end)
end)

describe('list.concat', function()
it('should concatenate two lists', function()
local a = { 1, 2, 3 }
local b = { 4, 5, 6 }
local result = list.concat(a, b)
eq(result, { 1, 2, 3, 4, 5, 6 })
end)
end)

describe('list.merge', function()
it('should merge multiple lists into one', function()
local t = { 1, 2 }
local a = { 3, 4 }
local b = { 5, 6 }
local result = list.merge(t, a, b)
eq(result, { 1, 2, 3, 4, 5, 6 })
end)
end)

describe('list.map', function()
it('should map a function over a list', function()
local l = { 1, 2, 3 }
local result = list.map(l, function(x) return x * 2 end)
eq(result, { 2, 4, 6 })
end)
end)

describe('list.filter', function()
it('should filter a list based on a callback function', function()
local l = { 1, 2, 3, 4, 5 }
local result = list.filter(l, function(x) return x % 2 == 0 end)
eq(result, { 2, 4 })
end)
end)

describe('list.each', function()
it('should iterate over each item in the list', function()
local l = { 'a', 'b', 'c' }
local result = {}
list.each(l, function(item) table.insert(result, item) end)
eq(result, { 'a', 'b', 'c' })
end)
end)

describe('list.reduce', function()
it('should reduce a list to a single value using an accumulator and callback function', function()
local l = { 1, 2, 3, 4 }
local result = list.reduce(l, 0, function(acc, x) return acc + x end)
eq(result, 10)
end)
end)

describe('list.find', function()
it('should find the first item in the list that satisfies the callback function', function()
local l = { 'apple', 'banana', 'cherry' }
local result = list.find(l, function(item) return string.len(item) == 5 end)
eq(result, 'apple')
end)

it('should return nil if no item satisfies the callback function', function()
local l = { 'apple', 'banana', 'cherry' }
local result = list.find(l, function(item) return item == 'pear' end)
eq(result, nil)
end)
end)

describe('list.replace', function()
it('should replace items in a list within a specified range', function()
local l = { 'a', 'b', 'c', 'd', 'e' }
local result = list.replace(l, 2, 4, { 'x', 'y', 'z' })
eq(result, { 'a', 'x', 'y', 'z', 'e' })
end)
end)

describe('list.extract', function()
it('should extract items from a list within a specified range', function()
local l = { 'a', 'b', 'c', 'd', 'e' }
local result = list.extract(l, 2, 4)
eq(result, { 'b', 'c', 'd' })
end)
end)

describe('list.some', function()
it('should return true if at least one item in the list satisfies the callback function', function()
local l = { 'apple', 'banana', 'cherry' }
local result = list.some(l, function(item) return item == 'banana' end)
eq(result, true)
end)

it('should return false if no item in the list satisfies the callback function', function()
local l = { 'apple', 'banana', 'cherry' }
local result = list.some(l, function(item) return item == 'pear' end)
eq(result, false)
end)
end)
end)
Loading

0 comments on commit 6eef36d

Please sign in to comment.