# Path: tests/conftest.py
# HELP_HISTORY = """Usage: history [-h] [-r | -e | -o FILE | -t TRANSCRIPT_FILE | -c] [-s] [-x]
#                [-v] [-a]
#                [arg]
# 
# View, run, edit, save, or clear previously entered commands
# 
# positional arguments:
#   arg                   empty               all history items
#                         a                   one history item by number
#                         a..b, a:b, a:, ..b  items by indices (inclusive)
#                         string              items containing string
#                         /regex/             items matching regular expression
# 
# optional arguments:
#   -h, --help            show this help message and exit
#   -r, --run             run selected history items
#   -e, --edit            edit and then run selected history items
#   -o, --output_file FILE
#                         output commands to a script file, implies -s
#   -t, --transcript TRANSCRIPT_FILE
#                         output commands and results to a transcript file,
#                         implies -s
#   -c, --clear           clear all history
# 
# formatting:
#   -s, --script          output commands in script format, i.e. without command
#                         numbers
#   -x, --expanded        output fully parsed commands with any aliases and
#                         macros expanded, instead of typed commands
#   -v, --verbose         display history and include expanded commands if they
#                         differ from the typed command
#   -a, --all             display all commands, including ones persisted from
#                         previous sessions
# """
# 
# def normalize(block):
#     """Normalize a block of text to perform comparison.
# 
#     Strip newlines from the very beginning and very end  Then split into separate lines and strip trailing whitespace
#     from each line.
#     """
#     assert isinstance(block, str)
#     block = block.strip('\n')
#     return [line.rstrip() for line in block.splitlines()]
# 
# def run_cmd(app, cmd):
#     """Clear out and err StdSim buffers, run the command, and return out and err"""
#     saved_sysout = sys.stdout
#     sys.stdout = app.stdout
# 
#     # This will be used to capture app.stdout and sys.stdout
#     copy_cmd_stdout = StdSim(app.stdout)
# 
#     # This will be used to capture sys.stderr
#     copy_stderr = StdSim(sys.stderr)
# 
#     try:
#         app.stdout = copy_cmd_stdout
#         with redirect_stdout(copy_cmd_stdout):
#             with redirect_stderr(copy_stderr):
#                 app.onecmd_plus_hooks(cmd)
#     finally:
#         app.stdout = copy_cmd_stdout.inner_stream
#         sys.stdout = saved_sysout
# 
#     out = copy_cmd_stdout.getvalue()
#     err = copy_stderr.getvalue()
#     return normalize(out), normalize(err)


import os
import tempfile
import pytest
import cmd2
    import json
from unittest import (
    mock,
)
from .conftest import (
    HELP_HISTORY,
    normalize,
    run_cmd,
)
    from cmd2.rl_utils import (
        readline,
    )
    from cmd2.cmd2 import (
        History,
        HistoryItem,
    )
    from cmd2.parsing import (
        Statement,
    )
    from cmd2.cmd2 import (
        History,
        HistoryItem,
    )
    from cmd2.parsing import (
        Statement,
    )
    from cmd2.history import (
        History,
    )
    from cmd2.history import (
        HistoryItem,
    )
    from cmd2.parsing import (
        Statement,
    )
    from cmd2.parsing import (
        StatementParser,
    )
    from cmd2.history import (
        History,
    )
    from cmd2.history import (
        History,
    )
    from cmd2.history import (
        HistoryItem,
    )
    from cmd2.parsing import (
        Statement,
    )
    from cmd2.rl_utils import (
        readline,
    )
        raw='help history',
        command='help',
        arg_list=['history'],
    )
    with pytest.raises(TypeError):
        _ = HistoryItem()


def test_history_item_properties(histitem):
    assert histitem.raw == 'help history'
    assert histitem.expanded == 'help history'
    assert str(histitem) == 'help history'


#
# test history command
#
def test_base_history(base_app):
    run_cmd(base_app, 'help')
    run_cmd(base_app, 'shortcuts')
    out, err = run_cmd(base_app, 'history')
    expected = normalize(
        """
    1  help
    2  shortcuts
"""
    )
    assert out == expected

    out, err = run_cmd(base_app, 'history he')

