Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import unittest
- import itertools
- from subprocess import Popen, PIPE, call
- from tempfile import mkdtemp
- from shutil import rmtree
- import signal
- class Alarm(Exception):
- pass
- class TransferTest(unittest.TestCase):
- """Subclass and override inputs/expectedOutputs (and possibly other
- stuff) to create new pretransfer tests."""
- # flags = ['-b' '-t' '../data/apertium-en-es.en-es.t1x' '../data/en-es.t1x.bin']
- inputs = [""]
- expectedOutputs = [""]
- expectedRetCodeFail = False
- def alarmHandler(self, signum, frame):
- raise Alarm
- def withTimeout(self, seconds, cmd, *args, **kwds):
- signal.signal(signal.SIGALRM, self.alarmHandler)
- signal.alarm(seconds)
- ret = cmd(*args, **kwds)
- signal.alarm(0) # reset the alarm
- return ret
- def communicateFlush(self, string):
- self.proc.stdin.write(string.encode('utf-8'))
- self.proc.stdin.write(b'\0')
- self.proc.stdin.flush()
- output = []
- char = None
- try:
- char = self.withTimeout(2, self.proc.stdout.read, 1)
- # print (char)
- except Alarm:
- pass
- while char and char != b'\0':
- output.append(char)
- try:
- char = self.withTimeout(2, self.proc.stdout.read, 1)
- except Alarm:
- break # send what we got up till now
- # print (output)
- return b"".join(output).decode('utf-8')
- def runTest(self):
- try:
- for inp, exp in zip(self.inputs, self.expectedOutputs):
- self.proc = Popen(['echo', inp.encode('utf-8')], stdout=PIPE, stderr=PIPE,)
- 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,)
- # print (self.proc.communicate()[0])
- self.assertEqual(self.proc.communicate()[0], exp+"[][\n]")
- self.proc.communicate() # let it terminate
- self.proc.stdin.close()
- self.proc.stdout.close()
- self.proc.stderr.close()
- retCode = self.proc.poll()
- if self.expectedRetCodeFail:
- self.assertNotEqual(retCode, 0)
- else:
- self.assertEqual(retCode, 0)
- finally:
- pass
- class InlineBlankTransferTest(TransferTest):
- inputs = ["[{<em>}]^beautiful<adj>/bello<adj>/bonito<adj>/lindo<adj>/hermoso<adj>$ [{<i><b>}]^mind<n><sg>/mente<n><f><sg>$^.<sent>/.<sent>$"]
- 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