class AbstractOutputTestCase(unittest.TestCase): #<ipython-input-2-fb4ced135814> _re_ipython_id = re.compile(r"<ipython-input-[0-9]+-[a-z0-9]+>") def setUp(self): self.maxDiff = None self.knitpy = Knitpy() def _output_test(self, input_file, output_file): with codecs.open(input_file, 'r', 'UTF-8') as f: input = f.read() # some exceptions are different on py2 and py3, so add a way to make both happy... # the version which were used to develop the tests (currently py2) should stay '.md' and # the exception should become '.md_pyX' if PY3: if os.path.exists(output_file+"_py3"): output_file = output_file+"_py3" else: if os.path.exists(output_file+"_py2"): output_file = output_file+"_py2" output = self.knitpy._knit(input, tempfile.gettempdir()) if not os.path.exists(output_file): _file = output_file+".off" with codecs.open(_file, 'w', 'UTF-8') as f: output = self._re_ipython_id.sub("<ipython-input>", output) output = output.replace(os.linesep, "\n") f.write(output) self.fail("Output does not exist, created one as %s. Remove '.off' to enable it.") with codecs.open(output_file, 'r', 'UTF-8') as f: exp = f.read() self.assert_equal_output(exp, output, filename=output_file) def assert_equal_output(self, expected, received, filename=None): # output written to a file does not seem to have os.linesep # handle everything here by replacing the os linesep by a simple \n expected = expected.replace(os.linesep, "\n").rstrip('\n') received = received.replace(os.linesep, "\n").rstrip('\n') # in errors, there is a unique id like <ipython-input-2-fb4ced135814> received = self._re_ipython_id.sub("<ipython-input>", received) # this is a hardcoded fix for py3, where there are quotes around the module: received = received.replace("'NoneExistingModule'", "NoneExistingModule") if filename and expected != received: _file = filename+".received" with codecs.open(_file, 'w', 'UTF-8') as f: f.write(received) self.assertEqual(expected, received)
def setUp(self): self.maxDiff = None self.knitpy = Knitpy()