Skip to content

Commit

Permalink
LSP tests for TyEnumDeclaration and TyStructDeclaration (FuelLabs…
Browse files Browse the repository at this point in the history
…#4057)

## Description
This PR adds tests for go_to and hover functionality for
`TyStructDeclaration` and `TyEnumDeclaration` tokens.

works towards FuelLabs#3989
  • Loading branch information
JoshuaBatty authored Feb 13, 2023
1 parent 3f3fe47 commit 1d31ff6
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 12 deletions.
2 changes: 2 additions & 0 deletions sway-lsp/tests/fixtures/tokens/enums/.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/enums/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 = "enums"

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

/// Test Struct Docs
struct TestStruct {
foo: u8,
}

/// Color enum with RGB variants
enum Color {
Red: (),
Green: (),
Blue: (),
}

/// My Enum documentation
pub enum MyEnum {
First: TestStruct,
Second: Color,
Third: (u8, Color),
/// Docs for variants
Fourth: u8,
}

fn func() {
let x = Color::Red;
let y: MyEnum = MyEnum::Fourth(8);
}
2 changes: 1 addition & 1 deletion sway-lsp/tests/fixtures/tokens/functions/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum DumbError {
Error: (),
}

// Function with generic types
// Function with complex types
pub fn func(r: Rezult<u8, DumbError>) -> Rezult<u8, DumbError> {
Rezult::Ok(1u8)
}
2 changes: 2 additions & 0 deletions sway-lsp/tests/fixtures/tokens/structs/.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/structs/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 = "structs"

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

/// My data enum
enum Data {
First: (),
Second: (),
}

/// My struct type
struct MyStruct<T, U> {
g: U,
x: T,
y: Data,
z: (u64, Data),
t: [Data; 5],
j: (u32, (Data, [Data; 2])),
o: Option<Identity>,
}

struct Simple {
x: u8,
}

fn func() {
let x = Simple {
x: 7
};
}
4 changes: 2 additions & 2 deletions sway-lsp/tests/integration/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pub(crate) async fn definition_check<'a>(
pub(crate) async fn hover_request<'a>(
service: &mut LspService<Backend>,
hover_docs: &'a HoverDocumentation<'a>,
id: i64,
ids: &mut impl Iterator<Item = i64>,
) -> Request {
let params = json!({
"textDocument": {
Expand All @@ -433,7 +433,7 @@ pub(crate) async fn hover_request<'a>(
"character": hover_docs.req_char
}
});
let hover = build_request_with_id("textDocument/hover", params, id);
let hover = build_request_with_id("textDocument/hover", params, ids.next().unwrap());
let response = call_request(service, hover.clone()).await.unwrap().unwrap();
let value = response.result().unwrap().clone();
let hover_res: Hover = serde_json::from_value(value).unwrap();
Expand Down
182 changes: 173 additions & 9 deletions sway-lsp/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,13 +1050,28 @@ async fn go_to_definition_for_functions() {
};
// Return type
let _ = lsp::definition_check(&mut service, &go_to, &mut i).await;
go_to.def_line = 23;
go_to.def_start_char = 9;
go_to.def_end_char = 15;
definition_check_with_req_offset(&mut service, &mut go_to, 33, 42, &mut i).await;
go_to.def_line = 28;
go_to.def_start_char = 9;
go_to.def_end_char = 18;
definition_check_with_req_offset(&mut service, &mut go_to, 33, 55, &mut i).await;

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

// Function parameter
// Function parameters
go_to.def_line = 2;
go_to.def_start_char = 7;
go_to.def_end_char = 12;
definition_check_with_req_offset(&mut service, &mut go_to, 13, 16, &mut i).await;

// TODO: @IGI-111 add test for generic function parameter
go_to.def_line = 23;
go_to.def_start_char = 9;
go_to.def_end_char = 15;
definition_check_with_req_offset(&mut service, &mut go_to, 33, 18, &mut i).await;
go_to.def_line = 28;
go_to.def_start_char = 9;
go_to.def_end_char = 18;
definition_check_with_req_offset(&mut service, &mut go_to, 33, 28, &mut i).await;

// Functions expression
go_to.def_line = 8;
Expand All @@ -1065,6 +1080,88 @@ async fn go_to_definition_for_functions() {
definition_check_with_req_offset(&mut service, &mut go_to, 19, 13, &mut i).await;
}

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

let mut go_to = GotoDefinition {
req_uri: &uri,
req_line: 10,
req_char: 8,
def_line: 9,
def_start_char: 19,
def_end_char: 20,
def_path: uri.as_str(),
};
// Type Params
let _ = lsp::definition_check(&mut service, &go_to, &mut i).await;
go_to.def_line = 3;
go_to.def_start_char = 5;
go_to.def_end_char = 9;
definition_check_with_req_offset(&mut service, &mut go_to, 12, 8, &mut i).await;
definition_check_with_req_offset(&mut service, &mut go_to, 13, 16, &mut i).await;
definition_check_with_req_offset(&mut service, &mut go_to, 14, 9, &mut i).await;
definition_check_with_req_offset(&mut service, &mut go_to, 15, 16, &mut i).await;
definition_check_with_req_offset(&mut service, &mut go_to, 15, 23, &mut i).await;
// TODO: check `o: Option<Identity>`

// Call Path
go_to.def_line = 19;
go_to.def_start_char = 7;
go_to.def_end_char = 13;
definition_check_with_req_offset(&mut service, &mut go_to, 24, 16, &mut i).await;
}

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

