From 00c1c18ece73a5b503db8998eb81fed45a7e8b3d Mon Sep 17 00:00:00 2001 From: Marek Kaput Date: Tue, 18 Jun 2024 21:45:30 +0200 Subject: [PATCH] LS: Add more test cases for hover and make fixtures fixes more stable (#5820) --- .../tests/e2e/hover.rs | 6 +- .../tests/test_data/hover/basic.txt | 173 +++++++++++++++--- .../tests/test_data/hover/partial.txt | 12 +- .../tests/test_data/hover/starknet.txt | 22 +-- 4 files changed, 166 insertions(+), 47 deletions(-) diff --git a/crates/cairo-lang-language-server/tests/e2e/hover.rs b/crates/cairo-lang-language-server/tests/e2e/hover.rs index 5d97d7a846a..b719e5d9c69 100644 --- a/crates/cairo-lang-language-server/tests/e2e/hover.rs +++ b/crates/cairo-lang-language-server/tests/e2e/hover.rs @@ -44,7 +44,7 @@ fn test_hover( inputs: &OrderedHashMap, _args: &OrderedHashMap, ) -> TestRunnerResult { - let (cairo, cursors) = cursors(&inputs["src/lib.cairo"]); + let (cairo, cursors) = cursors(&inputs["cairo_code"]); let mut ls = sandbox! { files { @@ -58,8 +58,8 @@ fn test_hover( let mut hovers = OrderedHashMap::default(); - for position in cursors.carets() { - let hover_name = format!("hover {}:{}", position.line, position.character); + for (n, position) in cursors.carets().into_iter().enumerate() { + let hover_name = format!("hover #{n}"); let mut report = String::new(); diff --git a/crates/cairo-lang-language-server/tests/test_data/hover/basic.txt b/crates/cairo-lang-language-server/tests/test_data/hover/basic.txt index f83d8d2d45a..5cfbe16055f 100644 --- a/crates/cairo-lang-language-server/tests/test_data/hover/basic.txt +++ b/crates/cairo-lang-language-server/tests/test_data/hover/basic.txt @@ -10,13 +10,16 @@ hello = "src" [config.global] edition = "2023_11" -//! > src/lib.cairo +//! > cairo_code fn main() { let mut x = 5; println!("The value of x is: {}", x); x = add_two(x); front_of_house::hosting::add_to_waitlist(); + + let mut rect = Rectangle { width: 30, height: 50 }; + let area = rect.area(); } /// `add_two` documentation. @@ -34,12 +37,12 @@ struct Rectangle { /// Rectangle trait. trait RectangleTrait { /// Calculate the area of the rectangle. - fn area(self: @Rectangle) -> u64; + fn area(self: @Rectangle) -> u64; } /// Implementing the `RectangleTrait` for the `Rectangle` struct. impl RectangleImpl of RectangleTrait { - fn area(self: @Rectangle) -> u64 { + fn area(self: @Rectangle) -> u64 { (*self.width) * (*self.height) } } @@ -48,17 +51,17 @@ impl RectangleImpl of RectangleTrait { #[generate_trait] impl RectangleImpl2 of RectangleTrait2 { /// Calculate the area of the rectangle #2. - fn area(self: @Rectangle) -> u64 { + fn area2(self: @Rectangle) -> u64 { (*self.width) * (*self.height) } } -enum Coin { - Penny, +enum Coin { + Penny, } -fn value_in_cents(coin: Coin) -> felt252 { - match coin { +fn value_in_cents(coin: Coin) -> felt252 { + match coin { Coin::Penny => 1, } } @@ -72,7 +75,7 @@ pub mod front_of_house { } } -//! > hover 1:13 +//! > hover #0 // = source context let mut x = 5; // = highlight @@ -80,7 +83,7 @@ No highlight information. // = popover Type: `core::integer::u32` -//! > hover 2:5 +//! > hover #1 // = source context println!("The value of x is: {}", x); // = highlight @@ -90,7 +93,7 @@ No highlight information. () ``` -//! > hover 3:5 +//! > hover #2 // = source context x = add_two(x); // = highlight @@ -100,7 +103,7 @@ No highlight information. let mut x: core::integer::u32 ``` -//! > hover 3:8 +//! > hover #3 // = source context x = add_two(x); // = highlight @@ -115,7 +118,7 @@ fn add_two(x: u32) -> u32 --- `add_two` documentation. -//! > hover 3:9 +//! > hover #4 // = source context x = add_two(x); // = highlight @@ -130,7 +133,7 @@ fn add_two(x: u32) -> u32 --- `add_two` documentation. -//! > hover 3:15 +//! > hover #5 // = source context x = add_two(x); // = highlight @@ -145,7 +148,7 @@ fn add_two(x: u32) -> u32 --- `add_two` documentation. -//! > hover 5:9 +//! > hover #6 // = source context front_of_house::hosting::add_to_waitlist(); // = highlight @@ -155,7 +158,7 @@ No highlight information. fn add_to_waitlist() -> () ``` -//! > hover 5:22 +//! > hover #7 // = source context front_of_house::hosting::add_to_waitlist(); // = highlight @@ -165,7 +168,7 @@ No highlight information. fn add_to_waitlist() -> () ``` -//! > hover 5:32 +//! > hover #8 // = source context front_of_house::hosting::add_to_waitlist(); // = highlight @@ -180,14 +183,83 @@ pub fn add_to_waitlist() --- Add to waitlist function. -//! > hover 9:8 +//! > hover #9 +// = source context + let mut rect = Rectangle { width: 30, height: 50 }; +// = highlight +No highlight information. +// = popover +Type: `hello::Rectangle` + +//! > hover #10 +// = source context + let mut rect = Rectangle { width: 30, height: 50 }; +// = highlight + let mut rect = Rectangle { width: 30, height: 50 }; +// = popover +```cairo +hello +``` +```cairo +struct Rectangle { + /// Width of the rectangle. + width: u64, + /// Height of the rectangle. + height: u64, +} + +``` +--- +Rectangle struct. + +//! > hover #11 +// = source context + let mut rect = Rectangle { width: 30, height: 50 }; +// = highlight +No highlight information. +// = popover +```cairo +hello::Rectangle +``` + +//! > hover #12 +// = source context + let area = rect.area(); +// = highlight +No highlight information. +// = popover +Type: `core::integer::u64` + +//! > hover #13 +// = source context + let area = rect.area(); +// = highlight + let area = rect.area(); +// = popover +```cairo +hello::RectangleTrait +``` +```cairo +fn area(self: @Rectangle) -> u64; +``` +--- +Calculate the area of the rectangle. + +//! > hover #14 // = source context fn add_two(x: u32) -> u32 { x + 2 } // = highlight No highlight information. // = popover -//! > hover 23:22 +//! > hover #15 +// = source context + fn area(self: @Rectangle) -> u64; +// = highlight +No highlight information. +// = popover + +//! > hover #16 // = source context fn area(self: @Rectangle) -> u64; // = highlight @@ -208,7 +280,14 @@ struct Rectangle { --- Rectangle struct. -//! > hover 28:22 +//! > hover #17 +// = source context + fn area(self: @Rectangle) -> u64 { +// = highlight +No highlight information. +// = popover + +//! > hover #18 // = source context fn area(self: @Rectangle) -> u64 { // = highlight @@ -229,7 +308,7 @@ struct Rectangle { --- Rectangle struct. -//! > hover 29:17 +//! > hover #19 // = source context (*self.width) * (*self.height) // = highlight @@ -239,11 +318,11 @@ No highlight information. @core::integer::u64 ``` -//! > hover 37:22 +//! > hover #20 // = source context - fn area(self: @Rectangle) -> u64 { + fn area2(self: @Rectangle) -> u64 { // = highlight - fn area(self: @Rectangle) -> u64 { + fn area2(self: @Rectangle) -> u64 { // = popover ```cairo hello @@ -260,7 +339,7 @@ struct Rectangle { --- Rectangle struct. -//! > hover 38:17 +//! > hover #21 // = source context (*self.width) * (*self.height) // = highlight @@ -270,7 +349,37 @@ No highlight information. @core::integer::u64 ``` -//! > hover 46:25 +//! > hover #22 +// = source context +enum Coin { +// = highlight +No highlight information. +// = popover +No hover information. + +//! > hover #23 +// = source context + Penny, +// = highlight +No highlight information. +// = popover +No hover information. + +//! > hover #24 +// = source context +fn value_in_cents(coin: Coin) -> felt252 { +// = highlight +No highlight information. +// = popover + +//! > hover #25 +// = source context +fn value_in_cents(coin: Coin) -> felt252 { +// = highlight +No highlight information. +// = popover + +//! > hover #26 // = source context fn value_in_cents(coin: Coin) -> felt252 { // = highlight @@ -287,7 +396,17 @@ enum Coin { ``` -//! > hover 48:15 +//! > hover #27 +// = source context + match coin { +// = highlight + match coin { +// = popover +```cairo +coin: hello::Coin +``` + +//! > hover #28 // = source context Coin::Penny => 1, // = highlight diff --git a/crates/cairo-lang-language-server/tests/test_data/hover/partial.txt b/crates/cairo-lang-language-server/tests/test_data/hover/partial.txt index 334b28ab682..eab227148d2 100644 --- a/crates/cairo-lang-language-server/tests/test_data/hover/partial.txt +++ b/crates/cairo-lang-language-server/tests/test_data/hover/partial.txt @@ -10,7 +10,7 @@ hello = "src" [config.global] edition = "2023_11" -//! > src/lib.cairo +//! > cairo_code fn main() { let mut xyz = unknown_function(); let y = xyz * 2; @@ -20,7 +20,7 @@ fn f(abc) -> felt252 { 2 * abc } -//! > hover 1:14 +//! > hover #0 // = source context let mut xyz = unknown_function(); // = highlight @@ -28,7 +28,7 @@ No highlight information. // = popover Type: `` -//! > hover 1:22 +//! > hover #1 // = source context let mut xyz = unknown_function(); // = highlight @@ -38,7 +38,7 @@ No highlight information. ``` -//! > hover 2:14 +//! > hover #2 // = source context let y = xyz * 2; // = highlight @@ -48,14 +48,14 @@ No highlight information. let mut xyz: ``` -//! > hover 5:7 +//! > hover #3 // = source context fn f(abc) -> felt252 { // = highlight No highlight information. // = popover -//! > hover 6:10 +//! > hover #4 // = source context 2 * abc // = highlight diff --git a/crates/cairo-lang-language-server/tests/test_data/hover/starknet.txt b/crates/cairo-lang-language-server/tests/test_data/hover/starknet.txt index f6df61cc979..43f67e23e74 100644 --- a/crates/cairo-lang-language-server/tests/test_data/hover/starknet.txt +++ b/crates/cairo-lang-language-server/tests/test_data/hover/starknet.txt @@ -10,7 +10,7 @@ hello = "src" [config.global] edition = "2023_11" -//! > src/lib.cairo +//! > cairo_code use Balance::contract_state_for_testing; /// The balance contract interface. @@ -49,7 +49,7 @@ mod Balance { } } -//! > hover 0:18 +//! > hover #0 // = source context use Balance::contract_state_for_testing; // = highlight @@ -62,14 +62,14 @@ hello pub fn contract_state_for_testing() -> ContractState ``` -//! > hover 23:25 +//! > hover #1 // = source context fn constructor(ref self: ContractState, value_: u128) { // = highlight No highlight information. // = popover -//! > hover 23:33 +//! > hover #2 // = source context fn constructor(ref self: ContractState, value_: u128) { // = highlight @@ -87,7 +87,7 @@ hello ``` -//! > hover 24:15 +//! > hover #3 // = source context self.value.write(value_); // = highlight @@ -97,7 +97,7 @@ No highlight information. hello::Balance::__member_module_value::ContractMemberState ``` -//! > hover 24:25 +//! > hover #4 // = source context self.value.write(value_); // = highlight @@ -107,7 +107,7 @@ hello::Balance::__member_module_value::ContractMemberState value_: core::integer::u128 ``` -//! > hover 28:30 +//! > hover #5 // = source context impl Balance of super::IBalance { // = highlight @@ -122,7 +122,7 @@ hello --- The balance contract interface. -//! > hover 28:39 +//! > hover #6 // = source context impl Balance of super::IBalancetractState> { // = highlight @@ -140,7 +140,7 @@ hello ``` -//! > hover 30:24 +//! > hover #7 // = source context self.value.read() // = highlight @@ -153,7 +153,7 @@ core::starknet::storage::StorageMemberAccessTrait fn read(self: @TMemberState) -> Self::Value; ``` -//! > hover 33:25 +//! > hover #8 // = source context self.value.write( self.value.read() + a ); // = highlight @@ -166,7 +166,7 @@ core::starknet::storage::StorageMemberAccessTrait fn write(ref self: TMemberState, value: Self::Value); ``` -//! > hover 33:50 +//! > hover #9 // = source context self.value.write( self.value.read() + a ); // = highlight