def test_cmd_loop_hooks():
    class Cmd(Interpreter):
        def loop_enter(self):
            self.stdout.write("entering loop\n")
        def loop_exit(self):
            self.stdout.write("exiting loop\n")
        @command
        def greet(self):
            self.stdout.write("Hello, world!\n")
        @command(name="quit")
        def _quit(self, *args):
            "Quit the shell"
            self.stdout.write("bye!\n")
            return True 

    cmd = Cmd()
    cmd.stdin = io.StringIO("greet\nquit\n")
    cmd.stdout = io.StringIO()
    cmd.stderr = io.StringIO()
    cmd.run()
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == """\
entering loop
Hello, world!
bye!
exiting loop
"""
    assert cmd.stderr.getvalue() == ""
def test_alias_args(cmd):
    cmd.alias("greet", "hello_world", target_args=("-n", "world"))
    cmd.stdin.write("hello_world")
    cmd.stdin.seek(0)
    cmd.run(max_commands=1)
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == "Hello, world!\n"
def test_alias_group(cmd):
    cmd.alias(name="xgrpx", target="grpx")
    cmd.stdin.write("xgrpx grpx_0")
    cmd.stdin.seek(0)
    cmd.run(max_commands=1)
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == "grpx_0!\n"
def test_alias(cmd):
    cmd.alias(name='foo', target='bar')
    cmd.stdin.write("foo")
    cmd.stdin.seek(0)
    cmd.run(max_commands=1)
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == TEXT_BAR + "\n"
def test_alias_pos(cmd):
    cmd.alias("bar", "foo")
    cmd.stdin.write("foo")
    cmd.stdin.seek(0)
    cmd.run(max_commands=1)
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == TEXT_BAR + "\n"
def test_alias_ok(cmd, boolean):
    hidden = boolean
    cmd.alias(name='ba', target='bar', hidden=hidden)
    cmd.alias(name='b', target='baz', hidden=hidden)
    cmd.stdin.write("ba\nb\nhelp\n")
    cmd.stdin.seek(0)
    cmd.run(max_commands=3)
    debug_cmd(cmd)
    if hidden:
        htext = """
~~~ topics: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bazbaz

~~~ subcommands: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help  bar   baz   grpx  grpy  greet
"""
    else:
        htext = """
~~~ topics: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bazbaz

~~~ subcommands: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help  bar   baz   grpx  grpy  greet ba    b    
"""
    expected_lines = [
        TEXT_BAR,
        TEXT_BAZ,
        htext,
        "",
    ]
    #print("/" * 80)
    #print("\n".join(expected_lines))
    #print("/" * 80)
    assert cmd.stdout.getvalue() == "\n".join(expected_lines)
def test_alias_relative_3(cmd):
    subgrpx, args = cmd.get_child(("grpx", "subgrpx"))
    subgrpx.alias('...grpy.grpy_0', "xgrpx3")
    cmd.stdin.write("grpx subgrpx xgrpx3")
    cmd.stdin.seek(0)
    cmd.run(max_commands=1)
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == "grpy_0!\n"
def test_cmd0_copy(cmd0):
    cmd0_copy = cmd0.copy()
    cmd0_copy.stdin = io.StringIO("add 3 4\ndump sum --cubes\nhelp dump maw")
    cmd0_copy.stdout = io.StringIO()
    cmd0_copy.stderr = io.StringIO()
    cmd0_copy.run()
    debug_cmd(cmd0_copy)
    assert cmd0_copy.stdout.getvalue() == """\
def test_cmdx_queue_command0_doc(cmdx):
    doc = "Hi, folk!"
    cmd = create_cmd()
    cmd.doc = doc
    cmd = cmdx(cmd)
    cmd.queue("comm")
    cmd.run()
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == doc + '\n' + COMM_TEXT + '\n'
def test_cmd_call_error_bad_commadn(trace):
    cmd = Interpreter()
    cmd.trace = trace
    cmd.stdin = io.StringIO("fun 2 3 5\n")
    cmd.stdout = io.StringIO()
    cmd.stderr = io.StringIO()
    cmd.run()
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == ""
    assert cmd.stderr.getvalue() == """\
def test_help_comm(cmdhis):
    assert not HISTORY(cmdhis, "-c")  # clears readline history
    cmdhis.stdin = io.StringIO()
    cmdhis.stdout = io.StringIO()
    cmdhis.stderr = io.StringIO()
    cmdhis.stdin.write("help comm")
    cmdhis.stdin.seek(0)
    cmdhis.run(max_commands=5)
    debug_cmd(cmdhis)
    assert cmdhis.stdout.getvalue() == """
def test_pyshell_cmd(cmdpy):
    assert not HISTORY(cmdpy, "-c")  # clears readline history
    cmdpy.stdin = io.StringIO()
    cmdpy.stdout = io.StringIO()
    cmdpy.stderr = io.StringIO()
    cmdpy.stdin.write("""py 'print("result:", 100 // 3)'\nhistory""")
    cmdpy.stdin.seek(0)
    cmdpy.run(max_commands=5)
    debug_cmd(cmdpy)
    expected_lines = ["result: 33", """\
#2 history commands:
       0) py 'print("result:", 100 // 3)'
       1) history"""]
    #print("out:", "\n" + cmdpy.stdout.getvalue())
    assert cmdpy.stdout.getvalue() == "\n".join(expected_lines) + "\n"
def test_cmd_call_error_bad_arg(trace):
    cmd = Interpreter()
    cmd.trace = trace
    @argument("args", type=int, nargs='+')
    @cmd.command
    def fun(interpreter, args):
        interpreter.output(sum(args))
    cmd.stdin = io.StringIO("fun 2 3 5\nfun 2 t 5")
    cmd.stdout = io.StringIO()
    cmd.stderr = io.StringIO()
    cmd.run()
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == """\
10
"""
    assert cmd.stderr.getvalue() == """\
def test_no_alias_err(cmd):
    cmd.stdin.write("ba")
    cmd.stdin.seek(0)
    cmd.run(max_commands=1)
    debug_cmd(cmd)
    assert cmd.stderr.getvalue() == "*** command 'ba' not found\n"
def test_get_element_relative(cmd, barbaz):
    element, args = get_element(cmd, barbaz)
    assert not element(cmd)
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == barbaz + "!\n"
def test_help_baz_by(cmd):
    cmd.stdin.write("help baz by")
    cmd.stdin.seek(0)
    cmd.run(max_commands=1)
    debug_cmd(cmd)
    assert cmd.stderr.getvalue() == """\
def test_help_grp(cmd):
    cmd.stdin.write("help grp")
    cmd.stdin.seek(0)
    cmd.run(max_commands=1)
    debug_cmd(cmd)
    assert cmd.stdout.getvalue() == """