let mut go_to = GotoDefinition {
req_uri: &uri,
req_line: 16,
req_char: 16,
def_line: 3,
def_start_char: 7,
def_end_char: 17,
def_path: uri.as_str(),
};
// Type Params
let _ = lsp::definition_check(&mut service, &go_to, &mut i).await;
go_to.def_line = 8;
go_to.def_start_char = 5;
go_to.def_end_char = 10;
definition_check_with_req_offset(&mut service, &mut go_to, 17, 15, &mut i).await;
definition_check_with_req_offset(&mut service, &mut go_to, 18, 20, &mut i).await;

// Variants
go_to.def_line = 9;
go_to.def_start_char = 4;
go_to.def_end_char = 7;
definition_check_with_req_offset(&mut service, &mut go_to, 24, 21, &mut i).await;
go_to.def_line = 20;
go_to.def_start_char = 4;
go_to.def_end_char = 10;
definition_check_with_req_offset(&mut service, &mut go_to, 25, 31, &mut i).await;

// Call Path
go_to.def_line = 15;
go_to.def_start_char = 9;
go_to.def_end_char = 15;
definition_check_with_req_offset(&mut service, &mut go_to, 25, 23, &mut i).await;
}

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

#[tokio::test]
Expand All @@ -1075,6 +1172,7 @@ async fn hover_docs_for_consts() {
test_fixtures_dir().join("tokens/consts/src/main.sw"),
)
.await;
let mut i = 0..;

let mut hover = HoverDocumentation {
req_uri: &uri,
Expand All @@ -1083,10 +1181,10 @@ async fn hover_docs_for_consts() {
documentation: " documentation for CONSTANT_1",
};

let _ = lsp::hover_request(&mut service, &hover, 1).await;
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
hover.req_char = 49;
hover.documentation = " CONSTANT_2 has a value of 200";
let _ = lsp::hover_request(&mut service, &hover, 2).await;
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
}

#[tokio::test]
Expand All @@ -1104,9 +1202,74 @@ async fn hover_docs_for_functions() {
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;
let mut i = 0..;
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
}

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

let data_documention = "```sway\nenum Data\n```\n---\n My data enum";

let mut i = 0..;
let mut hover = HoverDocumentation {
req_uri: &uri,
req_line: 12,
req_char: 10,
documentation: data_documention,
};
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
hover.req_line = 13;
hover.req_char = 15;
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
hover.req_line = 14;
hover.req_char = 10;
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
hover.req_line = 15;
hover.req_char = 16;
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;

hover = HoverDocumentation {
req_uri: &uri,
req_line: 9,
req_char: 8,
documentation: "```sway\nstruct MyStruct\n```\n---\n My struct type",
};
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
}

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

let mut i = 0..;
let mut hover = HoverDocumentation {
req_uri: &uri,
req_line: 16,
req_char: 19,
documentation: "```sway\nstruct TestStruct\n```\n---\n Test Struct Docs",
};
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
hover.req_line = 18;
hover.req_char = 20;
hover.documentation = "```sway\nenum Color\n```\n---\n Color enum with RGB variants";
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
hover.req_line = 25;
hover.req_char = 29;
hover.documentation = " Docs for variants";
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
}
#[tokio::test]
async fn hover_docs_with_code_examples() {
let (mut service, _) = LspService::new(Backend::new);
Expand All @@ -1118,7 +1281,8 @@ async fn hover_docs_with_code_examples() {
req_char: 24,
documentation: "```sway\nstruct Data\n```\n---\n Struct holding:\n\n 1. A `value` of type `NumberOrString`\n 2. An `address` of type `u64`",
};
let _ = lsp::hover_request(&mut service, &hover, 1).await;
let mut i = 0..;
let _ = lsp::hover_request(&mut service, &hover, &mut i).await;
}

#[tokio::test]
Expand Down

0 comments on commit 1d31ff6

Please sign in to comment.