diff options
Diffstat (limited to 'server-beta/test')
-rw-r--r-- | server-beta/test/find_lib/init.lua | 106 | ||||
-rw-r--r-- | server-beta/test/rename/init.lua | 35 | ||||
-rw-r--r-- | server-beta/test/type_inference/init.lua | 62 |
3 files changed, 42 insertions, 161 deletions
diff --git a/server-beta/test/find_lib/init.lua b/server-beta/test/find_lib/init.lua deleted file mode 100644 index 51f53b75..00000000 --- a/server-beta/test/find_lib/init.lua +++ /dev/null @@ -1,106 +0,0 @@ -local core = require 'core' -local parser = require 'parser' -local buildVM = require 'vm' - -rawset(_G, 'TEST', true) - -function TEST(fullkey) - return function (script) - local start = script:find('<?', 1, true) - local finish = script:find('?>', 1, true) - local pos = (start + finish) // 2 + 1 - local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:parse(new_script, 'lua', 'Lua 5.3') - assert(ast) - local vm = buildVM(ast) - assert(vm) - local source = core.findSource(vm, pos) - local _, name = core.findLib(source) - assert(name == fullkey) - end -end - -TEST 'require' [[ -<?require?> 'xxx' -]] - -TEST 'req<require>' [[ -local <?req?> = require -]] - -TEST 'req<require>' [[ -local req = require -local t = { - xx = req, -} -t[<?'xx'?>]() -]] - -TEST 'table' [[ -<?table?>.unpack() -]] - -TEST 'xx<table>' [[ -local <?xx?> = require 'table' -]] - -TEST 'xx<table>' [[ -local rq = require -local lib = 'table' -local <?xx?> = rq(lib) -]] - -TEST 'table.insert' [[ -table.<?insert?>() -]] - -TEST 'table.insert' [[ -local t = table -t.<?insert?>() -]] - -TEST 'table.insert' [[ -local insert = table.insert -<?insert?>() -]] - -TEST 'table.insert' [[ -local t = require 'table' -t.<?insert?>() -]] - -TEST 'table.insert' [[ -require 'table'.<?insert?>() -]] - -TEST '*string.sub' [[ -local str = 'xxx' -str.<?sub?>() -]] - -TEST '*string:sub' [[ -local str = 'xxx' -str:<?sub?>(1, 1) -]] - -TEST '*string.sub' [[ -('xxx').<?sub?>() -]] - -TEST 'fs<bee::filesystem>' [[ -local <?fs?> = require 'bee.filesystem' -]] - -TEST 'fs.current_path' [[ -local filesystem = require 'bee.filesystem' - -ROOT = filesystem.<?current_path?>() -]] - -TEST(nil)[[ -print(<?insert?>) -]] - -TEST '_G' [[ -local x = <?_G?> -]] diff --git a/server-beta/test/rename/init.lua b/server-beta/test/rename/init.lua index e9fc5aee..8e672e69 100644 --- a/server-beta/test/rename/init.lua +++ b/server-beta/test/rename/init.lua @@ -1,39 +1,6 @@ -local core = require 'core.rename' +local core = require 'core.rename' local files = require 'files' -local function catch_target(script) - local list = {} - local cur = 1 - while true do - local start, finish = script:find('<[!?].-[!?]>', cur) - if not start then - break - end - list[#list+1] = { - start = start + 2, - finish = finish - 2, - } - cur = finish + 1 - end - return list -end - -local function founded(targets, results) - if #targets ~= #results then - return false - end - for _, target in ipairs(targets) do - for _, result in ipairs(results) do - if target.start == result.start and target.finish == result.finish then - goto NEXT - end - end - do return false end - ::NEXT:: - end - return true -end - local function replace(text, positions) local buf = {} table.sort(positions, function (a, b) diff --git a/server-beta/test/type_inference/init.lua b/server-beta/test/type_inference/init.lua index 59d853eb..4a9f30aa 100644 --- a/server-beta/test/type_inference/init.lua +++ b/server-beta/test/type_inference/init.lua @@ -1,22 +1,37 @@ -local parser = require 'parser' -local core = require 'core' -local buildVM = require 'vm' -local config = require 'config' +local files = require 'files' +local config = require 'config' +local searcher = require 'searcher' +local guide = require 'parser.guide' rawset(_G, 'TEST', true) -function TEST(res) +local function getSource(pos) + local ast = files.getAst('') + return guide.eachSourceContain(ast.ast, pos, function (source) + if source.type == 'local' + or source.type == 'getlocal' + or source.type == 'setlocal' + or source.type == 'setglobal' + or source.type == 'getglobal' + or source.type == 'field' + or source.type == 'method' then + return source + end + end) +end + +function TEST(wanted) return function (script) + files.removeAll() local start = script:find('<?', 1, true) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 - local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:parse(new_script, 'lua', 'Lua 5.3') - local vm = buildVM(ast) - assert(vm) - local result = core.findSource(vm, pos) - assert(result) - assert(res == result:bindValue():getType()) + local newScript = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') + files.setText('', newScript) + local source = getSource(pos) + assert(source) + local result = searcher.typeInference(source) or 'any' + assert(wanted == result) end end @@ -30,20 +45,30 @@ TEST 'boolean' [[ local <?var?> = true ]] -TEST 'number' [[ +TEST 'integer' [[ local <?var?> = 1 ]] +TEST 'number' [[ +local <?var?> = 1.0 +]] + TEST 'string' [[ local var = '111' t.<?x?> = var ]] -TEST 'string' [[ +TEST 'any' [[ local <?var?> var = '111' ]] +TEST 'string' [[ +local var +var = '111' +print(<?var?>) +]] + TEST 'function' [[ function <?xx?>() end @@ -55,8 +80,8 @@ end ]] TEST 'function' [[ -local <?xx?> -xx = function () +local xx +<?xx?> = function () end ]] @@ -64,11 +89,6 @@ TEST 'table' [[ local <?t?> = {} ]] -TEST 'table' [[ -local <?t?> -t = {} -]] - TEST 'function' [[ <?x?>() ]] |