================
@@ -2,47 +2,91 @@
 Test lldb-dap completions request
 """
 
+# FIXME: remove when LLDB_MINIMUM_PYTHON_VERSION > 3.8
+from __future__ import annotations
+
+import json
+from typing import Optional
 import lldbdap_testcase
-import dap_server
-from lldbsuite.test import lldbutil
+from dataclasses import dataclass, replace, asdict
 from lldbsuite.test.decorators import skipIf
 from lldbsuite.test.lldbtest import line_number
 
-session_completion = {
-    "text": "session",
-    "label": "session",
-    "detail": "Commands controlling LLDB session.",
-}
-settings_completion = {
-    "text": "settings",
-    "label": "settings",
-    "detail": "Commands for managing LLDB settings.",
-}
-memory_completion = {
-    "text": "memory",
-    "label": "memory",
-    "detail": "Commands for operating on memory in the current target 
process.",
-}
-command_var_completion = {
-    "text": "var",
-    "label": "var",
-    "detail": "Show variables for the current stack frame. Defaults to all 
arguments and local variables in scope. Names of argument, local, file static 
and file global variables can be specified.",
-}
-variable_var_completion = {"text": "var", "label": "var", "detail": 
"vector<baz> &"}
-variable_var1_completion = {"text": "var1", "label": "var1", "detail": "int &"}
-variable_var2_completion = {"text": "var2", "label": "var2", "detail": "int &"}
+
+@dataclass(frozen=True)
+class CompletionItem:
+    label: str
+    text: Optional[str] = None
+    detail: Optional[str] = None
+    start: Optional[int] = None
+    length: int = 0
+
+    def __repr__(self):
+        # use json as it easier to see the diff on failure.
+        return json.dumps(asdict(self), indent=4)
+
+    def clone(self, **kwargs) -> CompletionItem:
+        """Creates a copy of this CompletionItem with specified fields 
modified."""
+        return replace(self, **kwargs)
+
+
+@dataclass(frozen=True)
+class TestCase:
+    input: str
+    expected: set[CompletionItem]
+    not_expected: Optional[set[CompletionItem]] = None
+
+
+session_completion = CompletionItem(
+    label="session",
+    detail="Commands controlling LLDB session.",
+)
+settings_completion = CompletionItem(
+    label="settings",
+    detail="Commands for managing LLDB settings.",
+)
+memory_completion = CompletionItem(
+    label="memory",
+    detail="Commands for operating on memory in the current target process.",
+)
+command_var_completion = CompletionItem(
+    label="var",
+    detail="Show variables for the current stack frame. Defaults to all 
arguments and local variables in scope. Names of argument, local, file static 
and file global variables can be specified.",
+    length=3,
+)
+variable_var_completion = CompletionItem(label="var", detail="vector<baz> &", 
length=3)
+variable_var1_completion = CompletionItem(label="var1", detail="int &")
+variable_var2_completion = CompletionItem(label="var2", detail="int &")
+
+str1_completion = CompletionItem(
+    label="str1",
+    detail="std::string &",
+)
 
 
 # Older version of libcxx produce slightly different typename strings for
 # templates like vector.
 @skipIf(compiler="clang", compiler_version=["<", "16.0"])
 class TestDAP_completions(lldbdap_testcase.DAPTestCaseBase):
-    def verify_completions(self, actual_list, expected_list, 
not_expected_list=[]):
-        for expected_item in expected_list:
-            self.assertIn(expected_item, actual_list)
-
-        for not_expected_item in not_expected_list:
-            self.assertNotIn(not_expected_item, actual_list)
+    def verify_completions(self, case: TestCase):
+        completions = {
+            CompletionItem(**comp)
+            for comp in self.dap_server.get_completions(case.input)
+        }
+
+        # handle expected completions
+        expected_completions = case.expected
+        for exp_comp in expected_completions:
+            # with self.subTest(f"Expected completion : {exp_comp}"):
+            self.assertIn(
+                exp_comp, completions, f"\nCompletion for input: {case.input}"
+            )
+
+        # unexpected completions
+        not_expected_label = case.not_expected or set()
+        for not_exp_comp in not_expected_label:
----------------
ashgti wrote:

nit: These aren't just labels, its the completion items, should we just do `for 
not_exp_comp in case.not_expected or set():`?

https://github.com/llvm/llvm-project/pull/177151
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to