| 1 |
#!/usr/bin/python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
import unittest |
| 5 |
|
| 6 |
from subprocess import Popen, PIPE |
| 7 |
|
| 8 |
|
| 9 |
class AdaptDocxTest(unittest.TestCase): |
| 10 |
""" |
| 11 |
Subclass and override inputs/expectedOutputs (and possibly other stuff) |
| 12 |
to create new adapt-docx tests. |
| 13 |
""" |
| 14 |
|
| 15 |
def runTest(self): |
| 16 |
pass |
| 17 |
|
| 18 |
def runCheckExit(self, proc, inp): |
| 19 |
"""Run proc on input, then close and assert that we exited with 0. |
| 20 |
Return output from proc.""" |
| 21 |
timeout = 30 |
| 22 |
res = proc.communicate(inp, timeout) |
| 23 |
for fd in [proc.stdin, proc.stdout, proc.stderr]: |
| 24 |
if fd is not None: |
| 25 |
fd.close() |
| 26 |
self.assertEqual(proc.poll(), 0) |
| 27 |
return res |
| 28 |
|
| 29 |
def adapt(self, xmlFile): |
| 30 |
entrada = (xmlFile + "\n").encode("utf8") |
| 31 |
cmd = ["../apertium/apertium-adapt-docx"] |
| 32 |
proc = Popen(cmd, stdin=PIPE, stdout=PIPE) |
| 33 |
return self.runCheckExit(proc, entrada)[0] |
| 34 |
|
| 35 |
def extractText(self, xmlFile, adapta): |
| 36 |
try: |
| 37 |
if adapta: |
| 38 |
entrada = self.adapt(xmlFile) |
| 39 |
else: |
| 40 |
cmd = ["cat", xmlFile] |
| 41 |
proc = Popen(cmd, stdin=PIPE, stdout=PIPE) |
| 42 |
entrada = self.runCheckExit(proc, None)[0] |
| 43 |
cmd = ["python3", "adaptdocx/extract_docx.py"] |
| 44 |
proc = Popen(cmd, stdin=PIPE, stdout=PIPE) |
| 45 |
result = self.runCheckExit(proc, entrada)[0] |
| 46 |
result = result.decode("utf8").split("\n") |
| 47 |
return result |
| 48 |
finally: |
| 49 |
pass |
| 50 |
|
| 51 |
|
| 52 |
class SameTextTest(AdaptDocxTest): |
| 53 |
|
| 54 |
def runTest(self): |
| 55 |
for nom in ["docx-01"]: |
| 56 |
original = [linia.replace("|", "") for linia in self.extractText("data/" + nom + ".xml", False)] |
| 57 |
adaptat = [linia.replace("|", "") for linia in self.extractText("data/" + nom + ".xml", True)] |
| 58 |
self.assertListEqual(original, adaptat) |
| 59 |
|
| 60 |
|
| 61 |
class CorrectBoundariesTest(AdaptDocxTest): |
| 62 |
|
| 63 |
def runTest(self): |
| 64 |
for nom in ["docx-01"]: |
| 65 |
obtingut = self.extractText("data/" + nom + ".xml", True) |
| 66 |
with open("data/" + nom + ".txt") as f: |
| 67 |
esperat = [linia.strip("\n") for linia in f.readlines()] |
| 68 |
self.assertListEqual(obtingut, esperat) |
| 69 |
|
| 70 |
|
| 71 |
class NoNewlinesTest(AdaptDocxTest): |
| 72 |
|
| 73 |
def runTest(self): |
| 74 |
for nom in ["docx-newlines"]: |
| 75 |
obtingut = self.adapt("data/" + nom + ".xml") |
| 76 |
esperat = open("data/" + nom + ".adapted.xml", "rb").read() |
| 77 |
self.assertEqual(obtingut, esperat) |