aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-05-02 00:21:32 +0530
committerAkshay Nair <phenax5@gmail.com>2025-05-02 16:23:05 +0530
commit00efc9757475b3b347fee0ce2aaccfbf4b1c33b5 (patch)
treeda75cc067b26afc4dc3b444ffafd68d17fcd947f /lua
parent0da44615dcf97ad34b4576da8ef0b77f6c9fc01f (diff)
downloadnull-browser-00efc9757475b3b347fee0ce2aaccfbf4b1c33b5.tar.gz
null-browser-00efc9757475b3b347fee0ce2aaccfbf4b1c33b5.zip
Add a simple lua test suite + add utils spec + fix keymap spec
Diffstat (limited to '')
-rw-r--r--lua/null-browser/api.lua2
-rw-r--r--lua/null-browser/test-utils.lua74
-rw-r--r--lua/null-browser/utils.lua35
3 files changed, 110 insertions, 1 deletions
diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua
index b3f1db1..f78ba4d 100644
--- a/lua/null-browser/api.lua
+++ b/lua/null-browser/api.lua
@@ -10,7 +10,7 @@ web.view = web.view or {}
web.history = web.history or {}
web.event = web.event or {}
-require 'lua.null-browser.utils'
+require 'null-browser.utils'
--- Add a keymap
---
diff --git a/lua/null-browser/test-utils.lua b/lua/null-browser/test-utils.lua
new file mode 100644
index 0000000..8871ce6
--- /dev/null
+++ b/lua/null-browser/test-utils.lua
@@ -0,0 +1,74 @@
+local t = {}
+
+local indent = 0
+
+local function with_indent(func)
+ indent = indent + 1
+ func()
+ indent = indent - 1
+end
+
+function t.describe(description, func)
+ local prefix = string.rep(' ', indent)
+ print(prefix .. '\x1b[1m' .. description .. '\x1b[0m')
+ io.flush()
+
+ with_indent(func)
+end
+
+function t.context(description, func)
+ local prefix = string.rep(' ', indent)
+ print(prefix .. '│ \x1b[32m' .. description .. '\x1b[0m')
+ io.flush()
+ with_indent(func)
+end
+
+function t.it(description, func)
+ local prefix = string.rep(' ', indent - 1)
+ print(prefix .. '└─⚪\x1b[36m' .. description .. '\x1b[0m')
+ io.flush()
+ with_indent(func)
+end
+
+---@diagnostic disable-next-line: unused-local
+function t.xit(description, func)
+ print('└─⚪\x1b[33m' .. description .. '\x1b[0m')
+ io.flush()
+end
+
+function t.expect(received)
+ local assert = {}
+
+ local function assert_error(messages)
+ print('❌AssertionError:')
+ for _, msg in ipairs(messages) do
+ print(' ' .. msg)
+ end
+ error('assertion_error')
+ end
+
+ function assert.to_equal(expected)
+ if web.utils.equals(received, expected) then return end
+ assert_error({ 'Expected: ' .. web.inspect(expected), 'Received: ' .. web.inspect(received) })
+ end
+
+ function assert.to_be_true()
+ if received == true then return end
+ assert_error({ 'Expected: ' .. web.inspect(received) .. ' to be true' })
+ end
+
+ function assert.to_be_false()
+ if received == false then return end
+ assert_error({ 'Expected: ' .. web.inspect(received) .. ' to be false' })
+ end
+
+ return assert
+end
+
+_G.describe = t.describe
+_G.context = t.context
+_G.it = t.it
+_G.xit = t.xit
+_G.expect = t.expect
+
+return t
diff --git a/lua/null-browser/utils.lua b/lua/null-browser/utils.lua
index 244fa6f..b6ff40a 100644
--- a/lua/null-browser/utils.lua
+++ b/lua/null-browser/utils.lua
@@ -25,3 +25,38 @@ function web.utils.table_merge(t, ...)
return t
end
+
+function web.utils.table_keys(tbl)
+ local keys = {}
+ for key, _ in pairs(tbl) do
+ table.insert(keys, key)
+ end
+ return keys
+end
+
+local function is_deep_equal_helper(a, b, table_pairs)
+ if type(a) ~= type(b) then return false end
+ if type(a) == 'table' and type(b) == 'table' then
+ local keys_a = web.utils.table_keys(a)
+ local keys_b = web.utils.table_keys(b)
+ if #keys_a ~= #keys_b then return false end
+
+ if table_pairs[a] == b then
+ return true
+ end
+
+ table_pairs[a] = b
+
+ for k in pairs(a) do
+ if not is_deep_equal_helper(a[k], b[k], table_pairs) then return false end
+ end
+
+ return true
+ end
+
+ return a == b
+end
+
+function web.utils.equals(a, b)
+ return is_deep_equal_helper(a, b, {})
+end