summaryrefslogtreecommitdiff
path: root/lib/lua-repl/t/plugin-basic-tests.lua
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-12-13 16:10:08 +1100
committerjacqueline <me@jacqueline.id.au>2023-12-13 16:10:08 +1100
commit64b106c13e18c33be0f2b0de532054e0ed3f731d (patch)
treeb54b1c90d941bc456b4d51e864970720bdf2d648 /lib/lua-repl/t/plugin-basic-tests.lua
parent5a2f0b08e0e3f20cda977b510b680d5843ae7283 (diff)
downloadtangara-fw-64b106c13e18c33be0f2b0de532054e0ed3f731d.tar.gz
add a cool lua repl
Diffstat (limited to 'lib/lua-repl/t/plugin-basic-tests.lua')
-rw-r--r--lib/lua-repl/t/plugin-basic-tests.lua206
1 files changed, 206 insertions, 0 deletions
diff --git a/lib/lua-repl/t/plugin-basic-tests.lua b/lib/lua-repl/t/plugin-basic-tests.lua
new file mode 100644
index 00000000..21283ef8
--- /dev/null
+++ b/lib/lua-repl/t/plugin-basic-tests.lua
@@ -0,0 +1,206 @@
+-- vim:foldmethod=marker
+local repl = require 'repl'
+local utils = require 'test-utils'
+pcall(require, 'luarocks.loader')
+require 'Test.More'
+
+plan(28)
+
+local clone = repl:clone()
+
+do -- basic tests {{{
+ local loaded
+
+ clone:loadplugin(function()
+ loaded = true
+ end)
+
+ ok(loaded)
+
+ error_like(function()
+ clone:loadplugin(function()
+ error 'uh-oh'
+ end)
+ end, 'uh%-oh')
+
+end -- }}}
+
+do -- loading the same plugin twice {{{
+ local function plugin()
+ end
+
+ local line_no
+
+ clone:loadplugin(plugin)
+ local _, err = pcall(function()
+ line_no = utils.next_line_number()
+ clone:loadplugin(plugin)
+ end)
+ like(err, tostring(line_no) .. ': plugin "function:%s+%S+" has already been loaded')
+
+ _, err = pcall(function()
+ line_no = utils.next_line_number()
+ clone:clone():loadplugin(plugin)
+ end)
+ like(err, tostring(line_no) .. ': plugin "function:%s+%S+" has already been loaded')
+
+ repl:clone():loadplugin(plugin)
+ repl:clone():loadplugin(plugin)
+end -- }}}
+
+do -- loading plugins by name {{{
+ local loaded
+
+ package.preload['repl.plugins.test'] = function()
+ loaded = true
+ end
+
+ clone:clone():loadplugin 'test'
+
+ ok(loaded)
+ loaded = false
+
+ clone:clone():loadplugin 'test'
+
+ ok(loaded, 'loading a plugin twice should initialize it twice')
+
+ package.preload['repl.plugins.test'] = function()
+ error 'uh-oh'
+ end
+
+ error_like(function()
+ clone:clone():loadplugin 'test'
+ end, 'uh%-oh')
+
+ package.preload['repl.plugins.test'] = nil
+
+ local line_no
+
+ local _, err = pcall(function()
+ line_no = utils.next_line_number()
+ clone:clone():loadplugin 'test'
+ end)
+ like(err, tostring(line_no) .. ': unable to locate plugin')
+end -- }}}
+
+do -- hasplugin tests {{{
+ local child = repl:clone()
+
+ local plugin = function()
+ end
+
+ child:loadplugin(plugin)
+
+ local grandchild = child:clone()
+
+ ok(not repl:hasplugin(plugin))
+ ok(child:hasplugin(plugin))
+ ok(grandchild:hasplugin(plugin))
+
+ plugin = function()
+ end
+
+ child:loadplugin(plugin)
+
+ ok(not repl:hasplugin(plugin))
+ ok(child:hasplugin(plugin))
+ ok(not grandchild:hasplugin(plugin))
+end -- }}}
+
+do -- global tests {{{
+ local clone = repl:clone()
+ local line_no
+
+ local _, err = pcall(function()
+ clone:loadplugin(function()
+ line_no = utils.next_line_number()
+ foo = 17
+ end)
+ end)
+
+ like(err, tostring(line_no) .. ': global environment is read%-only %(key = "foo"%)')
+
+ _, err = pcall(function()
+ clone:loadplugin(function()
+ line_no = utils.next_line_number()
+ _G.foo = 17
+ end)
+ end)
+
+ like(err, tostring(line_no) .. ': global environment is read%-only %(key = "foo"%)')
+end -- }}}
+
+do -- ifplugin tests {{{
+ local clone = repl:clone()
+ local has_run
+
+ package.preload['repl.plugins.test'] = function()
+ end
+
+ clone:ifplugin('test', function()
+ has_run = true
+ end)
+
+ ok(not has_run)
+
+ clone:loadplugin 'test'
+
+ ok(has_run)
+
+ has_run = false
+
+ clone:ifplugin('test', function()
+ has_run = true
+ end)
+
+ ok(has_run)
+end -- }}}
+
+do -- ifplugin multiple times {{{
+ local clone = repl:clone()
+ local has_run
+ local has_run2
+
+ package.preload['repl.plugins.test'] = function()
+ end
+
+ clone:ifplugin('test', function()
+ has_run = true
+ end)
+
+ clone:ifplugin('test', function()
+ has_run2 = true
+ end)
+
+ clone:loadplugin 'test'
+
+ ok(has_run)
+ ok(has_run2)
+end -- }}}
+
+do -- plugin return value {{{
+ local clone = repl:clone()
+
+ local result = clone:loadplugin(function()
+ return 17
+ end)
+
+ local result2 = clone:loadplugin(function()
+ end)
+
+ local result3, result4 = clone:loadplugin(function()
+ return 18, 19
+ end)
+
+ local result5, result6, result7 = clone:loadplugin(function()
+ return 20, nil, 21
+ end)
+
+ is(result, 17)
+ is(result2, nil)
+ is(result3, 18)
+ is(result4, 19)
+ is(result5, 20)
+ is(result6, nil)
+ is(result7, 21)
+end -- }}}