]> git.lizzy.rs Git - rust.git/commitdiff
add tests for extracting if/match/while/for exprs
authorVladyslav Katasonov <cpud47@gmail.com>
Thu, 4 Feb 2021 23:30:34 +0000 (02:30 +0300)
committerVladyslav Katasonov <cpud47@gmail.com>
Thu, 4 Feb 2021 23:30:34 +0000 (02:30 +0300)
crates/assists/src/handlers/extract_function.rs

index dfb3da7a5ee164e08b28c9e56a66dd399ba499fa..5d6f5bb267cd93b65ffcd8adb246a9c875b2e0ff 100644 (file)
@@ -1010,6 +1010,126 @@ fn $0fun_name() {
         );
     }
 
+    #[test]
+    fn no_args_if() {
+        check_assist(
+            extract_function,
+            r#"
+fn foo() {
+    $0if true { }$0
+}"#,
+            r#"
+fn foo() {
+    fun_name();
+}
+
+fn $0fun_name() {
+    if true { }
+}"#,
+        );
+    }
+
+    #[test]
+    fn no_args_if_else() {
+        check_assist(
+            extract_function,
+            r#"
+fn foo() -> i32 {
+    $0if true { 1 } else { 2 }$0
+}"#,
+            r#"
+fn foo() -> i32 {
+    fun_name()
+}
+
+fn $0fun_name() -> i32 {
+    if true { 1 } else { 2 }
+}"#,
+        );
+    }
+
+    #[test]
+    fn no_args_if_let_else() {
+        check_assist(
+            extract_function,
+            r#"
+fn foo() -> i32 {
+    $0if let true = false { 1 } else { 2 }$0
+}"#,
+            r#"
+fn foo() -> i32 {
+    fun_name()
+}
+
+fn $0fun_name() -> i32 {
+    if let true = false { 1 } else { 2 }
+}"#,
+        );
+    }
+
+    #[test]
+    fn no_args_match() {
+        check_assist(
+            extract_function,
+            r#"
+fn foo() -> i32 {
+    $0match true {
+        true => 1,
+        false => 2,
+    }$0
+}"#,
+            r#"
+fn foo() -> i32 {
+    fun_name()
+}
+
+fn $0fun_name() -> i32 {
+    match true {
+        true => 1,
+        false => 2,
+    }
+}"#,
+        );
+    }
+
+    #[test]
+    fn no_args_while() {
+        check_assist(
+            extract_function,
+            r#"
+fn foo() {
+    $0while true { }$0
+}"#,
+            r#"
+fn foo() {
+    fun_name();
+}
+
+fn $0fun_name() {
+    while true { }
+}"#,
+        );
+    }
+
+    #[test]
+    fn no_args_for() {
+        check_assist(
+            extract_function,
+            r#"
+fn foo() {
+    $0for v in &[0, 1] { }$0
+}"#,
+            r#"
+fn foo() {
+    fun_name();
+}
+
+fn $0fun_name() {
+    for v in &[0, 1] { }
+}"#,
+        );
+    }
+
     #[test]
     fn no_args_from_loop_unit() {
         check_assist(