summaryrefslogtreecommitdiff
path: root/test/code_action/init.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/code_action/init.lua')
-rw-r--r--test/code_action/init.lua144
1 files changed, 133 insertions, 11 deletions
diff --git a/test/code_action/init.lua b/test/code_action/init.lua
index 2329ce4c..264cfacf 100644
--- a/test/code_action/init.lua
+++ b/test/code_action/init.lua
@@ -2,38 +2,40 @@ local core = require 'core.code-action'
local files = require 'files'
local lang = require 'language'
local catch = require 'catch'
+local furi = require 'file-uri'
rawset(_G, 'TEST', true)
local EXISTS = {}
-local function eq(a, b)
- if a == EXISTS and b ~= nil then
+local function eq(expected, result)
+ if expected == EXISTS and result ~= nil then
return true
end
- if b == EXISTS and a ~= nil then
+ if result == EXISTS and expected ~= nil then
return true
end
- local tp1, tp2 = type(a), type(b)
+ local tp1, tp2 = type(expected), type(result)
if tp1 ~= tp2 then
- return false
+ return false, string.format(": expected type %s, got %s", tp1, tp2)
end
if tp1 == 'table' then
local mark = {}
- for k in pairs(a) do
- if not eq(a[k], b[k]) then
- return false
+ for k in pairs(expected) do
+ local ok, err = eq(expected[k], result[k])
+ if not ok then
+ return false, string.format(".%s%s", k, err)
end
mark[k] = true
end
- for k in pairs(b) do
+ for k in pairs(result) do
if not mark[k] then
- return false
+ return false, string.format(".%s: missing key in result", k)
end
end
return true
end
- return a == b
+ return expected == result, string.format(": expected %s, got %s", expected, result)
end
function TEST(script)
@@ -47,6 +49,32 @@ function TEST(script)
end
end
+local function TEST_CROSSFILE(testfiles)
+ local mainscript = table.remove(testfiles, 1)
+ return function(expected)
+ for _, data in ipairs(testfiles) do
+ local uri = furi.encode(TESTROOT .. data.path)
+ files.setText(uri, data.content)
+ files.compileState(uri)
+ end
+
+ local newScript, catched = catch(mainscript, '?')
+ files.setText(TESTURI, newScript)
+ files.compileState(TESTURI)
+
+ local _ <close> = function ()
+ for _, info in ipairs(testfiles) do
+ files.remove(furi.encode(TESTROOT .. info.path))
+ end
+ files.remove(TESTURI)
+ end
+
+ local results = core(TESTURI, catched['?'][1][1], catched['?'][1][2])
+ assert(results)
+ assert(eq(expected, results))
+ end
+end
+
TEST [[
print(<?a?>, b, c)
]]
@@ -154,3 +182,97 @@ local t = {
-- edit = EXISTS,
-- },
--}
+
+TEST_CROSSFILE {
+[[
+ <?unrequiredModule?>.myFunction()
+]],
+ {
+ path = 'unrequiredModule.lua',
+ content = [[
+ local m = {}
+ m.myFunction = print
+ return m
+ ]]
+ }
+} {
+ {
+ title = lang.script('ACTION_AUTOREQUIRE', 'unrequiredModule', 'unrequiredModule'),
+ kind = 'refactor.rewrite',
+ command = {
+ title = 'autoRequire',
+ command = 'lua.autoRequire',
+ arguments = {
+ {
+ uri = TESTURI,
+ target = furi.encode(TESTROOT .. 'unrequiredModule.lua'),
+ name = 'unrequiredModule',
+ requireName = 'unrequiredModule'
+ },
+ },
+ }
+ }
+}
+
+TEST_CROSSFILE {
+[[
+ <?myModule?>.myFunction()
+]],
+ {
+ path = 'myModule/init.lua',
+ content = [[
+ local m = {}
+ m.myFunction = print
+ return m
+ ]]
+ }
+} {
+ {
+ title = lang.script('ACTION_AUTOREQUIRE', 'myModule.init', 'myModule'),
+ kind = 'refactor.rewrite',
+ command = {
+ title = 'autoRequire',
+ command = 'lua.autoRequire',
+ arguments = {
+ {
+ uri = TESTURI,
+ target = furi.encode(TESTROOT .. 'myModule/init.lua'),
+ name = 'myModule',
+ requireName = 'myModule.init'
+ },
+ },
+ }
+ },
+ {
+ title = lang.script('ACTION_AUTOREQUIRE', 'init', 'myModule'),
+ kind = 'refactor.rewrite',
+ command = {
+ title = 'autoRequire',
+ command = 'lua.autoRequire',
+ arguments = {
+ {
+ uri = TESTURI,
+ target = furi.encode(TESTROOT .. 'myModule/init.lua'),
+ name = 'myModule',
+ requireName = 'init'
+ },
+ },
+ }
+ },
+ {
+ title = lang.script('ACTION_AUTOREQUIRE', 'myModule', 'myModule'),
+ kind = 'refactor.rewrite',
+ command = {
+ title = 'autoRequire',
+ command = 'lua.autoRequire',
+ arguments = {
+ {
+ uri = TESTURI,
+ target = furi.encode(TESTROOT .. 'myModule/init.lua'),
+ name = 'myModule',
+ requireName = 'myModule'
+ },
+ },
+ }
+ },
+}