diff --git a/lib/eli/fs.lua b/lib/eli/fs.lua index ff4c764d..55bbae8b 100644 --- a/lib/eli/fs.lua +++ b/lib/eli/fs.lua @@ -593,7 +593,7 @@ local ELI_DIR_LOCK_ID = "ELI_DIR_LOCK" ---Locks access to directory ---@param path string ---@param lock_file_name string? ----@return EliDirLock|nil, string? +---@return EliDirLock?, string? function fs.lock_directory(path, lock_file_name) check_efs_available"lock_dir" return fs_extra.lock_dir(path, lock_file_name) @@ -603,7 +603,7 @@ end --- ---Unlocks access to directory ---@param fs_lock EliDirLock ----@return boolean|nil, string? +---@return EliDirLock?, string? function fs.unlock_directory(fs_lock) if type(fs_lock) == ELI_DIR_LOCK_ID or (type(fs_lock) == "userdata" and fs_lock.__type --[[@as string]] == ELI_DIR_LOCK_ID) then return fs_lock --[[@as EliDirLock]]:unlock() @@ -617,7 +617,7 @@ end --- ---returns type of file ---@param path string ----@return boolean|nil, string +---@return string function fs.file_type(path) local last_character = path:sub(#path, #path) if table_extensions.includes({ "/", "\\" }, last_character) then @@ -630,7 +630,7 @@ end --- ---returns type of file ---@param path_or_file string|file* ----@return boolean|nil, string +---@return table function fs.file_info(path_or_file) if type(path_or_file) == "string" then local last_character = path_or_file:sub(#path_or_file, #path_or_file) diff --git a/lib/tests/all.lua b/lib/tests/all.lua index 4014b2a2..1abc592d 100644 --- a/lib/tests/all.lua +++ b/lib/tests/all.lua @@ -1,5 +1,5 @@ TEST = require"u-test" -local _test = TEST +local test = TEST require"t_hjson" require"t_bigint" require"t_base64" @@ -32,5 +32,5 @@ require"internals.util" ]] require"elify" -local _ntests, _nfailed = _test.result() -_test.summary() +local ntests, nfailed = test.result() +test.summary() diff --git a/lib/tests/cli.lua b/lib/tests/cli.lua index 40948447..f40c7a6d 100644 --- a/lib/tests/cli.lua +++ b/lib/tests/cli.lua @@ -1,33 +1,33 @@ -local _test = TEST or require "u-test" -local _ok, _eliCli = pcall(require, "eli.cli") +local test = TEST or require"u-test" +local ok, eli_cli = pcall(require, "eli.cli") -if not _ok then - _test["eli.cli available"] = function() - _test.assert(false, "eli.cli not available") +if not ok then + test["eli.cli available"] = function () + test.assert(false, "eli.cli not available") end if not TEST then - _test.summary() + test.summary() os.exit() else return end end -_test["eli.cli available"] = function() - _test.assert(true) +test["eli.cli available"] = function () + test.assert(true) end -_test["parse args"] = function() +test["parse args"] = function () arg = { [-1] = "", [0] = "", - [1] = "-q" + [1] = "-q", } - print(require"hjson".stringify(_eliCli.parse_args())) - _test.assert(true) + print(require"hjson".stringify(eli_cli.parse_args())) + test.assert(true) end if not TEST then - _test.summary() + test.summary() end diff --git a/lib/tests/elify.lua b/lib/tests/elify.lua index 4866ac6d..26437089 100644 --- a/lib/tests/elify.lua +++ b/lib/tests/elify.lua @@ -41,9 +41,9 @@ test["zip"] = function () end test["os"] = function () - local _eliOs = require"eli.os" - test.assert(os ~= _eliOs) - for k, v in pairs(_eliOs) do + local eli_os = require"eli.os" + test.assert(os ~= eli_os) + for k, v in pairs(eli_os) do test.assert(os[k] == v) end end @@ -54,20 +54,20 @@ test["etype"] = function () test.assert(etype(nil) == "nil") test.assert(etype(0) == "number") test.assert(etype{} == "table") - local _t = { __type = "test" } - setmetatable(_t, _t) - test.assert(etype(_t) == "test") + local t = { __type = "test" } + setmetatable(t, t) + test.assert(etype(t) == "test") end test["get_overriden_values"] = function () - local _overriden = require"eli.elify".get_overriden_values() - test.assert(_overriden.os == require"os") - test.assert(_overriden.type ~= type) + local overriden = require"eli.elify".get_overriden_values() + test.assert(overriden.os == require"os") + test.assert(overriden.type ~= type) end test["extensions.string"] = function () - local _esx = require"eli.extensions.string" - for k, v in pairs(_esx) do + local string_extensions = require"eli.extensions.string" + for k, v in pairs(string_extensions) do if k ~= "globalize" then test.assert(string[k] == v) end @@ -75,8 +75,8 @@ test["extensions.string"] = function () end test["extensions.table"] = function () - local _etx = require"eli.extensions.table" - for k, v in pairs(_etx) do + local table_extensions = require"eli.extensions.table" + for k, v in pairs(table_extensions) do if k ~= "globalize" then test.assert(table[k] == v) end diff --git a/lib/tests/elios.lua b/lib/tests/elios.lua index 856f68ab..7b84e2f6 100644 --- a/lib/tests/elios.lua +++ b/lib/tests/elios.lua @@ -1,25 +1,25 @@ -local _test = TEST or require"u-test" -local _ok, _elios = pcall(require, "eli.os") +local test = TEST or require"u-test" +local ok, eli_os = pcall(require, "eli.os") -if not _ok then - _test["eli.os available"] = function () - _test.assert(false, "eli.os not available") +if not ok then + test["eli.os available"] = function () + test.assert(false, "eli.os not available") end if not TEST then - _test.summary() + test.summary() os.exit() else return end end -_test["eli.os available"] = function () - _test.assert(true) +test["eli.os available"] = function () + test.assert(true) end -if not _elios.EOS then +if not eli_os.EOS then if not TEST then - _test.summary() + test.summary() print"EOS not detected, only basic tests executed..." os.exit() else @@ -28,25 +28,25 @@ if not _elios.EOS then end end -_test["sleep"] = function () - local _referencePoint = os.date"%S" - _elios.sleep(10) - local _afterSleep = os.date"%S" - local _diff = _afterSleep - _referencePoint - if _diff < 0 then _diff = _diff + 60 end - _test.assert(_diff > 6 and _diff < 14) +test["sleep"] = function () + local reference_point = os.date"%S" + eli_os.sleep(10) + local after_sleep = os.date"%S" + local diff = after_sleep - reference_point + if diff < 0 then diff = diff + 60 end + test.assert(diff > 6 and diff < 14) end -_test["chdir & cwd"] = function () - local _cwd = _elios.cwd() - _elios.chdir"tmp" - local _newCwd = _elios.cwd() - _test.assert(_cwd ~= _newCwd) - _elios.chdir(_cwd) - _newCwd = _elios.cwd() - _test.assert(_cwd == _newCwd) +test["chdir & cwd"] = function () + local cwd = eli_os.cwd() + eli_os.chdir"tmp" + local new_cwd = eli_os.cwd() + test.assert(cwd ~= new_cwd) + eli_os.chdir(cwd) + new_cwd = eli_os.cwd() + test.assert(cwd == new_cwd) end if not TEST then - _test.summary() + test.summary() end diff --git a/lib/tests/env.lua b/lib/tests/env.lua index 1f998d55..8a41519d 100644 --- a/lib/tests/env.lua +++ b/lib/tests/env.lua @@ -1,51 +1,50 @@ - -local _test = TEST or require 'u-test' -local _ok, _eliEnv = pcall(require, "eli.env") +local test = TEST or require"u-test" +local ok, eli_env = pcall(require, "eli.env") -if not _ok then - _test["eli.env available"] = function () - _test.assert(false, "eli.env not available") +if not ok then + test["eli.env available"] = function () + test.assert(false, "eli.env not available") end - if not TEST then - _test.summary() + if not TEST then + test.summary() os.exit() - else - return + else + return end end -_test["eli.env available"] = function () - _test.assert(true) +test["eli.env available"] = function () + test.assert(true) end -_test["get_env"] = function () - local _path = _eliEnv.get_env("PATH") - _test.assert(type(_path) == 'string') +test["get_env"] = function () + local path = eli_env.get_env"PATH" + test.assert(type(path) == "string") end -if not _eliEnv.EENV then - if not TEST then - _test.summary() +if not eli_env.EENV then + if not TEST then + test.summary() print"EENV not detected, only basic tests executed..." os.exit() - else + else print"EENV not detected, only basic tests executed..." - return + return end end -_test["set_env"] = function () - local _ok = _eliEnv.set_env("t", "test_value") - _test.assert(_ok) - local _t = _eliEnv.get_env("t") - _test.assert(_t == 'test_value') +test["set_env"] = function () + local ok = eli_env.set_env("t", "test_value") + test.assert(ok) + local t = eli_env.get_env"t" + test.assert(t == "test_value") end -_test["environment"] = function () - local _env = _eliEnv.environment() - _test.assert(type(_env) == 'table') +test["environment"] = function () + local env = eli_env.environment() + test.assert(type(env) == "table") end -if not TEST then - _test.summary() -end \ No newline at end of file +if not TEST then + test.summary() +end diff --git a/lib/tests/extensions/string.lua b/lib/tests/extensions/string.lua index 14096dd7..56f6c9c4 100644 --- a/lib/tests/extensions/string.lua +++ b/lib/tests/extensions/string.lua @@ -1,65 +1,67 @@ -local _test = TEST or require "u-test" -local _ok, _exString = pcall(require, "eli.extensions.string") +local test = TEST or require"u-test" +local ok, string_extensions = pcall(require, "eli.extensions.string") -if not _ok then - _test["eli.extensions.string available"] = function() - _test.assert(false, "eli.extensions.string not available") +if not ok then + test["eli.extensions.string available"] = function () + test.assert(false, "eli.extensions.string not available") end if not TEST then - _test.summary() + test.summary() os.exit() else return end end -_test["eli.extensions.string available"] = function() - _test.assert(true) +test["eli.extensions.string available"] = function () + test.assert(true) end -_test["trim"] = function() - local _result = _exString.trim(" \ttest \t\n ") - _test.assert(_result == "test") +test["trim"] = function () + local result = string_extensions.trim" \ttest \t\n " + test.assert(result == "test") end -_test["join"] = function() - local _result = _exString.join(", ", "test", "join", "string") - _test.assert(_result == "test, join, string") - local _ok, _result = pcall(_exString.join, ", ", "test", { "join" }, "string") - _test.assert(_ok and _result:match("test, table:")) - local _result = _exString.join(", ", { "test", "join", "string" }) - _test.assert(_result == "test, join, string") +test["join"] = function () + local result = string_extensions.join(", ", "test", "join", "string") + test.assert(result == "test, join, string") + local ok, result = pcall(string_extensions.join, ", ", "test", { "join" }, "string") + test.assert(ok and result:match"test, table:") + local result = string_extensions.join(", ", { "test", "join", "string" }) + test.assert(result == "test, join, string") end -_test["join_strings"] = function() - local _result = _exString.join_strings(", ", "test", "join", { test = "string" }, "string") - _test.assert(_result == "test, join, string") - local _ok, _result = pcall(_exString.join_strings, ", ", "test", { "join" }, "string") - _test.assert(_ok and _result == "test, string") - local _result = _exString.join_strings(", ", { "test", "join", { test = "string" }, "string" }) - _test.assert(_result == "test, join, string") +test["join_strings"] = function () + local result = string_extensions.join_strings(", ", "test", "join", { test = "string" }, "string") + test.assert(result == "test, join, string") + local ok, result = pcall(string_extensions.join_strings, ", ", "test", { "join" }, "string") + test.assert(ok and result == "test, string") + local result = string_extensions.join_strings(", ", { "test", "join", { test = "string" }, "string" }) + test.assert(result == "test, join, string") end -_test["split"] = function() - local _result = _exString.split("test, join, string", ",", true) - _test.assert(_result[1] == "test" and _result[2] == "join" and _result[3] == "string") +test["split"] = function () + local result = string_extensions.split("test, join, string", ",", true) + test.assert(result[1] == "test" and result[2] == "join" and result[3] == "string") end -_test["interpolate"] = function() - local _result = _exString.interpolate("Hello from ${name}! This is formatted: ${n} ${n2}\nAnd this is escaped \\${escaped} and this a table value ${t}" - , { name = "printf", n = 1, n2 = 2, escaped = "anyValiue", t = {} }) - _test.assert(_result:match("Hello from printf! This is formatted: 1 2\nAnd this is escaped ${escaped} and this a table value table: ")) +test["interpolate"] = function () + local result = string_extensions.interpolate( + "Hello from ${name}! This is formatted: ${n} ${n2}\nAnd this is escaped \\${escaped} and this a table value ${t}" + , { name = "printf", n = 1, n2 = 2, escaped = "anyValiue", t = {} }) + test.assert(result:match + "Hello from printf! This is formatted: 1 2\nAnd this is escaped ${escaped} and this a table value table: ") end -_test["globalize"] = function() - _exString.globalize() - for k, v in pairs(_exString) do +test["globalize"] = function () + string_extensions.globalize() + for k, v in pairs(string_extensions) do if k ~= "globalize" then - _test.assert(string[k] == v) + test.assert(string[k] == v) end end end if not TEST then - _test.summary() + test.summary() end diff --git a/lib/tests/extensions/table.lua b/lib/tests/extensions/table.lua index 6dd43be3..15f349ad 100644 --- a/lib/tests/extensions/table.lua +++ b/lib/tests/extensions/table.lua @@ -1,182 +1,182 @@ -local _test = TEST or require 'u-test' -local _ok, _exTable = pcall(require, "eli.extensions.table") +local test = TEST or require"u-test" +local ok, table_extensions = pcall(require, "eli.extensions.table") -if not _ok then - _test["eli.extensions.table available"] = function() - _test.assert(false, "eli.extensions.table not available") +if not ok then + test["eli.extensions.table available"] = function () + test.assert(false, "eli.extensions.table not available") end if not TEST then - _test.summary() + test.summary() os.exit() else return end end -_test["eli.extensions.table available"] = function() - _test.assert(true) +test["eli.extensions.table available"] = function () + test.assert(true) end -_test["keys"] = function() - local _source = { a = 'a', c = 'c', b = 'b' } - local _keys = _exTable.keys(_source) - _test.assert(#_keys == 3) - for i, v in ipairs(_keys) do - _test.assert(_source[v]) +test["keys"] = function () + local source = { a = "a", c = "c", b = "b" } + local keys = table_extensions.keys(source) + test.assert(#keys == 3) + for i, v in ipairs(keys) do + test.assert(source[v]) end end -_test["values"] = function() - local _source = { a = 'a', c = 'c', b = 'b' } - local _values = _exTable.values(_source) - _test.assert(#_values == 3) - for i, v in ipairs(_values) do - _test.assert(_source[v] == v) +test["values"] = function () + local source = { a = "a", c = "c", b = "b" } + local values = table_extensions.values(source) + test.assert(#values == 3) + for i, v in ipairs(values) do + test.assert(source[v] == v) end end -_test["to_array"] = function() - local _source = { a = 'aa', c = 'cc', b = 'bb' } - local _arr = _exTable.to_array(_source) - _test.assert(#_arr == 3) +test["to_array"] = function () + local source = { a = "aa", c = "cc", b = "bb" } + local arr = table_extensions.to_array(source) + test.assert(#arr == 3) - for i, v in ipairs(_arr) do - _test.assert(_source[v.key] == v.value) + for i, v in ipairs(arr) do + test.assert(source[v.key] == v.value) end end -_test["is_array (array)"] = function() - local _source = { 'a', 'c', 'b' } - _test.assert(_exTable.is_array(_source)) +test["is_array (array)"] = function () + local source = { "a", "c", "b" } + test.assert(table_extensions.is_array(source)) end -_test["is_array (not array)"] = function() - local _source = { a = 'a', c = 'c', b = 'b' } - _test.assert(not _exTable.is_array(_source)) +test["is_array (not array)"] = function () + local source = { a = "a", c = "c", b = "b" } + test.assert(not table_extensions.is_array(source)) end -_test["filter"] = function() - local _source = { a = 'aa', c = 'cc', b = 'bb' } - local _result = _exTable.filter(_source, function(k, v) return k ~= 'a' end) +test["filter"] = function () + local source = { a = "aa", c = "cc", b = "bb" } + local result = table_extensions.filter(source, function (k, v) return k ~= "a" end) - _test.assert(_result.a == nil, "filtered key found in result") - _source.a = nil - for k, v in pairs(_result) do - _test.assert(_source[k] == v) + test.assert(result.a == nil, "filtered key found in result") + source.a = nil + for k, v in pairs(result) do + test.assert(source[k] == v) end end -_test["get"] = function() - local _t = { +test["get"] = function () + local t = { t2 = { - v1 = "aaa" + v1 = "aaa", }, - v2 = "bbb" + v2 = "bbb", } - _test.assert(type(_exTable.get(_t, "t2")) == "table") - _test.assert(_exTable.get(_t, "v2") == "bbb") - _test.assert(_exTable.get(_t, "v3") == nil) - _test.assert(_exTable.get(_t, "v3", "ccc") == "ccc") - - _test.assert(_exTable.get(_t, { "t2", "v1" }) == "aaa") - _test.assert(_exTable.get(_t, { "t2", "v2" }) == nil) - _test.assert(_exTable.get("invalid", { "t2", "v2" }) == nil) - - local _t2 = { "aaa", "bbb", "ccc" } - _test.assert(_exTable.get(_t2, { "t2", "v2" }) == nil) - _test.assert(_exTable.get(_t2, { "1" }) == "aaa") - _test.assert(_exTable.get(_t2, { "3" }) == "ccc") + test.assert(type(table_extensions.get(t, "t2")) == "table") + test.assert(table_extensions.get(t, "v2") == "bbb") + test.assert(table_extensions.get(t, "v3") == nil) + test.assert(table_extensions.get(t, "v3", "ccc") == "ccc") + + test.assert(table_extensions.get(t, { "t2", "v1" }) == "aaa") + test.assert(table_extensions.get(t, { "t2", "v2" }) == nil) + test.assert(table_extensions.get("invalid", { "t2", "v2" }) == nil) + + local t2 = { "aaa", "bbb", "ccc" } + test.assert(table_extensions.get(t2, { "t2", "v2" }) == nil) + test.assert(table_extensions.get(t2, { "1" }) == "aaa") + test.assert(table_extensions.get(t2, { "3" }) == "ccc") end -_test["set"] = function() - local _t = { +test["set"] = function () + local t = { t2 = { - v1 = "aaa" + v1 = "aaa", }, - v2 = "bbb" + v2 = "bbb", } - _exTable.set(_t, "t3", {}) - _test.assert(type(_exTable.get(_t, "t3")) == "table") - _test.assert(_exTable.get(_t, "v3") == nil) - _exTable.set(_t, "v3", "vvv") - _test.assert(_exTable.get(_t, "v3") == "vvv") - - _exTable.set(_t, { "t2", "v1" }, "zzz") - _test.assert(_exTable.get(_t, { "t2", "v1" }) == "zzz") - - local _t2 = { "aaa", "bbb", "ccc" } - _exTable.set(_t2, "2", "zzz") - _test.assert(_exTable.get(_t2, { "2" }) == "zzz") + table_extensions.set(t, "t3", {}) + test.assert(type(table_extensions.get(t, "t3")) == "table") + test.assert(table_extensions.get(t, "v3") == nil) + table_extensions.set(t, "v3", "vvv") + test.assert(table_extensions.get(t, "v3") == "vvv") + + table_extensions.set(t, { "t2", "v1" }, "zzz") + test.assert(table_extensions.get(t, { "t2", "v1" }) == "zzz") + + local t2 = { "aaa", "bbb", "ccc" } + table_extensions.set(t2, "2", "zzz") + test.assert(table_extensions.get(t2, { "2" }) == "zzz") end -_test["map"] = function() - local _t = { +test["map"] = function () + local t = { t2 = { - v1 = "aaa" + v1 = "aaa", }, - v2 = "bbb" + v2 = "bbb", } - local _result = _exTable.map(_t, function(v, k) return tostring(k) .. " = " .. tostring(v) end) - _test.assert(_result.t2:match("t2 = table:")) - _test.assert(_result.v2 == "v2 = bbb") + local result = table_extensions.map(t, function (v, k) return tostring(k) .. " = " .. tostring(v) end) + test.assert(result.t2:match"t2 = table:") + test.assert(result.v2 == "v2 = bbb") end -_test["reduce"] = function() - local _t = { +test["reduce"] = function () + local t = { t2 = { - v1 = "aaa" + v1 = "aaa", }, - v2 = "bbb" + v2 = "bbb", } - local _result = _exTable.reduce(_t, function(acc, v, k) + local result = table_extensions.reduce(t, function (acc, v, k) acc[k] = true return acc end, {}) - _test.assert(_result.t2 == true) - _test.assert(_result.v2 == true) + test.assert(result.t2 == true) + test.assert(result.v2 == true) - local _t2 = { "aaa", "bbb", "ccc" } - local _result2 = _exTable.reduce(_t2, function(acc, v) + local t2 = { "aaa", "bbb", "ccc" } + local result2 = table_extensions.reduce(t2, function (acc, v) return acc .. v end, "") - _test.assert(_result2 == "aaabbbccc") + test.assert(result2 == "aaabbbccc") end -_test["includes"] = function() - local _nested = { - v1 = "aaa" +test["includes"] = function () + local nested = { + v1 = "aaa", } - local _t = { - t2 = _nested, - v2 = "bbb" + local t = { + t2 = nested, + v2 = "bbb", } - _test.assert(table.includes(_t, "bbb") == true) - _test.assert(table.includes(_t, nil) == false) - _test.assert(table.includes(_t, "v2") == false) - _test.assert(table.includes(_t, _nested) == true) - _test.assert(table.includes(_t, { v1 = "aaa" }) == false) - _test.assert(table.includes(_t, { v1 = "aaa" }, true) == true) - - local _notTable = "not table" - _test.assert(table.includes(_notTable--[[@as table]] , "bbb") == false) + test.assert(table.includes(t, "bbb") == true) + test.assert(table.includes(t, nil) == false) + test.assert(table.includes(t, "v2") == false) + test.assert(table.includes(t, nested) == true) + test.assert(table.includes(t, { v1 = "aaa" }) == false) + test.assert(table.includes(t, { v1 = "aaa" }, true) == true) + + local not_table = "not table" + test.assert(table.includes(not_table --[[@as table]], "bbb") == false) end -_test["has_key"] = function() - local _t = { +test["has_key"] = function () + local t = { t2 = { - v1 = "aaa" + v1 = "aaa", }, - v2 = "bbb" + v2 = "bbb", } - _test.assert(table.has_key(_t, "t2") == true) - _test.assert(table.has_key(_t, nil) == false) - _test.assert(table.has_key(_t, "t3") == false) - _test.assert(table.has_key(_t, "v2") == true) + test.assert(table.has_key(t, "t2") == true) + test.assert(table.has_key(t, nil) == false) + test.assert(table.has_key(t, "t3") == false) + test.assert(table.has_key(t, "v2") == true) - local _notTable = "not table" - _test.assert(table.has_key(_notTable--[[@as table]] , "t2") == false) + local not_table = "not table" + test.assert(table.has_key(not_table --[[@as table]], "t2") == false) end if not TEST then - _test.summary() + test.summary() end diff --git a/lib/tests/fs.lua b/lib/tests/fs.lua index e38dc327..2464bceb 100644 --- a/lib/tests/fs.lua +++ b/lib/tests/fs.lua @@ -1,6 +1,6 @@ local test = require"u-test" -local ok, eliFs = pcall(require, "eli.fs") -local eliPath = require"eli.path" +local ok, eli_fs = pcall(require, "eli.fs") +local eli_path = require"eli.path" if not ok then test["eli.fs not available"] = function () @@ -17,95 +17,101 @@ end test["eli.fs available"] = function () test.assert(true) end test["copy file (path)"] = function () - local _ok, _error = eliFs.safe_copy_file("assets/test.file", + local ok, err = eli_fs.safe_copy_file("assets/test.file", "tmp/test.file") - test.assert(_ok, _error) - local _ok, _hash = eliFs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - local _ok, _hash2 = - eliFs.safe_hash_file("tmp/test.file", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + test.assert(ok, err) + local ok, file_hash = eli_fs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + local ok, file_hash2 = + eli_fs.safe_hash_file("tmp/test.file", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") end test["copy file (file*)"] = function () do - local _src = io.open("assets/test.file", "rb") - local _dst = io.open("tmp/test.file2", "wb") - local _ok, _error = eliFs.safe_copy_file(_src, _dst) - test.assert(_ok, _error) + local src = io.open("assets/test.file", "rb") + local dst = io.open("tmp/test.file2", "wb") + local ok, err = eli_fs.safe_copy_file(src, dst) + test.assert(ok, err) end - local _ok, _hash = eliFs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - local _ok, _hash2 = eliFs.safe_hash_file("tmp/test.file2", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + local ok, file_hash = eli_fs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + local ok, file_hash2 = eli_fs.safe_hash_file("tmp/test.file2", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") end test["copy file (mixed)"] = function () do - local _src = io.open("assets/test.file", "rb") - local _ok, _error = eliFs.safe_copy_file(_src, "tmp/test.file3") - test.assert(_ok, _error) + local src = io.open("assets/test.file", "rb") + local ok, err = eli_fs.safe_copy_file(src, "tmp/test.file3") + test.assert(ok, err) end - local _ok, _hash = eliFs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - local _ok, _hash2 = eliFs.safe_hash_file("tmp/test.file3", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + local ok, file_hash = eli_fs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + local ok, file_hash2 = eli_fs.safe_hash_file("tmp/test.file3", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") do - local _dst = io.open("tmp/test.file4", "wb") - local _ok, _error = eliFs.safe_copy_file("assets/test.file", _dst) - test.assert(_ok, _error) + local dst = io.open("tmp/test.file4", "wb") + local ok, err = eli_fs.safe_copy_file("assets/test.file", dst) + test.assert(ok, err) end - local _ok, _hash2 = eliFs.safe_hash_file("tmp/test.file4", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + local ok, file_hash2 = eli_fs.safe_hash_file("tmp/test.file4", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") end test["copy file (permissions)"] = function () - local _ok, _error = eliFs.safe_copy_file("assets/test.bin", + local ok, err = eli_fs.safe_copy_file("assets/test.bin", "tmp/test.bin") - test.assert(_ok, _error) - local info = eliFs.file_info"assets/test.bin" - local info2 = eliFs.file_info"tmp/test.bin" + test.assert(ok, err) + local info = eli_fs.file_info"assets/test.bin" + local info2 = eli_fs.file_info"tmp/test.bin" test.assert(info.permissions == info2.permissions, "permissions do not match") end test["copy (file)"] = function () - local _ok, _error = eliFs.safe_copy("assets/test.file", + local ok, err = eli_fs.safe_copy("assets/test.file", "tmp/test.file") - test.assert(_ok, _error) - local _ok, _hash = eliFs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - local _ok, _hash2 = - eliFs.safe_hash_file("tmp/test.file", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + test.assert(ok, err) + local ok, file_hash = eli_fs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + local ok, file_hash2 = + eli_fs.safe_hash_file("tmp/test.file", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") end test["copy (directory)"] = function () local SOURCE_DIR = "assets/copy-dir" local DEST_DIR = "tmp/copy-dir" - eliFs.remove(DEST_DIR, { recurse = true }) - eliFs.mkdirp(DEST_DIR) - local _ok, _error = eliFs.safe_copy(SOURCE_DIR, DEST_DIR) - test.assert(_ok, _error) - local paths = eliFs.read_dir(SOURCE_DIR, { recurse = true }) - for _, filePath in ipairs(paths) do - local sourceFilePath = eliPath.combine(SOURCE_DIR, filePath) - if eliFs.file_type(sourceFilePath) == "directory" then + eli_fs.remove(DEST_DIR, { recurse = true }) + eli_fs.mkdirp(DEST_DIR) + local ok, err = eli_fs.safe_copy(SOURCE_DIR, DEST_DIR) + test.assert(ok, err) + local paths = eli_fs.read_dir(SOURCE_DIR, { recurse = true }) --[=[@as string[]]=] + for _, file_path in ipairs(paths) do + local sourceFilePath = eli_path.combine(SOURCE_DIR, file_path) + if eli_fs.file_type(sourceFilePath) == "directory" then goto continue end - local _ok, _hash = eliFs.safe_hash_file(sourceFilePath, { type = "sha256", hex = true }) - test.assert(_ok, _hash) - local destFilePath = eliPath.combine(DEST_DIR, filePath) - local _ok, _hash2 = - eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + local ok, file_hash = eli_fs.safe_hash_file(sourceFilePath, { type = "sha256", hex = true }) + test.assert(ok, file_hash) + local destFilePath = eli_path.combine(DEST_DIR, file_path) + local ok, file_hash2 = + eli_fs.safe_hash_file(destFilePath, { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") ::continue:: end end @@ -114,58 +120,61 @@ test["copy (directory - overwrite)"] = function () local SOURCE_DIR = "assets/copy-dir" local SOURCE_DIR2 = "assets/copy-dir2" local DEST_DIR = "tmp/copy-dir2" - eliFs.remove(DEST_DIR, { recurse = true }) - eliFs.mkdirp(DEST_DIR) - local _ok, _error = eliFs.safe_copy(SOURCE_DIR, DEST_DIR) - test.assert(_ok, _error) - local paths = eliFs.read_dir(SOURCE_DIR, { recurse = true }) - for _, filePath in ipairs(paths) do - local sourceFilePath = eliPath.combine(SOURCE_DIR, filePath) - if eliFs.file_type(sourceFilePath) == "directory" then + eli_fs.remove(DEST_DIR, { recurse = true }) + eli_fs.mkdirp(DEST_DIR) + local ok, err = eli_fs.safe_copy(SOURCE_DIR, DEST_DIR) + test.assert(ok, err) + local paths = eli_fs.read_dir(SOURCE_DIR, { recurse = true }) --[=[@as string[]]=] + for _, file_path in ipairs(paths) do + local sourceFilePath = eli_path.combine(SOURCE_DIR, file_path) + if eli_fs.file_type(sourceFilePath) == "directory" then goto continue end - local _ok, _hash = eliFs.safe_hash_file(sourceFilePath, { type = "sha256", hex = true }) - test.assert(_ok, _hash) - local destFilePath = eliPath.combine(DEST_DIR, filePath) - local _ok, _hash2 = - eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + local ok, file_hash = eli_fs.safe_hash_file(sourceFilePath, { type = "sha256", hex = true }) + test.assert(ok, file_hash) + local destFilePath = eli_path.combine(DEST_DIR, file_path) + local ok, file_hash2 = + eli_fs.safe_hash_file(destFilePath, { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") ::continue:: end - local _ok, _error = eliFs.safe_copy(SOURCE_DIR2, DEST_DIR) - test.assert(_ok, _error) + local ok, err = eli_fs.safe_copy(SOURCE_DIR2, DEST_DIR) + test.assert(ok, err) for _, filePath in ipairs(paths) do - local sourceFilePath = eliPath.combine(SOURCE_DIR, filePath) - if eliFs.file_type(sourceFilePath) == "directory" then + local sourceFilePath = eli_path.combine(SOURCE_DIR, filePath) + if eli_fs.file_type(sourceFilePath) == "directory" then goto continue end - local _ok, _hash = eliFs.safe_hash_file(sourceFilePath, { type = "sha256", hex = true }) - test.assert(_ok, _hash) - local destFilePath = eliPath.combine(DEST_DIR, filePath) - local _ok, _hash2 = - eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + local ok, file_hash = eli_fs.safe_hash_file(sourceFilePath, { type = "sha256", hex = true }) + test.assert(ok, file_hash) + local destFilePath = eli_path.combine(DEST_DIR, filePath) + local ok, file_hash2 = + eli_fs.safe_hash_file(destFilePath, { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") ::continue:: end - local _ok, _error = eliFs.safe_copy(SOURCE_DIR2, DEST_DIR, { overwrite = true }) - test.assert(_ok, _error) - local paths = eliFs.read_dir(SOURCE_DIR2, { recurse = true }) - for _, filePath in ipairs(paths) do - local sourceFilePath = eliPath.combine(SOURCE_DIR2, filePath) - if eliFs.file_type(sourceFilePath) == "directory" then + local ok, err = eli_fs.safe_copy(SOURCE_DIR2, DEST_DIR, { overwrite = true }) + test.assert(ok, err) + local paths = eli_fs.read_dir(SOURCE_DIR2, { recurse = true }) --[=[@as string[]]=] + for _, file_path in ipairs(paths) do + local sourceFilePath = eli_path.combine(SOURCE_DIR2, file_path) + if eli_fs.file_type(sourceFilePath) == "directory" then goto continue end - local _ok, _hash = eliFs.safe_hash_file(sourceFilePath, { type = "sha256", hex = true }) - test.assert(_ok, _hash) - local destFilePath = eliPath.combine(DEST_DIR, filePath) - local _ok, _hash2 = - eliFs.safe_hash_file(destFilePath, { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + local ok, file_hash = eli_fs.safe_hash_file(sourceFilePath, { type = "sha256", hex = true }) + test.assert(ok, file_hash) + local destFilePath = eli_path.combine(DEST_DIR, file_path) + local ok, file_hash2 = + eli_fs.safe_hash_file(destFilePath, { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") ::continue:: end end @@ -173,171 +182,172 @@ end test["copy (directory + filtering)"] = function () local SOURCE_DIR = "assets/copy-dir" local DEST_DIR = "tmp/copy-dir3" - eliFs.remove(DEST_DIR, { recurse = true }) - eliFs.mkdirp(DEST_DIR) - local _ok, _error = eliFs.safe_copy(SOURCE_DIR, DEST_DIR, { ignore = { "file.txt" } }) - test.assert(_ok, _error) - test.assert(not eliFs.exists(eliPath.combine(DEST_DIR, "file.txt")), "file.txt should not exist") - - eliFs.remove(DEST_DIR, { recurse = true }) - eliFs.mkdirp(DEST_DIR) - local _ok, _error = eliFs.safe_copy(SOURCE_DIR, DEST_DIR, { + eli_fs.remove(DEST_DIR, { recurse = true }) + eli_fs.mkdirp(DEST_DIR) + local ok, err = eli_fs.safe_copy(SOURCE_DIR, DEST_DIR, { ignore = { "file.txt" } }) + test.assert(ok, err) + test.assert(not eli_fs.exists(eli_path.combine(DEST_DIR, "file.txt")), "file.txt should not exist") + + eli_fs.remove(DEST_DIR, { recurse = true }) + eli_fs.mkdirp(DEST_DIR) + local ok, err = eli_fs.safe_copy(SOURCE_DIR, DEST_DIR, { ignore = function (path) return path:match"file.txt" end, }) - test.assert(_ok, _error) - test.assert(not eliFs.exists(eliPath.combine(DEST_DIR, "file.txt")), "file.txt should not exist") + test.assert(ok, err) + test.assert(not eli_fs.exists(eli_path.combine(DEST_DIR, "file.txt")), "file.txt should not exist") end test["hash file (file*)"] = function () - local _src = io.open("assets/test.file", "rb") - local _ok, _hash = eliFs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) - test.assert(_ok, _hash) - local _ok, _hash2 = eliFs.safe_hash_file(_src, { type = "sha256", hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == _hash2, "hashes do not match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + local src = io.open("assets/test.file", "rb") + local ok, file_hash = eli_fs.safe_hash_file("assets/test.file", { type = "sha256", hex = true }) + test.assert(ok, file_hash) + local ok, file_hash2 = eli_fs.safe_hash_file(src, { type = "sha256", hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == file_hash2, + "hashes do not match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") end test["read file"] = function () - local _ok, _file1 = eliFs.safe_read_file"assets/test.file" - test.assert(_ok, _file1) - local _ok, _file2 = eliFs.safe_read_file"tmp/test.file" - test.assert(_ok, _file2) - test.assert(_file1 == _file2, "written data does not match") + local ok, file1 = eli_fs.safe_read_file"assets/test.file" + test.assert(ok, file1) + local ok, file2 = eli_fs.safe_read_file"tmp/test.file" + test.assert(ok, file2) + test.assert(file1 == file2, "written data does not match") end test["write file"] = function () - local _ok, _file1 = eliFs.safe_read_file"assets/test.file" - test.assert(_ok, _file1) - local _ok, _error = eliFs.safe_write_file("tmp/test.file2", _file1) - test.assert(_ok, _error) - local _ok, _file2 = eliFs.safe_read_file"tmp/test.file2" - test.assert(_ok, _file2) - test.assert(_file1 == _file2, "written data does not match") + local ok, file1 = eli_fs.safe_read_file"assets/test.file" + test.assert(ok, file1) + local ok, err = eli_fs.safe_write_file("tmp/test.file2", file1) + test.assert(ok, err) + local ok, file2 = eli_fs.safe_read_file"tmp/test.file2" + test.assert(ok, file2) + test.assert(file1 == file2, "written data does not match") end test["move (file)"] = function () - local _ok, _error = eliFs.safe_move("tmp/test.file", "tmp/test.file2") - test.assert(_ok, _error) - local _ok, _file1 = eliFs.safe_read_file"assets/test.file" - test.assert(_ok, _file1) - local _ok, _file2 = eliFs.safe_read_file"tmp/test.file2" - test.assert(_ok, _file2) - test.assert(_file1 == _file2, "written data does not match") + local ok, err = eli_fs.safe_move("tmp/test.file", "tmp/test.file2") + test.assert(ok, err) + local ok, file1 = eli_fs.safe_read_file"assets/test.file" + test.assert(ok, file1) + local ok, file2 = eli_fs.safe_read_file"tmp/test.file2" + test.assert(ok, file2) + test.assert(file1 == file2, "written data does not match") end -- extra test["mkdir"] = function () - local _ok, _error = eliFs.safe_mkdir"tmp/test-dir" - test.assert(_ok, _error) - local _ok, _exists = eliFs.safe_dir_exists"tmp/test-dir" - test.assert(_exists, (_exists or "not exists")) + local ok, err = eli_fs.safe_mkdir"tmp/test-dir" + test.assert(ok, err) + local _, exists = eli_fs.safe_dir_exists"tmp/test-dir" + test.assert(exists, (exists or "not exists")) end test["mkdirp"] = function () - local _ok = eliFs.safe_mkdirp"tmp/test-dir/test/test" - test.assert(_ok) - local _ok, _exists = eliFs.safe_dir_exists"tmp/test-dir/test/test" - test.assert(_ok and _exists, (_exists or "not exists")) + local ok = eli_fs.safe_mkdirp"tmp/test-dir/test/test" + test.assert(ok) + local ok, exists = eli_fs.safe_dir_exists"tmp/test-dir/test/test" + test.assert(ok and exists, (exists or "not exists")) end test["create_dir"] = function () - eliFs.safe_remove("tmp/test-dir", { recurse = true }) - local _ok, _error = eliFs.safe_create_dir"tmp/test-dir" - test.assert(_ok, _error) - local _ok, _exists = eliFs.safe_dir_exists"tmp/test-dir" - test.assert(_exists, (_exists or "not exists")) + eli_fs.safe_remove("tmp/test-dir", { recurse = true }) + local ok, err = eli_fs.safe_create_dir"tmp/test-dir" + test.assert(ok, err) + local _, exists = eli_fs.safe_dir_exists"tmp/test-dir" + test.assert(exists, (exists or "not exists")) - local _ok = eliFs.safe_create_dir"tmp/test-dir/test/test" - test.assert(_ok) - local _ok, _exists = eliFs.safe_dir_exists"tmp/test-dir/test/test" - test.assert(_ok and not _exists, (_exists or "exists")) + local ok = eli_fs.safe_create_dir"tmp/test-dir/test/test" + test.assert(ok) + local ok, exists = eli_fs.safe_dir_exists"tmp/test-dir/test/test" + test.assert(ok and not exists, (exists or "exists")) - local _ok = eliFs.safe_create_dir("tmp/test-dir/test/test", true) - test.assert(_ok) - local _ok, _exists = eliFs.safe_dir_exists"tmp/test-dir/test/test" - test.assert(_ok and _exists, (_exists or "not exists")) + local ok = eli_fs.safe_create_dir("tmp/test-dir/test/test", true) + test.assert(ok) + local ok, exists = eli_fs.safe_dir_exists"tmp/test-dir/test/test" + test.assert(ok and exists, (exists or "not exists")) end test["remove (file)"] = function () - local _ok, _file1 = eliFs.safe_remove"tmp/test.file2" - test.assert(_ok, _file1) - local _ok, _file2 = eliFs.safe_read_file"tmp/test.file2" - test.assert(not _ok, _file2) - local _ok, _error = eliFs.safe_move("tmp/test.file", "tmp/test.file2") - test.assert(_ok, _error) - local _ok, _file1 = eliFs.safe_remove("tmp/test.file2", { recurse = true }) - test.assert(_ok, _file1) - local _ok, _file2 = eliFs.safe_read_file"tmp/test.file2" - test.assert(not _ok, _file2) + local ok, file1 = eli_fs.safe_remove"tmp/test.file2" + test.assert(ok, file1) + local ok, file2 = eli_fs.safe_read_file"tmp/test.file2" + test.assert(not ok, file2) + local ok, err = eli_fs.safe_move("tmp/test.file", "tmp/test.file2") + test.assert(ok, err) + local ok, file1 = eli_fs.safe_remove("tmp/test.file2", { recurse = true }) + test.assert(ok, file1) + local ok, file2 = eli_fs.safe_read_file"tmp/test.file2" + test.assert(not ok, file2) end test["remove (dir)"] = function () - local _ok, _file1 = eliFs.safe_remove"tmp/test-dir/test/test" - test.assert(_ok, _file1) - local _ok, _exists = eliFs.safe_exists"tmp/test-dir/test/test" - test.assert(_ok and not _exists) + local ok, file1 = eli_fs.safe_remove"tmp/test-dir/test/test" + test.assert(ok, file1) + local ok, exists = eli_fs.safe_exists"tmp/test-dir/test/test" + test.assert(ok and not exists) end test["remove (keep)"] = function () - eliFs.safe_create_dir"tmp/test-dir" + eli_fs.safe_create_dir"tmp/test-dir" - eliFs.safe_create_dir("tmp/test-dir/test/test", true) - eliFs.safe_create_dir("tmp/test-dir/test/test-another", true) + eli_fs.safe_create_dir("tmp/test-dir/test/test", true) + eli_fs.safe_create_dir("tmp/test-dir/test/test-another", true) - eliFs.safe_create_dir("tmp/test-dir/test2/test2", true) - eliFs.safe_create_dir("tmp/test-dir/test2/test2-another", true) + eli_fs.safe_create_dir("tmp/test-dir/test2/test2", true) + eli_fs.safe_create_dir("tmp/test-dir/test2/test2-another", true) fs.write_file("tmp/test-dir/test/test/test.file", "test") fs.write_file("tmp/test-dir/test2/test2/test2.file", "test") - eliFs.safe_remove("tmp/test-dir", { + eli_fs.safe_remove("tmp/test-dir", { recurse = true, keep = function (path, fullpath) - path = eliPath.normalize(path, "unix", { endsep = "leave" }) + path = eli_path.normalize(path, "unix", { endsep = "leave" }) return path == "test/test/" or path == "test2/test2/test2.file" end, }) - test.assert(eliFs.exists"tmp/test-dir/test2/test2/test2.file") - test.assert(eliFs.exists"tmp/test-dir/test/test/") - test.assert(eliFs.exists"tmp/test-dir/test/test/test.file") + test.assert(eli_fs.exists"tmp/test-dir/test2/test2/test2.file") + test.assert(eli_fs.exists"tmp/test-dir/test/test/") + test.assert(eli_fs.exists"tmp/test-dir/test/test/test.file") - test.assert(not eliFs.exists"tmp/test-dir/test/test-another/") - test.assert(not eliFs.exists"tmp/test-dir/test2/test2-another/") + test.assert(not eli_fs.exists"tmp/test-dir/test/test-another/") + test.assert(not eli_fs.exists"tmp/test-dir/test2/test2-another/") end test["move (dir)"] = function () - local _ok, _error = eliFs.safe_move("tmp/test-dir/test", + local ok, err = eli_fs.safe_move("tmp/test-dir/test", "tmp/test-dir/test2") - test.assert(_ok, _error) - local _ok, _exists = eliFs.safe_exists"tmp/test-dir/test2" - test.assert(_ok and _exists, _exists) + test.assert(ok, err) + local ok, exists = eli_fs.safe_exists"tmp/test-dir/test2" + test.assert(ok and exists, exists) end test["remove (recurse)"] = function () - local _ok, _error = eliFs.safe_remove("tmp/test-dir", { recurse = true }) - test.assert(_ok, _error) - local _ok, _exists = eliFs.safe_exists"tmp/test-dir" - test.assert(_ok and not _exists, _exists) + local ok, err = eli_fs.safe_remove("tmp/test-dir", { recurse = true }) + test.assert(ok, err) + local ok, exists = eli_fs.safe_exists"tmp/test-dir" + test.assert(ok and not exists, exists) end test["remove (content_only)"] = function () - local _ok, _error = eliFs.safe_mkdir"tmp/test-dir" - test.assert(_ok, _error) - local _ok, _error = eliFs.safe_copy_file("assets/test.file", + local ok, err = eli_fs.safe_mkdir"tmp/test-dir" + test.assert(ok, err) + local ok, err = eli_fs.safe_copy_file("assets/test.file", "tmp/test-dir/test.file") - test.assert(_ok, _error) - local _ok, _error = eliFs.safe_remove("tmp/test-dir", + test.assert(ok, err) + local ok, err = eli_fs.safe_remove("tmp/test-dir", { content_only = true, recurse = true }) - test.assert(_ok, _error) - local _ok, _exists = eliFs.safe_exists"tmp/test-dir" - test.assert(_ok and _exists, _exists) - local _ok, _exists = eliFs.safe_exists"tmp/test-dir/test.file" - test.assert(_ok and not _exists, _exists) + test.assert(ok, err) + local ok, exists = eli_fs.safe_exists"tmp/test-dir" + test.assert(ok and exists, exists) + local ok, exists = eli_fs.safe_exists"tmp/test-dir/test.file" + test.assert(ok and not exists, exists) end -if not eliFs.EFS then +if not eli_fs.EFS then if not TEST then test.summary() print"EFS not detected, only basic tests executed..." @@ -349,130 +359,130 @@ if not eliFs.EFS then end test["file_type (file)"] = function () - eliFs.safe_remove"tmp/test.file" - local _ok, _type = eliFs.safe_file_type"tmp/test.file" - test.assert(_ok and _type == nil) - local _ok, _error = eliFs.safe_copy_file("assets/test.file", + eli_fs.safe_remove"tmp/test.file" + local ok, file_type = eli_fs.safe_file_type"tmp/test.file" + test.assert(ok and file_type == nil) + local ok, err = eli_fs.safe_copy_file("assets/test.file", "tmp/test.file") - test.assert(_ok, _error) - local _ok, _type = eliFs.safe_file_type"tmp/test.file" - test.assert(_ok and _type == "file") + test.assert(ok, err) + local ok, file_type = eli_fs.safe_file_type"tmp/test.file" + test.assert(ok and file_type == "file") end test["file_type (dir)"] = function () - local _ok, _type = eliFs.safe_file_type"tmp/" - test.assert(_ok and _type == "directory") + local ok, file_type = eli_fs.safe_file_type"tmp/" + test.assert(ok and file_type == "directory") end test["file_info (file)"] = function () - eliFs.safe_remove"tmp/test.file" - local ok, info = eliFs.safe_file_info"assets/test.file" + eli_fs.safe_remove"tmp/test.file" + local ok, info = eli_fs.safe_file_info"assets/test.file" test.assert(ok and info ~= nil) test.assert(info.mode == "file") test.assert(type(info.size) == "number" and info.size > 0) local size_from_path = info.size local f = io.open("assets/test.file", "rb") - local ok, info = eliFs.safe_file_info(f) + local ok, info = eli_fs.safe_file_info(f) test.assert(ok and info ~= nil) test.assert(info.mode == "file") test.assert(type(info.size) == "number" and info.size > 0) test.assert(info.size == size_from_path) - local ok, info = eliFs.safe_file_info"assets/test.file.not-existing" + local ok, info = eli_fs.safe_file_info"assets/test.file.not-existing" test.assert(ok and info == nil) end test["file_info (dir)"] = function () - local ok, info = eliFs.safe_file_info"assets" + local ok, info = eli_fs.safe_file_info"assets" test.assert(ok and info ~= nil) test.assert(info.mode == "directory") test.assert(type(info.size) == "number" and info.size > 0) - local ok, info = eliFs.safe_file_info"assets.not-existing" + local ok, info = eli_fs.safe_file_info"assets.not-existing" test.assert(ok and info == nil) end test["open_dir"] = function () - local _ok, _dir = eliFs.safe_open_dir"tmp/" - test.assert(_ok and _dir.__type == "ELI_DIR") + local ok, dir = eli_fs.safe_open_dir"tmp/" + test.assert(ok and dir.__type == "ELI_DIR") end test["read_dir & iter_dir"] = function () - local _ok, _dirEntries = eliFs.safe_read_dir"tmp/" - test.assert(_ok and #_dirEntries > 0) + local ok, dir_entries = eli_fs.safe_read_dir"tmp/" + test.assert(ok and #dir_entries > 0) local count = 0 - for _dirEntry in eliFs.iter_dir"tmp/" do count = count + 1 end - test.assert(#_dirEntries == count) + for _ in eli_fs.iter_dir"tmp/" do count = count + 1 end + test.assert(#dir_entries == count) end -local function _external_lock(file) - local _cmd = (os.getenv"QEMU" or "") .. +local function external_lock(file) + local cmd = (os.getenv"QEMU" or "") .. " " .. arg[-1] .. " -e \"x, err = fs.lock_file('" .. file .. "','w'); " .. "if etype(x) == 'ELI_FILE_LOCK' then os.exit(0); end; notAvailable = tostring(err):match('Resource temporarily unavailable') or tostring(err):match('locked a portion of the file'); " .. "exitCode = notAvailable and 11 or 12; os.exit(exitCode)\"" - local _ok, _, _code = os.execute(_cmd) - return _ok, _code + local ok, _, code = os.execute(cmd) + return ok, code end -local _lock -local _lockedFile = io.open("assets/test.file", "ab") +local lock +local locked_file = io.open("assets/test.file", "ab") test["lock_file (passed file)"] = function () - local _error - _lock, _error = eliFs.lock_file(_lockedFile, "w") - test.assert(_lock ~= nil, _error) - local _ok, _code, _ = _external_lock"assets/test.file" - test.assert(not _ok and _code == 11, "Should not be able to lock twice!") + local err + lock, err = eli_fs.lock_file(locked_file, "w") + test.assert(lock ~= nil, err) + local ok, code, _ = external_lock"assets/test.file" + test.assert(not ok and code == 11, "Should not be able to lock twice!") end test["lock (active - passed file)"] = function () - test.assert(_lock:is_active(), "Lock should be active") + test.assert(lock:is_active(), "Lock should be active") end test["unlock_file (passed file)"] = function () - local _ok, _code, _ = _external_lock"assets/test.file" - test.assert(not _ok and _code == 11, "Should not be able to lock twice!") - local _ok, _error = eliFs.unlock_file(_lock) - test.assert(_ok, _error) - local _ok, _code, _ = _external_lock"assets/test.file" - test.assert(_ok and _code == 0, "Should be able to lock now!") + local ok, code, _ = external_lock"assets/test.file" + test.assert(not ok and code == 11, "Should not be able to lock twice!") + local ok, err = eli_fs.unlock_file(lock) + test.assert(ok, err) + local ok, code, _ = external_lock"assets/test.file" + test.assert(ok and code == 0, "Should be able to lock now!") end test["lock (not active - passed file)"] = function () - test.assert(not _lock:is_active(), "Lock should not be active") + test.assert(not lock:is_active(), "Lock should not be active") end -if _lockedFile ~= nil then _lockedFile:close() end +if locked_file ~= nil then locked_file:close() end -local _lock +local lock test["lock_file (owned file)"] = function () - local _error - _lock, _error = eliFs.lock_file("assets/test.file", "w") - test.assert(_lock ~= nil, _error) - local _ok, _code, _ = _external_lock"assets/test.file" - test.assert(not _ok and _code == 11, "Should not be able to lock twice!") + local err + lock, err = eli_fs.lock_file("assets/test.file", "w") + test.assert(lock ~= nil, err) + local ok, code, _ = external_lock"assets/test.file" + test.assert(not ok and code == 11, "Should not be able to lock twice!") end test["lock (active - owned file)"] = function () - test.assert(_lock:is_active(), "Lock should be active") + test.assert(lock:is_active(), "Lock should be active") end test["unlock_file (owned file)"] = function () - local _ok, _code, _ = _external_lock"assets/test.file" - test.assert(not _ok and _code == 11, "Should not be able to lock twice!") - local _ok, _error = eliFs.unlock_file(_lock) - test.assert(_ok, _error) - local _ok, _code, _ = _external_lock"assets/test.file" - test.assert(_ok and _code == 0, "Should be able to lock now!") + local ok, code, _ = external_lock"assets/test.file" + test.assert(not ok and code == 11, "Should not be able to lock twice!") + local ok, err = eli_fs.unlock_file(lock) + test.assert(ok, err) + local ok, code, _ = external_lock"assets/test.file" + test.assert(ok and code == 0, "Should be able to lock now!") end test["lock (not active - owned file)"] = function () - test.assert(not _lock:is_active(), "Lock should not be active") + test.assert(not lock:is_active(), "Lock should not be active") end test["lock (cleanup)"] = function () function t() - local _lock, _error = eliFs.lock_file("assets/test.file", "w") - test.assert(_lock ~= nil, _error) - _lock:unlock() + local lock, err = eli_fs.lock_file("assets/test.file", "w") + test.assert(lock ~= nil, err) + lock:unlock() end t() @@ -482,26 +492,26 @@ end test["lock_file (owned file - )"] = function () do - local _lock , _error = eliFs.lock_file("assets/test.file", "w") - test.assert(_lock ~= nil, _error) - test.assert(_lock:is_active(), "Lock should be active") + local lock , err = eli_fs.lock_file("assets/test.file", "w") + test.assert(lock ~= nil, err) + test.assert(lock:is_active(), "Lock should be active") end - local _lock , _error = eliFs.lock_file("assets/test.file", "w") - test.assert(_lock ~= nil, _error) - test.assert(_lock:is_active(), "Lock should be active") + local lock , err = eli_fs.lock_file("assets/test.file", "w") + test.assert(lock ~= nil, err) + test.assert(lock:is_active(), "Lock should be active") end test["lock_dir & unlock_dir"] = function () - local _lock, _error = eliFs.lock_dir"tmp" - test.assert(_lock, _error) - test.assert(_lock:is_active(), "Lock should be active") - local _ok, _locked = eliFs.safe_link_info"tmp/lockfile" - test.assert(_ok and _locked) - local _ok, _error = eliFs.safe_unlock_dir(_lock) - test.assert(_ok, _error) - test.assert(not _lock:is_active(), "Lock should not be active") - local _ok, _locked = eliFs.safe_link_info"tmp/lockfile" - test.assert(_ok and not _locked) + local lock, err = eli_fs.lock_dir"tmp" + test.assert(lock, err) + test.assert(lock:is_active(), "Lock should be active") + local ok, locked = eli_fs.safe_link_info"tmp/lockfile" + test.assert(ok and locked) + local ok, err = eli_fs.safe_unlock_dir(lock) + test.assert(ok, err) + test.assert(not lock:is_active(), "Lock should not be active") + local ok, locked = eli_fs.safe_link_info"tmp/lockfile" + test.assert(ok and not locked) end if not TEST then test.summary() end diff --git a/lib/tests/global.lua b/lib/tests/global.lua index 7b6b0ba6..245cab0c 100644 --- a/lib/tests/global.lua +++ b/lib/tests/global.lua @@ -1,39 +1,40 @@ -local _test = TEST or require 'u-test' -local _ok, _eliGlobals = pcall(require, "eli.global") +local test = TEST or require"u-test" +local ok, eli_globals = pcall(require, "eli.global") -_test["global available"] = function () - _test.assert(_ok) +test["global available"] = function () + test.assert(ok) end -_test["printf C like"] = function () - local _oldWrite = io.write - local _result - io.write = function(data) - _result = data +test["printf C like"] = function () + local old_write = io.write + local result + io.write = function (data) + result = data end - _eliGlobals.printf("Hello from %s! This is formatted: %f %e\n", "printf", 1, 2) - io.write = _oldWrite - _test.assert(_result == "Hello from printf! This is formatted: 1.000000 2.000000e+00\n") + eli_globals.printf("Hello from %s! This is formatted: %f %e\n", "printf", 1, 2) + io.write = old_write + test.assert(result == "Hello from printf! This is formatted: 1.000000 2.000000e+00\n") end -_test["printf interpolated"] = function () - local _oldWrite = io.write - local _result - io.write = function(data) - _result = data +test["printf interpolated"] = function () + local old_write = io.write + local result + io.write = function (data) + result = data end - _eliGlobals.printf("Hello from ${name}! This is formatted: ${n} ${n2}\nAnd this is escaped \\${escaped}", { name = "printf", n = 1, n2 = 2, escaped = "anyValiue"}) - io.write = _oldWrite - _test.assert(_result == "Hello from printf! This is formatted: 1 2\nAnd this is escaped ${escaped}") + eli_globals.printf("Hello from ${name}! This is formatted: ${n} ${n2}\nAnd this is escaped \\${escaped}", + { name = "printf", n = 1, n2 = 2, escaped = "anyValiue" }) + io.write = old_write + test.assert(result == "Hello from printf! This is formatted: 1 2\nAnd this is escaped ${escaped}") end -_test["ELI_VERSION"] = function () - _test.assert(ELI_VERSION, "ELI_VERSION not defined!") +test["ELI_VERSION"] = function () + test.assert(ELI_VERSION, "ELI_VERSION not defined!") end -_test["ELI_LIB_VERSION"] = function () - _test.assert(ELI_LIB_VERSION, "ELI_LIB_VERSION not defined!") +test["ELI_LIB_VERSION"] = function () + test.assert(ELI_LIB_VERSION, "ELI_LIB_VERSION not defined!") end -if not TEST then - _test.summary() -end \ No newline at end of file +if not TEST then + test.summary() +end diff --git a/lib/tests/internals/util.lua b/lib/tests/internals/util.lua index a4f257c8..84b884ae 100644 --- a/lib/tests/internals/util.lua +++ b/lib/tests/internals/util.lua @@ -1,45 +1,45 @@ -local _test = TEST or require "u-test" -local _ok, _internalUtil = pcall(require, "eli.internals.util") +local test = TEST or require"u-test" +local ok, internal_util = pcall(require, "eli.internals.util") -if not _ok then - _test["eli.internals.util available"] = function() - _test.assert(false, "eli.internals.util not available") +if not ok then + test["eli.internals.util available"] = function () + test.assert(false, "eli.internals.util not available") end if not TEST then - _test.summary() + test.summary() os.exit() else return end end -_test["eli.internals.util get_root_dir"] = function() - local _paths = { +test["eli.internals.util get_root_dir"] = function () + local paths = { "src/__app/aaa/remove-all.lua", "src/__app/aaa/configure.lua", "src/__app/aaa/about.hjson", "src/__app/specs.json", - "src/__app/ami.lua" + "src/__app/ami.lua", } - _test.assert(_internalUtil.get_root_dir(_paths):match("^src[/\\]__app[/\\]$")) - _paths = { + test.assert(internal_util.get_root_dir(paths):match"^src[/\\]__app[/\\]$") + paths = { "src/__app/aaa/remove-all.lua", "src/__app/aaa/configure.lua", "src/__app/aaa/about.hjson", "src/__app/specs.json", - "src/ami.lua" + "src/ami.lua", } - _test.assert(_internalUtil.get_root_dir(_paths):match("^src[/\\]$")) - _paths = { + test.assert(internal_util.get_root_dir(paths):match"^src[/\\]$") + paths = { "src/__app/aaa/remove-all.lua", "src/__app/aaa/configure.lua", "src/__app/aaa/about.hjson", "specs.json", - "src/ami.lua" + "src/ami.lua", } - _test.assert(_internalUtil.get_root_dir(_paths):match("^$")) + test.assert(internal_util.get_root_dir(paths):match"^$") end if not TEST then - _test.summary() + test.summary() end diff --git a/lib/tests/net.lua b/lib/tests/net.lua index 79c06cca..b2609db2 100644 --- a/lib/tests/net.lua +++ b/lib/tests/net.lua @@ -31,10 +31,10 @@ test["download_string"] = function () end test["download (progress)"] = function () - local _print = io.write - local _printed = "" + local print_fn = io.write + local printed = "" local function new_print(msg) - _printed = _printed .. msg + printed = printed .. msg end io.write = new_print local _, _ = eliNet.safe_download_string("http://speedtest.ftp.otenet.gr/files/test1Mb.db", @@ -43,9 +43,9 @@ test["download (progress)"] = function () show_default_progress = 5, buffer_capacity = 1024 * 100, }) - io.write = _print -- restore - test.assert(_printed:match"(%d+)%%", "no progress detected") - _printed = "" + io.write = print_fn -- restore + test.assert(printed:match"(%d+)%%", "no progress detected") + printed = "" io.write = new_print local _, _ = eliNet.safe_download_string("http://speedtest.ftp.otenet.gr/files/test1Mb.db", { @@ -53,8 +53,8 @@ test["download (progress)"] = function () show_default_progress = true, buffer_capacity = 1024 * 100, }) - io.write = _print -- restore - test.assert(_printed:match"(%d+)%%", "no progress detected") + io.write = print_fn -- restore + test.assert(printed:match"(%d+)%%", "no progress detected") end test["download_large_file"] = function () @@ -102,8 +102,8 @@ test["RestClient get"] = function () if ok then break end end test.assert(ok, "request failed - " .. tostring(response)) - local _data = response.data - test.assert(_data.args.test == "aaa" and _data.args.test2 == "bbb", "Failed to verify result") + local data = response.data + test.assert(data.args.test == "aaa" and data.args.test2 == "bbb", "Failed to verify result") client = RestClient:new(HTTPBIN_URL .. "get", { timeout = TIMEOUT }) for _ = 1, RETRIES do @@ -259,10 +259,10 @@ end test["RestClient res (advanced)"] = function () local client = RestClient:new(HTTPBIN_URL, { content_type = "text/plain", timeout = TIMEOUT }) test.assert(tostring(client:get_url()) == HTTPBIN_URL) - local arrayClients = client:res{ "test", "test2/test3" } - test.assert(tostring(arrayClients[1]:get_url()) == HTTPBIN_URL .. "test") - test.assert(tostring(arrayClients[2]:get_url()) == HTTPBIN_URL .. "test2/test3") - local _objectClientsTemplate = { + local array_clients = client:res{ "test", "test2/test3" } + test.assert(tostring(array_clients[1]:get_url()) == HTTPBIN_URL .. "test") + test.assert(tostring(array_clients[2]:get_url()) == HTTPBIN_URL .. "test2/test3") + local object_clients_template = { test = "test", test2 = { "test3", "test4" }, test3 = { @@ -271,7 +271,7 @@ test["RestClient res (advanced)"] = function () test2 = "test2", }, } - local objectClients = client:res(_objectClientsTemplate) + local objectClients = client:res(object_clients_template) test.assert(tostring(objectClients.test:get_url()) == HTTPBIN_URL .. "test") test.assert(tostring(objectClients.test2[1]:get_url()) == HTTPBIN_URL .. "test2/test3") test.assert(tostring(objectClients.test2[2]:get_url()) == HTTPBIN_URL .. "test2/test4") diff --git a/lib/tests/proc.lua b/lib/tests/proc.lua index 59cc2a1d..f49ca56b 100644 --- a/lib/tests/proc.lua +++ b/lib/tests/proc.lua @@ -44,9 +44,9 @@ test["exec (stderr)"] = function () if pathSeparator == "\\" then cli = "cmd /c" end - local _result = eli_proc.exec(cli .. " \"echo error 173 >&2\"", { stderr = "pipe" }) - test.assert(_result.exit_code == 0 and _result.stdout_stream == nil and - string.trim(_result.stderr_stream:read"a") == "error 173") + local result = eli_proc.exec(cli .. " \"echo error 173 >&2\"", { stderr = "pipe" }) + test.assert(result.exit_code == 0 and result.stdout_stream == nil and + string.trim(result.stderr_stream:read"a") == "error 173") end test["exec (stderr - path)"] = function () @@ -136,8 +136,8 @@ test["spawn (custom stdout)"] = function () local result = isUnixLike and eli_proc.spawn("sh", { "-c", "printf '173'" }, options) or eli_proc.spawn("cmd", { "/c", "echo 173" }, options) - local _stdout = string.trim(eli_fs.read_file"tmp/test.stdout") - test.assert(result.exit_code == 0 and _stdout == "173") + local stdout = string.trim(eli_fs.read_file"tmp/test.stdout") + test.assert(result.exit_code == 0 and stdout == "173") end test["spawn (custom stderr)"] = function () @@ -208,8 +208,8 @@ test["spawn (stdin/stdout/stderr as path)"] = function () local result = isUnixLike and eli_proc.spawn("sh", options) or eli_proc.spawn("cmd", options) - local _stdout = result.stdout_stream:read"a" - test.assert(result.exit_code == 0 and _stdout:match"13354") + local stdout = result.stdout_stream:read"a" + test.assert(result.exit_code == 0 and stdout:match"13354") end test["spawn (process group)"] = function () diff --git a/lib/tests/t_bigint.lua b/lib/tests/t_bigint.lua index 857566cc..22024546 100644 --- a/lib/tests/t_bigint.lua +++ b/lib/tests/t_bigint.lua @@ -1,217 +1,217 @@ -local _test = TEST or require"u-test" -local _ok, _bigint = pcall(require, "bigint") +local test = TEST or require"u-test" +local ok, bigint = pcall(require, "bigint") -if not _ok then - _test["bigint available"] = function () - _test.assert(false, "bigint not available") +if not ok then + test["bigint available"] = function () + test.assert(false, "bigint not available") end if not TEST then - _test.summary() + test.summary() os.exit() else return end end -_test["bigint available"] = function () - _test.assert(true) +test["bigint available"] = function () + test.assert(true) end -_test["bigint.new"] = function () +test["bigint.new"] = function () -- bigint from float number (5.3 -> 5) - local _a = _bigint.new(5.3) - local _b = _bigint.new"5" - _test.assert(_a == _b) + local a = bigint.new(5.3) + local b = bigint.new"5" + test.assert(a == b) -- bigint from string "5.3" (should fail) - local success, _c = pcall(function () return _bigint.new"5.3" end) - _test.assert(not success) + local success, c = pcall(function () return bigint.new"5.3" end) + test.assert(not success) -- bigint from integer number (5 -> 5) - local _d = _bigint.new(5) - _test.assert(_d == _b) + local d = bigint.new(5) + test.assert(d == b) -- bigint from string "5" (5 -> 5) - local _e = _bigint.new"5" - _test.assert(_e == _b) + local e = bigint.new"5" + test.assert(e == b) -- bigint from bigint 5 (5 -> 5) - local _f = _bigint.new(_b) - _test.assert(_f == _b) + local f = bigint.new(b) + test.assert(f == b) -- init with no param (0) - local _g = _bigint.new() - _test.assert(_g == _bigint.new"0") + local g = bigint.new() + test.assert(g == bigint.new"0") end -_test["bigint.add"] = function () +test["bigint.add"] = function () -- add 2 bigints - local _a = _bigint.new"123456789012345678901234567890" - local _b = _bigint.new"123456789012345678901234567890" - local _c = _bigint.new"246913578024691357802469135780" - _test.assert(_a + _b == _c) + local a = bigint.new"123456789012345678901234567890" + local b = bigint.new"123456789012345678901234567890" + local c = bigint.new"246913578024691357802469135780" + test.assert(a + b == c) -- add bigint and number - _a = _bigint.new"123456789012345678901234567890" - _b = 110 - _c = _bigint.new"123456789012345678901234568000" - _test.assert(_a + _b == _c) + a = bigint.new"123456789012345678901234567890" + b = 110 + c = bigint.new"123456789012345678901234568000" + test.assert(a + b == c) -- add bigint and string - _a = _bigint.new"123456789012345678901234567890" - _b = "110" - _c = _bigint.new"123456789012345678901234568000" - _test.assert(_a + _b == _c) + a = bigint.new"123456789012345678901234567890" + b = "110" + c = bigint.new"123456789012345678901234568000" + test.assert(a + b == c) end -_test["bigint.add_abs"] = function () +test["bigint.add_abs"] = function () -- add 2 bigints - local _a = _bigint.new"123456789012345678901234567890" - local _b = _bigint.new"123456789012345678901234567890" * -1 - local _c = _bigint.new"246913578024691357802469135780" - _test.assert(_bigint.add_abs(_a, _b) == _c) + local a = bigint.new"123456789012345678901234567890" + local b = bigint.new"123456789012345678901234567890" * -1 + local c = bigint.new"246913578024691357802469135780" + test.assert(bigint.add_abs(a, b) == c) -- add bigint and number - _a = _bigint.new"123456789012345678901234567890" - _b = -110 - _c = _bigint.new"123456789012345678901234568000" - _test.assert(_bigint.add_abs(_a, _b) == _c) + a = bigint.new"123456789012345678901234567890" + b = -110 + c = bigint.new"123456789012345678901234568000" + test.assert(bigint.add_abs(a, b) == c) -- add bigint and string - _a = _bigint.new"123456789012345678901234567890" - _b = "-110" - _c = _bigint.new"123456789012345678901234568000" - _test.assert(_bigint.add_abs(_a, _b) == _c) + a = bigint.new"123456789012345678901234567890" + b = "-110" + c = bigint.new"123456789012345678901234568000" + test.assert(bigint.add_abs(a, b) == c) end -_test["bigint.sub"] = function () +test["bigint.sub"] = function () -- sub 2 bigints - local _a = _bigint.new"123456789012345678901234567890" - local _b = _bigint.new"123456789012345678901234567890" - local _c = _bigint.new"0" - _test.assert(_a - _b == _c) + local a = bigint.new"123456789012345678901234567890" + local b = bigint.new"123456789012345678901234567890" + local c = bigint.new"0" + test.assert(a - b == c) -- sub bigint and number - _a = _bigint.new"123456789012345678901234567890" - _b = 110 - _c = _bigint.new"123456789012345678901234567780" - _test.assert(_a - _b == _c) + a = bigint.new"123456789012345678901234567890" + b = 110 + c = bigint.new"123456789012345678901234567780" + test.assert(a - b == c) -- sub bigint and string - _a = _bigint.new"123456789012345678901234567890" - _b = "110" - _c = _bigint.new"123456789012345678901234567780" - _test.assert(_a - _b == _c) + a = bigint.new"123456789012345678901234567890" + b = "110" + c = bigint.new"123456789012345678901234567780" + test.assert(a - b == c) end -_test["bigint.sub_abs"] = function () +test["bigint.sub_abs"] = function () -- sub 2 bigints - local _a = _bigint.new"123456789012345678901234567890" - local _b = _bigint.new"123456789012345678901234567890" * -1 - local _c = _bigint.new"0" - _test.assert(_bigint.sub_abs(_a, _b) == _c) + local a = bigint.new"123456789012345678901234567890" + local b = bigint.new"123456789012345678901234567890" * -1 + local c = bigint.new"0" + test.assert(bigint.sub_abs(a, b) == c) -- sub bigint and number - _a = _bigint.new"123456789012345678901234567890" - _b = -110 - _c = _bigint.new"123456789012345678901234567780" - _test.assert(_bigint.sub_abs(_a, _b) == _c) + a = bigint.new"123456789012345678901234567890" + b = -110 + c = bigint.new"123456789012345678901234567780" + test.assert(bigint.sub_abs(a, b) == c) -- sub bigint and string - _a = _bigint.new"123456789012345678901234567890" - _b = "-110" - _c = _bigint.new"123456789012345678901234567780" - _test.assert(_bigint.sub_abs(_a, _b) == _c) + a = bigint.new"123456789012345678901234567890" + b = "-110" + c = bigint.new"123456789012345678901234567780" + test.assert(bigint.sub_abs(a, b) == c) end -_test["bigint.mul"] = function () +test["bigint.mul"] = function () -- mul 2 bigints - local _a = _bigint.new"123456789012345678901234567890" - local _b = _bigint.new"4" - local _c = _bigint.new"493827156049382715604938271560" - _test.assert(_a * _b == _c) + local a = bigint.new"123456789012345678901234567890" + local b = bigint.new"4" + local c = bigint.new"493827156049382715604938271560" + test.assert(a * b == c) -- mul bigint and number - _a = _bigint.new"123456789012345678901234567890" - _b = 4 - _c = _bigint.new"493827156049382715604938271560" - _test.assert(_a * _b == _c) + a = bigint.new"123456789012345678901234567890" + b = 4 + c = bigint.new"493827156049382715604938271560" + test.assert(a * b == c) -- mul bigint and string - _a = _bigint.new"123456789012345678901234567890" - _b = "4" - _c = _bigint.new"493827156049382715604938271560" - _test.assert(_a * _b == _c) + a = bigint.new"123456789012345678901234567890" + b = "4" + c = bigint.new"493827156049382715604938271560" + test.assert(a * b == c) end -_test["bigint.div"] = function () +test["bigint.div"] = function () -- div 2 bigints - local _a = _bigint.new"123456789012345678901234567890" - local _b = _bigint.new"4" - local _c = _bigint.new"30864197253086419725308641972" - _test.assert(_a / _b == _c) + local a = bigint.new"123456789012345678901234567890" + local b = bigint.new"4" + local c = bigint.new"30864197253086419725308641972" + test.assert(a / b == c) -- div bigint and number - _a = _bigint.new"123456789012345678901234567890" - _b = 4 - _c = _bigint.new"30864197253086419725308641972" - _test.assert(_a / _b == _c) + a = bigint.new"123456789012345678901234567890" + b = 4 + c = bigint.new"30864197253086419725308641972" + test.assert(a / b == c) -- div bigint and string - _a = _bigint.new"123456789012345678901234567890" - _b = "4" - _c = _bigint.new"30864197253086419725308641972" - _test.assert(_a / _b == _c) + a = bigint.new"123456789012345678901234567890" + b = "4" + c = bigint.new"30864197253086419725308641972" + test.assert(a / b == c) end -_test["bigint.mod"] = function () +test["bigint.mod"] = function () -- mod 2 bigints - local _a = _bigint.new"123456789012345678901234567890" - local _b = _bigint.new"4" - local _c = _bigint.new"2" - _test.assert(_a % _b == _c) + local a = bigint.new"123456789012345678901234567890" + local b = bigint.new"4" + local c = bigint.new"2" + test.assert(a % b == c) -- mod bigint and number - _a = _bigint.new"123456789012345678901234567890" - _b = 4 - _c = _bigint.new"2" - _test.assert(_a % _b == _c) + a = bigint.new"123456789012345678901234567890" + b = 4 + c = bigint.new"2" + test.assert(a % b == c) -- mod bigint and string - _a = _bigint.new"123456789012345678901234567890" - _b = "4" - _c = _bigint.new"2" - _test.assert(_a % _b == _c) + a = bigint.new"123456789012345678901234567890" + b = "4" + c = bigint.new"2" + test.assert(a % b == c) end -_test["bigint.pow"] = function () +test["bigint.pow"] = function () -- pow 2 bigints - local _a = _bigint.new"2" - local _b = _bigint.new"4" - local _c = _bigint.new"16" - _test.assert(_a ^ _b == _c) + local a = bigint.new"2" + local b = bigint.new"4" + local c = bigint.new"16" + test.assert(a ^ b == c) -- pow bigint and number - _a = _bigint.new"2" - _b = 4 - _c = _bigint.new"16" - _test.assert(_a ^ _b == _c) + a = bigint.new"2" + b = 4 + c = bigint.new"16" + test.assert(a ^ b == c) -- pow bigint and string - _a = _bigint.new"2" - _b = "4" - _c = _bigint.new"16" - _test.assert(_a ^ _b == _c) + a = bigint.new"2" + b = "4" + c = bigint.new"16" + test.assert(a ^ b == c) end -_test["bigint.neg"] = function () +test["bigint.neg"] = function () -- unm bigint - local _a = _bigint.new"123456789012345678901234567890" - local _b = _bigint.new"-123456789012345678901234567890" - _test.assert(-_a == _b) + local a = bigint.new"123456789012345678901234567890" + local b = bigint.new"-123456789012345678901234567890" + test.assert(-a == b) end if not TEST then - _test.summary() + test.summary() end diff --git a/lib/tests/t_hjson.lua b/lib/tests/t_hjson.lua index 592ff6b4..d29910a1 100644 --- a/lib/tests/t_hjson.lua +++ b/lib/tests/t_hjson.lua @@ -1,43 +1,43 @@ -local _test = TEST or require "u-test" -local _ok, _hjson = pcall(require, "hjson") -local _ok2, _elitUtil = pcall(require, "eli.util") +local test = TEST or require"u-test" +local ok, hjson = pcall(require, "hjson") +local _, eli_util = pcall(require, "eli.util") -if not _ok then - _test["hjson available"] = function() - _test.assert(false, "hjson not available") +if not ok then + test["hjson available"] = function () + test.assert(false, "hjson not available") end if not TEST then - _test.summary() + test.summary() os.exit() else return end end -_test["hjson available"] = function() - _test.assert(true) +test["hjson available"] = function () + test.assert(true) end -local _data = { - test = { - nested = true, - type = "object" - }, - root = "root level", - test2 = { "nested", "array" }, - test3 = { - nested1 = { - type = "nested object", - level = 2 - }, - nested2 = { - type = "nested object", - level = 2 - } - } +local data = { + test = { + nested = true, + type = "object", + }, + root = "root level", + test2 = { "nested", "array" }, + test3 = { + nested1 = { + type = "nested object", + level = 2, + }, + nested2 = { + type = "nested object", + level = 2, + }, + }, } -local _encoded = [[{ +local encoded = [[{ root: root level test: { nested: true @@ -59,7 +59,7 @@ local _encoded = [[{ } }]] -local _encodedJson = [[{ +local encoded_json = [[{ "root": "root level", "test": { "nested": true, @@ -81,26 +81,26 @@ local _encodedJson = [[{ } }]] -_test["encode"] = function() - local _result = _hjson.encode(_data, { sortKeys = true }) - _test.assert(_result == _encoded) +test["encode"] = function () + local result = hjson.encode(data, { sortKeys = true }) + test.assert(result == encoded) end -_test["encode_to_json"] = function() - local _result = _hjson.encode_to_json(_data, { sortKeys = true }) - _test.assert(_result == _encodedJson) +test["encode_to_json"] = function () + local result = hjson.encode_to_json(data, { sortKeys = true }) + test.assert(result == encoded_json) end -_test["decode"] = function() - local _result = _hjson.decode(_encoded) - _test.assert(_elitUtil.equals(_result, _data, true)) +test["decode"] = function () + local result = hjson.decode(encoded) + test.assert(eli_util.equals(result, data, true)) end -_test["decode json"] = function() - local _result = _hjson.decode(_encodedJson) - _test.assert(_elitUtil.equals(_result, _data, true)) +test["decode json"] = function () + local result = hjson.decode(encoded_json) + test.assert(eli_util.equals(result, data, true)) end if not TEST then - _test.summary() + test.summary() end diff --git a/lib/tests/tar.lua b/lib/tests/tar.lua index fee58949..36c0ced5 100644 --- a/lib/tests/tar.lua +++ b/lib/tests/tar.lua @@ -27,34 +27,34 @@ test["extract"] = function () eli_fs.remove"tmp/f1.txt" eli_fs.remove"tmp/f2.txt" eli_fs.remove"tmp/f3.txt" - local _ok, _error = eli_tar.safe_extract("assets/test.tar", "tmp") - test.assert(_ok, _error) - local _ok, _hash = eli_fs.safe_hash_file("tmp/f1.txt", { hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == "3cabbf41959954a7fd8a90918849b0906d90aa05444ba2d37c7e6dd548c45060", "hashes dont match") - local _ok, _hash = eli_fs.safe_hash_file("tmp/f2.txt", { hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == "76f60fe07ed3fac881ff3022b141f1cd56242152a43fa90b1a3ac423a2efc7ba", "hashes dont match") - local _ok, _hash = eli_fs.safe_hash_file("tmp/f3.txt", { hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == "7e5449fc89e75e0b8c6cbb3568720d074fa435f88dd9bf5e8b82c012a6c86c2a", "hashes dont match") + local ok, err = eli_tar.safe_extract("assets/test.tar", "tmp") + test.assert(ok, err) + local ok, file_hash = eli_fs.safe_hash_file("tmp/f1.txt", { hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == "3cabbf41959954a7fd8a90918849b0906d90aa05444ba2d37c7e6dd548c45060", "hashes dont match") + local ok, file_hash = eli_fs.safe_hash_file("tmp/f2.txt", { hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == "76f60fe07ed3fac881ff3022b141f1cd56242152a43fa90b1a3ac423a2efc7ba", "hashes dont match") + local ok, file_hash = eli_fs.safe_hash_file("tmp/f3.txt", { hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == "7e5449fc89e75e0b8c6cbb3568720d074fa435f88dd9bf5e8b82c012a6c86c2a", "hashes dont match") end test["extract_file"] = function () eli_fs.remove"tmp/f2.txt.untared" - local _ok, _error = eli_tar.safe_extract_file("assets/test.tar", "f2.txt", "tmp/f2.txt.untared") - test.assert(_ok, _error) - local _ok, _hash = eli_fs.safe_hash_file("tmp/f2.txt.untared", { hex = true }) - test.assert(_ok, _hash) - test.assert(_hash == "76f60fe07ed3fac881ff3022b141f1cd56242152a43fa90b1a3ac423a2efc7ba", "hashes dont match") + local ok, err = eli_tar.safe_extract_file("assets/test.tar", "f2.txt", "tmp/f2.txt.untared") + test.assert(ok, err) + local ok, file_hash = eli_fs.safe_hash_file("tmp/f2.txt.untared", { hex = true }) + test.assert(ok, file_hash) + test.assert(file_hash == "76f60fe07ed3fac881ff3022b141f1cd56242152a43fa90b1a3ac423a2efc7ba", "hashes dont match") end test["extract_string"] = function () - local _ok, _file = eli_tar.safe_extract_string("assets/test.tar", "f3.txt") - test.assert(_ok, _file) - local _ok, _hash = eli_hash.safe_sha256_sum(_file, true) - test.assert(_ok, _hash) - test.assert(_hash == "7e5449fc89e75e0b8c6cbb3568720d074fa435f88dd9bf5e8b82c012a6c86c2a", "hashes dont match") + local ok, file = eli_tar.safe_extract_string("assets/test.tar", "f3.txt") + test.assert(ok, file) + local ok, file_hash = eli_hash.safe_sha256_sum(file, true) + test.assert(ok, file_hash) + test.assert(file_hash == "7e5449fc89e75e0b8c6cbb3568720d074fa435f88dd9bf5e8b82c012a6c86c2a", "hashes dont match") end if not TEST then diff --git a/lib/tests/util.lua b/lib/tests/util.lua index bc0ff461..f4131e14 100644 --- a/lib/tests/util.lua +++ b/lib/tests/util.lua @@ -1,287 +1,286 @@ -local _test = TEST or require"u-test" -local _ok, _eliUtil = pcall(require, "eli.util") +local test = TEST or require"u-test" +local ok, eli_util = pcall(require, "eli.util") -if not _ok then - _test["eli.util available"] = function () - _test.assert(false, "eli.util not available") +if not ok then + test["eli.util available"] = function () + test.assert(false, "eli.util not available") end if not TEST then - _test.summary() + test.summary() os.exit() else return end end -_test["eli.util available"] = function () - _test.assert(true) +test["eli.util available"] = function () + test.assert(true) end -_test["is_array (array)"] = function () - local _source = { "a", "c", "b" } - _test.assert(_eliUtil.is_array(_source)) +test["is_array (array)"] = function () + local source = { "a", "c", "b" } + test.assert(eli_util.is_array(source)) end -_test["is_array (array packed)"] = function () - local _source = table.pack("a", "c", "b") - _test.assert(_eliUtil.is_array(_source)) +test["is_array (array packed)"] = function () + local source = table.pack("a", "c", "b") + test.assert(eli_util.is_array(source)) end -_test["is_array (not array)"] = function () - local _source = { a = "a", c = "c", b = "b" } - _test.assert(not _eliUtil.is_array(_source)) +test["is_array (not array)"] = function () + local source = { a = "a", c = "c", b = "b" } + test.assert(not eli_util.is_array(source)) end -_test["merge_arrays"] = function () - local _t1 = { 1, "2", true } - local _t2 = { 3, "4", false } - local _result = _eliUtil.merge_arrays(_t1, _t2) - for k, v in pairs(_t1) do - _test.assert(_result[k] == v) +test["merge_arrays"] = function () + local t1 = { 1, "2", true } + local t2 = { 3, "4", false } + local result = eli_util.merge_arrays(t1, t2) + for k, v in pairs(t1) do + test.assert(result[k] == v) end - for k, v in pairs(_t2) do - _test.assert(_result[k + #_t1] == v) + for k, v in pairs(t2) do + test.assert(result[k + #t1] == v) end end -_test["merge_arrays - combine"] = function () - local _t1 = { 1, "2", true } - local _t2 = { 3, "4", false } - local _result = _eliUtil.merge_arrays(_t1, _t2, { merge_strategy = "combine" }) - for k, v in pairs(_t1) do - _test.assert(_result[k] == v) +test["merge_arrays - combine"] = function () + local t1 = { 1, "2", true } + local t2 = { 3, "4", false } + local result = eli_util.merge_arrays(t1, t2, { merge_strategy = "combine" }) + for k, v in pairs(t1) do + test.assert(result[k] == v) end - for k, v in pairs(_t2) do - _test.assert(_result[k + #_t1] == v) + for k, v in pairs(t2) do + test.assert(result[k + #t1] == v) end end -_test["merge_arrays - prefer-1"] = function () - local _t1 = { 1, "2", true } - local _t2 = { 3, "4", false } - local _result = _eliUtil.merge_arrays(_t1, _t2, { merge_strategy = "prefer-t1" }) - for k, v in pairs(_t1) do - _test.assert(_result[k] == v) +test["merge_arrays - prefer-1"] = function () + local t1 = { 1, "2", true } + local t2 = { 3, "4", false } + local result = eli_util.merge_arrays(t1, t2, { merge_strategy = "prefer-t1" }) + for k, v in pairs(t1) do + test.assert(result[k] == v) end - for k, v in pairs(_t2) do - _test.assert(_result[k + #_t1] == nil) + for k, v in pairs(t2) do + test.assert(result[k + #t1] == nil) end end -_test["merge_arrays - prefer-2"] = function () - local _t1 = { 1, "2", true } - local _t2 = { 3, "4", false } - local _result = _eliUtil.merge_arrays(_t1, _t2, { merge_strategy = "prefer-t2" }) - for k, v in pairs(_t2) do - _test.assert(_result[k] == v) +test["merge_arrays - prefer-2"] = function () + local t1 = { 1, "2", true } + local t2 = { 3, "4", false } + local result = eli_util.merge_arrays(t1, t2, { merge_strategy = "prefer-t2" }) + for k, v in pairs(t2) do + test.assert(result[k] == v) end end -_test["merge_arrays - overlay"] = function () - local _t1 = { 1, "2", true } - local _t2 = { 3, "4", false } - local _result = _eliUtil.merge_arrays(_t1, _t2, { merge_strategy = "overlay" }) - for k, v in pairs(_t2) do - _test.assert(_result[k] == v) +test["merge_arrays - overlay"] = function () + local t1 = { 1, "2", true } + local t2 = { 3, "4", false } + local result = eli_util.merge_arrays(t1, t2, { merge_strategy = "overlay" }) + for k, v in pairs(t2) do + test.assert(result[k] == v) end end -_test["merge_arrays (not arrays)"] = function () - local _t1 = { a = "a", c = "c", b = "b" } - local _t2 = { d = "d", f = "f", e = "e" } - local _result, _error = _eliUtil.merge_arrays(_t1, _t2) - _test.assert(not _result and _error:find"t1") - local _result, _error = _eliUtil.merge_arrays({ 1, 2, 3 }, _t2) - _test.assert(not _result and _error:find"t2") +test["merge_arrays (not arrays)"] = function () + local t1 = { a = "a", c = "c", b = "b" } + local t2 = { d = "d", f = "f", e = "e" } + local result, err = eli_util.merge_arrays(t1, t2) + test.assert(not result and err:find"t1") + local result, err = eli_util.merge_arrays({ 1, 2, 3 }, t2) + test.assert(not result and err:find"t2") end -_test["merge_tables (dictionaries - unique keys)"] = function () - local _t1 = { a = "a", c = "c", b = "b" } - local _t2 = { d = "d", f = "f", e = "e" } - local _result = _eliUtil.merge_tables(_t1, _t2) - for k, v in pairs(_t1) do - _test.assert(_result[k] == v) +test["merge_tables (dictionaries - unique keys)"] = function () + local t1 = { a = "a", c = "c", b = "b" } + local t2 = { d = "d", f = "f", e = "e" } + local result = eli_util.merge_tables(t1, t2) + for k, v in pairs(t1) do + test.assert(result[k] == v) end - for k, v in pairs(_t2) do - _test.assert(_result[k] == v) + for k, v in pairs(t2) do + test.assert(result[k] == v) end end -_test["merge_tables (dictionaries - no overwrite)"] = function () - local _t1 = { a = "a", c = "c", b = "b" } - local _t2 = { d = "d", f = "f", e = "e", c = "a" } - local _result = _eliUtil.merge_tables(_t1, _t2) - for k, v in pairs(_t1) do - _test.assert(_result[k] == v) +test["merge_tables (dictionaries - no overwrite)"] = function () + local t1 = { a = "a", c = "c", b = "b" } + local t2 = { d = "d", f = "f", e = "e", c = "a" } + local result = eli_util.merge_tables(t1, t2) + for k, v in pairs(t1) do + test.assert(result[k] == v) end - for k, v in pairs(_t2) do + for k, v in pairs(t2) do if k ~= "c" then - _test.assert(_result[k] == v) + test.assert(result[k] == v) end end end -_test["merge_tables (dictionaries - overwrite)"] = function () - local _t1 = { a = "a", c = "c", b = "b" } - local _t2 = { d = "d", f = "f", e = "e", c = "a" } - local _result = _eliUtil.merge_tables(_t1, _t2, true) - for k, v in pairs(_t1) do +test["merge_tables (dictionaries - overwrite)"] = function () + local t1 = { a = "a", c = "c", b = "b" } + local t2 = { d = "d", f = "f", e = "e", c = "a" } + local result = eli_util.merge_tables(t1, t2, true) + for k, v in pairs(t1) do if (k ~= "c") then - _test.assert(_result[k] == v) + test.assert(result[k] == v) end end - for k, v in pairs(_t2) do - _test.assert(_result[k] == v) + for k, v in pairs(t2) do + test.assert(result[k] == v) end end -_test["merge_tables (array)"] = function () - local _t1 = { "a", "c", "b" } - local _t2 = { "d", "f", "e" } - local _result = _eliUtil.merge_tables(_t1, _t2) - _test.assert(#_result == 6) +test["merge_tables (array)"] = function () + local t1 = { "a", "c", "b" } + local t2 = { "d", "f", "e" } + local result = eli_util.merge_tables(t1, t2) + test.assert(#result == 6) - local _matched = 0 - for i, v in ipairs(_result) do - for i2, v2 in ipairs(_t1) do + local matched = 0 + for i, v in ipairs(result) do + for i2, v2 in ipairs(t1) do if v == v2 then - _matched = _matched + 1 + matched = matched + 1 end end end - for i, v in ipairs(_result) do - for i2, v2 in ipairs(_t2) do + for i, v in ipairs(result) do + for i2, v2 in ipairs(t2) do if v == v2 then - _matched = _matched + 1 + matched = matched + 1 end end end - _test.assert(_matched == 6) + test.assert(matched == 6) end -_test["merge_tables (array) - combine"] = function () - local _t1 = { 1, "2", true } - local _t2 = { 3, "4", false } - local _result = _eliUtil.merge_tables(_t1, _t2, { array_merge_strategy = "combine" }) - for k, v in pairs(_t1) do - _test.assert(_result[k] == v) +test["merge_tables (array) - combine"] = function () + local t1 = { 1, "2", true } + local t2 = { 3, "4", false } + local result = eli_util.merge_tables(t1, t2, { array_merge_strategy = "combine" }) + for k, v in pairs(t1) do + test.assert(result[k] == v) end - for k, v in pairs(_t2) do - _test.assert(_result[k + #_t1] == v) + for k, v in pairs(t2) do + test.assert(result[k + #t1] == v) end end -_test["merge_tables (array) - prefer-1"] = function () - local _t1 = { 1, "2", true } - local _t2 = { 3, "4", false } - local _result = _eliUtil.merge_tables(_t1, _t2, { array_merge_strategy = "prefer-t1" }) - for k, v in pairs(_t1) do - _test.assert(_result[k] == v) +test["merge_tables (array) - prefer-1"] = function () + local t1 = { 1, "2", true } + local t2 = { 3, "4", false } + local result = eli_util.merge_tables(t1, t2, { array_merge_strategy = "prefer-t1" }) + for k, v in pairs(t1) do + test.assert(result[k] == v) end - for k, v in pairs(_t2) do - _test.assert(_result[k + #_t1] == nil) + for k, v in pairs(t2) do + test.assert(result[k + #t1] == nil) end end -_test["merge_tables (array) - prefer-2"] = function () - local _t1 = { 1, "2", true } - local _t2 = { 3, "4", false } - local _result = _eliUtil.merge_tables(_t1, _t2, { array_merge_strategy = "prefer-t2" }) - for k, v in pairs(_t2) do - _test.assert(_result[k] == v) +test["merge_tables (array) - prefer-2"] = function () + local t1 = { 1, "2", true } + local t2 = { 3, "4", false } + local result = eli_util.merge_tables(t1, t2, { array_merge_strategy = "prefer-t2" }) + for k, v in pairs(t2) do + test.assert(result[k] == v) end end -_test["merge_tables (array) - overlay"] = function () - local _t1 = { 1, "2", true } - local _t2 = { 3, "4", false } - local _result = _eliUtil.merge_tables(_t1, _t2, { array_merge_strategy = "overlay" }) - for k, v in pairs(_t2) do - _test.assert(_result[k] == v) +test["merge_tables (array) - overlay"] = function () + local t1 = { 1, "2", true } + local t2 = { 3, "4", false } + local result = eli_util.merge_tables(t1, t2, { array_merge_strategy = "overlay" }) + for k, v in pairs(t2) do + test.assert(result[k] == v) end end -_test["global_log_factory (GLOBAL_LOGGER == nil)"] = function () - local _debug = _eliUtil.global_log_factory("test/util", "debug") - _test.assert(pcall(_debug, "test")) +test["global_log_factory (GLOBAL_LOGGER == nil)"] = function () + local debug = eli_util.global_log_factory("test/util", "debug") + test.assert(pcall(debug, "test")) end -_test["global_log_factory (GLOBAL_LOGGER == 'ELI_LOGGER')"] = function () - local _called = false +test["global_log_factory (GLOBAL_LOGGER == 'ELI_LOGGER')"] = function () + local called = false GLOBAL_LOGGER = { log = function (logger, msg, lvl) - _called = true - _test.assert(logger.__type == "ELI_LOGGER") - _test.assert(lvl == "debug") + called = true + test.assert(logger.__type == "ELI_LOGGER") + test.assert(lvl == "debug") end, __type = "ELI_LOGGER", } setmetatable(GLOBAL_LOGGER, GLOBAL_LOGGER) - local _debug = _eliUtil.global_log_factory("test/util", "debug") - _test.assert(pcall(_debug, "test")) - _test.assert(_called) + local debug = eli_util.global_log_factory("test/util", "debug") + test.assert(pcall(debug, "test")) + test.assert(called) end -_test["clone - primitive"] = function () - local _n = 5 - local _s = "stringToClone" - local _nil = nil - local _fn = function () end +test["clone - primitive"] = function () + local n = 5 + local s = "stringToClone" + local nil_value = nil + local fn = function () end - _test.assert(_n == _eliUtil.clone(_n)) - _test.assert(_s == _eliUtil.clone(_s)) - _test.assert(_nil == _eliUtil.clone(_nil)) - _test.assert(_fn == _eliUtil.clone(_fn)) + test.assert(n == eli_util.clone(n)) + test.assert(s == eli_util.clone(s)) + test.assert(nil_value == eli_util.clone(nil_value)) + test.assert(fn == eli_util.clone(fn)) end -_test["equals - primitive"] = function () +test["equals - primitive"] = function () local function _validate(v1, v2) - return _eliUtil.equals(v1, v2) == (v1 == v2) + return eli_util.equals(v1, v2) == (v1 == v2) end - _test.assert(_validate("aaa", "aaa")) - _test.assert(_validate("aaa", "bbb")) - _test.assert(_validate(2, 2)) - _test.assert(_validate(2, 3)) - _test.assert(_validate(2, 2)) - _test.assert(_validate(nil, 3)) - _test.assert(_validate(nil, nil)) - _test.assert(_validate(true, false)) - _test.assert(_validate(true, true)) - _test.assert(_validate(true, true)) + test.assert(_validate("aaa", "aaa")) + test.assert(_validate("aaa", "bbb")) + test.assert(_validate(2, 2)) + test.assert(_validate(2, 3)) + test.assert(_validate(2, 2)) + test.assert(_validate(nil, 3)) + test.assert(_validate(nil, nil)) + test.assert(_validate(true, false)) + test.assert(_validate(true, true)) + test.assert(_validate(true, true)) end -_test["clone & equals - shallow"] = function () - local _t = { - _n = 5, - _s = "stringToClone", - _nil = nil, - _fn = function () end, +test["clone & equals - shallow"] = function () + local t = { + n = 5, + s = "stringToClone", + nil_value = nil, + fn = function () end, } - local _clone = _eliUtil.clone(_t) - _test.assert(_clone ~= _t) - _test.assert(_eliUtil.equals(_t, _clone, 1)) + local clone = eli_util.clone(t) + test.assert(clone ~= t) + test.assert(eli_util.equals(t, clone, 1)) end -_test["clone & equals - deep"] = function () - local _debug = _eliUtil.global_log_factory("test/util", "debug") - local _t = { - _n = 5, - _s = "stringToClone", - _nil = nil, - _fn = function () end, - _t = { - _n = 5, - _s = "stringToClone", - _nil = nil, - _fn = function () end, +test["clone & equals - deep"] = function () + local t = { + n = 5, + s = "stringToClone", + nil_value = nil, + fn = function () end, + t = { + n = 5, + s = "stringToClone", + nil_value = nil, + fn = function () end, }, } - local _clone = _eliUtil.clone(_t, true) - _test.assert(_clone ~= _t) - _test.assert(not _eliUtil.equals(_t, _clone, 1)) - _test.assert(_eliUtil.equals(_t, _clone, true)) + local clone = eli_util.clone(t, true) + test.assert(clone ~= t) + test.assert(not eli_util.equals(t, clone, 1)) + test.assert(eli_util.equals(t, clone, true)) end if not TEST then - _test.summary() + test.summary() end diff --git a/lib/tests/zip.lua b/lib/tests/zip.lua index a7f00dad..82daba00 100644 --- a/lib/tests/zip.lua +++ b/lib/tests/zip.lua @@ -1,9 +1,9 @@ local test = TEST or require"u-test" -local _ok, _eliZip = pcall(require, "eli.zip") -local _ok2, _eliFs = pcall(require, "eli.fs") -local _ok2, _eliHash = pcall(require, "eli.hash") +local ok, eli_zip = pcall(require, "eli.zip") +local ok2, eli_fs = pcall(require, "eli.fs") +local ok2, elifile_hash = pcall(require, "eli.hash") -if not _ok then +if not ok then test["eli.zip available"] = function () test.assert(false, "eli.zip not available") end @@ -20,69 +20,69 @@ test["eli.zip available"] = function () end test["extract"] = function () - _eliFs.remove"tmp/test.file" - local _ok, _error = _eliZip.safe_extract("assets/test.zip", "tmp") - test.assert(_ok, _error) - local _ok, _hash = _eliFs.safe_hash_file("tmp/test.file", { hex = true }) - test.assert(_ok, _hash) - local _ok, _hash2 = _eliFs.safe_hash_file("assets/test.file", { hex = true }) - test.assert(_ok, _hash2) - test.assert(_eliHash.equals(_hash, _hash2, true), - "hashes dont match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + eli_fs.remove"tmp/test.file" + local ok, err = eli_zip.safe_extract("assets/test.zip", "tmp") + test.assert(ok, err) + local ok, file_hash = eli_fs.safe_hash_file("tmp/test.file", { hex = true }) + test.assert(ok, file_hash) + local ok, file_hash2 = eli_fs.safe_hash_file("assets/test.file", { hex = true }) + test.assert(ok, file_hash2) + test.assert(elifile_hash.equals(file_hash, file_hash2, true), + "hashes dont match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") end test["extract_file"] = function () - _eliFs.remove"tmp/test.file.unzipped" - local _ok, _error = _eliZip.safe_extract_file("assets/test.zip", "test.file", "tmp/test.file.unzipped") - test.assert(_ok, _error) - local _ok, _hash = _eliFs.safe_hash_file("tmp/test.file.unzipped", { hex = true }) - local _ok, _hash2 = _eliFs.safe_hash_file("assets/test.file", { hex = true }) - test.assert(_ok, _hash2) - test.assert(_eliHash.equals(_hash, _hash2, true), - "hashes dont match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + eli_fs.remove"tmp/test.file.unzipped" + local ok, err = eli_zip.safe_extract_file("assets/test.zip", "test.file", "tmp/test.file.unzipped") + test.assert(ok, err) + local ok, file_hash = eli_fs.safe_hash_file("tmp/test.file.unzipped", { hex = true }) + local ok, file_hash2 = eli_fs.safe_hash_file("assets/test.file", { hex = true }) + test.assert(ok, file_hash2) + test.assert(elifile_hash.equals(file_hash, file_hash2, true), + "hashes dont match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") end test["extract_string"] = function () - local _ok, _file = _eliZip.safe_extract_string("assets/test.zip", "test.file") - test.assert(_ok, _file) - local _ok, _hash = _eliHash.safe_sha256_sum(_file, true) - test.assert(_ok, _hash) - local _ok, _hash2 = _eliFs.safe_hash_file("assets/test.file", { hex = true }) - test.assert(_ok, _hash2) - test.assert(_eliHash.equals(_hash, _hash2, true), - "hashes dont match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + local ok, file = eli_zip.safe_extract_string("assets/test.zip", "test.file") + test.assert(ok, file) + local ok, file_hash = elifile_hash.safe_sha256_sum(file, true) + test.assert(ok, file_hash) + local ok, file_hash2 = eli_fs.safe_hash_file("assets/test.file", { hex = true }) + test.assert(ok, file_hash2) + test.assert(elifile_hash.equals(file_hash, file_hash2, true), + "hashes dont match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") end test["compress"] = function () - _eliFs.remove"tmp/test.file.zip" - local _ok, _file = _eliZip.safe_compress("assets/test.file", "tmp/test.file.zip") - test.assert(_ok, _file) - local _ok, _error = _eliZip.safe_extract_file("tmp/test.file.zip", "test.file", "tmp/test.file.unzipped") - test.assert(_ok, _error) - local _ok, _hash = _eliFs.safe_hash_file("tmp/test.file.unzipped", { hex = true }) - local _ok, _hash2 = _eliFs.safe_hash_file("assets/test.file", { hex = true }) - test.assert(_ok, _hash2) - test.assert(_eliHash.equals(_hash, _hash2, true), - "hashes dont match (" .. tostring(_hash) .. "<>" .. tostring(_hash2) .. ")") + eli_fs.remove"tmp/test.file.zip" + local ok, file = eli_zip.safe_compress("assets/test.file", "tmp/test.file.zip") + test.assert(ok, file) + local ok, err = eli_zip.safe_extract_file("tmp/test.file.zip", "test.file", "tmp/test.file.unzipped") + test.assert(ok, err) + local ok, file_hash = eli_fs.safe_hash_file("tmp/test.file.unzipped", { hex = true }) + local ok, file_hash2 = eli_fs.safe_hash_file("assets/test.file", { hex = true }) + test.assert(ok, file_hash2) + test.assert(elifile_hash.equals(file_hash, file_hash2, true), + "hashes dont match (" .. tostring(file_hash) .. "<>" .. tostring(file_hash2) .. ")") end test["compress (filter)"] = function () - _eliFs.remove"tmp/test.file.zip" - local ok, file = _eliZip.safe_compress("assets", "tmp/test.file.zip", { + eli_fs.remove"tmp/test.file.zip" + local ok, file = eli_zip.safe_compress("assets", "tmp/test.file.zip", { filter = function (path, info) return path == "test.file" end, content_only = true, }) test.assert(ok, file) - local ok, error = _eliZip.safe_extract_file("tmp/test.file.zip", "test.file", "tmp/test.file.unzipped") + local ok, error = eli_zip.safe_extract_file("tmp/test.file.zip", "test.file", "tmp/test.file.unzipped") test.assert(ok, error) - local ok, hash = _eliFs.safe_hash_file("tmp/test.file.unzipped", { hex = true }) - local ok, hash2 = _eliFs.safe_hash_file("assets/test.file", { hex = true }) + local ok, hash = eli_fs.safe_hash_file("tmp/test.file.unzipped", { hex = true }) + local ok, hash2 = eli_fs.safe_hash_file("assets/test.file", { hex = true }) test.assert(ok, hash2) - test.assert(_eliHash.equals(hash, hash2, true), + test.assert(elifile_hash.equals(hash, hash2, true), "hashes dont match (" .. tostring(hash) .. "<>" .. tostring(hash2) .. ")") - local files = _eliZip.get_files"tmp/test.file.zip" + local files = eli_zip.get_files"tmp/test.file.zip" test.assert(#files == 1, "files count mismatch") end