2019-10-04 09:18:52 +00:00
|
|
|
import wexpect
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import unittest
|
2020-02-22 22:03:28 +00:00
|
|
|
from tests import PexpectTestCase
|
2019-10-04 09:18:52 +00:00
|
|
|
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
sys.path.insert(0, here)
|
|
|
|
|
|
|
|
print(wexpect.__version__)
|
|
|
|
|
|
|
|
# With quotes (C:\Program Files\Python37\python.exe needs quotes)
|
|
|
|
python_executable = '"' + sys.executable + '" '
|
|
|
|
child_script = here + '\\lines_printer.py'
|
|
|
|
|
|
|
|
class ReadLineTestCase(PexpectTestCase.PexpectTestCase):
|
|
|
|
def testReadline(self):
|
|
|
|
fooPath = python_executable + ' ' + child_script
|
|
|
|
prompt = ': '
|
|
|
|
num = 5
|
2020-04-05 11:26:51 +00:00
|
|
|
|
2019-10-04 09:18:52 +00:00
|
|
|
# Start the child process
|
|
|
|
p = wexpect.spawn(fooPath)
|
|
|
|
# Wait for prompt
|
|
|
|
p.expect(prompt)
|
|
|
|
p.sendline(str(num))
|
|
|
|
p.expect('Bye!\r\n')
|
|
|
|
expected_lines = p.before.splitlines(True) # Keep the line end
|
|
|
|
expected_lines += [p.match.group()]
|
2020-04-05 11:26:51 +00:00
|
|
|
|
|
|
|
# Termination of the SpawnSocket is slow. We have to wait to prevent the failure of the next test.
|
|
|
|
if wexpect.spawn_class_name == 'SpawnSocket':
|
|
|
|
p.wait()
|
|
|
|
|
2019-10-04 09:18:52 +00:00
|
|
|
# Start the child process
|
|
|
|
p = wexpect.spawn(fooPath)
|
|
|
|
# Wait for prompt
|
|
|
|
p.expect(prompt)
|
2020-04-05 11:26:51 +00:00
|
|
|
|
2019-10-04 09:18:52 +00:00
|
|
|
p.sendline(str(num))
|
|
|
|
for i in range(num +2): # +1 the line of sendline +1: Bye
|
|
|
|
line = p.readline()
|
|
|
|
self.assertEqual(expected_lines[i], line)
|
2020-04-05 11:26:51 +00:00
|
|
|
|
|
|
|
# Termination of the SpawnSocket is slow. We have to wait to prevent the failure of the next test.
|
|
|
|
if wexpect.spawn_class_name == 'SpawnSocket':
|
|
|
|
p.wait()
|
|
|
|
|
2019-10-04 09:18:52 +00:00
|
|
|
# Start the child process
|
|
|
|
p = wexpect.spawn(fooPath)
|
|
|
|
# Wait for prompt
|
|
|
|
p.expect(prompt)
|
2020-04-05 11:26:51 +00:00
|
|
|
|
2019-10-04 09:18:52 +00:00
|
|
|
p.sendline(str(num))
|
|
|
|
readlines_lines = p.readlines()
|
|
|
|
self.assertEqual(expected_lines, readlines_lines)
|
2020-04-05 11:26:51 +00:00
|
|
|
|
|
|
|
# Termination of the SpawnSocket is slow. We have to wait to prevent the failure of the next test.
|
|
|
|
if wexpect.spawn_class_name == 'SpawnSocket':
|
|
|
|
p.wait()
|
|
|
|
|
|
|
|
|
2019-10-04 09:18:52 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
suite = unittest.makeSuite(ReadLineTestCase,'test')
|