from sys import stdout
import ExpectPy

ExpectPy.settings.loguser = 0

# we write to /dev/null so there is no clean up afterward
spawn_id = ExpectPy.spawn('/bin/ed', 'ed', '/dev/null')

try:
  # if this isn't taken now, then the GLOB below will take it incorrectly
  spawn_id.expect((ExpectPy.EXACT, "0\r\n", 0))

  spawn_id.send('0a\rHello\r.\rw\r')
  # get the full-duplex output from the above commands
  spawn_id.expect((ExpectPy.EXACT, '.\r\nw\r\n', 0))

  rc = spawn_id.expect(
    (ExpectPy.EXACT, '6\r\n', 1),
    (ExpectPy.GLOB, '*\r\n', 2)
  )
  if rc == 1:
    stdout.write('Test okay\n')
    status = 1
  elif rc == 2:
    stdout.write('Test failed\n')
    status = 0

  spawn_id.send('q\r')
except EOFError:
  stdout.write('Test ended prematurely\n')

spawn_id.close()