#!/usr/bin/env python2

from os.path import dirname, realpath, join
from os import putenv
import pexpect


prompts = [
    'Password: ',
    'Passphrase: ',
    '> ',
]

inputs = [
    'abc',
    '0123456789',
    'a' * 255,
    'abc' * (255 // 3),
]


TIMEOUT = 2


def runtest(prompt):

    putenv('PROMPT', prompt)
    dir = dirname(realpath(__file__))
    tester = join(dir, 'test-ttyprompt')

    test = pexpect.spawn(tester)
    assert test.expect_exact(prompt, timeout=TIMEOUT) == 0
    test.sendcontrol('D')
    assert test.expect_exact('(null) 0', timeout=TIMEOUT) == 0
    test.close()

    test = pexpect.spawn(tester)
    assert test.expect_exact(prompt, timeout=TIMEOUT) == 0
    test.sendline()
    assert test.expect_exact('"" 0', timeout=TIMEOUT) == 0
    test.close()

    for input in inputs:
        test = pexpect.spawn(tester)
        assert test.expect_exact(prompt, timeout=TIMEOUT) == 0
        test.sendline(input)
        assert test.expect_exact(input + ' ' + str(len(input))) == 0
        test.close()


if '__main__' in __name__:
    for prompt in prompts:
        runtest(prompt)
