Skip to content

Commit ed6351d

Browse files
committed
test(cmd2): add coverage for _get_commands_aliases_and_macros_for_completion
Added a unit test to verify that visible commands, aliases, and macros are correctly returned as CompletionItem objects for tab completion. Specifically addressed the case where a command has no docstring.
1 parent 9f49d1a commit ed6351d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/test_cmd2.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,6 +2268,47 @@ def test_get_macro_completion_items(base_app) -> None:
22682268
assert cur_res.descriptive_data[0].rstrip() == base_app.macros[cur_res].value
22692269

22702270

2271+
def test_get_commands_aliases_and_macros_for_completion(base_app) -> None:
2272+
# Add an alias and a macro
2273+
run_cmd(base_app, 'alias create fake_alias help')
2274+
run_cmd(base_app, 'macro create fake_macro !echo macro')
2275+
2276+
# Add a command without a docstring
2277+
import types
2278+
2279+
def do_no_doc(self, arg):
2280+
pass
2281+
2282+
base_app.do_no_doc = types.MethodType(do_no_doc, base_app)
2283+
2284+
results = base_app._get_commands_aliases_and_macros_for_completion()
2285+
2286+
# All visible commands + our new command + alias + macro
2287+
expected_count = len(base_app.get_visible_commands()) + len(base_app.aliases) + len(base_app.macros)
2288+
assert len(results) == expected_count
2289+
2290+
# Verify alias
2291+
alias_item = next((item for item in results if item == 'fake_alias'), None)
2292+
assert alias_item is not None
2293+
assert alias_item.descriptive_data[0] == "Alias for: help"
2294+
2295+
# Verify macro
2296+
macro_item = next((item for item in results if item == 'fake_macro'), None)
2297+
assert macro_item is not None
2298+
assert macro_item.descriptive_data[0] == "Macro: !echo macro"
2299+
2300+
# Verify command with docstring (help)
2301+
help_item = next((item for item in results if item == 'help'), None)
2302+
assert help_item is not None
2303+
# First line of help docstring
2304+
assert "List available commands" in help_item.descriptive_data[0]
2305+
2306+
# Verify command without docstring
2307+
no_doc_item = next((item for item in results if item == 'no_doc'), None)
2308+
assert no_doc_item is not None
2309+
assert no_doc_item.descriptive_data[0] == ""
2310+
2311+
22712312
def test_get_settable_completion_items(base_app) -> None:
22722313
results = base_app._get_settable_completion_items()
22732314
assert len(results) == len(base_app.settables)

0 commit comments

Comments
 (0)