MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / test_styled_completion_sort

Function test_styled_completion_sort

tests/test_completion.py:985–1020  ·  view source on GitHub ↗

Test that sorting is done with the display_plain field.

()

Source from the content-addressed store, hash-verified

983
984
985def test_styled_completion_sort() -> None:
986 """Test that sorting is done with the display_plain field."""
987
988 # First sort with strings that include ANSI style sequences.
989 red_apple = "\x1b[31mApple\x1b[0m"
990 green_cherry = "\x1b[32mCherry\x1b[0m"
991 blue_banana = "\x1b[34mBanana\x1b[0m"
992
993 # This sorts by ASCII: [31m (Red), [32m (Green), [34m (Blue)
994 unsorted_strs = [blue_banana, red_apple, green_cherry]
995 sorted_strs = sorted(unsorted_strs, key=utils.DEFAULT_STR_SORT_KEY)
996 assert sorted_strs == [red_apple, green_cherry, blue_banana]
997
998 # Now create a Completions object with these values.
999 unsorted_items = [
1000 CompletionItem("banana", display=blue_banana),
1001 CompletionItem("cherry", display=green_cherry),
1002 CompletionItem("apple", display=red_apple),
1003 ]
1004
1005 completions = Completions(unsorted_items)
1006
1007 # Expected order: Apple (A), Banana (B), Cherry (C)
1008 expected_plain = ["Apple", "Banana", "Cherry"]
1009 expected_styled = [red_apple, blue_banana, green_cherry]
1010
1011 for index, item in enumerate(completions):
1012 # Prove the ANSI stripping worked correctly
1013 assert item.display_plain == expected_plain[index]
1014
1015 # Prove the sort order used the plain text, not the ANSI codes
1016 assert item.display == expected_styled[index]
1017
1018 # Prove the order of completions is not the same as the raw string sort order
1019 completion_displays = [item.display for item in completions]
1020 assert completion_displays != sorted_strs
1021
1022
1023# Used by redirect_complete tests

Callers

nothing calls this directly

Calls 2

CompletionItemClass · 0.90
CompletionsClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…