SilentFlame

__init.py for transfer

Apr 10th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. import unittest
  5.  
  6. import itertools
  7. from subprocess import Popen, PIPE, call
  8. from tempfile import mkdtemp
  9. from shutil import rmtree
  10.  
  11. import signal
  12. class Alarm(Exception):
  13. pass
  14.  
  15. class TransferTest(unittest.TestCase):
  16. """Subclass and override inputs/expectedOutputs (and possibly other
  17. stuff) to create new pretransfer tests."""
  18.  
  19. # flags = ['-b' '-t' '../data/apertium-en-es.en-es.t1x' '../data/en-es.t1x.bin']
  20. inputs = [""]
  21. expectedOutputs = [""]
  22. expectedRetCodeFail = False
  23.  
  24. def alarmHandler(self, signum, frame):
  25. raise Alarm
  26.  
  27. def withTimeout(self, seconds, cmd, *args, **kwds):
  28. signal.signal(signal.SIGALRM, self.alarmHandler)
  29. signal.alarm(seconds)
  30. ret = cmd(*args, **kwds)
  31. signal.alarm(0) # reset the alarm
  32. return ret
  33.  
  34. def communicateFlush(self, string):
  35. self.proc.stdin.write(string.encode('utf-8'))
  36.  
  37. self.proc.stdin.write(b'\0')
  38. self.proc.stdin.flush()
  39.  
  40. output = []
  41. char = None
  42. try:
  43. char = self.withTimeout(2, self.proc.stdout.read, 1)
  44. # print (char)
  45. except Alarm:
  46. pass
  47. while char and char != b'\0':
  48. output.append(char)
  49. try:
  50. char = self.withTimeout(2, self.proc.stdout.read, 1)
  51. except Alarm:
  52. break # send what we got up till now
  53.  
  54. # print (output)
  55. return b"".join(output).decode('utf-8')
  56.  
  57. def runTest(self):
  58. try:
  59.  
  60. for inp, exp in zip(self.inputs, self.expectedOutputs):
  61. self.proc = Popen(['echo', inp.encode('utf-8')], stdout=PIPE, stderr=PIPE,)
  62. self.proc = Popen(['../apertium/apertium-transfer', '-b','-t','../../../apertium-en-es/apertium-en-es.en-es.t1x', '../../../apertium-en-es/en-es.t1x.bin'], stdin = self.proc.stdout, stdout=PIPE,)
  63. # print (self.proc.communicate()[0])
  64. self.assertEqual(self.proc.communicate()[0], exp+"[][\n]")
  65.  
  66. self.proc.communicate() # let it terminate
  67. self.proc.stdin.close()
  68. self.proc.stdout.close()
  69. self.proc.stderr.close()
  70. retCode = self.proc.poll()
  71. if self.expectedRetCodeFail:
  72. self.assertNotEqual(retCode, 0)
  73. else:
  74. self.assertEqual(retCode, 0)
  75.  
  76. finally:
  77. pass
  78.  
  79. class InlineBlankTransferTest(TransferTest):
  80. inputs = ["[{<em>}]^beautiful<adj>/bello<adj>/bonito<adj>/lindo<adj>/hermoso<adj>$ [{<i><b>}]^mind<n><sg>/mente<n><f><sg>$^.<sent>/.<sent>$"]
  81. expectedOutputs = ["^Nom_adj<SN><UNDET><f><sg>{[{<i><b>}]^mente<n><3><4>$ [{<em>}]^bello<adj><3><4>$}$^punt<sent>{^.<sent>$}$"]
Add Comment
Please, Sign In to add comment