"""Tests for two-process terminal frontend

Currently only has the most simple test possible, starting a console and running
a single command.

Authors:

* Min RK
"""

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import sys
import time

import nose.tools as nt
from nose import SkipTest

import IPython.testing.tools as tt
from IPython.testing import decorators as dec
from IPython.utils import py3compat

#-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------

@dec.skip_win32
def test_console_starts():
    """test that `ipython console` starts a terminal"""
    from IPython.external import pexpect
    
    args = ['console', '--colors=NoColor']
    # FIXME: remove workaround for 2.6 support
    if sys.version_info[:2] > (2,6):
        args = ['-m', 'IPython'] + args
        cmd = sys.executable
    else:
        cmd = 'ipython'
    
    try:
        p = pexpect.spawn(cmd, args=args)
    except IOError:
        raise SkipTest("Couldn't find command %s" % cmd)
    
    # timeout after one minute
    t = 60
    idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=t)
    p.sendline('5')
    idx = p.expect([r'Out\[\d+\]: 5', pexpect.EOF], timeout=t)
    idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=t)
    # send ctrl-D;ctrl-D to exit
    p.sendeof()
    p.sendeof()
    p.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=t)
    if p.isalive():
        p.terminate()

def test_help_output():
    """ipython console --help-all works"""
    tt.help_all_output_test('console')


@@ -1719,8 +1719,1102 @@
 sole')%0A%0A
+%0Adef test_display_text():%0A    %22Ensure display protocol plain/text key is supported%22%0A    # equivalent of:%0A    #%0A    #   x = %25lsmagic%0A    #   from IPython.display import display; display(x);%0A%0A    from IPython.external import pexpect%0A    %0A    args = %5B'console', '--colors=NoColor'%5D%0A    # FIXME: remove workaround for 2.6 support%0A    if sys.version_info%5B:2%5D %3E (2,6):%0A        args = %5B'-m', 'IPython'%5D + args%0A        cmd = sys.executable%0A    else:%0A        cmd = 'ipython'%0A    %0A    try:%0A        p = pexpect.spawn(cmd, args=args)%0A    except IOError:%0A        raise SkipTest(%22Couldn't find command %25s%22 %25 cmd)%0A    %0A    # timeout after one minute%0A    t = 60%0A    idx = p.expect(%5Br'In %5C%5B%5Cd+%5C%5D', pexpect.EOF%5D, timeout=t)%0A    p.sendline('x = %25lsmagic')%0A    idx = p.expect(%5Br'In %5C%5B%5Cd+%5C%5D', pexpect.EOF%5D, timeout=t)%0A    p.sendline('from IPython.display import display; display(x);')%0A    p.expect(%5Br'Available line magics:', pexpect.EOF%5D, timeout=t)%0A%0A    # send ctrl-D;ctrl-D to exit%0A    p.sendeof()%0A    p.sendeof()%0A    p.expect(%5Bpexpect.EOF, pexpect.TIMEOUT%5D, timeout=t)%0A    if p.isalive():%0A        p.terminate()%0A

test for text/plain display handling in console