# Path: cmd2/rl_utils.py
# class RlType(Enum):
#     GNU = 1
#     PYREADLINE = 2
#     NONE = 3
#             ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
#         STD_OUT_HANDLE = -11
#         STD_ERROR_HANDLE = -12
#         def enable_win_vt100(handle: HANDLE) -> bool:
#         def pyreadline_remove_history_item(pos: int) -> None:
# def rl_force_redisplay() -> None:  # pragma: no cover
# def rl_get_point() -> int:  # pragma: no cover
# def rl_get_prompt() -> str:  # pragma: no cover
# def rl_set_prompt(prompt: str) -> None:  # pragma: no cover
# def rl_escape_prompt(prompt: str) -> str:
# def rl_unescape_prompt(prompt: str) -> str:
# 
# Path: cmd2/utils.py
# class StdSim:
#     """
#     Class to simulate behavior of sys.stdout or sys.stderr.
#     Stores contents in internal buffer and optionally echos to the inner stream it is simulating.
#     """
# 
#     def __init__(
#         self,
#         inner_stream: Union[TextIO, 'StdSim'],
#         *,
#         echo: bool = False,
#         encoding: str = 'utf-8',
#         errors: str = 'replace',
#     ) -> None:
#         """
#         StdSim Initializer
# 
#         :param inner_stream: the wrapped stream. Should be a TextIO or StdSim instance.
#         :param echo: if True, then all input will be echoed to inner_stream
#         :param encoding: codec for encoding/decoding strings (defaults to utf-8)
#         :param errors: how to handle encoding/decoding errors (defaults to replace)
#         """
#         self.inner_stream = inner_stream
#         self.echo = echo
#         self.encoding = encoding
#         self.errors = errors
#         self.pause_storage = False
#         self.buffer = ByteBuf(self)
# 
#     def write(self, s: str) -> None:
#         """
#         Add str to internal bytes buffer and if echo is True, echo contents to inner stream
# 
#         :param s: String to write to the stream
#         """
#         if not isinstance(s, str):
#             raise TypeError(f'write() argument must be str, not {type(s)}')
# 
#         if not self.pause_storage:
#             self.buffer.byte_buf += s.encode(encoding=self.encoding, errors=self.errors)
#         if self.echo:
#             self.inner_stream.write(s)
# 
#     def getvalue(self) -> str:
#         """Get the internal contents as a str"""
#         return self.buffer.byte_buf.decode(encoding=self.encoding, errors=self.errors)
# 
#     def getbytes(self) -> bytes:
#         """Get the internal contents as bytes"""
#         return bytes(self.buffer.byte_buf)
# 
#     def read(self, size: Optional[int] = -1) -> str:
#         """
#         Read from the internal contents as a str and then clear them out
# 
#         :param size: Number of bytes to read from the stream
#         """
#         if size is None or size == -1:
#             result = self.getvalue()
#             self.clear()
#         else:
#             result = self.buffer.byte_buf[:size].decode(encoding=self.encoding, errors=self.errors)
#             self.buffer.byte_buf = self.buffer.byte_buf[size:]
# 
#         return result
# 
#     def readbytes(self) -> bytes:
#         """Read from the internal contents as bytes and then clear them out"""
#         result = self.getbytes()
#         self.clear()
#         return result
# 
#     def clear(self) -> None:
#         """Clear the internal contents"""
#         self.buffer.byte_buf.clear()
# 
#     def isatty(self) -> bool:
#         """StdSim only considered an interactive stream if `echo` is True and `inner_stream` is a tty."""
#         if self.echo:
#             return self.inner_stream.isatty()
#         else:
#             return False
# 
#     @property
#     def line_buffering(self) -> bool:
#         """
#         Handle when the inner stream doesn't have a line_buffering attribute which is the case
#         when running unit tests because pytest sets stdout to a pytest EncodedFile object.
#         """
#         try:
#             return bool(self.inner_stream.line_buffering)
#         except AttributeError:
#             return False
# 
#     def __getattr__(self, item: str) -> Any:
#         if item in self.__dict__:
#             return self.__dict__[item]
#         else:
#             return getattr(self.inner_stream, item)


import sys
import cmd2
from contextlib import (
    redirect_stderr,
    redirect_stdout,
)
from typing import (
    List,
    Optional,
    Union,
)
from unittest import (
    mock,
)
from cmd2_ext_test import (
    ExternalTestMixin,
)
from pytest import (
    fixture,
)
from cmd2.rl_utils import (
    readline,
)
from cmd2.utils import (
    StdSim,
)


def complete_tester(text: str, line: str, begidx: int, endidx: int, app) -> Optional[str]:
    """
    This is a convenience function to test cmd2.complete() since
    in a unit test environment there is no actual console readline
    is monitoring. Therefore we use mock to provide readline data
    to complete().

    :param text: the string prefix we are attempting to match
    :param line: the current input line with leading whitespace removed
    :param begidx: the beginning index of the prefix text
    :param endidx: the ending index of the prefix text
    :param app: the cmd2 app that will run completions
    :return: The first matched string or None if there are no matches
             Matches are stored in app.completion_matches
             These matches also have been sorted by complete()
    """

    def get_line():
        return line

    def get_begidx():
        return begidx

    def get_endidx():
        return endidx

    # Run the readline tab completion function with readline mocks in place
    with mock.patch.object(readline, 'get_line_buffer', get_line):

