From 3b961f4bd3b5a41173394bb4ca4db7e5d3a719b6 Mon Sep 17 00:00:00 2001 From: Marek Kaput Date: Wed, 15 Jan 2025 13:47:10 +0100 Subject: [PATCH] Rewrite goto tests to insta commit-id:db86574a --- tests/e2e/goto_definition.rs | 97 ------------- tests/e2e/goto_definition/enums.rs | 60 ++++++++ tests/e2e/goto_definition/fns.rs | 19 +++ tests/e2e/goto_definition/macros.rs | 11 ++ tests/e2e/goto_definition/mod.rs | 75 ++++++++++ tests/e2e/goto_definition/paths.rs | 158 ++++++++++++++++++++ tests/e2e/goto_definition/structs.rs | 62 ++++++++ tests/e2e/goto_definition/traits.rs | 137 ++++++++++++++++++ tests/e2e/goto_definition/vars.rs | 53 +++++++ tests/e2e/support/cursor.rs | 20 +++ tests/e2e/support/insta/mod.rs | 16 ++ tests/e2e/support/mod.rs | 1 + tests/test_data/goto/enum_variants.txt | 28 ---- tests/test_data/goto/inline_macros.txt | 13 -- tests/test_data/goto/items.txt | 185 ------------------------ tests/test_data/goto/kw_crate.txt | 47 ------ tests/test_data/goto/modules.txt | 53 ------- tests/test_data/goto/struct_members.txt | 63 -------- tests/test_data/goto/variables.txt | 60 -------- 19 files changed, 612 insertions(+), 546 deletions(-) delete mode 100644 tests/e2e/goto_definition.rs create mode 100644 tests/e2e/goto_definition/enums.rs create mode 100644 tests/e2e/goto_definition/fns.rs create mode 100644 tests/e2e/goto_definition/macros.rs create mode 100644 tests/e2e/goto_definition/mod.rs create mode 100644 tests/e2e/goto_definition/paths.rs create mode 100644 tests/e2e/goto_definition/structs.rs create mode 100644 tests/e2e/goto_definition/traits.rs create mode 100644 tests/e2e/goto_definition/vars.rs create mode 100644 tests/e2e/support/insta/mod.rs delete mode 100644 tests/test_data/goto/enum_variants.txt delete mode 100644 tests/test_data/goto/inline_macros.txt delete mode 100644 tests/test_data/goto/items.txt delete mode 100644 tests/test_data/goto/kw_crate.txt delete mode 100644 tests/test_data/goto/modules.txt delete mode 100644 tests/test_data/goto/struct_members.txt delete mode 100644 tests/test_data/goto/variables.txt diff --git a/tests/e2e/goto_definition.rs b/tests/e2e/goto_definition.rs deleted file mode 100644 index 920aee51..00000000 --- a/tests/e2e/goto_definition.rs +++ /dev/null @@ -1,97 +0,0 @@ -use cairo_lang_test_utils::parse_test_file::TestRunnerResult; -use cairo_lang_utils::ordered_hash_map::OrderedHashMap; -use lsp_types::{ - ClientCapabilities, GotoCapability, GotoDefinitionParams, GotoDefinitionResponse, - TextDocumentClientCapabilities, TextDocumentPositionParams, lsp_request, -}; - -use crate::support::cairo_project_toml::CAIRO_PROJECT_TOML_2024_07; -use crate::support::cursor::{peek_caret, peek_selection}; -use crate::support::{cursors, sandbox}; - -cairo_lang_test_utils::test_file_test!( - goto_definition, - "tests/test_data/goto", - { - enum_variants: "enum_variants.txt", - inline_macros: "inline_macros.txt", - items: "items.txt", - kw_crate: "kw_crate.txt", - modules: "modules.txt", - struct_members: "struct_members.txt", - variables: "variables.txt", - }, - test_goto_definition -); - -fn caps(base: ClientCapabilities) -> ClientCapabilities { - ClientCapabilities { - text_document: base.text_document.or_else(Default::default).map(|it| { - TextDocumentClientCapabilities { - definition: Some(GotoCapability { - dynamic_registration: Some(false), - link_support: None, - }), - ..it - } - }), - ..base - } -} - -fn test_goto_definition( - inputs: &OrderedHashMap, - _args: &OrderedHashMap, -) -> TestRunnerResult { - let (cairo, cursors) = cursors(&inputs["cairo_code"]); - - let mut ls = sandbox! { - files { - "cairo_project.toml" => CAIRO_PROJECT_TOML_2024_07, - "src/lib.cairo" => cairo.clone(), - } - client_capabilities = caps; - }; - - ls.open_all_cairo_files_and_wait_for_project_update(); - - let mut goto_definitions = OrderedHashMap::default(); - - for (n, position) in cursors.carets().into_iter().enumerate() { - let mut report = String::new(); - - report.push_str(&peek_caret(&cairo, position)); - let code_action_params = GotoDefinitionParams { - text_document_position_params: TextDocumentPositionParams { - text_document: ls.doc_id("src/lib.cairo"), - position, - }, - work_done_progress_params: Default::default(), - partial_result_params: Default::default(), - }; - let response = - ls.send_request::(code_action_params); - - match response { - Some(GotoDefinitionResponse::Scalar(location)) => { - report.push_str("---\n"); - report.push_str(&peek_selection(&cairo, &location.range)); - } - Some(GotoDefinitionResponse::Array(locations)) => { - for location in locations { - report.push_str("---\n"); - report.push_str(&peek_selection(&cairo, &location.range)); - } - } - Some(GotoDefinitionResponse::Link(_)) => { - panic!("unexpected GotoDefinitionResponse::Link"); - } - None => { - report.push_str("None"); - } - } - goto_definitions.insert(format!("Goto definition #{}", n), report); - } - - TestRunnerResult::success(goto_definitions) -} diff --git a/tests/e2e/goto_definition/enums.rs b/tests/e2e/goto_definition/enums.rs new file mode 100644 index 00000000..6d3eeb0d --- /dev/null +++ b/tests/e2e/goto_definition/enums.rs @@ -0,0 +1,60 @@ +use crate::goto_definition::goto_definition; +use crate::support::insta::test_transform; + +#[test] +fn enum_item_in_type() { + test_transform!(goto_definition, r#" + enum Foo { Bar } + fn calc(foo: Foo) {} + "#, @r" + enum Foo { Bar } + fn calc(foo: Foo) {} + "); +} + +#[test] +fn enum_item_in_expr() { + test_transform!(goto_definition, r#" + enum Foo { Bar } + fn main() { + let foo = Foo::Bar; + } + "#, @r" + enum Foo { Bar } + fn main() { + let foo = Foo::Bar; + } + "); +} + +#[test] +fn enum_variant_in_expr() { + test_transform!(goto_definition, r#" + enum Foo { Bar } + fn main() { let foo = Foo::Bar; } + "#, @r" + enum Foo { Bar } + fn main() { let foo = Foo::Bar; } + "); +} + +#[test] +fn enum_variant_in_pattern() { + test_transform!(goto_definition, r#" + enum Foo { Bar } + fn main() { + let foo = Foo::Bar; + match foo { + Foo::Bar => {} + } + } + "#, @r" + enum Foo { Bar } + fn main() { + let foo = Foo::Bar; + match foo { + Foo::Bar => {} + } + } + "); +} diff --git a/tests/e2e/goto_definition/fns.rs b/tests/e2e/goto_definition/fns.rs new file mode 100644 index 00000000..ec7aa4ea --- /dev/null +++ b/tests/e2e/goto_definition/fns.rs @@ -0,0 +1,19 @@ +use crate::goto_definition::goto_definition; +use crate::support::insta::test_transform; + +#[test] +fn fn_call() { + test_transform!(goto_definition, r" + fn main() { foo(); } + fn foo() {} // good + mod bar { + fn foo() {} // bad + } + ", @r" + fn main() { foo(); } + fn foo() {} // good + mod bar { + fn foo() {} // bad + } + ") +} diff --git a/tests/e2e/goto_definition/macros.rs b/tests/e2e/goto_definition/macros.rs new file mode 100644 index 00000000..99785f9c --- /dev/null +++ b/tests/e2e/goto_definition/macros.rs @@ -0,0 +1,11 @@ +use crate::goto_definition::goto_definition; +use crate::support::insta::test_transform; + +#[test] +fn inline_macro() { + test_transform!(goto_definition, r#" + fn main() { + print!("Hello, world!"); + } + "#, @"none response") +} diff --git a/tests/e2e/goto_definition/mod.rs b/tests/e2e/goto_definition/mod.rs new file mode 100644 index 00000000..d3254ca8 --- /dev/null +++ b/tests/e2e/goto_definition/mod.rs @@ -0,0 +1,75 @@ +use lsp_types::{ + ClientCapabilities, GotoCapability, GotoDefinitionParams, GotoDefinitionResponse, + TextDocumentClientCapabilities, TextDocumentPositionParams, lsp_request, +}; + +use crate::support::cairo_project_toml::CAIRO_PROJECT_TOML_2024_07; +use crate::support::cursor::render_selections; +use crate::support::{cursors, sandbox}; + +mod enums; +mod fns; +mod macros; +mod paths; +mod structs; +mod traits; +mod vars; + +fn caps(base: ClientCapabilities) -> ClientCapabilities { + ClientCapabilities { + text_document: base.text_document.or_else(Default::default).map(|it| { + TextDocumentClientCapabilities { + definition: Some(GotoCapability { + dynamic_registration: Some(false), + link_support: None, + }), + ..it + } + }), + ..base + } +} + +fn goto_definition(cairo_code: &str) -> String { + let (cairo, cursors) = cursors(cairo_code); + + let mut ls = sandbox! { + files { + "cairo_project.toml" => CAIRO_PROJECT_TOML_2024_07, + "src/lib.cairo" => cairo.clone(), + } + client_capabilities = caps; + }; + + ls.open_all_cairo_files_and_wait_for_project_update(); + + assert_eq!(cursors.carets().len(), 1); + let position = cursors.carets()[0]; + + let code_action_params = GotoDefinitionParams { + text_document_position_params: TextDocumentPositionParams { + text_document: ls.doc_id("src/lib.cairo"), + position, + }, + work_done_progress_params: Default::default(), + partial_result_params: Default::default(), + }; + let response = ls.send_request::(code_action_params); + + let ranges = match response { + Some(GotoDefinitionResponse::Scalar(location)) => { + vec![location.range] + } + Some(GotoDefinitionResponse::Array(locations)) => { + locations.into_iter().map(|location| location.range).collect() + } + Some(GotoDefinitionResponse::Link(_)) => { + panic!("unexpected GotoDefinitionResponse::Link"); + } + None => { + return "none response".into(); + } + }; + + render_selections(&cairo, &ranges) +} diff --git a/tests/e2e/goto_definition/paths.rs b/tests/e2e/goto_definition/paths.rs new file mode 100644 index 00000000..9e8fcf0b --- /dev/null +++ b/tests/e2e/goto_definition/paths.rs @@ -0,0 +1,158 @@ +use crate::goto_definition::goto_definition; +use crate::support::insta::test_transform; + +#[test] +fn module_in_path1() { + test_transform!(goto_definition, r" + fn main() { + module::bar::foo(); + } + mod module { // good + mod module {} // bad + fn foo() {} + } + ", @r" + fn main() { + module::bar::foo(); + } + mod module { // good + mod module {} // bad + fn foo() {} + } + ") +} + +#[test] +fn module_in_path2() { + test_transform!(goto_definition, r" + fn main() { + module::bar::foo(); + } + mod module { + mod bar { // good + fn foo() {} + } + } + ", @r" + fn main() { + module::bar::foo(); + } + mod module { + mod bar { // good + fn foo() {} + } + } + ") +} + +#[test] +fn fn_in_submodule() { + test_transform!(goto_definition, r" + fn main() { + module::foo(); + } + fn foo() {} // bad + mod module { + fn foo() {} // good + } + ", @r" + fn main() { + module::foo(); + } + fn foo() {} // bad + mod module { + fn foo() {} // good + } + ") +} + +#[test] +fn crate_in_use() { + test_transform!(goto_definition, r" + use crate::foo::func; + mod foo { + pub fn func() {} + } + ", @r" + use crate::foo::func; + mod foo { + pub fn func() {} + } + ") +} + +#[test] +fn crate_in_use_in_submodule() { + test_transform!(goto_definition, r" + mod bar { + use crate::foo::func; + } + mod foo { + pub fn func() {} + } + ", @r" + mod bar { + use crate::foo::func; + } + mod foo { + pub fn func() {} + } + ") +} + +#[test] +fn crate_in_path_in_expr() { + test_transform!(goto_definition, r" + fn main() { + let _ = crate::foo::func(); + } + mod foo { + pub fn func() {} + } + ", @r" + fn main() { + let _ = crate::foo::func(); + } + mod foo { + pub fn func() {} + } + ") +} + +#[test] +fn use_item_via_crate() { + test_transform!(goto_definition, r" + pub trait Foo { + fn foo(self: T); + } + mod module { + use crate::Foo; + } + ", @r" + pub trait Foo { + fn foo(self: T); + } + mod module { + use crate::Foo; + } + ") +} + +#[test] +fn use_item_via_super() { + test_transform!(goto_definition, r" + pub trait Foo { + fn foo(self: T); + } + mod module { + use super::Foo; + } + ", @r" + pub trait Foo { + fn foo(self: T); + } + mod module { + use super::Foo; + } + ") +} diff --git a/tests/e2e/goto_definition/structs.rs b/tests/e2e/goto_definition/structs.rs new file mode 100644 index 00000000..ed4484cf --- /dev/null +++ b/tests/e2e/goto_definition/structs.rs @@ -0,0 +1,62 @@ +use crate::goto_definition::goto_definition; +use crate::support::insta::test_transform; + +#[test] +fn struct_item_in_constructor() { + test_transform!(goto_definition, r" + struct Foo { } + fn main() { + let foo = Foo {}; + } + ", @r" + struct Foo { } + fn main() { + let foo = Foo {}; + } + ") +} + +#[test] +fn struct_item_in_type() { + test_transform!(goto_definition, r" + struct Foo { } + fn calc(foo: Foo) {} + ", @r" + struct Foo { } + fn calc(foo: Foo) {} + ") +} + +#[test] +fn struct_member_via_field_access() { + test_transform!(goto_definition, r" + #[derive(Drop)] + struct Circle { radius: u64 } + fn foo(circle: Circle) -> u64 { + circle.radius + } + ", @r" + #[derive(Drop)] + struct Circle { radius: u64 } + fn foo(circle: Circle) -> u64 { + circle.radius + } + ") +} + +#[test] +fn struct_member_in_constructor() { + test_transform!(goto_definition, r" + #[derive(Drop)] + struct Circle { radius: u64 } + fn main() { + let circle = Circle { radius: 42 }; + } + ", @r" + #[derive(Drop)] + struct Circle { radius: u64 } + fn main() { + let circle = Circle { radius: 42 }; + } + ") +} diff --git a/tests/e2e/goto_definition/traits.rs b/tests/e2e/goto_definition/traits.rs new file mode 100644 index 00000000..6093411f --- /dev/null +++ b/tests/e2e/goto_definition/traits.rs @@ -0,0 +1,137 @@ +use crate::goto_definition::goto_definition; +use crate::support::insta::test_transform; + +#[test] +fn trait_name_in_impl() { + test_transform!(goto_definition, r" + pub trait Foo { + fn foo(self: T); + } + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + ", @r" + pub trait Foo { + fn foo(self: T); + } + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + ") +} + +#[test] +fn full_path_trait_name_in_expr() { + test_transform!(goto_definition, r" + pub trait Foo { + fn foo(self: T); + } + #[derive(Copy, Drop)] + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + fn main() { + let bar = Bar {}; + Foo::foo(bar); + } + ", @r" + pub trait Foo { + fn foo(self: T); + } + #[derive(Copy, Drop)] + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + fn main() { + let bar = Bar {}; + Foo::foo(bar); + } + ") +} + +#[test] +fn method_in_impl() { + test_transform!(goto_definition, r" + pub trait Foo { + fn foo(self: T); + } + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + ", @r" + pub trait Foo { + fn foo(self: T); + } + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + ") +} + +#[test] +fn dot_method_in_expr() { + test_transform!(goto_definition, r" + pub trait Foo { + fn foo(self: T); + } + #[derive(Copy, Drop)] + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + fn main() { + let bar = Bar {}; + bar.foo(); + } + ", @r" + pub trait Foo { + fn foo(self: T); + } + #[derive(Copy, Drop)] + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + fn main() { + let bar = Bar {}; + bar.foo(); + } + ") +} + +#[test] +fn full_path_method_in_expr() { + test_transform!(goto_definition, r" + pub trait Foo { + fn foo(self: T); + } + #[derive(Copy, Drop)] + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + fn main() { + let bar = Bar {}; + Foo::foo(bar); + } + ", @r" + pub trait Foo { + fn foo(self: T); + } + #[derive(Copy, Drop)] + pub struct Bar {} + impl FooBar of Foo { + fn foo(self: Bar) {} + } + fn main() { + let bar = Bar {}; + Foo::foo(bar); + } + ") +} diff --git a/tests/e2e/goto_definition/vars.rs b/tests/e2e/goto_definition/vars.rs new file mode 100644 index 00000000..b9bba7fa --- /dev/null +++ b/tests/e2e/goto_definition/vars.rs @@ -0,0 +1,53 @@ +use crate::goto_definition::goto_definition; +use crate::support::insta::test_transform; + +#[test] +fn var_in_expr() { + test_transform!(goto_definition, r" + fn main() { + let abc: felt252 = 0; // good + let _ = abc * 2; + } + fn foo() { + let abc: felt252 = 1; // bad + } + ", @r" + fn main() { + let abc: felt252 = 0; // good + let _ = abc * 2; + } + fn foo() { + let abc: felt252 = 1; // bad + } + ") +} + +#[test] +fn fn_param_in_expr() { + test_transform!(goto_definition, r" + fn main(abc: felt252, def: felt252) { // good + let _ = abc * 2; + } + fn foo(abc: felt252) {} // bad + ", @r" + fn main(abc: felt252, def: felt252) { // good + let _ = abc * 2; + } + fn foo(abc: felt252) {} // bad + ") +} + +// FIXME(#120): This used to work before goto definition refactoring. +#[test] +fn closure_param_in_expr() { + test_transform!(goto_definition, r" + fn foo(a: felt252) -> felt252 { + let abc: felt252 = 0; // bad + let c = |abc| { // good + abc + 3 + }; + } + + fn foo(abc: felt252) {} // bad + ", @"none response") +} diff --git a/tests/e2e/support/cursor.rs b/tests/e2e/support/cursor.rs index d817dddb..3359f22f 100644 --- a/tests/e2e/support/cursor.rs +++ b/tests/e2e/support/cursor.rs @@ -122,6 +122,26 @@ pub fn peek_selection(text: &str, range: &Range) -> String { + "\n" } +/// Adds selection markers for all ranges to the source text. +pub fn render_selections(text: &str, ranges: &[Range]) -> String { + let mut text = text.to_owned(); + ranges + .iter() + .flat_map(|range| { + assert!(range.start <= range.end); + [ + (index_in_text(&text, range.start), ""), + (index_in_text(&text, range.end), ""), + ] + }) + .sorted_by_key(|(idx, _)| *idx) + .fold(0, |offset, (idx, marker)| { + text.insert_str(idx + offset, marker); + offset + marker.len() + }); + text +} + /// Converts a [`Position`] to a char-bounded index in the text. /// /// This function assumes UTF-8 position encoding. diff --git a/tests/e2e/support/insta/mod.rs b/tests/e2e/support/insta/mod.rs new file mode 100644 index 00000000..d0108bf4 --- /dev/null +++ b/tests/e2e/support/insta/mod.rs @@ -0,0 +1,16 @@ +macro_rules! test_transform { + ($transform:expr, $before:literal, @$after:literal) => {{ + let before = ::indoc::indoc!($before); + let description = ::std::format!( + "// transform: {transform}\n{before}", + transform = stringify!($transform), + before = before.trim_end(), + ); + let after = $transform(before); + ::insta::with_settings!({ description => description }, { + ::insta::assert_snapshot!(after, @$after); + }); + }}; +} + +pub(crate) use test_transform; diff --git a/tests/e2e/support/mod.rs b/tests/e2e/support/mod.rs index 3ba83acb..60b83c9f 100644 --- a/tests/e2e/support/mod.rs +++ b/tests/e2e/support/mod.rs @@ -2,6 +2,7 @@ pub mod cairo_project_toml; pub mod client_capabilities; pub mod cursor; pub mod fixture; +pub mod insta; pub mod jsonrpc; mod mock_client; pub mod normalize; diff --git a/tests/test_data/goto/enum_variants.txt b/tests/test_data/goto/enum_variants.txt deleted file mode 100644 index 2a79f294..00000000 --- a/tests/test_data/goto/enum_variants.txt +++ /dev/null @@ -1,28 +0,0 @@ -//! > Test goto definition of an enum variant. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -enum Foo { - Bar, - Baz, -} - -fn main() { - let foo = Foo::Bar; - match foo { - Foo::Bar => {} - _ => {} - } -} - -//! > Goto definition #0 - let foo = Foo::Bar; ---- - Bar, - -//! > Goto definition #1 - Foo::Bar => {} ---- - Bar, diff --git a/tests/test_data/goto/inline_macros.txt b/tests/test_data/goto/inline_macros.txt deleted file mode 100644 index 2a266c4e..00000000 --- a/tests/test_data/goto/inline_macros.txt +++ /dev/null @@ -1,13 +0,0 @@ -//! > Test goto definition on inline macro. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -fn main() { - print!("Hello, world!"); -} - -//! > Goto definition #0 - print!("Hello, world!"); -None diff --git a/tests/test_data/goto/items.txt b/tests/test_data/goto/items.txt deleted file mode 100644 index 1e410026..00000000 --- a/tests/test_data/goto/items.txt +++ /dev/null @@ -1,185 +0,0 @@ -//! > Test goto definition of a function. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -fn main() { - foo(); -} - -fn foo() {} // good - -mod bar { - fn foo() {} // bad -} - -//! > Goto definition #0 - foo(); ---- -fn foo() {} // good - -//! > ========================================================================== - -//! > Test goto definition of a struct. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -struct Foo { - field: felt252, -} - -fn main() { - let foo = Foo { field: 0 }; -} - -fn calc(foo: Foo) {} - -//! > Goto definition #0 - let foo = Foo { field: 0 }; ---- -struct Foo { - field: felt252, -} - -//! > Goto definition #1 -fn calc(foo: Foo) {} ---- -struct Foo { - field: felt252, -} - -//! > ========================================================================== - -//! > Test goto definition of an enum. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -enum Foo { - Bar, - Baz, -} - -fn main() { - let foo = Foo::Bar; -} - -fn calc(foo: Foo) {} - -//! > Goto definition #0 - let foo = Foo::Bar; ---- -enum Foo { - Bar, - Baz, -} - -//! > Goto definition #1 -fn calc(foo: Foo) {} ---- -enum Foo { - Bar, - Baz, -} - -//! > ========================================================================== - -//! > Test goto definition with traits. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -pub trait ShapeGeometry { - fn boundary(self: T) -> u64; - fn area(self: T) -> u64; -} - -mod rectangle { - use super::ShapeGeometry; - - #[derive(Copy, Drop)] - pub struct Rectangle { - pub height: u64, - pub width: u64, - } - - impl RectangleGeometry of ShapeGeometryngle> { - fn boundary(self: Rectangle) -> u64 { - 2 * (self.height + self.width) - } - fn area(self: Rectangle) -> u64 { - self.height * self.width - } - } -} - -use rectangle::Rectangle; - -fn main() { - let rect = Rectangle { height: 5, width: 7 }; - let area = ShapeGeometry::area(rect); -} - -//! > Goto definition #0 - use super::ShapeGeometry; ---- -pub trait ShapeGeometry { - fn boundary(self: T) -> u64; - fn area(self: T) -> u64; -} - -//! > Goto definition #1 - impl RectangleGeometry of ShapeGeometry { ---- -pub trait ShapeGeometry { - fn boundary(self: T) -> u64; - fn area(self: T) -> u64; -} - -//! > Goto definition #2 - impl RectangleGeometry of ShapeGeometryngle> { ---- - #[derive(Copy, Drop)] - pub struct Rectangle { - pub height: u64, - pub width: u64, - } - -//! > Goto definition #3 - fn boundary(self: Rectangle) -> u64 { ---- - impl RectangleGeometry of ShapeGeometry { - fn boundary(self: Rectangle) -> u64 { - 2 * (self.height + self.width) - } - fn area(self: Rectangle) -> u64 { - self.height * self.width - } - } - -//! > Goto definition #4 - fn boundary(self: Rectangle) -> u64 { ---- - #[derive(Copy, Drop)] - pub struct Rectangle { - pub height: u64, - pub width: u64, - } - -//! > Goto definition #5 - let area = ShapeGeometry::area(rect); ---- -pub trait ShapeGeometry { - fn boundary(self: T) -> u64; - fn area(self: T) -> u64; -} - -//! > Goto definition #6 - let area = ShapeGeometry::area(rect); ---- - fn area(self: T) -> u64; diff --git a/tests/test_data/goto/kw_crate.txt b/tests/test_data/goto/kw_crate.txt deleted file mode 100644 index 4c80dddb..00000000 --- a/tests/test_data/goto/kw_crate.txt +++ /dev/null @@ -1,47 +0,0 @@ -//! > Test goto definition on `crate` keyword in `use` statement. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -use crate::foo::func; - -mod foo { - pub fn func() {} -} - -//! > Goto definition #0 -use crate::foo::func; ---- -use crate::foo::func; - -mod foo { - pub fn func() {} -} - -//! > ========================================================================== - -//! > Test goto definition on `crate` keyword used in expressions. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -fn main() { - let _ = crate::foo::func(); -} - -mod foo { - pub fn func() {} -} - -//! > Goto definition #0 - let _ = crate::foo::func(); ---- -fn main() { - let _ = crate::foo::func(); -} - -mod foo { - pub fn func() {} -} diff --git a/tests/test_data/goto/modules.txt b/tests/test_data/goto/modules.txt deleted file mode 100644 index b369aa9e..00000000 --- a/tests/test_data/goto/modules.txt +++ /dev/null @@ -1,53 +0,0 @@ -//! > Test goto definition of a module. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -fn main() { - module::bar::foo(); -} - -mod module { - // good - mod module { - // bad - } - - mod bar { // good - fn foo() {} - } -} - -//! > Goto definition #0 - module::bar::foo(); ---- -mod module { - -//! > Goto definition #1 - module::bar::foo(); ---- - mod bar { // good - -//! > ========================================================================== - -//! > Test goto definition of a function in a submodule. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -fn main() { - module::foo(); -} - -fn foo() {} // bad - -mod module { - fn foo() {} // good -} - -//! > Goto definition #0 - module::foo(); ---- - fn foo() {} // good diff --git a/tests/test_data/goto/struct_members.txt b/tests/test_data/goto/struct_members.txt deleted file mode 100644 index 48fda962..00000000 --- a/tests/test_data/goto/struct_members.txt +++ /dev/null @@ -1,63 +0,0 @@ -//! > Test goto definition on struct member access. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -#[derive(Drop)] -struct Rectangle { - width: u64, - height: u64, -} - -fn calculate_area(rectangle: Rectangle) -> u64 { - rectangle.width * rectangle.height -} - -//! > Goto definition #0 - rectangle.width * rectangle.height ---- -fn calculate_area(rectangle: Rectangle) -> u64 { - -//! > Goto definition #1 - rectangle.width * rectangle.height ---- - width: u64, - -//! > Goto definition #2 - rectangle.width * rectangle.height ---- -fn calculate_area(rectangle: Rectangle) -> u64 { - -//! > Goto definition #3 - rectangle.width * rectangle.height ---- - height: u64, - -//! > ========================================================================== - -//! > Test goto definition on struct member constructor. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -#[derive(Drop)] -struct Rectangle { - width: u64, - height: u64, -} - -fn main() { - let rectangle = Rectangle { width: 0, height: 0 }; -} - -//! > Goto definition #0 - let rectangle = Rectangle { width: 0, height: 0 }; ---- - width: u64, - -//! > Goto definition #1 - let rectangle = Rectangle { width: 0, height: 0 }; ---- - height: u64, diff --git a/tests/test_data/goto/variables.txt b/tests/test_data/goto/variables.txt deleted file mode 100644 index 4acddb78..00000000 --- a/tests/test_data/goto/variables.txt +++ /dev/null @@ -1,60 +0,0 @@ -//! > Test goto definition on variables. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -fn main() { - let abc: felt252 = 0; // good - let _ = abc * 2; -} - -fn foo() { - let abc: felt252 = 1; // bad -} - -//! > Goto definition #0 - let _ = abc * 2; ---- - let abc: felt252 = 0; // good - -//! > ========================================================================== - -//! > Test goto definition on function parameters. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -fn main(abc: felt252, def: felt252) { - let _ = abc * 2; -} - -fn foo(abc: felt252) {} - -//! > Goto definition #0 - let _ = abc * 2; ---- -fn main(abc: felt252, def: felt252) { - -//! > ========================================================================== - -//! > Test goto definition on closure parameters. - -//! > test_runner_name -test_goto_definition - -//! > cairo_code -// FIXME(#120): This used to work before goto definition refactoring. -fn foo(a: felt252) -> felt252 { - let abc: felt252 = 0; // bad - let c = |abc| { // good - abc + 3 - }; -} - -fn foo(abc: felt252) {} - -//! > Goto definition #0 - abc + 3 -None