From 553bdb51b339386e7ac7a267565eea6aa43766d1 Mon Sep 17 00:00:00 2001 From: Mesar Hameed Date: Sat, 6 Sep 2014 19:33:51 +0100 Subject: [PATCH] tests/harness/runHarness.py: deduplicate error reporting code. Also seems to have a performance improvement. This is due to not needing to building up error message strings for tests that pass. assert a == b, foo() Make use of that foo is being called lazily. $ python -m cProfile runHarness.py en-ueb-g2-dictionary_harness.txt ... Ran 99299 tests (99.26% success), with 735 failures and 0 errors. Before refactor: 16618186 function calls (16616268 primitive calls) in 22.915 seconds After refactor: 16127571 function calls (16125653 primitive calls) in 21.783 seconds --- tests/harness/runHarness.py | 62 ++++++++++++------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/tests/harness/runHarness.py b/tests/harness/runHarness.py index d0bcfb11d1..140b359ceb 100755 --- a/tests/harness/runHarness.py +++ b/tests/harness/runHarness.py @@ -140,62 +140,40 @@ def hyphenateword(self, tables, word, mode): else: return "".join( list(map(lambda a,b: "-"+a if b=='1' else a, word, hyphen_mask)) ) + def report_error(self, errorType, received, brlCursorPos=0): + template = "%-25s '%s'" + report = [] + report.append("--- {errorType} Failure: {file} ---".format(errorType=errorType, file=self.__str__())) + if self.comment: + report.append(template % ("comment:", "".join(self.comment))) + report.append(template % ("input:", self.input)) + if self.expectedOutput != received: + report.append(template % ("expected:", self.expectedOutput)) + report.append(template % ("received:", received)) + if errorType == "Braille Cursor Difference": + cursorLocationIndicators = showCurPos(len(self.expectedOutput), brlCursorPos, pos2=self.expectedBrlCursorPos) + report.append(template % ("BRLCursorAt %d expected %d:" %(brlCursorPos, self.expectedBrlCursorPos), cursorLocationIndicators)) + report.append("--- end ---") + return u("\n".join(report)) + def check_translate(self): if self.cursorPos is not None: tBrl, temp1, temp2, tBrlCurPos = translate(self.tables, self.input, mode=self.mode, cursorPos=self.cursorPos) else: tBrl, temp1, temp2, tBrlCurPos = translate(self.tables, self.input, mode=self.mode) - template = "%-25s '%s'" - tBrlCurPosStr = showCurPos(len(tBrl), tBrlCurPos) - report = [ - "--- Braille Difference Failure: %s ---" % self.__str__(), - template % ("comment:", "".join(self.comment)), - template % ("input:", self.input), - template % ("expected brl:", self.expectedOutput), - template % ("actual brl:", tBrl), - "--- end ---", - ] - assert tBrl == self.expectedOutput, u("\n".join(report)) + assert tBrl == self.expectedOutput, self.report_error("Braille Difference", tBrl) def check_backtranslate(self): backtranslate_output = backTranslateString(self.tables, self.input, None, mode=self.mode) - template = "%-25s '%s'" - report = [ - "--- Backtranslate failure: %s ---" % self.__str__(), - template % ("comment:", "".join(self.comment)), - template % ("input:", self.input), - template % ("expected text:", self.expectedOutput), - template % ("actual backtranslated text:", backtranslate_output), - "--- end ---", - ] - assert backtranslate_output == self.expectedOutput, u("\n".join(report)) + assert backtranslate_output == self.expectedOutput, self.report_error("Backtranslate", backtranslate_output) def check_cursor(self): tBrl, temp1, temp2, tBrlCurPos = translate(self.tables, self.input, mode=self.mode, cursorPos=self.cursorPos) - template = "%-25s '%s'" - etBrlCurPosStr = showCurPos(len(tBrl), tBrlCurPos, pos2=self.expectedBrlCursorPos) - report = [ - "--- Braille Cursor Difference Failure: %s ---" %self.__str__(), - template % ("comment:", "".join(self.comment)), - template % ("input:", self.input), - template % ("received brl:", tBrl), - template % ("BRLCursorAt %d expected %d:" %(tBrlCurPos, self.expectedBrlCursorPos), - etBrlCurPosStr), - "--- end ---" - ] - assert tBrlCurPos == self.expectedBrlCursorPos, u("\n".join(report)) + assert tBrlCurPos == self.expectedBrlCursorPos, self.report_error("Braille Cursor Difference", tBrl, brlCursorPos=tBrlCurPos) def check_hyphenate(self): hyphenated_word = self.hyphenateword(self.tables, self.input, mode=self.mode) - template = "%-25s '%s'" - report = [ - "--- Hyphenation failure: %s ---" % self.__str__(), - template % ("input:", self.input), - template % ("expected hyphenated word:", self.expectedOutput), - template % ("actual hyphenated word:", hyphenated_word), - "--- end ---", - ] - assert hyphenated_word == self.expectedOutput, u("\n".join(report)) + assert hyphenated_word == self.expectedOutput, self.report_error("Hyphenation", hyphenated_word) def test_allCases(): if 'HARNESS_DIR' in os.environ: