Skip to content

Commit

Permalink
LSP tests for TyFunctionDeclaration (FuelLabs#4031)
Browse files Browse the repository at this point in the history
## Description
Adds go to definition and hover doc comment tests for the
`TyFunctionDeclaration` type.

While working on this I noticed we aren't collecting tokens in function
parameters and return type with generic types. I've opened up FuelLabs#4030 for
this to be addressed.

Works towards FuelLabs#3989
  • Loading branch information
JoshuaBatty authored Feb 9, 2023
1 parent 1e37122 commit 9a00b5a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sway-lsp/tests/fixtures/tokens/functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
8 changes: 8 additions & 0 deletions sway-lsp/tests/fixtures/tokens/functions/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "functions"

[dependencies]
std = { path = "../../../../../sway-lib-std" }
36 changes: 36 additions & 0 deletions sway-lsp/tests/fixtures/tokens/functions/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
contract;

struct Point {
x: u32,
y: u32,
}

/// A simple function declaration with a struct as a return type
fn foo() -> Point {
Point { x: 1, y: 2 }
}

/// A function declaration with struct as a function parameter
pub fn bar(p: Point) -> Point {
Point { x: p.x, y: p.y }
}

// Function expressions
fn test() {
let p = foo();
let p2 = bar(p);
}

pub enum Rezult<T, E> {
Ok: T,
Err: E,
}

pub enum DumbError {
Error: (),
}

// Function with generic types
pub fn func(r: Rezult<u8, DumbError>) -> Rezult<u8, DumbError> {
Rezult::Ok(1u8)
}
57 changes: 57 additions & 0 deletions sway-lsp/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ async fn show_ast() {
shutdown_and_exit(&mut service).await;
}

//------------------- GO TO DEFINITION -------------------//

#[tokio::test]
async fn go_to_definition() {
let (mut service, _) = LspService::new(Backend::new);
Expand Down Expand Up @@ -777,6 +779,43 @@ async fn go_to_definition_for_consts() {
definition_check_with_req_offset(&mut service, &mut go_to, 10, 17, 7).await;
}

#[tokio::test]
async fn go_to_definition_for_functions() {
let (mut service, _) = LspService::new(Backend::new);
let uri = init_and_open(
&mut service,
test_fixtures_dir().join("tokens/functions/src/main.sw"),
)
.await;

let mut go_to = GotoDefintion {
req_uri: &uri,
req_line: 8,
req_char: 14,
def_line: 2,
def_start_char: 7,
def_end_char: 12,
def_path: uri.as_str(),
};
// Return type
let _ = lsp::definition_check(&mut service, &go_to, 1).await;

// TODO: @IGI-111 add test for generic return type

// Function parameter
definition_check_with_req_offset(&mut service, &mut go_to, 13, 16, 2).await;

// TODO: @IGI-111 add test for generic function parameter

// Functions expression
go_to.def_line = 8;
go_to.def_start_char = 3;
go_to.def_end_char = 6;
definition_check_with_req_offset(&mut service, &mut go_to, 19, 13, 3).await;
}

//------------------- HOVER DOCUMENTATION -------------------//

#[tokio::test]
async fn hover_docs_for_consts() {
let (mut service, _) = LspService::new(Backend::new);
Expand All @@ -799,6 +838,24 @@ async fn hover_docs_for_consts() {
let _ = lsp::hover_request(&mut service, &hover, 2).await;
}

#[tokio::test]
async fn hover_docs_for_functions() {
let (mut service, _) = LspService::new(Backend::new);
let uri = init_and_open(
&mut service,
test_fixtures_dir().join("tokens/functions/src/main.sw"),
)
.await;

let hover = HoverDocumentation {
req_uri: &uri,
req_line: 20,
req_char: 14,
documentation: "```sway\npub fn bar(p: Point) -> Point\n```\n---\n A function declaration with struct as a function parameter",
};
let _ = lsp::hover_request(&mut service, &hover, 1).await;
}

#[tokio::test]
async fn hover_docs_with_code_examples() {
let (mut service, _) = LspService::new(Backend::new);
Expand Down

0 comments on commit 9a00b5a

Please sign in to comment.