From 66d35a5d20e2fcfcb420ee02bb4daa92e3fa947f Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Wed, 12 Jun 2024 11:05:29 +0530 Subject: [PATCH] add completion tests for string union types Signed-off-by: shruti2522 make fmt Signed-off-by: shruti2522 add comments Signed-off-by: shruti2522 add completion tests for string union types Signed-off-by: shruti2522 update comment Signed-off-by: shruti2522 add completion tests for string union types Signed-off-by: shruti2522 --- kclvm/tools/src/LSP/src/completion.rs | 57 ++++++++++++++++++- .../completion_test/dot/completion.k | 7 +++ .../completion_test/without_dot/completion.k | 7 +++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/kclvm/tools/src/LSP/src/completion.rs b/kclvm/tools/src/LSP/src/completion.rs index 049d0a740..8cb74d1e2 100644 --- a/kclvm/tools/src/LSP/src/completion.rs +++ b/kclvm/tools/src/LSP/src/completion.rs @@ -779,7 +779,7 @@ mod tests { let mut expected_labels: Vec = vec![ "", // generate from error recovery of "pkg." "subpkg", "math", "Person", "Person{}", "P", "P{}", "p", "p1", "p2", "p3", "p4", - "aaaa", + "aaaa", "Config", "Config{}", "n", ] .iter() .map(|s| s.to_string()) @@ -939,7 +939,7 @@ mod tests { assert_eq!(got_labels, expected_labels); let pos = KCLPos { - filename: file, + filename: file.clone(), line: 30, column: Some(11), }; @@ -952,6 +952,25 @@ mod tests { let expected_labels: Vec<&str> = vec!["a"]; assert_eq!(got_labels, expected_labels); + + // test completion for string union type + let pos = KCLPos { + filename: file.clone(), + line: 36, + column: Some(30), + }; + + let got = completion(Some('.'), &program, &pos, &gs, &tool).unwrap(); + let got_labels: Vec = match got { + CompletionResponse::Array(arr) => arr.iter().map(|item| item.label.clone()).collect(), + CompletionResponse::List(_) => panic!("test failed"), + }; + + let expected_labels: Vec = STRING_MEMBER_FUNCTIONS + .iter() + .map(|(name, ty)| func_ty_complete_label(name, &ty.into_func_type())) + .collect(); + assert_eq!(got_labels, expected_labels); } #[test] @@ -1089,7 +1108,7 @@ mod tests { assert_eq!(got_insert_text, expected_insert_text); let pos = KCLPos { - filename: file, + filename: file.clone(), line: 30, column: Some(11), }; @@ -1102,6 +1121,38 @@ mod tests { let expected_labels: Vec<&str> = vec!["a"]; assert_eq!(got_labels, expected_labels); + + // test completion for str union types + let pos = KCLPos { + filename: file.clone(), + line: 36, + column: Some(30), + }; + + let got = completion(Some('.'), &program, &pos, &gs, &tool).unwrap(); + let got_labels: Vec = match &got { + CompletionResponse::Array(arr) => arr.iter().map(|item| item.label.clone()).collect(), + CompletionResponse::List(_) => panic!("test failed"), + }; + + let expected_labels: Vec = STRING_MEMBER_FUNCTIONS + .iter() + .map(|(name, ty)| func_ty_complete_label(name, &ty.into_func_type())) + .collect(); + assert_eq!(got_labels, expected_labels); + + let got_insert_text: Vec = match &got { + CompletionResponse::Array(arr) => arr + .iter() + .map(|item| item.insert_text.clone().unwrap()) + .collect(), + CompletionResponse::List(_) => panic!("test failed"), + }; + let expected_insert_text: Vec = STRING_MEMBER_FUNCTIONS + .iter() + .map(|(name, ty)| func_ty_complete_insert_text(name, &ty.into_func_type())) + .collect(); + assert_eq!(got_insert_text, expected_insert_text); } #[test] diff --git a/kclvm/tools/src/LSP/src/test_data/completion_test/dot/completion.k b/kclvm/tools/src/LSP/src/test_data/completion_test/dot/completion.k index 18eacc29e..07d6f91f5 100644 --- a/kclvm/tools/src/LSP/src/test_data/completion_test/dot/completion.k +++ b/kclvm/tools/src/LSP/src/test_data/completion_test/dot/completion.k @@ -28,3 +28,10 @@ schema P: a: int = 1 aaaa = P{}. + +schema Config: + containerRuntime?: "runc" | "rund" + +n = Config { + containerRuntime = "runc". +} \ No newline at end of file diff --git a/kclvm/tools/src/LSP/src/test_data/completion_test/without_dot/completion.k b/kclvm/tools/src/LSP/src/test_data/completion_test/without_dot/completion.k index 1bac86b60..65c8639c7 100644 --- a/kclvm/tools/src/LSP/src/test_data/completion_test/without_dot/completion.k +++ b/kclvm/tools/src/LSP/src/test_data/completion_test/without_dot/completion.k @@ -28,3 +28,10 @@ schema P: a: int = 1 aaaa = P{} + +schema Config: + containerRuntime?: "runc" | "rund" + +n = Config { + containerRuntime = "runc" +}