Skip to content

Commit

Permalink
[feat]: create new concatenation method for tables
Browse files Browse the repository at this point in the history
  • Loading branch information
castlele committed Jan 7, 2025
1 parent 38a5d6c commit c87ead3
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: test
run: |
luarocks make
./run_tests.sh "*"
./run_tests.sh "*.lua"
deploy:
runs-on: ubuntu-latest
Expand Down
10 changes: 9 additions & 1 deletion build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ local function runTestsCommand(cmd)
return string.format(runner, cmd)
end

---@return string
local function getCurrentFileName()
local filePath = vim.fn.expand("%")
local pathComponents = vim.fn.split(filePath, "/")
return pathComponents[#pathComponents]
end

---@diagnostic disable-next-line
conf = {
install = "sudo luarocks make",
remove = "sudo luarocks remove cluautils",
allTest = runTestsCommand("*"),
currentTest = runTestsCommand(getCurrentFileName()),
allTest = runTestsCommand("*.lua"),
threadTest = [[
bear -- make build
make test_thread
Expand Down
4 changes: 2 additions & 2 deletions cluautils-1.18-0.rockspec → cluautils-1.18-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package = "cluautils"
version = "1.18-0"
version = "1.18-1"
source = {
url = "git://github.com/castlele/cluautils.git",
tag = "1.18.0"
tag = "1.18.1"
}
description = {
homepage = "*** please enter a project homepage ***",
Expand Down
2 changes: 1 addition & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tests_dir=./tests
FILTER=$1
IS_ANY_ERROR=0

for test_file in $tests_dir/${FILTER}.lua; do
for test_file in $(find "$tests_dir" -name "$FILTER" -type f -depth 1); do
lua $test_file

if [[ $? -ne 0 ]]; then
Expand Down
14 changes: 14 additions & 0 deletions src/table_utils/table_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function M.is_equal(self, other_table)
return true
end

---@deprecated Use table.concat() instead
---@param self table
---@param other_table table
---@param element_condition? fun(any):boolean
Expand Down Expand Up @@ -84,6 +85,19 @@ function M.concat_tables(self, other_table, element_condition)
return final_result
end

---@param lhs table
---@param rhs table
---@return table
function M.concat(lhs, rhs)
local result = lhs

for k, v in pairs(rhs) do
result[k] = v
end

return result
end

---@generic T
---@param self table
---@param callback fun(element: T):boolean
Expand Down
35 changes: 26 additions & 9 deletions src/tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ local colorTable = {
}
local currentResults = {}


---@param color ColorTable
---@param str string
---@return string
local function colorString(color, str)
return string.format(
return string.format(
"%s[%sm%s%s[%sm",
string.char(27),
color,
Expand All @@ -34,7 +33,6 @@ local function wrapWith(str, symbol)
return symbol .. str .. symbol
end


tests.describe = function(label, func)
print(colorString(colorTable.YELLOW, "Test cases: " .. wrapWith(label, "'")))
func()
Expand All @@ -44,29 +42,49 @@ tests.describe = function(label, func)
for _, value in pairs(currentResults) do
if not value then
isAnyFailed = true
print(colorString(colorTable.RED, "Test cases failed: " .. wrapWith(label, "'")))
print(
colorString(
colorTable.RED,
"Test cases failed: " .. wrapWith(label, "'")
)
)
end
end

if isAnyFailed then
os.exit(-1)
end

print(colorString(colorTable.YELLOW, "Test cases succeeded: " .. wrapWith(label, "'")))
print(
colorString(
colorTable.YELLOW,
"Test cases succeeded: " .. wrapWith(label, "'")
)
)
end

tests.it = function(name, func)
local isSuccess = xpcall(func, function (msg)
local isSuccess = xpcall(func, function(msg)
if msg then
print(msg)
end
end)

if isSuccess then
print(colorString(colorTable.GREEN, "Test " .. wrapWith(name, "'") .. " passed"))
print(
colorString(
colorTable.GREEN,
"Test " .. wrapWith(name, "'") .. " passed"
)
)
currentResults[name] = true
else
print(colorString(colorTable.RED, "Test " .. wrapWith(name, "'") .. " failed"))
print(
colorString(
colorTable.RED,
"Test " .. wrapWith(name, "'") .. " failed"
)
)
currentResults[name] = false
end
end
Expand All @@ -77,5 +95,4 @@ tests.expect = function(condition, message)
end
end


return tests
Loading

0 comments on commit c87ead3

Please sign in to comment.