Skip to content

Commit c1073bc

Browse files
GearsDatapackslpil
authored andcommitted
Test edge-cases
1 parent 24b2ae2 commit c1073bc

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

compiler-core/src/language_server/tests/action.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10372,3 +10372,68 @@ pub fn main() {
1037210372
find_position_of("case").select_until(find_position_of("}\n").under_char('\n'))
1037310373
);
1037410374
}
10375+
10376+
#[test]
10377+
fn extract_function_which_uses_constant() {
10378+
assert_code_action!(
10379+
EXTRACT_FUNCTION,
10380+
"
10381+
const pi = 3.14
10382+
10383+
pub fn main() {
10384+
let radius = 4.5
10385+
10386+
let circumference = radius *. pi *. 2.0
10387+
10388+
echo circumference
10389+
}
10390+
",
10391+
find_position_of("radius *.").select_until(find_position_of("2.0\n").under_char('\n'))
10392+
);
10393+
}
10394+
10395+
#[test]
10396+
fn extract_function_which_uses_constant_in_guard() {
10397+
assert_code_action!(
10398+
EXTRACT_FUNCTION,
10399+
r#"
10400+
const pi = 3.14
10401+
10402+
pub fn main() {
10403+
let value = 3.15
10404+
10405+
let string = case value {
10406+
0.0 -> "Zero"
10407+
1.0 -> "One"
10408+
_ if value == pi -> "PI"
10409+
_ -> "Something else"
10410+
}
10411+
10412+
echo string
10413+
}
10414+
"#,
10415+
find_position_of("case").select_until(find_position_of("}\n").under_char('\n'))
10416+
);
10417+
}
10418+
10419+
#[test]
10420+
fn no_code_action_to_extract_function_when_expression_is_not_fully_selected() {
10421+
assert_no_code_actions!(
10422+
EXTRACT_FUNCTION,
10423+
r#"
10424+
fn print(text: String) { todo }
10425+
10426+
pub fn main() {
10427+
let arguments = todo
10428+
10429+
case arguments {
10430+
["help"] -> print("USAGE TEXT HERE")
10431+
_ -> panic as "Invalid args"
10432+
}
10433+
}
10434+
"#,
10435+
find_position_of("print")
10436+
.under_char('i')
10437+
.select_until(find_position_of("TEXT"))
10438+
);
10439+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\nconst pi = 3.14\n\npub fn main() {\n let radius = 4.5\n\n let circumference = radius *. pi *. 2.0\n\n echo circumference\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
const pi = 3.14
8+
9+
pub fn main() {
10+
let radius = 4.5
11+
12+
let circumference = radius *. pi *. 2.0
13+
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
14+
15+
echo circumference
16+
}
17+
18+
19+
----- AFTER ACTION
20+
21+
const pi = 3.14
22+
23+
pub fn main() {
24+
let radius = 4.5
25+
26+
let circumference = function(radius)
27+
28+
echo circumference
29+
}
30+
31+
fn function(radius: Float) -> Float {
32+
radius *. pi *. 2.0
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "\nconst pi = 3.14\n\npub fn main() {\n let value = 3.15\n\n let string = case value {\n 0.0 -> \"Zero\"\n 1.0 -> \"One\"\n _ if value == pi -> \"PI\"\n _ -> \"Something else\"\n }\n\n echo string\n}\n"
4+
---
5+
----- BEFORE ACTION
6+
7+
const pi = 3.14
8+
9+
pub fn main() {
10+
let value = 3.15
11+
12+
let string = case value {
13+
▔▔▔▔▔▔▔▔▔▔▔▔
14+
0.0 -> "Zero"
15+
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
16+
1.0 -> "One"
17+
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
18+
_ if value == pi -> "PI"
19+
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
20+
_ -> "Something else"
21+
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
22+
}
23+
▔▔▔
24+
25+
echo string
26+
}
27+
28+
29+
----- AFTER ACTION
30+
31+
const pi = 3.14
32+
33+
pub fn main() {
34+
let value = 3.15
35+
36+
let string = function(value)
37+
38+
echo string
39+
}
40+
41+
fn function(value: Float) -> String {
42+
case value {
43+
0.0 -> "Zero"
44+
1.0 -> "One"
45+
_ if value == pi -> "PI"
46+
_ -> "Something else"
47+
}
48+
}

0 commit comments

Comments
 (0)