# Path: cmd2/rl_utils.py
# class RlType(Enum):
#     GNU = 1
#     PYREADLINE = 2
#     NONE = 3
#             ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
#         STD_OUT_HANDLE = -11
#         STD_ERROR_HANDLE = -12
#         def enable_win_vt100(handle: HANDLE) -> bool:
#         def pyreadline_remove_history_item(pos: int) -> None:
# def rl_force_redisplay() -> None:  # pragma: no cover
# def rl_get_point() -> int:  # pragma: no cover
# def rl_get_prompt() -> str:  # pragma: no cover
# def rl_set_prompt(prompt: str) -> None:  # pragma: no cover
# def rl_escape_prompt(prompt: str) -> str:
# def rl_unescape_prompt(prompt: str) -> str:
# 
# Path: cmd2/utils.py
# class StdSim:
#     """
#     Class to simulate behavior of sys.stdout or sys.stderr.
#     Stores contents in internal buffer and optionally echos to the inner stream it is simulating.
#     """
# 
#     def __init__(
#         self,
#         inner_stream: Union[TextIO, 'StdSim'],
#         *,
#         echo: bool = False,
#         encoding: str = 'utf-8',
#         errors: str = 'replace',
#     ) -> None:
#         """
#         StdSim Initializer
# 
#         :param inner_stream: the wrapped stream. Should be a TextIO or StdSim instance.
#         :param echo: if True, then all input will be echoed to inner_stream
#         :param encoding: codec for encoding/decoding strings (defaults to utf-8)
#         :param errors: how to handle encoding/decoding errors (defaults to replace)
#         """
#         self.inner_stream = inner_stream
#         self.echo = echo
#         self.encoding = encoding
#         self.errors = errors
#         self.pause_storage = False
#         self.buffer = ByteBuf(self)
# 
#     def write(self, s: str) -> None:
#         """
#         Add str to internal bytes buffer and if echo is True, echo contents to inner stream
# 
#         :param s: String to write to the stream
#         """
#         if not isinstance(s, str):
#             raise TypeError(f'write() argument must be str, not {type(s)}')
# 
#         if not self.pause_storage:
#             self.buffer.byte_buf += s.encode(encoding=self.encoding, errors=self.errors)
#         if self.echo:
#             self.inner_stream.write(s)
# 
#     def getvalue(self) -> str:
#         """Get the internal contents as a str"""
#         return self.buffer.byte_buf.decode(encoding=self.encoding, errors=self.errors)
# 
#     def getbytes(self) -> bytes:
#         """Get the internal contents as bytes"""
#         return bytes(self.buffer.byte_buf)
# 
#     def read(self, size: Optional[int] = -1) -> str:
#         """
#         Read from the internal contents as a str and then clear them out
# 
#         :param size: Number of bytes to read from the stream
#         """
#         if size is None or size == -1:
#             result = self.getvalue()
#             self.clear()
#         else:
#             result = self.buffer.byte_buf[:size].decode(encoding=self.encoding, errors=self.errors)
#             self.buffer.byte_buf = self.buffer.byte_buf[size:]
# 
#         return result
# 
#     def readbytes(self) -> bytes:
#         """Read from the internal contents as bytes and then clear them out"""
#         result = self.getbytes()
#         self.clear()
#         return result
# 
#     def clear(self) -> None:
#         """Clear the internal contents"""
#         self.buffer.byte_buf.clear()
# 
#     def isatty(self) -> bool:
#         """StdSim only considered an interactive stream if `echo` is True and `inner_stream` is a tty."""
#         if self.echo:
#             return self.inner_stream.isatty()
#         else:
#             return False
# 
#     @property
#     def line_buffering(self) -> bool:
#         """
#         Handle when the inner stream doesn't have a line_buffering attribute which is the case
#         when running unit tests because pytest sets stdout to a pytest EncodedFile object.
#         """
#         try:
#             return bool(self.inner_stream.line_buffering)
#         except AttributeError:
#             return False
# 
#     def __getattr__(self, item: str) -> Any:
#         if item in self.__dict__:
#             return self.__dict__[item]
#         else:
#             return getattr(self.inner_stream, item)


# Path: tests_isolated/test_commandset/conftest.py
import sys
import cmd2
from contextlib import (
    redirect_stderr,
    redirect_stdout,
)
from typing import (
    List,
    Optional,
    Union,
)
from unittest import (
    mock,
)
from cmd2_ext_test import (
    ExternalTestMixin,
)
from pytest import (
    fixture,
)
from cmd2.rl_utils import (
    readline,
)
from cmd2.utils import (
    StdSim,
)



def complete_tester(text: str, line: str, begidx: int, endidx: int, app) -> Optional[str]:
    """
    This is a convenience function to test cmd2.complete() since
    in a unit test environment there is no actual console readline
    is monitoring. Therefore we use mock to provide readline data
    to complete().

    :param text: the string prefix we are attempting to match
    :param line: the current input line with leading whitespace removed
    :param begidx: the beginning index of the prefix text
    :param endidx: the ending index of the prefix text
    :param app: the cmd2 app that will run completions
    :return: The first matched string or None if there are no matches
             Matches are stored in app.completion_matches
             These matches also have been sorted by complete()
    """

    def get_line():
        return line

    def get_begidx():
        return begidx

    def get_endidx():
        return endidx

    # Run the readline tab completion function with readline mocks in place
    with mock.patch.object(readline, 'get_line_buffer', get_line):

        with mock.patch.object(readline, 'get_begidx', get_begidx):