#!/usr/bin/env python3

# Copyright (C) 2017 Alexandre Abadie <alexandre.abadie@inria.fr>
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

import sys
from testrunner import run


EXPECTED_HELP = (
    'Command              Description',
    '---------------------------------------',
    'start_test           starts a test',
    'end_test             ends a test',
    'echo                 prints the input command',
    'reboot               Reboot the node',
    'ps                   Prints information about running threads.',
    'app_metadata         Returns application metadata'
)

EXPECTED_PS = (
    '\tpid | state    Q | pri',
    '\t  1 | pending  Q |  15',
    '\t  2 | running  Q |   7'
)

CMDS = (
    ('start_test', ('[TEST_START]')),
    ('end_test', ('[TEST_END]')),
    ('\n', ('>')),
    ('123456789012345678901234567890123456789012345678901234567890',
        ('shell: command not found: '
         '123456789012345678901234567890123456789012345678901234567890')),
    ('unknown_command', ('shell: command not found: unknown_command')),
    ('help', EXPECTED_HELP),
    ('echo a string', ('\"echo\"\"a\"\"string\"')),
    ('ps', EXPECTED_PS),
    ('reboot', ('test_shell.'))
)


def check_cmd(child, cmd, expected):
    child.sendline(cmd)
    for line in expected:
        child.expect_exact(line)


def testfunc(child):
    # check startup message
    child.expect('test_shell.')

    # loop other defined commands and expected output
    for cmd, expected in CMDS:
        check_cmd(child, cmd, expected)


if __name__ == "__main__":
    sys.exit(run(testfunc))

@@ -821,16 +821,444 @@
   7'%0A)%0A%0A
+# In native we are directly executing the binary (no terminal program). We must%0A# therefore use Ctrl-V (DLE or %22data link escape%22) before Ctrl-C to send a%0A# literal ETX instead of SIGINT.%0A# When using a board it is also a problem because we are using a %22user friendly%22%0A# terminal that interprets Ctrl-C. So until we have rawterm we must also use%0A# ctrl-v in boards.%0A%0ADLE = '%5Cx16'%0A%0ACONTROL_C = DLE+'%5Cx03'%0ACONTROL_D = DLE+'%5Cx04'%0A%0A
 CMDS = (
@@ -1709,16 +1709,109 @@
 ED_PS),%0A
+    ('garbage1234'+CONTROL_C, ('%3E')),  # test cancelling a line%0A    ('help', EXPECTED_HELP),%0A
     ('re

Test cancelling a line with ctrl-C.