From 4cbd5fdcd91cf9fba76cc6041c9d2bb2a56fd5fa Mon Sep 17 00:00:00 2001 From: Marek Kaput Date: Mon, 13 Jan 2025 13:52:40 +0100 Subject: [PATCH] Add more tests for textDocument/references commit-id:f830ed8f --- tests/e2e/references.rs | 7 ++ tests/test_data/references/enum_variants.txt | 48 +++++++++ tests/test_data/references/enums.txt | 86 +++++++++++++++ tests/test_data/references/fns.txt | 56 ++++++++++ tests/test_data/references/methods.txt | 67 ++++++++++++ tests/test_data/references/struct_members.txt | 63 +++++++++++ tests/test_data/references/structs.txt | 80 ++++++++++++++ tests/test_data/references/traits.txt | 60 +++++++++++ tests/test_data/references/variables.txt | 100 ++++++++++++++++++ 9 files changed, 567 insertions(+) create mode 100644 tests/test_data/references/enum_variants.txt create mode 100644 tests/test_data/references/enums.txt create mode 100644 tests/test_data/references/methods.txt create mode 100644 tests/test_data/references/struct_members.txt create mode 100644 tests/test_data/references/structs.txt create mode 100644 tests/test_data/references/traits.txt create mode 100644 tests/test_data/references/variables.txt diff --git a/tests/e2e/references.rs b/tests/e2e/references.rs index 25dc4055..b07e8944 100644 --- a/tests/e2e/references.rs +++ b/tests/e2e/references.rs @@ -13,8 +13,15 @@ cairo_lang_test_utils::test_file_test!( references, "tests/test_data/references", { + enum_variants: "enum_variants.txt", + enums: "enums.txt", fns: "fns.txt", inline_macros: "inline_macros.txt", + methods: "methods.txt", + struct_members: "struct_members.txt", + structs: "structs.txt", + traits: "traits.txt", + variables: "variables.txt", }, test_references ); diff --git a/tests/test_data/references/enum_variants.txt b/tests/test_data/references/enum_variants.txt new file mode 100644 index 00000000..6748be61 --- /dev/null +++ b/tests/test_data/references/enum_variants.txt @@ -0,0 +1,48 @@ +//! > Test references of enum variants. + +//! > test_runner_name +test_references(include_declaration: false) + +//! > cairo_code +enum Foo { + Bar, + Baz, +} + +fn main() { + let foo = Foo::Bar; + match foo { + Foo::Bar => {} // FIXME(#164): This should work. + _ => {} + } +} + +//! > References #0 + let foo = Foo::Bar; +--- + +//! > ========================================================================== + +//! > Test references of enum variants including declaration. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +enum Foo { + Bar, + Baz, +} + +fn main() { + let foo = Foo::Bar; + match foo { + Foo::Bar => {} // FIXME(#164): This should work. + _ => {} + } +} + +//! > References #0 + let foo = Foo::Bar; +--- + Bar, diff --git a/tests/test_data/references/enums.txt b/tests/test_data/references/enums.txt new file mode 100644 index 00000000..d06fb56c --- /dev/null +++ b/tests/test_data/references/enums.txt @@ -0,0 +1,86 @@ +//! > Test find references of an enum. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +enum Foo { + Bar, + Baz, +} + +fn main() { + let foo = Foo::Bar; + let foobar: Foo = foo; +} + +fn calc(foo: Foo) {} + +mod rectangle { + use super::Foo; +} + +//! > References #0 +enum Foo { +--- +enum Foo { + Bar, + Baz, +} +enum Foo { + let foo = Foo::Bar; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; + +//! > References #1 + let foo = Foo::Bar; +--- +enum Foo { + Bar, + Baz, +} +enum Foo { + let foo = Foo::Bar; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; + +//! > References #2 + let foobar: Foo = foo; +--- +enum Foo { + Bar, + Baz, +} +enum Foo { + let foo = Foo::Bar; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; + +//! > References #3 +fn calc(foo: Foo) {} +--- +enum Foo { + Bar, + Baz, +} +enum Foo { + let foo = Foo::Bar; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; + +//! > References #4 + use super::Foo; +--- +enum Foo { + Bar, + Baz, +} +enum Foo { + let foo = Foo::Bar; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; diff --git a/tests/test_data/references/fns.txt b/tests/test_data/references/fns.txt index ee63ba49..c1b7eed8 100644 --- a/tests/test_data/references/fns.txt +++ b/tests/test_data/references/fns.txt @@ -96,3 +96,59 @@ fn pow2(x: felt252) -> felt252 { x * x } fn pow2(x: felt252) -> felt252 { x * x } let x = pow2(2) + pow2(3); let x = pow2(2) + pow2(3); + +//! > ========================================================================== + +//! > Test references of function parameter. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +fn pow(num: felt252) -> felt252 { + num * num +} + +//! > References #0 +fn pow(num: felt252) -> felt252 { +--- +fn pow(num: felt252) -> felt252 { + num * num +} +fn pow(num: felt252) -> felt252 { + +//! > References #1 + num * num +--- +fn pow(num: felt252) -> felt252 { + num * num + num * num + +//! > ========================================================================== + +//! > Test references of function parameter captured by a closure. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +fn pow(num: felt252) -> felt252 { + let f = |x| num * x; + num * f(num) +} + +//! > References #0 + let f = |x| num * x; +--- +fn pow(num: felt252) -> felt252 { + let f = |x| num * x; + num * f(num) + num * f(num) + +//! > References #1 + num * f(num) +--- +fn pow(num: felt252) -> felt252 { + let f = |x| num * x; + num * f(num) + num * f(num) diff --git a/tests/test_data/references/methods.txt b/tests/test_data/references/methods.txt new file mode 100644 index 00000000..00adee60 --- /dev/null +++ b/tests/test_data/references/methods.txt @@ -0,0 +1,67 @@ +//! > Test references of methods. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +#[derive(Drop)] +struct Foo {} + +trait FooTrait { + fn area(self: @Foo) -> u64; +} + +impl FooImpl of FooTrait { + // FIXME(#170): Does not work as expected. + fn area(self: @Foo) -> u64 { 0 } +} + +#[derive(Drop)] +struct Bar {} + +trait BarTrait { + fn area(self: @Bar) -> u64; +} + +impl BarImpl of BarTrait { + fn area(self: @Bar) -> u64 { 0 } +} + +fn main() { + let foo = Foo {}; + let x = foo.area(); + let y = FooTrait::area(foo); +} + +//! > References #0 + fn area(self: @Foo) -> u64; +--- + fn area(self: @Foo) -> u64; + fn area(self: @Foo) -> u64; + let x = foo.area(); + let y = FooTrait::area(foo); + +//! > References #1 + fn area(self: @Foo) -> u64 { 0 } +--- +impl FooImpl of FooTrait { + // FIXME(#170): Does not work as expected. + fn area(self: @Foo) -> u64 { 0 } +} +impl FooImpl of FooTrait { + +//! > References #2 + let x = foo.area(); +--- + fn area(self: @Foo) -> u64; + fn area(self: @Foo) -> u64; + let x = foo.area(); + let y = FooTrait::area(foo); + +//! > References #3 + let y = FooTrait::area(foo); +--- + fn area(self: @Foo) -> u64; + fn area(self: @Foo) -> u64; + let x = foo.area(); + let y = FooTrait::area(foo); diff --git a/tests/test_data/references/struct_members.txt b/tests/test_data/references/struct_members.txt new file mode 100644 index 00000000..e90844c9 --- /dev/null +++ b/tests/test_data/references/struct_members.txt @@ -0,0 +1,63 @@ +//! > Test references of struct members. + +//! > test_runner_name +test_references(include_declaration: false) + +//! > cairo_code +#[derive(Drop)] +struct Rectangle { + // FIXME(#129): The results for this are very off. + width: u64, + height: u64, +} + +fn main() { + let rectangle = Rectangle { width: 0, height: 0 }; +} + +fn calculate_area(rectangle: Rectangle) -> u64 { + rectangle.width * rectangle.height +} + +//! > References #0 + width: u64, +--- +struct Rectangle { + let rectangle = Rectangle { width: 0, height: 0 }; +fn calculate_area(rectangle: Rectangle) -> u64 { + +//! > References #1 + let rectangle = Rectangle { width: 0, height: 0 }; +--- + let rectangle = Rectangle { width: 0, height: 0 }; + rectangle.width * rectangle.height + +//! > References #2 + rectangle.width * rectangle.height +--- + let rectangle = Rectangle { width: 0, height: 0 }; + rectangle.width * rectangle.height + +//! > ========================================================================== + +//! > Test references of struct members including declaration. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +#[derive(Drop)] +struct Rectangle { + width: u64, + height: u64, +} + +fn calculate_area(rectangle: Rectangle) -> u64 { + rectangle.width * rectangle.height +} + +//! > References #0 + rectangle.width * rectangle.height +--- + width: u64, + rectangle.width * rectangle.height diff --git a/tests/test_data/references/structs.txt b/tests/test_data/references/structs.txt new file mode 100644 index 00000000..bb9142cc --- /dev/null +++ b/tests/test_data/references/structs.txt @@ -0,0 +1,80 @@ +//! > Test find references of a struct. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +struct Foo { + field: felt252, +} + +fn main() { + let foo = Foo { field: 0 }; + let foobar: Foo = foo; +} + +fn calc(foo: Foo) {} + +mod rectangle { + use super::Foo; +} + +//! > References #0 +struct Foo { +--- +struct Foo { + field: felt252, +} +struct Foo { + let foo = Foo { field: 0 }; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; + +//! > References #1 + let foo = Foo { field: 0 }; +--- +struct Foo { + field: felt252, +} +struct Foo { + let foo = Foo { field: 0 }; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; + +//! > References #2 + let foobar: Foo = foo; +--- +struct Foo { + field: felt252, +} +struct Foo { + let foo = Foo { field: 0 }; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; + +//! > References #3 +fn calc(foo: Foo) {} +--- +struct Foo { + field: felt252, +} +struct Foo { + let foo = Foo { field: 0 }; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; + +//! > References #4 + use super::Foo; +--- +struct Foo { + field: felt252, +} +struct Foo { + let foo = Foo { field: 0 }; + let foobar: Foo = foo; +fn calc(foo: Foo) {} + use super::Foo; diff --git a/tests/test_data/references/traits.txt b/tests/test_data/references/traits.txt new file mode 100644 index 00000000..f6dd4fac --- /dev/null +++ b/tests/test_data/references/traits.txt @@ -0,0 +1,60 @@ +//! > Test find references of a trait. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +pub trait ShapeGeometry { + fn area(self: T) -> u64; +} + +mod rectangle { + use super::ShapeGeometry; + + #[derive(Copy, Drop)] + pub struct Rectangle {} + + impl RectangleGeometry of ShapeGeometry { + fn area(self: Rectangle) -> u64 { 0 } + } +} + +use rectangle::Rectangle; + +fn main() { + let rect = Rectangle {}; + let area = ShapeGeometry::area(rect); +} + +//! > References #0 + use super::ShapeGeometry; +--- +pub trait ShapeGeometry { + fn area(self: T) -> u64; +} +pub trait ShapeGeometry { + use super::ShapeGeometry; + impl RectangleGeometry of ShapeGeometry { + let area = ShapeGeometry::area(rect); + +//! > References #1 + impl RectangleGeometry of ShapeGeometry { +--- +pub trait ShapeGeometry { + fn area(self: T) -> u64; +} +pub trait ShapeGeometry { + use super::ShapeGeometry; + impl RectangleGeometry of ShapeGeometry { + let area = ShapeGeometry::area(rect); + +//! > References #2 + let area = ShapeGeometry::area(rect); +--- +pub trait ShapeGeometry { + fn area(self: T) -> u64; +} +pub trait ShapeGeometry { + use super::ShapeGeometry; + impl RectangleGeometry of ShapeGeometry { + let area = ShapeGeometry::area(rect); diff --git a/tests/test_data/references/variables.txt b/tests/test_data/references/variables.txt new file mode 100644 index 00000000..9bfef171 --- /dev/null +++ b/tests/test_data/references/variables.txt @@ -0,0 +1,100 @@ +//! > Test references of variables. + +//! > test_runner_name +test_references(include_declaration: false) + +//! > cairo_code +fn main() { + let foobar = 1233; // bad + let x = foobar + 1; // good + bar(); + let y = x + foobar * foobar; // good +} + +fn bar() { + let foobar = 42; // bad +} + +//! > References #0 + let foobar = 1233; // bad +none response + +//! > References #1 + let x = foobar + 1; // good +--- + let x = foobar + 1; // good + let y = x + foobar * foobar; // good + let y = x + foobar * foobar; // good + +//! > ========================================================================== + +//! > Test references of variables including declaration. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +fn main() { + // FIXME(#164): This should work for the caret on declaration. + let foobar = 1233; // good + let x = foobar + foobar; // good +} + +//! > References #0 + let foobar = 1233; // good +none response + +//! > References #1 + let x = foobar + foobar; // good +--- + let foobar = 1233; // good + let x = foobar + foobar; // good + let x = foobar + foobar; // good + +//! > ========================================================================== + +//! > Test references of variable declared via a complex pattern. + +//! > test_runner_name +test_references(include_declaration: true) + +//! > cairo_code +fn main() { + let (foobar, foobar2) = (1, 2); // good + let x = foobar + foobar2; // good + let y = foobar2 * foobar2; // bad + let z = foobar2 + foobar; // good +} + +//! > References #0 + let x = foobar + foobar2; // good +--- + let (foobar, foobar2) = (1, 2); // good + let x = foobar + foobar2; // good + let z = foobar2 + foobar; // good + +//! > ========================================================================== + +//! > Test references of variable captured by a closure; + +//! > test_runner_name +test_references(include_declaration: false) + +//! > cairo_code +fn main() { + let foobar = 1; + let x = foobar + 1; + let f = |y| x + y + foobar; +} + +//! > References #0 + let x = foobar + 1; +--- + let x = foobar + 1; + let f = |y| x + y + foobar; + +//! > References #1 + let f = |y| x + y + foobar; +--- + let x = foobar + 1; + let f = |y| x + y + foobar;