def test_fsmparse(self): try: # 1. write a new python module containing a class with a staticmethod with open("testparser.py", "w") as fp: fp.write(""" from six import text_type as str from ferenda.elements import Body, Paragraph class Testobject(object): @staticmethod def get_parser(): return Parser() class Parser(object): def parse(self, source): res = Body() for chunk in source: res.append(Paragraph([str(len(chunk.strip()))])) return res """) import imp fp, pathname, desc = imp.find_module("testparser") imp.load_module("testparser", fp, pathname, desc) # 2. write a textfile with two paragraphs with open("testparseinput.txt", "w") as fp: fp.write("""This is one paragraph. And another. """) # 3. patch print and call fsmparse d = Devel() printmock = MagicMock() with patch('builtins.print', printmock): # 3.1 fsmparse dynamically imports the module and call the method # with every chunk from the text file # 3.2 fsmparse asserts that the method returned a callable # 3.3 fsmparse calls it with a iterable of text chunks from the # textfile # 3.4 fsmparse recieves a Element structure and prints a # serialized version d.fsmparse("testparser.Testobject.get_parser", "testparseinput.txt") self.assertTrue(printmock.called) # 4. check that the expected thing was printed want = """ <Body> <Paragraph> <str>22</str> </Paragraph> <Paragraph> <str>12</str> </Paragraph> </Body> """.strip() + "\n" printmock.assert_has_calls([call(want)]) finally: util.robust_remove("testparser.py") util.robust_remove("testparser.pyc") util.robust_remove("testparseinput.txt") if os.path.exists("__pycache__") and os.path.isdir("__pycache__"): shutil.rmtree("__pycache__")
def test_fsmparse(self): try: # 1. write a new python module containing a class with a staticmethod with open("testparser.py", "w") as fp: fp.write(""" from six import text_type as str from ferenda.elements import Body, Paragraph class Testobject(object): @staticmethod def get_parser(): return Parser() class Parser(object): def parse(self, source): res = Body() for chunk in source: res.append(Paragraph([str(len(chunk.strip()))])) return res """) import imp fp, pathname, desc = imp.find_module("testparser") imp.load_module("testparser", fp, pathname, desc) # 2. write a textfile with two paragraphs with open("testparseinput.txt", "w") as fp: fp.write("""This is one paragraph. And another. """) # 3. patch print and call fsmparse d = Devel() printmock = MagicMock() with patch('builtins.print', printmock): # 3.1 fsmparse dynamically imports the module and call the method # with every chunk from the text file # 3.2 fsmparse asserts that the method returned a callable # 3.3 fsmparse calls it with a iterable of text chunks from the # textfile # 3.4 fsmparse recieves a Element structure and prints a # serialized version d.fsmparse("testparser.Testobject.get_parser", "testparseinput.txt") self.assertTrue(printmock.called) # 4. check that the expected thing was printed want = """ <Body> <Paragraph> <str>22</str> </Paragraph> <Paragraph> <str>12</str> </Paragraph> </Body> """.strip()+"\n" printmock.assert_has_calls([call(want)]) finally: util.robust_remove("testparser.py") util.robust_remove("testparser.pyc") util.robust_remove("testparseinput.txt") if os.path.exists("__pycache__") and os.path.isdir("__pycache__"): shutil.rmtree("__pycache__")