Skip to content
This repository was archived by the owner on Mar 25, 2022. It is now read-only.

Commit

Permalink
tests: write scenarios for lib requiring
Browse files Browse the repository at this point in the history
  • Loading branch information
artur-barsegyan committed Dec 12, 2019
1 parent 41e1cea commit 1565165
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 52 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ log.txt
work_log.md

# MacOS
.DS_Store
.DS_Store

# Completions
/completions

# Any local test scripts
*.sh
Empty file added completions/.keep
Empty file.
File renamed without changes.
File renamed without changes.
12 changes: 4 additions & 8 deletions spec/test-scm-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ version = "scm-0"
source = {
url = "file://"
}
description = {
license = "MIT"
}
dependencies = {
"busted",
"luacheck",
"luacov",
"luacov-coveralls",
"ser-alloyed ~> 1.0"
"luaunit >= 0.2.2-1",
-- "luacheck",
-- "luacov",
-- "luacov-coveralls",
}
build = {
type = "none"
Expand Down
2 changes: 1 addition & 1 deletion tarantool-lsp/analyze.lua
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ function analyze.module(mod)

local internalLibs = docs:getInternalLibrariesList()
if internalLibs[mod] then
local ok, lib = pcall(require, 'tarantool-lsp.data.' .. mod)
local ok, lib = pcall(require, 'completions.' .. mod)
if ok then
local _scope = {}
translate_luacomplete(_scope, lib)
Expand Down
84 changes: 42 additions & 42 deletions tarantool-lsp/methods.lua
Original file line number Diff line number Diff line change
Expand Up @@ -614,48 +614,48 @@ method_handlers["textDocument/completion"] = function(params, id)
end
end

local current_line = document.lines[params.position.line + 1]["text"]
local left_part = current_line:sub(0, params.position.character)
local last_token = left_part:match("[%w.:_]*$")
if last_token then
local raw_completions = {}
local ADD_COMPLETION = function(cmplt)
raw_completions[cmplt] = true
end

-- [?] Completion handler returns input string at the first element
local tnt_completions = console.completion_handler(last_token, 0, last_token:len()) or {}
local doc_completions = docs:getCompletions(last_token)
fun.each(ADD_COMPLETION, fun.tail(tnt_completions))
fun.each(ADD_COMPLETION, fun.remove_if(function(cmplt)
if raw_completions[cmplt .. '('] then
return false
end

return true
end, doc_completions))

for _, cmplt in fun.map(function(cmplt) return cmplt end, raw_completions) do
local showedCmplt = cmplt
local insertedCmplt = cmplt
local cmpltKind = completionKinds["Field"]

if cmplt:find("[(]") then
cmpltKind = completionKinds["Function"]
showedCmplt = cmplt:gsub("%(", "")
end

local doc = docs:get(showedCmplt)

table.insert(items, {
label = showedCmplt,
kind = doc and doc.type or cmpltKind,
insertText = insertedCmplt,
documentation = doc and doc.description,
detail = doc and doc.brief
})
end
end
-- local current_line = document.lines[params.position.line + 1]["text"]
-- local left_part = current_line:sub(0, params.position.character)
-- local last_token = left_part:match("[%w.:_]*$")
-- if last_token then
-- local raw_completions = {}
-- local ADD_COMPLETION = function(cmplt)
-- raw_completions[cmplt] = true
-- end

-- -- [?] Completion handler returns input string at the first element
-- local tnt_completions = console.completion_handler(last_token, 0, last_token:len()) or {}
-- local doc_completions = docs:getCompletions(last_token)
-- fun.each(ADD_COMPLETION, fun.tail(tnt_completions))
-- fun.each(ADD_COMPLETION, fun.remove_if(function(cmplt)
-- if raw_completions[cmplt .. '('] then
-- return false
-- end

-- return true
-- end, doc_completions))

-- for _, cmplt in fun.map(function(cmplt) return cmplt end, raw_completions) do
-- local showedCmplt = cmplt
-- local insertedCmplt = cmplt
-- local cmpltKind = completionKinds["Field"]

-- if cmplt:find("[(]") then
-- cmpltKind = completionKinds["Function"]
-- showedCmplt = cmplt:gsub("%(", "")
-- end

-- local doc = docs:get(showedCmplt)

-- table.insert(items, {
-- label = showedCmplt,
-- kind = doc and doc.type or cmpltKind,
-- insertText = insertedCmplt,
-- documentation = doc and doc.description,
-- detail = doc and doc.brief
-- })
-- end
-- end

return rpc.respond(id, {
isIncomplete = false,
Expand Down
32 changes: 32 additions & 0 deletions test/completions/golden_files/csv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
return {
fields = {
csv = {
fields = {
csv = {
args = false,
type = "module"
},
dump = {
args = {
{}
},
type = "function"
},
iterate = {
args = {
{}
},
type = "function"
},
load = {
args = {
{}
},
type = "function"
}
},
type = "table"
}
},
type = "table"
}
22 changes: 22 additions & 0 deletions test/completions/golden_files/json.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
return {
fields = {
json = {
fields = {
decode = {
args = {
{}
},
type = "function"
},
encode = {
args = {
{}
},
type = "function"
},
},
type = "table"
}
},
type = "table"
}
171 changes: 171 additions & 0 deletions test/completions/libs_completion_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
local mock_loop = require('spec.mock_loop')
local test = require('luatest')

local DEFAULT_CSV_ITEMS = {
{ detail="<method>", insertText="dump", kind=2, label="dump() "},
{ detail="<method>", insertText="load", kind=2, label="load() "},
{ detail="<method>", insertText="iterate", kind=2, label="iterate() "}
}

local DEFAULT_JSON_ITEMS = {
{ detail="<method>", insertText="decode", kind=2, label="decode() "},
{ detail="<method>", insertText="encode", kind=2, label="encode() "}
}

package.loaded['tarantool-lsp.tnt-doc.doc-manager'] = {
init = function() end,
getInternalLibrariesList = function()
return {
csv = true,
json = true
}
end
}

package.loaded['completions.csv'] = require('test.completions.golden_files.csv')
package.loaded['completions.json'] = require('test.completions.golden_files.json')

local function make_completion_call(before, after, position)
local resp

mock_loop(function(rpc)
local doc = {
uri = "file:///tmp/fake.lua"
}
rpc.notify("textDocument/didOpen", {
textDocument = {uri = doc.uri, text = before}
})
rpc.notify("textDocument/didChange", {
textDocument = {uri = doc.uri, text = after},
contentChanges = {}
})
rpc.request("textDocument/completion", {
textDocument = doc,
position = position
}, function(out)
resp = out
end)
end)

return resp
end

local libs_completion = test.group("libs-completion")

libs_completion.test_basic = function()
local text = "local csv = require('csv')\n"

local cmplt = make_completion_call(text, text .. "csv.", {line = 1, character = 4})
test.assertEquals(cmplt, {
isIncomplete = false,
items = DEFAULT_CSV_ITEMS
})
end

-- The reason of the test is 'dirty scopes'. Doesn't take account of right border of the scopes.
-- In case of file extending, we have updated right borders, which we can update manually without AST data.
--[[
Case 1: Document was extended, current scopes are valid and new text righter that scope borders
Case 2: Document was extended, current scopes are valid but new text placed on the any previous scope
```lua
do
local m = require('...')
end
c|sv.
```
where | - is the right border
]]
-- libs_completion.test_closed_scope = function()
-- local text =
-- [[
-- do
-- local csv = require('csv')
-- end

-- ]]

-- local cmpltText =
-- [[
-- do
-- local csv = require('csv')
-- end

-- csv.
-- ]]
-- local cmplt = make_completion_call(text, cmpltText, {line = 4, character = 4})
-- test.assertEquals(cmplt, { isIncomplete = false, items = {} })
-- end

libs_completion.test_global_scope = function()
local text =
[[
local csv = require('csv')
]]

local cmpltText =
[[
local csv = require('csv')
local function abc()
csv.
end
]]
local cmplt = make_completion_call(text, cmpltText, {line = 3, character = 6})
test.assertEquals(cmplt, {
isIncomplete = false,
items = DEFAULT_CSV_ITEMS
})
end

libs_completion.test_local_scope = function()
local text =
[[
local function abc()
local csv = require('csv')
end
]]

local cmpltText =
[[
local function abc()
local csv = require('csv')
csv.
end
]]

local cmplt = make_completion_call(text, cmpltText, {line = 2, character = 6})
test.assertEquals(cmplt, {
isIncomplete = false,
items = DEFAULT_CSV_ITEMS
})
end

libs_completion.test_variable_shadowing = function()
local text =
[[
local lib = require('csv')
do
local lib = require('json')
end
]]

local cmpltText =
[[
local lib = require('csv')
do
local lib = require('json')
lib.
end
]]
local cmplt = make_completion_call(text, cmpltText, {line = 4, character = 6})
test.assertEquals(cmplt, {
isIncomplete = false,
items = DEFAULT_JSON_ITEMS
})
end

-- TODO: Reassignment to another variable

0 comments on commit 1565165

Please sign in to comment.