Skip to content

Commit

Permalink
Add more tests for textDocument/references
Browse files Browse the repository at this point in the history
commit-id:f830ed8f
  • Loading branch information
mkaput committed Jan 16, 2025
1 parent 92ef9b0 commit 4cbd5fd
Show file tree
Hide file tree
Showing 9 changed files with 567 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/e2e/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
48 changes: 48 additions & 0 deletions tests/test_data/references/enum_variants.txt
Original file line number Diff line number Diff line change
@@ -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::Ba<caret>r;
match foo {
Foo::Bar => {} // FIXME(#164): This should work.
_ => {}
}
}

//! > References #0
let foo = Foo::Ba<caret>r;
---

//! > ==========================================================================

//! > 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::Ba<caret>r;
match foo {
Foo::Bar => {} // FIXME(#164): This should work.
_ => {}
}
}

//! > References #0
let foo = Foo::Ba<caret>r;
---
<sel>Bar</sel>,
86 changes: 86 additions & 0 deletions tests/test_data/references/enums.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! > Test find references of an enum.

//! > test_runner_name
test_references(include_declaration: true)

//! > cairo_code
enum Fo<caret>o {
Bar,
Baz,
}

fn main() {
let foo = Fo<caret>o::Bar;
let foobar: Fo<caret>o = foo;
}

fn calc(foo: Fo<caret>o) {}

mod rectangle {
use super::Fo<caret>o;
}

