Skip to content

Commit

Permalink
Merge pull request #11 from vyperlang/charles/review
Browse files Browse the repository at this point in the history
Charles/review
  • Loading branch information
z80dev authored Dec 9, 2023
2 parents 397578c + 48330e8 commit 3a9aa06
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 273 deletions.
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ pytest = "^7.4.3"
[tool.poetry.scripts]
vyper-lsp = 'vyper_lsp.main:main'

[tool.coverage.run]
source = ["vyper_lsp"]
omit = ["vyper_lsp/analyzer/SourceAnalyzer.py",
"vyper_lsp/__init__.py",
"vyper_lsp/__main__.py",
]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
44 changes: 41 additions & 3 deletions tests/test_completions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from lsprotocol.types import (
CompletionContext,
CompletionParams,
CompletionTriggerKind,
Position,
TextDocumentIdentifier,
)
Expand All @@ -27,7 +28,7 @@ def baz():
self.
"""

doc = Document(uri="examples/Foo.vy", source=src)
doc = Document(uri="<inline source code>", source=src)
pos = Position(line=11, character=7)
context = CompletionContext(trigger_character=".", trigger_kind=2)
params = CompletionParams(
Expand Down Expand Up @@ -62,15 +63,52 @@ def baz():
x: Foo = Foo.
"""

doc = Document(uri="examples/Foo.vy", source=src)
doc = Document(uri="<inline source code>", source=src)
pos = Position(line=15, character=18)
context = CompletionContext(trigger_character=".", trigger_kind=2)
params = CompletionParams(
text_document={"uri": doc.uri, "source": src}, position=pos, context=context
text_document=TextDocumentIdentifier(uri=doc.uri), position=pos, context=context
)

analyzer = AstAnalyzer(ast)
completions = analyzer.get_completions_in_doc(doc, params)
assert len(completions.items) == 2
assert "BAR" in [c.label for c in completions.items]
assert "BAZ" in [c.label for c in completions.items]


def test_completion_fn_decorator(ast):
src = """
@internal
def foo():
return
@external
def bar():
self.foo()
"""
ast.build_ast(src)

src += """
@
"""

doc = Document(uri="<inline source code>", source=src)
pos = Position(line=8, character=1)
context = CompletionContext(
trigger_character="@", trigger_kind=CompletionTriggerKind.TriggerCharacter
)
params = CompletionParams(
text_document=TextDocumentIdentifier(uri=doc.uri), position=pos, context=context
)

analyzer = AstAnalyzer(ast)
completions = analyzer.get_completions_in_doc(doc, params)
assert len(completions.items) == 6
labels = [c.label for c in completions.items]
assert "internal" in labels
assert "external" in labels
assert "payable" in labels
assert "nonpayable" in labels
assert "view" in labels
assert "pure" in labels
21 changes: 21 additions & 0 deletions tests/test_debouncer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import time
from vyper_lsp.debounce import Debouncer # Import Debouncer from your module


def test_debounce():
result = []

def test_function(arg):
result.append(arg)

debouncer = Debouncer(wait=0.5)
debounced_func = debouncer.debounce(test_function)

debounced_func("first call")
time.sleep(0.2) # Sleep for less than the debounce period
debounced_func("second call")
time.sleep(
0.6
) # Sleep for more than the debounce period to allow the function to execute

assert result == ["second call"]
64 changes: 63 additions & 1 deletion tests/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ def foo(x: int128, y: int128) -> int128:
@external
def bar():
self.foo(1, 2)
@internal
def baz(x: int128) -> int128:
return x
@external
def foobar():
self.foo(self.baz(1), 2)
"""
ast.build_ast(src)

doc = Document(uri="examples/Foo.vy", source=src)
doc = Document(uri="<inline source code>", source=src)

pos = Position(line=7, character=13)
params = SignatureHelpParams(
Expand All @@ -29,3 +37,57 @@ def bar():
assert sig_help.active_signature == 0
assert sig_help.signatures[0].active_parameter == 1
assert sig_help.signatures[0].label == "foo(x: int128, y: int128) -> int128"

pos = Position(line=15, character=22)
params = SignatureHelpParams(
text_document=TextDocumentIdentifier(doc.uri), position=pos
)
sig_help = analyzer.signature_help(doc, params)
assert sig_help
assert sig_help.active_signature == 0
assert sig_help.signatures[0].active_parameter == 1
assert sig_help.signatures[0].label == "baz(x: int128) -> int128"


def test_hover(ast: AST):
src = """
@internal
def foo(
x: int128,
y: int128
) -> int128:
return x + y
@external
def bar():
self.foo(1, 2)
@internal
def noreturn(x: uint256):
y: uint256 = x
@internal
def baz():
self.noreturn(1)
"""
ast.build_ast(src)

doc = Document(uri="<inline source code>", source=src)

pos = Position(line=10, character=11)

analyzer = AstAnalyzer(ast)
hover = analyzer.hover_info(doc, pos)
assert hover
assert (
hover
== """(Internal Function) def foo(
x: int128,
y: int128
) -> int128:"""
)

pos = Position(line=18, character=11)
hover = analyzer.hover_info(doc, pos)
assert hover
assert hover == "(Internal Function) def noreturn(x: uint256):"
43 changes: 43 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from vyper_lsp import utils


def test_get_word_at_cursor():
text = "self.foo = 123"
assert utils.get_word_at_cursor(text, 0) == "self"
assert utils.get_word_at_cursor(text, 1) == "self"
assert utils.get_word_at_cursor(text, 5) == "foo"
assert utils.get_word_at_cursor(text, 12) == "123"

text = "foo_bar = 123"
assert utils.get_word_at_cursor(text, 0) == "foo_bar"
assert utils.get_word_at_cursor(text, 4) == "foo_bar"


def test_get_expression_at_cursor():
text = "self.foo = 123"
assert utils.get_expression_at_cursor(text, 0) == "self.foo"
assert utils.get_expression_at_cursor(text, 1) == "self.foo"
assert utils.get_expression_at_cursor(text, 5) == "self.foo"
assert utils.get_expression_at_cursor(text, 12) == "123"

text = "foo_bar = self.baz (1,2,3)"
assert utils.get_expression_at_cursor(text, 0) == "foo_bar"
assert utils.get_expression_at_cursor(text, 4) == "foo_bar"
assert utils.get_expression_at_cursor(text, 21) == "self.baz (1,2,3)"


def test_get_internal_fn_name_at_cursor():
text = "self.foo = 123"
assert utils.get_internal_fn_name_at_cursor(text, 0) is None
assert utils.get_internal_fn_name_at_cursor(text, 1) is None
assert utils.get_internal_fn_name_at_cursor(text, 5) is None
assert utils.get_internal_fn_name_at_cursor(text, 12) is None

text = "foo_bar = self.baz (1,2,3)"
assert utils.get_internal_fn_name_at_cursor(text, 0) is None
assert utils.get_internal_fn_name_at_cursor(text, 4) is None
assert utils.get_internal_fn_name_at_cursor(text, 21) == "baz"

text = "self.foo(self.bar())"
assert utils.get_internal_fn_name_at_cursor(text, 7) == "foo"
assert utils.get_internal_fn_name_at_cursor(text, 15) == "bar"
Loading

0 comments on commit 3a9aa06

Please sign in to comment.