]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
9676e286aaae11eb1959cb5868af85e6f3681d25
[paperdoorknob] / paperdoorknob_test.py
1 # paperdoorknob: Print glowfic
2 #
3 # This program is free software: you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation, version 3.
6
7
8 import unittest
9 import io
10 import subprocess
11 import paperdoorknob
12 from testing.fakeserver import FakeGlowficServer
13 from fetch import DirectFetcher
14
15 TIMEOUT = 8
16
17
18 class TestPaperDoorknob(unittest.TestCase):
19 def setUp(self) -> None:
20 self._server = self.enterContext(FakeGlowficServer())
21 self._port = self._server.port()
22
23 def testReplies(self) -> None:
24 with DirectFetcher(TIMEOUT) as f:
25 replies = paperdoorknob.replies(
26 paperdoorknob.clean(
27 paperdoorknob.parse(
28 f.fetch(f"http://localhost:{self._port}"))))
29 self.assertEqual([r.text.strip() for r in replies],
30 ["This is glowfic", "You sure?", "Pretty sure."])
31
32 def testProcess(self) -> None:
33 with DirectFetcher(TIMEOUT) as f:
34 buf = io.BytesIO()
35 paperdoorknob.process(
36 f"http://localhost:{self._port}", f, buf, 'pandoc')
37 self.assertEqual(buf.getvalue(), b'''\\documentclass{article}
38 \\begin{document}
39 This is glowfic
40 You \\emph{sure}?
41 Pretty sure.
42 \\end{document}
43 ''')
44
45 def testPDF(self) -> None:
46 with DirectFetcher(TIMEOUT) as f:
47 with open("test.tex", 'wb') as out:
48 paperdoorknob.process(
49 f"http://localhost:{self._port}", f, out, 'pandoc')
50 subprocess.run(['pdflatex', 'test.tex'],
51 stdin=subprocess.DEVNULL, check=True)
52
53
54 if __name__ == '__main__':
55 unittest.main()