]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
Contemplate generating LaTeX directly
[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 from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
15
16 TIMEOUT = 8
17
18
19 class TestPaperDoorknob(unittest.TestCase):
20 def setUp(self) -> None:
21 self._server = self.enterContext(FakeGlowficServer())
22 self._port = self._server.port()
23
24 def testReplies(self) -> None:
25 with DirectFetcher(TIMEOUT) as f:
26 replies = paperdoorknob.replies(
27 paperdoorknob.clean(
28 paperdoorknob.parse(
29 f.fetch(f"http://localhost:{self._port}"))))
30 self.assertEqual([r.text.strip() for r in replies],
31 ["This is glowfic", "You sure?", "Pretty sure."])
32
33 def testProcess(self) -> None:
34 texifier = PandocTexifier('pandoc')
35 with DirectFetcher(TIMEOUT) as f:
36 buf = io.BytesIO()
37 paperdoorknob.process(
38 f"http://localhost:{self._port}", f, texifier, buf)
39 self.assertEqual(buf.getvalue(), b'''\\documentclass{article}
40 \\begin{document}
41 This is glowfic
42 You \\emph{sure}?
43 Pretty sure.
44 \\end{document}
45 ''')
46
47 def testDirectTexifier(self) -> None:
48 texifier = VerifyingTexifier(
49 PandocTexifier('pandoc'), DirectTexifier())
50 with DirectFetcher(TIMEOUT) as f:
51 buf = io.BytesIO()
52 paperdoorknob.process(
53 f"http://localhost:{self._port}", f, texifier, buf)
54
55 def testPDF(self) -> None:
56 texifier = PandocTexifier('pandoc')
57 with DirectFetcher(TIMEOUT) as f:
58 with open("test.tex", 'wb') as out:
59 paperdoorknob.process(
60 f"http://localhost:{self._port}", f, texifier, out)
61 subprocess.run(['pdflatex', 'test.tex'],
62 stdin=subprocess.DEVNULL, check=True)
63
64
65 if __name__ == '__main__':
66 unittest.main()