commit 66ea1399c6a97936b84c725e027a8c5671894b80 Author: Tanmai Khanna Date: Sat Aug 29 02:34:01 2020 +0530 Interchunk tests diff --git a/tests/data/apertium-nno-nob.nno-nob.t2x b/tests/data/apertium-nno-nob.nno-nob.t2x new file mode 100644 index 0000000..1778747 --- /dev/null +++ b/tests/data/apertium-nno-nob.nno-nob.t2x @@ -0,0 +1,224 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/bincompat.t2x.bin b/tests/data/bincompat.t2x.bin new file mode 100644 index 0000000..5cde7a7 Binary files /dev/null and b/tests/data/bincompat.t2x.bin differ diff --git a/tests/data/nno-nob.t2x.bin b/tests/data/nno-nob.t2x.bin new file mode 100644 index 0000000..1411e9a Binary files /dev/null and b/tests/data/nno-nob.t2x.bin differ diff --git a/tests/interchunk/__init__.py b/tests/interchunk/__init__.py new file mode 100644 index 0000000..a498147 --- /dev/null +++ b/tests/interchunk/__init__.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import unittest + +from subprocess import Popen, PIPE, call + +import signal + + +class Alarm(Exception): + pass + + +class InterchunkTest(unittest.TestCase): + """Subclass and override inputs/expectedOutputs (and possibly other +stuff) to create new interchunk tests.""" + + bindata = "data/nno-nob.t2x.bin" + t2xdata = "data/apertium-nno-nob.nno-nob.t2x" + flags = ["-z"] + 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) + 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 + + return b"".join(output).decode('utf-8') + + def compile(self): + compileCmd = ["../apertium/apertium-preprocess-transfer", + self.t2xdata, + self.bindata] + self.assertEqual(call(compileCmd), + 0) + + def runTest(self): + self.compile() + try: + cmd = ["../apertium/apertium-interchunk"] \ + + self.flags \ + + [self.t2xdata, self.bindata] + self.proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) + + for inp, exp in zip(self.inputs, self.expectedOutputs): + self.assertEqual(self.communicateFlush(inp+"[][\n]"), + 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 SimpleInterchunkTest(InterchunkTest): + inputs = ["^prn{^ho$}$ ^prn2{^ho$}$", + "^vblex{^gå$}$^default{^.$}$"] + expectedOutputs = ["^prn2{^ho$}$^prn{^ho$}$", + "^vblex{^gå$}$^default{^.$}$"] + +class WordboundBlankTest(InterchunkTest): + inputs = ["^n_n{[[t:b:123456]]^worda$ ;[testblank] [[t:s:xyzab12]]^wordb# xyz$}$", + "^prn{[[t:b:abc823]]^ho$}$ ^prn2{[[t:i:poa023; t:span:12xas23]]^ho$}$"] + expectedOutputs = ["^n_n{[[t:b:123456]]^worda$ ;[testblank] [[t:s:xyzab12]]^wordb# xyz$}$", + "^prn2{[[t:i:poa023; t:span:12xas23]]^ho$}$^prn{[[t:b:abc823]]^ho$}$"] + +class SuperblankTest(InterchunkTest): + inputs = [ "[blank1];; ^test1{^worda$}$ ;[blank2] ^test2{^wordb# xyz$}$ ;[blank3]; ^test3{^wordc# xyz$}$ [blank4];;", #superblankrule1 + "[blank1];; ^test1{^worda$}$ ;[blank2] ^test2{^wordb# xyz$}$ ;[blank3]; ^test3{^wordc# xyz$}$ [blank4];;", #Blanks when no rules match + "[blank1];; ^test2{^worda$}$ ;[blank2] ^test2{^wordb# xyz$}$ ;[blank3]; ^test3{^wordc# xyz$}$ [blank4];;", #superblankrule2 -> When output rule has more than input blanks, print all then spaces + "[blank1];; ^test3{^worda$}$ ;[blank2] ^test2{^wordb# xyz$}$ ;[blank3]; ^test1{^wordc# xyz$}$ [blank4];;", #superblankrule3 -> Output rule has no , flush all blanks after rule output + "[blank1];; ^test1{^worda$}$ ;[blank2] ^test3{^wordb# xyz$}$ ;[blank3]; ^test2{^wordc# xyz$}$ [blank4];;", #superblankrule4 -> Output rule has one , print one blank, then flush all after rule output + "[blank1];; ^test1{^worda$}$ ;[blank2] ^test2{^wordb# xyz$}$ ;[blank3]; ^test3{^wordc# xyz$}$ [blank4];; ^test1{^worda$}$ ;[blank5] ^test3{^wordb# xyz$}$ ;[blank6]; ^test2{^wordc# xyz$}$ [blank7];;"] #Multiple matching rules -> superblankrule1 & superblankrule4 + + expectedOutputs = [ "[blank1];; ^test2{^wordb# xyz$}$ ;[blank2] ^test1{^worda$}$ ;[blank3]; ^test3{^wordc# xyz$}$ [blank4];;", + "[blank1];; ^test1{^worda$}$ ;[blank2] ^test2{^wordb# xyz$}$ ;[blank3]; ^test3{^wordc# xyz$}$ [blank4];;", + "[blank1];; ^test2{^wordb# xyz$}$ ;[blank2] ^test2{^worda$}$ ;[blank3]; ^test2{^wordb# xyz$}$ ^test2{^worda$}$ ^test3{^wordc# xyz$}$ [blank4];;", + "[blank1];; ^test2{^wordb# xyz$}$^test3{^worda$}$^test1{^wordc# xyz$}$ ;[blank2] ;[blank3]; [blank4];;", + "[blank1];; ^test3{^wordb# xyz$}$^test1{^worda$}$ ;[blank2] ^test2{^wordc# xyz$}$ ;[blank3]; [blank4];;", + "[blank1];; ^test2{^wordb# xyz$}$ ;[blank2] ^test1{^worda$}$ ;[blank3]; ^test3{^wordc# xyz$}$ [blank4];; ^test3{^wordb# xyz$}$^test1{^worda$}$ ;[blank5] ^test2{^wordc# xyz$}$ ;[blank6]; [blank7];;"] + + +class BincompatTest(SimpleInterchunkTest): + bindata = "data/bincompat.t2x.bin" + + def compile(self): + pass diff --git a/tests/postchunk/__init__.py b/tests/postchunk/__init__.py index 69547cc..1a25be7 100644 --- a/tests/postchunk/__init__.py +++ b/tests/postchunk/__init__.py @@ -118,9 +118,9 @@ class SingleLUWordboundBlankTest(PostchunkTest): class SuperblankTest(PostchunkTest): inputs = [ "[blank1];; ^n_n{^worda$ ;[blank2] ^wordb# xyz$}$ ;[blank3]; ", "[blank1];; ^n_k{^worda$ ;[blank2] ^wordb# xyz$}$ ;[blank3]; ", #Blanks when no rules match - "[blank1];; ^n_n2{^worda$ ;[blank2] ^wordb# xyz$ ;[blank3]; ^wordc$}$ ;[blank4]; ", #When output rule has more than input blanks - "[blank1];; ^n_n3{^worda$ ;[blank2] ^wordb# xyz$ ;[blank3]; ^wordc$}$ ;[blank4]; ", #Output rule has no - "[blank1];; ^n_n4{^worda$ ;[blank2] ^wordb# xyz$ ;[blank3]; ^wordc$}$ ;[blank4]; ", #Output rule has one + "[blank1];; ^n_n2{^worda$ ;[blank2] ^wordb# xyz$ ;[blank3]; ^wordc$}$ ;[blank4]; ", #superblank rule 1 -> When output rule has more than input blanks, print all then spaces + "[blank1];; ^n_n3{^worda$ ;[blank2] ^wordb# xyz$ ;[blank3]; ^wordc$}$ ;[blank4]; ", #superblank rule 2 -> Output rule has no , flush all blanks after rule output + "[blank1];; ^n_n4{^worda$ ;[blank2] ^wordb# xyz$ ;[blank3]; ^wordc$}$ ;[blank4]; ", #superblank rule 3 -> Output rule has one , print one blank, then flush all after rule output "[blank1];; ^n_n{^worda$ ;[blank2] ^wordb# xyz$}$ ;[blank3]; ^n_n4{^worda$ ;[blank4] ^wordb# xyz$ ;[blank5]; ^wordc$}$ ;[blank6]; "] #Multiple matching rules expectedOutputs = [ "[blank1];; ^wordb# xyz$ ;[blank2] ^worda$ ^worda+wordb# xyz$ ;[blank3]; ", diff --git a/tests/run_tests.py b/tests/run_tests.py index 96fc24d..5930e63 100755 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -8,6 +8,7 @@ import unittest import tagger import pretransfer import transfer +import interchunk import postchunk import adaptdocx @@ -17,6 +18,7 @@ if __name__ == "__main__": for module in [tagger, pretransfer, transfer, + interchunk, postchunk, adaptdocx]: suite = unittest.TestLoader().loadTestsFromModule(module)