2020)
2121from cmd2 import rich_utils as ru
2222from cmd2 import string_utils as su
23- from cmd2 .history import HistoryItem
24- from cmd2 .parsing import Statement
2523from cmd2 .pt_utils import pt_filter_style
2624
2725from .conftest import with_ansi_style
@@ -34,7 +32,6 @@ def __init__(self) -> None:
3432 self .complete = Mock (return_value = cmd2 .Completions ())
3533
3634 self .always_show_hint = False
37- self .history = []
3835 self .statement_parser = Mock ()
3936 self .statement_parser .terminators = [';' ]
4037 self .statement_parser .shortcuts = []
@@ -506,68 +503,84 @@ def test_get_completions_custom_delimiters(self, mock_cmd_app: MockCmd) -> None:
506503
507504
508505class TestCmd2History :
509- def make_history_item (self , text ):
510- statement = Mock (spec = Statement )
511- statement .raw = text
512- item = Mock (spec = HistoryItem )
513- item .statement = statement
514- return item
515-
516- def test_load_history_strings (self , mock_cmd_app ):
517- """Test loading history strings yields all items in forward order."""
518- history = pt_utils .Cmd2History (cast (Any , mock_cmd_app ))
519-
520- # Set up history items
521- # History in cmd2 is oldest to newest
522- items = [
523- self .make_history_item ("cmd1" ),
524- self .make_history_item ("cmd2" ),
525- self .make_history_item ("cmd2" ), # Duplicate
526- self .make_history_item ("cmd3" ),
527- ]
528- mock_cmd_app .history = items
506+ def test_load_history_strings (self ):
507+ """Test loading history strings yields all items newest to oldest."""
529508
530- # Expected: cmd1, cmd2, cmd2, cmd3 (raw iteration)
531- result = list (history .load_history_strings ())
509+ history_strings = ["cmd1" , "cmd2" , "cmd2" , "cmd3" , "cmd2" ]
510+ history = pt_utils .Cmd2History (history_strings )
511+ assert history ._loaded
532512
533- assert result == ["cmd1" , "cmd2" , "cmd2" , "cmd3" ]
513+ # Consecutive duplicates are removed
514+ expected = ["cmd2" , "cmd3" , "cmd2" , "cmd1" ]
515+ assert list (history .load_history_strings ()) == expected
534516
535- def test_load_history_strings_empty (self , mock_cmd_app ):
517+ def test_load_history_strings_empty (self ):
536518 """Test loading history strings with empty history."""
537- history = pt_utils .Cmd2History (cast (Any , mock_cmd_app ))
538-
539- mock_cmd_app .history = []
540-
541- result = list (history .load_history_strings ())
519+ history = pt_utils .Cmd2History ()
520+ assert history ._loaded
521+ assert list (history .load_history_strings ()) == []
522+
523+ history = pt_utils .Cmd2History ([])
524+ assert history ._loaded
525+ assert list (history .load_history_strings ()) == []
526+
527+ history = pt_utils .Cmd2History (None )
528+ assert history ._loaded
529+ assert list (history .load_history_strings ()) == []
530+
531+ def test_get_strings (self ):
532+ history_strings = ["cmd1" , "cmd2" , "cmd2" , "cmd3" , "cmd2" ]
533+ history = pt_utils .Cmd2History (history_strings )
534+ assert history ._loaded
535+
536+ # Consecutive duplicates are removed
537+ expected = ["cmd1" , "cmd2" , "cmd3" , "cmd2" ]
538+ assert history .get_strings () == expected
539+
540+ def test_append_string (self ):
541+ """Test that append_string() does nothing."""
542+ history = pt_utils .Cmd2History ()
543+ assert history ._loaded
544+ assert not history ._loaded_strings
545+
546+ history .append_string ("new command" )
547+ assert not history ._loaded_strings
548+
549+ def test_store_string (self ):
550+ """Test that store_string() does nothing."""
551+ history = pt_utils .Cmd2History ()
552+ assert history ._loaded
553+ assert not history ._loaded_strings
542554
543- assert result == []
544-
545- def test_get_strings (self , mock_cmd_app ):
546- """Test get_strings returns deduped strings and does not cache."""
547- history = pt_utils .Cmd2History (cast (Any , mock_cmd_app ))
548-
549- items = [
550- self .make_history_item ("cmd1" ),
551- self .make_history_item ("cmd2" ),
552- self .make_history_item ("cmd2" ), # Duplicate
553- self .make_history_item ("cmd3" ),
554- ]
555- mock_cmd_app .history = items
556-
557- # Expect deduped: cmd1, cmd2, cmd3
558- strings = history .get_strings ()
559- assert strings == ["cmd1" , "cmd2" , "cmd3" ]
560-
561- # Modify underlying history to prove it does NOT use cache
562- mock_cmd_app .history .append (self .make_history_item ("cmd4" ))
563- strings2 = history .get_strings ()
564- assert strings2 == ["cmd1" , "cmd2" , "cmd3" , "cmd4" ]
565-
566- def test_store_string (self , mock_cmd_app ):
567- """Test store_string does nothing."""
568- history = pt_utils .Cmd2History (cast (Any , mock_cmd_app ))
569-
570- # Just ensure it doesn't raise error or modify cmd2 history
571555 history .store_string ("new command" )
572-
573- assert len (mock_cmd_app .history ) == 0
556+ assert not history ._loaded_strings
557+
558+ def test_add_command (self ):
559+ """Test that add_command() adds data."""
560+ history = pt_utils .Cmd2History ()
561+ assert history ._loaded
562+ assert not history ._loaded_strings
563+
564+ history .add_command ("new command" )
565+ assert len (history ._loaded_strings ) == 1
566+ assert history ._loaded_strings [0 ] == "new command"
567+
568+ # Show that consecutive duplicates are filtered
569+ history .add_command ("new command" )
570+ assert len (history ._loaded_strings ) == 1
571+ assert history ._loaded_strings [0 ] == "new command"
572+
573+ # Show that new items are placed at the front
574+ history .add_command ("even newer command" )
575+ assert len (history ._loaded_strings ) == 2
576+ assert history ._loaded_strings [0 ] == "even newer command"
577+ assert history ._loaded_strings [1 ] == "new command"
578+
579+ def test_clear (self ):
580+ history_strings = ["cmd1" , "cmd2" ]
581+ history = pt_utils .Cmd2History (history_strings )
582+ assert history ._loaded
583+ assert history .get_strings () == history_strings
584+
585+ history .clear ()
586+ assert not history .get_strings ()
0 commit comments