# Path: tests/conftest.py
# HELP_HISTORY = """Usage: history [-h] [-r | -e | -o FILE | -t TRANSCRIPT_FILE | -c] [-s] [-x]
#                [-v] [-a]
#                [arg]
# 
# View, run, edit, save, or clear previously entered commands
# 
# positional arguments:
#   arg                   empty               all history items
#                         a                   one history item by number
#                         a..b, a:b, a:, ..b  items by indices (inclusive)
#                         string              items containing string
#                         /regex/             items matching regular expression
# 
# optional arguments:
#   -h, --help            show this help message and exit
#   -r, --run             run selected history items
#   -e, --edit            edit and then run selected history items
#   -o, --output_file FILE
#                         output commands to a script file, implies -s
#   -t, --transcript TRANSCRIPT_FILE
#                         output commands and results to a transcript file,
#                         implies -s
#   -c, --clear           clear all history
# 
# formatting:
#   -s, --script          output commands in script format, i.e. without command
#                         numbers
#   -x, --expanded        output fully parsed commands with any aliases and
#                         macros expanded, instead of typed commands
#   -v, --verbose         display history and include expanded commands if they
#                         differ from the typed command
#   -a, --all             display all commands, including ones persisted from
#                         previous sessions
# """
# 
# def normalize(block):
#     """Normalize a block of text to perform comparison.
# 
#     Strip newlines from the very beginning and very end  Then split into separate lines and strip trailing whitespace
#     from each line.
#     """
#     assert isinstance(block, str)
#     block = block.strip('\n')
#     return [line.rstrip() for line in block.splitlines()]
# 
# def run_cmd(app, cmd):
#     """Clear out and err StdSim buffers, run the command, and return out and err"""
#     saved_sysout = sys.stdout
#     sys.stdout = app.stdout
# 
#     # This will be used to capture app.stdout and sys.stdout
#     copy_cmd_stdout = StdSim(app.stdout)
# 
#     # This will be used to capture sys.stderr
#     copy_stderr = StdSim(sys.stderr)
# 
#     try:
#         app.stdout = copy_cmd_stdout
#         with redirect_stdout(copy_cmd_stdout):
#             with redirect_stderr(copy_stderr):
#                 app.onecmd_plus_hooks(cmd)
#     finally:
#         app.stdout = copy_cmd_stdout.inner_stream
#         sys.stdout = saved_sysout
# 
#     out = copy_cmd_stdout.getvalue()
#     err = copy_stderr.getvalue()
#     return normalize(out), normalize(err)


# Path: tests/test_history.py
import os
import tempfile
import pytest
import cmd2
    import json
from unittest import (
    mock,
)
from .conftest import (
    HELP_HISTORY,
    normalize,
    run_cmd,
)
    from cmd2.rl_utils import (
        readline,
    )
    from cmd2.cmd2 import (
        History,
        HistoryItem,
    )
    from cmd2.parsing import (
        Statement,
    )
    from cmd2.cmd2 import (
        History,
        HistoryItem,
    )
    from cmd2.parsing import (
        Statement,
    )
    from cmd2.history import (
        History,
    )
    from cmd2.history import (
        HistoryItem,
    )
    from cmd2.parsing import (
        Statement,
    )
    from cmd2.parsing import (
        StatementParser,
    )
    from cmd2.history import (
        History,
    )
    from cmd2.history import (
        History,
    )
    from cmd2.history import (
        HistoryItem,
    )
    from cmd2.parsing import (
        Statement,
    )
    from cmd2.rl_utils import (
        readline,
    )

        raw='help history',
        command='help',
        arg_list=['history'],
    )
    with pytest.raises(TypeError):
        _ = HistoryItem()


def test_history_item_properties(histitem):
    assert histitem.raw == 'help history'
    assert histitem.expanded == 'help history'
    assert str(histitem) == 'help history'


#
# test history command
#
def test_base_history(base_app):
    run_cmd(base_app, 'help')
    run_cmd(base_app, 'shortcuts')
    out, err = run_cmd(base_app, 'history')
    expected = normalize(
        """
    1  help
    2  shortcuts
"""
    )
    assert out == expected

    out, err = run_cmd(base_app, 'history he')

    expected = normalize(