//! > References #0
enum Fo<caret>o {
---
<sel>enum Foo {
Bar,
Baz,
}</sel>
enum <sel>Foo</sel> {
let foo = <sel>Foo</sel>::Bar;
let foobar: <sel>Foo</sel> = foo;
fn calc(foo: <sel>Foo</sel>) {}
use super::<sel>Foo</sel>;

//! > References #1
let foo = Fo<caret>o::Bar;
---
<sel>enum Foo {
Bar,
Baz,
}</sel>
enum <sel>Foo</sel> {
let foo = <sel>Foo</sel>::Bar;
let foobar: <sel>Foo</sel> = foo;
fn calc(foo: <sel>Foo</sel>) {}
use super::<sel>Foo</sel>;

//! > References #2
let foobar: Fo<caret>o = foo;
---
<sel>enum Foo {
Bar,
Baz,
}</sel>
enum <sel>Foo</sel> {
let foo = <sel>Foo</sel>::Bar;
let foobar: <sel>Foo</sel> = foo;
fn calc(foo: <sel>Foo</sel>) {}
use super::<sel>Foo</sel>;

//! > References #3
fn calc(foo: Fo<caret>o) {}
---
<sel>enum Foo {
Bar,
Baz,
}</sel>
enum <sel>Foo</sel> {
let foo = <sel>Foo</sel>::Bar;
let foobar: <sel>Foo</sel> = foo;
fn calc(foo: <sel>Foo</sel>) {}
use super::<sel>Foo</sel>;

//! > References #4
use super::Fo<caret>o;
---
<sel>enum Foo {
Bar,
Baz,
}</sel>
enum <sel>Foo</sel> {
let foo = <sel>Foo</sel>::Bar;
let foobar: <sel>Foo</sel> = foo;
fn calc(foo: <sel>Foo</sel>) {}
use super::<sel>Foo</sel>;
56 changes: 56 additions & 0 deletions tests/test_data/references/fns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,59 @@ fn pow<caret>2(x: felt252) -> felt252 { x * x }
fn <sel>pow2</sel>(x: felt252) -> felt252 { x * x }
let x = <sel>pow2</sel>(2) + pow2(3);
let x = pow2(2) + <sel>pow2</sel>(3);

//! > ==========================================================================

//! > Test references of function parameter.

//! > test_runner_name
test_references(include_declaration: true)

//! > cairo_code
fn pow(nu<caret>m: felt252) -> felt252 {
nu<caret>m * num
}

//! > References #0
fn pow(nu<caret>m: felt252) -> felt252 {
---
<sel>fn pow(num: felt252) -> felt252 {
num * num
}</sel>
fn <sel>pow</sel>(num: felt252) -> felt252 {

//! > References #1
nu<caret>m * num
---
fn pow(<sel>num: felt252</sel>) -> felt252 {
<sel>num</sel> * num
num * <sel>num</sel>

//! > ==========================================================================

//! > 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| nu<caret>m * x;
nu<caret>m * f(num)
}

//! > References #0
let f = |x| nu<caret>m * x;
---
fn pow(<sel>num: felt252</sel>) -> felt252 {
let f = |x| <sel>num</sel> * x;
<sel>num</sel> * f(num)
num * f(<sel>num</sel>)

//! > References #1
nu<caret>m * f(num)
---
fn pow(<sel>num: felt252</sel>) -> felt252 {
let f = |x| <sel>num</sel> * x;
<sel>num</sel> * f(num)
num * f(<sel>num</sel>)
67 changes: 67 additions & 0 deletions tests/test_data/references/methods.txt
Original file line number Diff line number Diff line change
@@ -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 are<caret>a(self: @Foo) -> u64;
}

impl FooImpl of FooTrait {
// FIXME(#170): Does not work as expected.
fn are<caret>a(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.are<caret>a();
let y = FooTrait::are<caret>a(foo);
}

//! > References #0
fn are<caret>a(self: @Foo) -> u64;
---
<sel>fn area(self: @Foo) -> u64;</sel>
fn <sel>area</sel>(self: @Foo) -> u64;
let x = foo.<sel>area</sel>();
let y = FooTrait::<sel>area</sel>(foo);

//! > References #1
fn are<caret>a(self: @Foo) -> u64 { 0 }
---
<sel>impl FooImpl of FooTrait {
// FIXME(#170): Does not work as expected.
fn area(self: @Foo) -> u64 { 0 }
}</sel>
impl <sel>FooImpl</sel> of FooTrait {

//! > References #2
let x = foo.are<caret>a();
---
<sel>fn area(self: @Foo) -> u64;</sel>
fn <sel>area</sel>(self: @Foo) -> u64;
let x = foo.<sel>area</sel>();
let y = FooTrait::<sel>area</sel>(foo);

//! > References #3
let y = FooTrait::are<caret>a(foo);
---
<sel>fn area(self: @Foo) -> u64;</sel>
fn <sel>area</sel>(self: @Foo) -> u64;
let x = foo.<sel>area</sel>();
let y = FooTrait::<sel>area</sel>(foo);
63 changes: 63 additions & 0 deletions tests/test_data/references/struct_members.txt
Original file line number Diff line number Diff line change
@@ -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.
wid<caret>th: u64,
height: u64,
}

fn main() {
let rectangle = Rectangle { wid<caret>th: 0, height: 0 };
}

fn calculate_area(rectangle: Rectangle) -> u64 {
rectangle.wi<caret>dth * rectangle.height
}

//! > References #0
wid<caret>th: u64,
---
struct <sel>Rectangle</sel> {
let rectangle = <sel>Rectangle</sel> { width: 0, height: 0 };
fn calculate_area(rectangle: <sel>Rectangle</sel>) -> u64 {

//! > References #1
let rectangle = Rectangle { wid<caret>th: 0, height: 0 };
---
let rectangle = Rectangle { <sel>width</sel>: 0, height: 0 };
rectangle.<sel>width</sel> * rectangle.height

//! > References #2
rectangle.wi<caret>dth * rectangle.height
---
let rectangle = Rectangle { <sel>width</sel>: 0, height: 0 };
rectangle.<sel>width</sel> * 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.wi<caret>dth * rectangle.height
}

//! > References #0
rectangle.wi<caret>dth * rectangle.height
---
<sel>width: u64</sel>,
rectangle.<sel>width</sel> * rectangle.height
Loading

0 comments on commit 4cbd5fd

Please sign in to comment.